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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines