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.33 by root, Wed Nov 3 03:36:16 2004 UTC vs.
Revision 1.53 by root, Fri Dec 10 05:37:09 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_data::matmap_t &matmap = ctx.pass->matmap;
75 75
76 pass::matmap_t::iterator i = matmap.find (this); 76 pass_data::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 ();
83
84 if (ctx.pass->l)
85 ctx.pass->l->vsh ();
86
84 vsh (ctx); 87 vsh (ctx);
85 88
86 if (ctx.pass_data->l) 89 // compute logarithmic depth - see the frustum matrix in view.C
87 ctx.pass_data->l->vsh (); 90 {
91 using namespace shader::compile;
88 92
93 temp_1f lz;
94
95 // TODO: negative z is not calculated in an acceptable way, clipping does horrible things(?)
96 lz = z (vout.position);
97 lz = ifelse (lz <= 0,
98 0,
99 log (lz + 1) / log (1e18)
100 ) - 1;
101 z (vout.position) = lz * w (vout.position);
102 }
103
89 po->vsh->compile (shader::shader_builder::stop ()); 104 vsh_src = shader::shader_builder::stop ();
90 105
91 shader::shader_builder::start (); 106 shader::shader_builder::start ();
92 shader::compile::fout.frag_color = shader::compile::float4 (1., 1., 0., 1.); 107
108 if (ctx.pass->l)
109 {
110 ctx.pass->l->fsh ();
93 fsh (ctx); 111 fsh (ctx);
112 }
113 else
114 // many drivers need both vertex and fragment shader, 'caboom!' otherwise
115 shader::compile::fout.frag_color = shader::compile::float4 (0., 0., 0., 0.);
116
94 po->fsh->compile (shader::shader_builder::stop ()); 117 fsh_src = shader::shader_builder::stop ();
95 118
96 po->link (); 119 shader::program_object po = shader::get_program (vsh_src, fsh_src);
97
98 matmap.insert (pass::matmap_t::value_type (this, po)); 120 matmap.insert (pass_data::matmap_t::value_type (this, po));
99 121
100 p = &po; 122 po->enable ();
101 } 123 }
102 else 124 else
103 p = &i->second; 125 i->second->enable ();
104 126
105 (*p)->enable (); 127 if (ctx.pass->l)
128 ctx.pass->l->enable ();
106} 129}
107 130
108void material::disable (view &ctx) 131void material::disable (view &ctx)
109{ 132{
110} 133}
111 134
112test_material::test_material () 135test_material::test_material ()
113//: tex ("textures/osama.jpg"), texvar (tex.name) 136//: tex ("textures/osama.jpg"), texvar (tex.name)
114: tex ("textures/rockwall.jpg"), texvar (tex.name) 137: tex ("textures/rockwall.jpg"), texvar (tex.name)
115, norm ("textures/rockwall_height.jpg"), normvar (norm.name) 138, norm ("textures/rockwall_normal.jpg"), normvar (norm.name)
116{ 139{
117} 140}
118 141
119static shader::varying_3f normal, lightvec; 142static shader::varying_3f normal;
120static shader::varying_2f texcoord; 143static shader::varying_2f texcoord;
121 144
122void test_material::vsh (view &ctx) 145void test_material::vsh (view &ctx)
123{ 146{
124 using namespace shader::compile; 147 using namespace shader::compile;
125 148
126 std_vsh (); 149 std_vsh ();
127 150
128 if (ctx.pass_data->l) 151 if (ctx.pass->l)
129 { 152 {
130 texcoord = xy (vin.tex_coord[0]); 153 texcoord = xy (vin.tex_coord[0]);
131 normal = normal_matrix * vin.normal; 154 normal = normal_matrix * vin.normal;
132 lightvec = xyz (ctx.pass_data->l->lightpos - model_view_matrix * vin.vertex);
133 } 155 }
134} 156}
135 157
136void test_material::fsh (view &ctx) 158void test_material::fsh (view &ctx)
137{ 159{
138 using namespace shader::compile; 160 using namespace shader::compile;
139 161
140 if (ctx.pass_data->l) 162 if (ctx.pass->l)
141 { 163 {
142 temp_3f lc; 164 temp_3f lc;
143 temp_1f fac; 165 temp_1f fac;
144 166
145 lc = (*ctx.pass_data->l)(); 167 temp_3f rot1, rot2, rot3;
146 168
169 yxz (rot1) = (texture_2d (normvar, texcoord) * 2.F - 1.F);
170
171 //rot1 = normal_matrix * rot1;
172
173 rot2 = float3 (x(rot1), z(rot1), -y(rot1));
174 rot3 = float3 (y(rot1), -x(rot1), z(rot1));
175
176 normal = mat3 (rot1, rot2, rot3) * normal;
177
147 fac = dot (normalize (normal), normalize (lightvec)); 178 fac = dot (normalize (normal), normalize (ctx.pass->l->sh_lightvec));
148 fac = pow (max (fac, 0.0), 2); 179 fac = max (pow (max (fac, 0.0), 6), 0.3);
149 xyz (fout.frag_color) = texture_2d (texvar, texcoord) * lc * fac; 180 xyz (fout.frag_color) = (texture_2d (texvar, texcoord) + 0.2F) * fac
181 * ctx.pass->l->sh_colour;
182 //xyz (fout.frag_color) = lc * fac;
150 } 183 }
151} 184}
152 185
153void test_material::enable (view &ctx) 186void test_material::enable (view &ctx)
154{ 187{
162 normvar->disable (); 195 normvar->disable ();
163 texvar->disable (); 196 texvar->disable ();
164 material::disable (ctx); 197 material::disable (ctx);
165} 198}
166 199
167test_material testmat; 200test_material *testmat;
201

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines