ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/view.C
(Generate patch)

Comparing libgender/view.C (file contents):
Revision 1.43 by root, Sun Oct 10 00:19:45 2004 UTC vs.
Revision 1.71 by root, Thu Nov 4 04:46:58 2004 UTC

1#include <cmath> 1#include <cmath>
2 2
3#define GL_GLEXT_PROTOTYPES 3#include "opengl.h"
4#include <GL/gl.h>
5#include <GL/glext.h>
6 4
7#include "view.h" 5#include "view.h"
8#include "oct.h" 6#include "oct.h"
9 7
8using namespace gl;
9
10pass pass_depth (0);
11
10vector<GLuint> occ_query_objects; 12vector<GLuint> occ_query_objects;
11 13
12static GLuint begin_occ_query () 14static GLuint begin_occ_query ()
13{ 15{
14 GLuint id; 16 GLuint id;
17 { 19 {
18 id = *(occ_query_objects.end () - 1); 20 id = *(occ_query_objects.end () - 1);
19 occ_query_objects.pop_back (); 21 occ_query_objects.pop_back ();
20 } 22 }
21 else 23 else
22 glGenQueriesARB (1, &id); 24 glGenQueries (1, &id);
23 25
24 glBeginQueryARB (GL_SAMPLES_PASSED, id); 26 glBeginQuery (GL_SAMPLES_PASSED, id);
25 return id; 27 return id;
26} 28}
27 29
28inline void end_occ_query () 30inline void end_occ_query ()
29{ 31{
30 glEndQueryARB (GL_SAMPLES_PASSED); 32 glEndQuery (GL_SAMPLES_PASSED);
31} 33}
32 34
33static GLuint occ_query_result (GLuint id) 35static GLuint occ_query_result (GLuint id)
34{ 36{
35 GLuint count; 37 GLuint count;
36 38
37 glGetQueryObjectuivARB (id, GL_QUERY_RESULT, &count); 39 glGetQueryObjectuiv (id, GL_QUERY_RESULT, &count);
38 occ_query_objects.push_back (id); 40 occ_query_objects.push_back (id);
39 41
40 return count; 42 return count;
41} 43}
42 44
43view::view () 45view::view ()
44: gamma(1.0) 46: gamma(1.0), nz_near (1.F), nz_far (2.F)
45{ 47{
46} 48}
47 49
48view::~view () 50view::~view ()
49{ 51{
50} 52}
51 53
52void view::begin_occ_query (recv_occ_query &recv) 54void view::begin_occ_query (recv_occ_query &recv, void *id)
53{ 55{
54 occ_queries.push_back (oq_data (&recv, ::begin_occ_query ())); 56 occ_queries.push_back (oq_data (&recv, ::begin_occ_query (), id));
55} 57}
56 58
57void view::end_occ_query () 59void view::end_occ_query ()
58{ 60{
59 ::end_occ_query (); 61 ::end_occ_query ();
66 68
67 drawn.insert (e); 69 drawn.insert (e);
68 return true; 70 return true;
69} 71}
70 72
73visibility_base *visible::get_visibility (view &ctx)
74{
75 view::visibility_map::iterator i = ctx.vismap.find (this);
76 visibility_base *vs;
77
78 if (i == ctx.vismap.end ())
79 {
80 vs = new_visibility ();
81 ctx.vismap.insert (view::visibility_map::value_type (this, vs));
82 }
83 else
84 {
85 vs = i->second;
86
87 if (vs->generation != ctx.generation)
88 {
89 if (vs->generation + 1 != ctx.generation)
90 clear_visibility (vs);
91
92 vs->generation = ctx.generation;
93 }
94 }
95
96 return vs;
97}
98
71void view::reset_projection () 99void view::reset_projection ()
72{ 100{
73 renormalize (orig, p); 101 renormalize (orig, p);
74 102
75 glViewport (0, 0, w, h); 103 glViewport (0, 0, w, h);
76 104
77 glMatrixMode (GL_PROJECTION); 105 glMatrixMode (GL_PROJECTION);
78 glLoadIdentity (); 106 glLoadIdentity ();
79 107
80 GLdouble aspect = (GLdouble)w/h; 108 GLdouble aspect = (GLdouble)w/h;
81 GLdouble ymax = z_near * tan (fov * (M_PI / 360.0)); 109 GLdouble ftan = tan (fov * (.5 * M_PI / 180.));
110 GLdouble ymax = z_near * ftan;
111 GLdouble xmax = ymax * aspect;
82 112
83 glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, z_near, z_far); 113 glFrustum (-xmax, xmax, -ymax, ymax, z_near, z_far);
114
115 perspfact = z_near / ymax * 0.5F * h;
84 116
85 d = normalize (d);//D 117 d = normalize (d);//D
86 u = normalize (u);//D 118 u = normalize (u);//D
87 119
88 vec3 rz = -d; 120 vec3 rz = -d;
94 m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0; 126 m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0;
95 m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0; 127 m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0;
96 m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; 128 m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1;
97 129
98 diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z); 130 diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z);
131 //printf ("diagfact = %f\n", diagfact);
99 diagfact = sqrtf (3.);//D WHY??? 132 diagfact = sqrtf (3.);//D WHY???
100 //printf ("diagfact = %f\n", diagfact);
101 133
102 glMultMatrixf (m); 134 glMultMatrixf (m);
103 glTranslatef (-p.x, -p.y, -p.z); 135
104
105 glGetFloatv (GL_PROJECTION_MATRIX, m); 136 glGetFloatv (GL_PROJECTION_MATRIX, m);
106 137
107 frustum.l = plane ( m(3,0) + m(0,0), m(3,1) + m(0,1), m(3,2) + m(0,2), m(3,3) + m(0,3) ); 138 frustum.l = plane ( m(3,0) + m(0,0), m(3,1) + m(0,1), m(3,2) + m(0,2), m(3,3) + m(0,3) );
108 frustum.r = plane ( m(3,0) - m(0,0), m(3,1) - m(0,1), m(3,2) - m(0,2), m(3,3) - m(0,3) ); 139 frustum.r = plane ( m(3,0) - m(0,0), m(3,1) - m(0,1), m(3,2) - m(0,2), m(3,3) - m(0,3) );
109 frustum.b = plane ( m(3,0) + m(1,0), m(3,1) + m(1,1), m(3,2) + m(1,2), m(3,3) + m(1,3) ); 140 frustum.b = plane ( m(3,0) + m(1,0), m(3,1) + m(1,1), m(3,2) + m(1,2), m(3,3) + m(1,3) );
110 frustum.t = plane ( m(3,0) - m(1,0), m(3,1) - m(1,1), m(3,2) - m(1,2), m(3,3) - m(1,3) ); 141 frustum.t = plane ( m(3,0) - m(1,0), m(3,1) - m(1,1), m(3,2) - m(1,2), m(3,3) - m(1,3) );
111 frustum.n = plane ( m(3,0) + m(2,0), m(3,1) + m(2,1), m(3,2) + m(2,2), m(3,3) + m(2,3) ); 142 frustum.n = plane ( m(3,0) + m(2,0), m(3,1) + m(2,1), m(3,2) + m(2,2), m(3,3) + m(2,3) );
112 frustum.f = plane ( m(3,0) - m(2,0), m(3,1) - m(2,1), m(3,2) - m(2,2), m(3,3) - m(2,3) ); 143 frustum.f = plane ( m(3,0) - m(2,0), m(3,1) - m(2,1), m(3,2) - m(2,2), m(3,3) - m(2,3) );
113 144
145 {
146 GLdouble frustlen = z_far - z_near;
147 GLdouble fheight = frustlen * ftan;
148 GLdouble fwidth = fheight * w / h;
149 point half (0, 0, z_near + frustlen * .5);
150 point corner (fwidth, fheight, frustlen);
151
152 frustum.s = sphere (p + d * (.5 * frustlen), length (corner - half));
153 }
154
155 {
156 GLdouble depth = h / ftan;
157
158 frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan / h));
159 }
160
114 glMatrixMode (GL_MODELVIEW); 161 glMatrixMode (GL_MODELVIEW);
115 glLoadIdentity (); 162 glLoadIdentity ();
163 glTranslatef (-p.x, -p.y, -p.z);
116} 164}
117 165
118void view::begin () 166void view::begin ()
119{ 167{
120 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 168 generation++;
121 glDepthRange (0, 1. - (1. / pow (2., 16.)));
122 169
123 vislist.clear (); 170 vislist.clear ();
124 171
125 generation++; 172 z_near = max (nz_near, 1.F);
126 173 z_far = max (nz_far, z_near * 2.F);
127 farlist.clear ();
128
129 // check occlusion queries
130 for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i)
131 {
132 occ_query oq(*this, i->second, occ_query_result (i->second));
133 i->first->event (oq);
134 }
135
136 occ_queries.clear ();
137
138 z_far = nz_far;
139 c_far = nc_far; 174 c_far = nc_far;
175
140 reset_projection (); 176 reset_projection ();
141 177
178 nz_near = 100.;
142 nc_far = nz_far = z_near + 1.F; 179 nc_far = nz_far = 1.F;
143 world.detect_visibility (*this);
144
145 printf ("far %f cf %f RCP %f,%f,%f +%f\n", z_far, c_far,
146 frustum.r.n.x,
147 frustum.r.n.y,
148 frustum.r.n.z,
149 frustum.r.d
150 );//D
151} 180}
152 181
153void view::end () 182void view::end ()
154{ 183{
155 vislist.clear (); 184 vislist.clear ();
156} 185}
157 186
158void view::pass (enum mode m) 187#define DEPTH_OFFSET (1. / (GLdouble)(1L << 16))
159{
160 mode = m;
161 188
162 glDisable (GL_ALPHA_TEST); 189void view::render (enum pass_type p, pass &data)
163 glDisable (GL_BLEND); 190{
191 pass_type = p;
192 pass_data = &data;
164 193
165 if (mode == view::DEPTH) 194 switch (pass_type)
166 { 195 {
196 case DEPTH:
197 glColorMask (1, 1, 1, 1);
198 glDepthMask (1);
199
200 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
201
202 //glEnable (GL_STENCIL_TEST); // for depth-passes
203 //glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE);
204 //glStencilFunc (GL_LESS, 1, 255);
205
167 glEnable (GL_POLYGON_OFFSET_FILL); 206 //glEnable (GL_POLYGON_OFFSET_FILL);
168 glPolygonOffset (0, 1); 207 //glPolygonOffset (0, 5);
208 glDisable (GL_MINMAX);
209 glDepthRange (DEPTH_OFFSET, 1.);
169 glDepthFunc (GL_LESS); 210 glDepthFunc (GL_LESS);
170 glDisable (GL_LIGHTING); 211 glEnable (GL_DEPTH_TEST);
171 glColorMask (0, 0, 0, 0); 212 glColorMask (0, 0, 0, 0);
172 cgGLDisableProfile (vsh_profile);// z-fighting?? 213 glDisable (GL_DEPTH_CLAMP_NV);
173 cgGLDisableProfile (fsh_profile); 214
174 } 215 world.detect_visibility (*this);
175 else 216
176 { 217 break;
177 glEnable (GL_MINMAX); 218
178 glDisable (GL_POLYGON_OFFSET_FILL); 219 case POSTDEPTH:
220 glDepthRange (0., 1. - DEPTH_OFFSET);
179 glDepthFunc (GL_LESS); 221 glDepthFunc (GL_LESS);
222 glColorMask (0, 0, 0, 0);
180 glDepthMask (1); 223 glDepthMask (0);
181 cgGLEnableProfile (vsh_profile); 224 glEnable (GL_DEPTH_CLAMP_NV);
182 cgGLEnableProfile (fsh_profile); 225 glDisable (GL_STENCIL_TEST);
226
227 // check occlusion queries
228 for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i)
229 {
230 occ_query oq(*this, i->data, ::occ_query_result (i->id));
231 i->recv->event (oq);
232 }
233
234 occ_queries.clear ();
235
236 break;
237
238 case LIGHTED:
239 glClear (GL_STENCIL_BUFFER_BIT);
240 //glEnable (GL_STENCIL_TEST);
241 glEnable (GL_MINMAX);
242 glDepthRange (0., 1. - DEPTH_OFFSET);
243 glDepthFunc (GL_LESS);
244 glColorMask (1, 1, 1, 1);
245 glDepthMask (0);
246 glDisable (GL_DEPTH_CLAMP_NV);
247
248 break;
183 } 249 }
184 250
185 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) 251 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i)
186 {
187 visibility_state &vs = vismap[*i];
188 bool oq = (vs.visibility == visibility_state::PARTIAL
189 || vs.visibility == visibility_state::FULL)
190 && (vs.last + 1. < timer.now)
191 && (mode == LIGHTED);
192
193 if (oq)
194 begin_occ_query (**i);
195
196 (*i)->display (*this); 252 (*i)->display (*this);
197 253
198 if (oq) 254 if (pass_type == DEPTH)
199 end_occ_query (); 255 printf ("fps %f NF %f:%f vis %d CAM (%d,%d,%d)\n", timer.fps, z_near, z_far, drawn.size (), orig.x, orig.y, orig.z);//D
200
201 }
202 256
203 drawn.clear (); 257 drawn.clear ();
204 258
259#if 0
205 if (mode == view::DEPTH) 260 if (pass == view::DEPTH)
206 { 261 {
207 glEnable (GL_DEPTH_CLAMP_NV); 262 glEnable (GL_DEPTH_CLAMP_NV);
208 glDepthMask (0); 263 glDepthMask (0);
209 glDepthFunc (GL_LESS); 264 glDepthFunc (GL_LESS);
210 if (mode == view::LIGHTED) glDepthFunc (GL_LEQUAL);
211 glDisable (GL_LIGHTING);
212 cgGLDisableProfile (vsh_profile);
213 cgGLDisableProfile (fsh_profile);
214
215#if 0
216 static int count; count++;
217 if (farlist.size ())
218 printf ("%d: size %d\n", count, farlist.size ());
219#endif
220 265
221 for (vector<octant *>::iterator i = farlist.begin (); i != farlist.end (); ++i) 266 for (vector<octant *>::iterator i = farlist.begin (); i != farlist.end (); ++i)
222 { 267 {
223 if (mode == view::DEPTH) begin_occ_query (**i); 268 begin_occ_query (**i);
224 if (mode == view::LIGHTED) glColor3f ((((long)*i >> 6) & 15) / 31. + 0.5,0,0);
225 (*i)->draw_bbox (*this); 269 (*i)->draw_bbox (*this);
226 if (mode == view::DEPTH) end_occ_query (); 270 end_occ_query ();
227 } 271 }
228 272
229 glDisable (GL_DEPTH_CLAMP_NV); 273 glDisable (GL_DEPTH_CLAMP_NV);
230 glDepthMask (1);
231 } 274 }
232 275#endif
233 glColorMask (1, 1, 1, 0);
234 glDepthMask (1);
235
236} 276}
237 277
278void light::enable ()
279{
280 lightpos->set (p);
281}
238 282
283void light::disable ()
284{
285}
286
287static shader::varying_1f camdist;
288static shader::varying_3f lightvec;
289
290void linear_light::vsh ()
291{
292 using namespace shader::compile;
293
294 lightvec = xyz (lightpos - model_view_matrix * vin.vertex);
295 camdist = max (1 - length (lightvec) / radius, 0);
296}
297
298shader::temp_3f linear_light::operator ()()
299{
300 using namespace shader::compile;
301
302 temp_3f res;
303 res = float3 (c.r / 255.F, c.g / 255.F, c.b / 255.F) * (min (intensity * camdist * 0.6F + 0.9F, 1.F));
304
305 return res;
306}
307
308

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines