ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libgender/material.C
(Generate patch)

Comparing cvsroot/libgender/material.C (file contents):
Revision 1.32 by root, Wed Nov 3 03:35:13 2004 UTC vs.
Revision 1.43 by root, Sat Nov 6 04:31:01 2004 UTC

5 5
6#include "opengl.h" 6#include "opengl.h"
7#include "material.h" 7#include "material.h"
8#include "view.h" 8#include "view.h"
9#include "util.h" 9#include "util.h"
10
11material::~material ()
12{
13}
14 10
15GLuint 11GLuint
16texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) 12texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord)
17{ 13{
18 GLuint name; 14 GLuint name;
67 SDL_FreeSurface (image); /* No longer needed */ 63 SDL_FreeSurface (image); /* No longer needed */
68 64
69 return name; 65 return name;
70} 66}
71 67
68material::~material ()
69{
70}
71
72void material::enable (view &ctx) 72void material::enable (view &ctx)
73{ 73{
74 pass::matmap_t &matmap = ctx.pass_data->matmap; 74 pass::matmap_t &matmap = ctx.pass_data->matmap;
75 75
76 pass::matmap_t::iterator i = matmap.find (this); 76 pass::matmap_t::iterator i = matmap.find (this);
77 shader::program_object *p;
78 77
79 if (i == matmap.end ()) 78 if (i == matmap.end ())
80 { 79 {
81 shader::program_object po; 80 string vsh_src, fsh_src;
82 81
83 shader::shader_builder::start (); 82 shader::shader_builder::start ();
84 vsh (ctx);
85 83
86 if (ctx.pass_data->l) 84 if (ctx.pass_data->l)
87 ctx.pass_data->l->vsh (); 85 ctx.pass_data->l->vsh ();
88 86
87 vsh (ctx);
88
89 {
90 using namespace shader::compile;
91
92 z (vout.position) = (log2 (z (vout.position) + 1.F) * 1.82F - 1);
93 //z (vout.position) = z (vout.position) - 1;
94 }
95
89 po->vsh->compile (shader::shader_builder::stop ()); 96 vsh_src = shader::shader_builder::stop ();
90 97
91 shader::shader_builder::start (); 98 shader::shader_builder::start ();
92 fsh (ctx);
93 po->fsh->compile (shader::shader_builder::stop ());
94 99
95 po->link (); 100 if (ctx.pass_data->l)
101 {
102 ctx.pass_data->l->fsh ();
96 103
104 fsh (ctx);
105 }
106 else
107 shader::compile::fout.frag_color = shader::compile::float4 (1., 0., 1., 1.);
108
109 fsh_src = shader::shader_builder::stop ();
110
111 shader::program_object po = shader::get_program (vsh_src, fsh_src);
97 matmap.insert (pass::matmap_t::value_type (this, po)); 112 matmap.insert (pass::matmap_t::value_type (this, po));
98 113
99 p = &po; 114 po->enable ();
100 } 115 }
101 else 116 else
102 p = &i->second; 117 i->second->enable ();
103
104 (*p)->enable ();
105} 118}
106 119
107void material::disable (view &ctx) 120void material::disable (view &ctx)
108{ 121{
109} 122}
110 123
111test_material::test_material () 124test_material::test_material ()
112//: tex ("textures/osama.jpg"), texvar (tex.name) 125//: tex ("textures/osama.jpg"), texvar (tex.name)
113: tex ("textures/rockwall.jpg"), texvar (tex.name) 126: tex ("textures/rockwall.jpg"), texvar (tex.name)
114, norm ("textures/rockwall_height.jpg"), normvar (norm.name) 127, norm ("textures/rockwall_normal.jpg"), normvar (norm.name)
115{ 128{
116} 129}
117 130
118static shader::varying_3f normal, lightvec; 131static shader::varying_3f normal;
119static shader::varying_2f texcoord; 132static shader::varying_2f texcoord;
120 133
121void test_material::vsh (view &ctx) 134void test_material::vsh (view &ctx)
122{ 135{
123 using namespace shader::compile; 136 using namespace shader::compile;
126 139
127 if (ctx.pass_data->l) 140 if (ctx.pass_data->l)
128 { 141 {
129 texcoord = xy (vin.tex_coord[0]); 142 texcoord = xy (vin.tex_coord[0]);
130 normal = normal_matrix * vin.normal; 143 normal = normal_matrix * vin.normal;
131 lightvec = xyz (ctx.pass_data->l->lightpos - model_view_matrix * vin.vertex);
132 } 144 }
133} 145}
134 146
135void test_material::fsh (view &ctx) 147void test_material::fsh (view &ctx)
136{ 148{
139 if (ctx.pass_data->l) 151 if (ctx.pass_data->l)
140 { 152 {
141 temp_3f lc; 153 temp_3f lc;
142 temp_1f fac; 154 temp_1f fac;
143 155
144 lc = (*ctx.pass_data->l)(); 156 temp_3f rot1, rot2, rot3;
145 157
158 yxz (rot1) = (texture_2d (normvar, texcoord) * 2.F - 1.F);
159
160 //rot1 = normal_matrix * rot1;
161
162 rot2 = float3 (x(rot1), z(rot1), -y(rot1));
163 rot3 = float3 (y(rot1), -x(rot1), z(rot1));
164
165 normal = mat3 (rot1, rot2, rot3) * normal;
166
146 fac = dot (normalize (normal), normalize (lightvec)); 167 fac = dot (normalize (normal), normalize (ctx.pass_data->l->sh_lightvec));
147 fac = pow (max (fac, 0.0), 2); 168 fac = max (pow (max (fac, 0.0), 6), 0.3);
148 xyz (fout.frag_color) = texture_2d (texvar, texcoord) * lc * fac; 169 xyz (fout.frag_color) = (texture_2d (texvar, texcoord) + 0.2F) * fac
170 * ctx.pass_data->l->sh_colour;
171 //xyz (fout.frag_color) = lc * fac;
149 } 172 }
150 else
151 fout.frag_color = float4(1,1,1,1);
152} 173}
153 174
154void test_material::enable (view &ctx) 175void test_material::enable (view &ctx)
155{ 176{
156 material::enable (ctx); 177 material::enable (ctx);
163 normvar->disable (); 184 normvar->disable ();
164 texvar->disable (); 185 texvar->disable ();
165 material::disable (ctx); 186 material::disable (ctx);
166} 187}
167 188
168test_material testmat; 189test_material *testmat;
190

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines