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.49 by root, Sun Nov 7 22:07:05 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
96 lz = z (vout.position);
97 z (vout.position) = (ifelse (lz < 0, lz, log (lz) * (2 / log (1e10))) - 1) * w (vout.position);
98 }
99
89 po->vsh->compile (shader::shader_builder::stop ()); 100 vsh_src = shader::shader_builder::stop ();
90 101
91 shader::shader_builder::start (); 102 shader::shader_builder::start ();
92 shader::compile::fout.frag_color = shader::compile::float4 (1., 1., 0., 1.);
93 fsh (ctx);
94 po->fsh->compile (shader::shader_builder::stop ());
95 103
96 po->link (); 104 if (ctx.pass->l)
105 {
106 ctx.pass->l->fsh ();
97 107
98 matmap.insert (pass::matmap_t::value_type (this, po)); 108 fsh (ctx);
109 }
110 else
111 shader::compile::fout.frag_color = shader::compile::float4 (1., 0., 1., 1.);
99 112
100 p = &po; 113 fsh_src = shader::shader_builder::stop ();
114
115 shader::program_object po = shader::get_program (vsh_src, fsh_src);
116 matmap.insert (pass_data::matmap_t::value_type (this, po));
117
118 po->enable ();
101 } 119 }
102 else 120 else
103 p = &i->second; 121 i->second->enable ();
104 122
105 (*p)->enable (); 123 if (ctx.pass->l)
124 ctx.pass->l->enable ();
106} 125}
107 126
108void material::disable (view &ctx) 127void material::disable (view &ctx)
109{ 128{
110} 129}
111 130
112test_material::test_material () 131test_material::test_material ()
113//: tex ("textures/osama.jpg"), texvar (tex.name) 132//: tex ("textures/osama.jpg"), texvar (tex.name)
114: tex ("textures/rockwall.jpg"), texvar (tex.name) 133: tex ("textures/rockwall.jpg"), texvar (tex.name)
115, norm ("textures/rockwall_height.jpg"), normvar (norm.name) 134, norm ("textures/rockwall_normal.jpg"), normvar (norm.name)
116{ 135{
117} 136}
118 137
119static shader::varying_3f normal, lightvec; 138static shader::varying_3f normal;
120static shader::varying_2f texcoord; 139static shader::varying_2f texcoord;
121 140
122void test_material::vsh (view &ctx) 141void test_material::vsh (view &ctx)
123{ 142{
124 using namespace shader::compile; 143 using namespace shader::compile;
125 144
126 std_vsh (); 145 std_vsh ();
127 146
128 if (ctx.pass_data->l) 147 if (ctx.pass->l)
129 { 148 {
130 texcoord = xy (vin.tex_coord[0]); 149 texcoord = xy (vin.tex_coord[0]);
131 normal = normal_matrix * vin.normal; 150 normal = normal_matrix * vin.normal;
132 lightvec = xyz (ctx.pass_data->l->lightpos - model_view_matrix * vin.vertex);
133 } 151 }
134} 152}
135 153
136void test_material::fsh (view &ctx) 154void test_material::fsh (view &ctx)
137{ 155{
138 using namespace shader::compile; 156 using namespace shader::compile;
139 157
140 if (ctx.pass_data->l) 158 if (ctx.pass->l)
141 { 159 {
142 temp_3f lc; 160 temp_3f lc;
143 temp_1f fac; 161 temp_1f fac;
144 162
145 lc = (*ctx.pass_data->l)(); 163 temp_3f rot1, rot2, rot3;
146 164
165 yxz (rot1) = (texture_2d (normvar, texcoord) * 2.F - 1.F);
166
167 //rot1 = normal_matrix * rot1;
168
169 rot2 = float3 (x(rot1), z(rot1), -y(rot1));
170 rot3 = float3 (y(rot1), -x(rot1), z(rot1));
171
172 normal = mat3 (rot1, rot2, rot3) * normal;
173
147 fac = dot (normalize (normal), normalize (lightvec)); 174 fac = dot (normalize (normal), normalize (ctx.pass->l->sh_lightvec));
148 fac = pow (max (fac, 0.0), 2); 175 fac = max (pow (max (fac, 0.0), 6), 0.3);
149 xyz (fout.frag_color) = texture_2d (texvar, texcoord) * lc * fac; 176 xyz (fout.frag_color) = (texture_2d (texvar, texcoord) + 0.2F) * fac
177 * ctx.pass->l->sh_colour;
178 //xyz (fout.frag_color) = lc * fac;
150 } 179 }
151} 180}
152 181
153void test_material::enable (view &ctx) 182void test_material::enable (view &ctx)
154{ 183{
162 normvar->disable (); 191 normvar->disable ();
163 texvar->disable (); 192 texvar->disable ();
164 material::disable (ctx); 193 material::disable (ctx);
165} 194}
166 195
167test_material testmat; 196test_material *testmat;
197

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines