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.18 by root, Fri Oct 29 17:21:54 2004 UTC vs.
Revision 1.24 by root, Sun Oct 31 22:55:23 2004 UTC

28 28
29GLuint 29GLuint
30texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord) 30texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord)
31{ 31{
32 GLuint name; 32 GLuint name;
33 int w, h;
34 SDL_Surface *image; 33 SDL_Surface *image;
35 SDL_Rect area; 34 SDL_Rect area;
36 Uint32 saved_flags; 35 Uint32 saved_flags;
37 Uint8 saved_alpha; 36 Uint8 saved_alpha;
38 37
39 /* Use the surface width and height expanded to powers of 2 */ 38 /* Use the surface width and height expanded to powers of 2 */
40 //w = power_of_two (surface->w);
41 //h = power_of_two (surface->h);
42 w = power_of_two (surface->w);
43 h = power_of_two (surface->h);
44 tex2oord[0] = 0.0f; /* Min X */ 39 tex2oord[0] = 0.F; /* Min X */
45 tex2oord[1] = 0.0f; /* Min Y */ 40 tex2oord[1] = 0.F; /* Min Y */
46 tex2oord[2] = (GLfloat) surface->w / w; /* Max X */ 41 tex2oord[2] = 1.F; /* Max X */
47 tex2oord[3] = (GLfloat) surface->h / h; /* Max Y */ 42 tex2oord[3] = 1.F; /* Max Y */
48 43
49 image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32, 44 image = SDL_CreateRGBSurface (SDL_SWSURFACE, surface->w, surface->h, 32,
50#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */ 45#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
51 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 46 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
52#else 47#else
53 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF 48 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
54#endif 49#endif
80 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 75 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
81 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 76 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
82 glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); 77 glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
83 glTexImage2D (GL_TEXTURE_2D, 78 glTexImage2D (GL_TEXTURE_2D,
84 0, 79 0,
85 GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); 80 GL_RGBA, surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
86 SDL_FreeSurface (image); /* No longer needed */ 81 SDL_FreeSurface (image); /* No longer needed */
87 82
88 return name; 83 return name;
89} 84}
90 85
91test_material::test_material () 86test_material::test_material ()
92: tex ("textures/osama.jpg"), texvar (tex.name) 87//: tex ("textures/osama.jpg"), texvar (tex.name)
88: tex ("textures/rockwall.jpg"), texvar (tex.name)
89, norm ("textures/rockwall_height.jpg"), normvar (norm.name)
93{ 90{
94 using namespace shader; 91 using namespace shader::compile;
95 92
96 p.vsh->start (); 93 p.vsh->start ();
97 94
98 temp_4f wpos; 95 temp_4f wpos;
99 96
100 wpos = shader::gl.model_view_projection_matrix * vin.vertex; 97 wpos = model_view_projection_matrix * vin.vertex;
98
99 //wpos = wpos / abs (w (wpos));
101 100
102 vout.position = wpos; 101 vout.position = wpos;
103 vout.tex_coord[0] = vin.tex_coord[0]; 102 vout.tex_coord[0] = vin.tex_coord[0];
104 vout.tex_coord[1] = shader::gl.model_view_matrix * shader::vec4 (x (vin.normal), y(vin.normal), z(vin.normal), 0); 103 vout.tex_coord[1] = model_view_matrix * shader::compile::vec4 (x(vin.normal), y(vin.normal), z(vin.normal), 0);
104 vout.tex_coord[2] = lightpos - model_view_matrix * vin.vertex;
105 105
106 p.vsh->end (); 106 p.vsh->end ();
107 p.vsh->compile (); 107 p.vsh->compile ();
108 108
109 p.fsh->start (); 109 p.fsh->start ();
110 110
111 temp_1f fac; 111 temp_1f fac;
112 112
113 fac = max (dot (fin.tex_coord[1], lightpos), 0.0); 113 temp_2f disp;
114 fac = pow (fac, 3); 114
115 temp_1f dx, dy;
116
117 temp_1f bumpnormal;
118 dx = dFdx (texture_2d (normvar, fin.tex_coord[0]));
119 dy = dFdy (texture_2d (normvar, fin.tex_coord[0]));
120
121 disp = shader::compile::vec2 (dx, dy) * 0.1;
122
123 fac = dot (normalize (fin.tex_coord[1]), normalize (fin.tex_coord[2]));
124 //fac = dot (shader::compile::vec3 (normalize (fin.tex_coord[1]) + normalize (fin.tex_coord[2])) * 0.5, bumpnormal);
125 //fac = dot (bumpnormal, normalize (fin.tex_coord[2]));
126 //fac = pow (max (fac, 0.0), 8);
115 xyz(fout.frag_color) = texture_2d (texvar, fin.tex_coord[0]) * (fac + 0.3); 127 xyz (fout.frag_color) = (texture_2d (texvar, fin.tex_coord[0] - disp) + 0.1) * max (fac, 0.0);
116 128
117 p.fsh->end (); 129 p.fsh->end ();
118 p.fsh->compile (); 130 p.fsh->compile ();
119 p.link (); 131 p.link ();
120} 132}
121 133
122void test_material::enable (view &ctx) 134void test_material::enable (view &ctx)
123{ 135{
124 p.enable (); 136 p.enable ();
125 lightpos->set (-ctx.d); 137 lightpos->set (vec3 (0, 0, 0));
126 texvar->enable (); 138 texvar->enable ();
139 normvar->enable ();
127} 140}
128 141
129void test_material::disable (view &ctx) 142void test_material::disable (view &ctx)
130{ 143{
144 normvar->disable ();
131 texvar->disable (); 145 texvar->disable ();
132 p.disable (); 146 p.disable ();
133} 147}
134 148

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines