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.28 by root, Mon Nov 1 22:23:38 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;
96 varying_1f camdist, zpos;
97 varying_2f texcoord;
98 varying_3f lightvec, normal;
99 99
100 wpos = shader::gl.model_view_projection_matrix * vin.vertex; 100 wpos = model_view_matrix * vin.vertex;
101 101
102 vout.position = wpos; 102 temp_1f wz;
103 //wz = log2 (abs (wz)) * sign (wz);
104
105 vout.position = projection_matrix * wpos;
103 vout.tex_coord[0] = vin.tex_coord[0]; 106 texcoord = xy (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); 107 normal = normal_matrix * vin.normal;
108 lightvec = xyz (lightpos - model_view_matrix * vin.vertex);
109 camdist = 1 - max (length (lightvec) / 10000., 0);
105 110
106 p.vsh->end (); 111 p.vsh->end ();
107 p.vsh->compile (); 112 p.vsh->compile ();
108 113
109 p.fsh->start (); 114 p.fsh->start ();
110 115
111 temp_1f fac; 116 temp_1f fac;
112 117 temp_2f disp;
113 fac = max (dot (fin.tex_coord[1], lightpos), 0.0); 118 temp_1f dx, dy;
119 temp_1f bumpnormal;
120 temp_1f dist;
121
122 fac = dot (normalize (normal), normalize (lightvec));
123 //fac = dot (shader::compile::vec3 (normalize (fin.tex_coord[1]) + normalize (fin.tex_coord[2])) * 0.5, bumpnormal);
124 //fac = dot (bumpnormal, normalize (fin.tex_coord[2]));
114 fac = pow (fac, 3); 125 fac = pow (max (fac, 0.0), 2);
115 xyz(fout.frag_color) = texture_2d (texvar, fin.tex_coord[0]) * (fac + 0.3); 126 xyz (fout.frag_color) = texture_2d (texvar, texcoord) * (fac + 0.4) * camdist;
116 127
117 p.fsh->end (); 128 p.fsh->end ();
118 p.fsh->compile (); 129 p.fsh->compile ();
119 p.link (); 130 p.link ();
120} 131}
121 132
122void test_material::enable (view &ctx) 133void test_material::enable (view &ctx)
123{ 134{
124 p.enable (); 135 p.enable ();
125 lightpos->set (-ctx.d); 136 lightpos->set (vec3 (0, 0, 0));
126 texvar->enable (); 137 texvar->enable ();
138 normvar->enable ();
127} 139}
128 140
129void test_material::disable (view &ctx) 141void test_material::disable (view &ctx)
130{ 142{
143 normvar->disable ();
131 texvar->disable (); 144 texvar->disable ();
132 p.disable (); 145 p.disable ();
133} 146}
134 147

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines