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

Comparing libgender/view.C (file contents):
Revision 1.38 by root, Sat Oct 9 21:25:18 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 = 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, near, 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
119
87 vec3 rz = -d; 120 vec3 rz = -d;
88 vec3 rx = cross (u, rz); 121 vec3 rx = cross (u, rz);
89 vec3 ry = cross (rz, rx); 122 vec3 ry = cross (rz, rx);
90 123
91 matrix &m = projection; 124 matrix &m = projection;
93 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;
94 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;
95 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;
96 129
97 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);
132 diagfact = sqrtf (3.);//D WHY???
98 133
99 glMultMatrixf (m); 134 glMultMatrixf (m);
100 glTranslatef (-p.x, -p.y, -p.z); 135
101
102 glGetFloatv (GL_PROJECTION_MATRIX, m); 136 glGetFloatv (GL_PROJECTION_MATRIX, m);
103 137
104 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) );
105 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) );
106 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) );
107 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) );
108 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) );
109 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) );
110 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
111 glMatrixMode (GL_MODELVIEW); 161 glMatrixMode (GL_MODELVIEW);
112 glLoadIdentity (); 162 glLoadIdentity ();
163 glTranslatef (-p.x, -p.y, -p.z);
113} 164}
114 165
115void view::begin () 166void view::begin ()
116{ 167{
117 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 168 generation++;
118 glDepthRange (0, 1. - (1. / pow (2., 16.)));
119 169
120 vismap.clear ();
121 vislist.clear (); 170 vislist.clear ();
122 171
123 generation++; 172 z_near = max (nz_near, 1.F);
173 z_far = max (nz_far, z_near * 2.F);
174 c_far = nc_far;
124 175
125 reset_projection (); 176 reset_projection ();
126 177
127 farlist.clear (); 178 nz_near = 100.;
128 179 nc_far = nz_far = 1.F;
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 nextfar = near;
139 world.detect_visibility (*this);
140 printf ("far %f nf %f RCP %f,%f,%f +%f\n", far, nextfar,
141 frustum.r.n.x,
142 frustum.r.n.y,
143 frustum.r.n.z,
144 frustum.r.d
145 );//D
146 far = nextfar;
147
148 reset_projection ();
149} 180}
150 181
151void view::end () 182void view::end ()
152{ 183{
153 vismap.clear (); 184 vislist.clear ();
154} 185}
155 186
156void view::pass (enum mode m) 187#define DEPTH_OFFSET (1. / (GLdouble)(1L << 16))
157{
158 mode = m;
159 188
160 glDisable (GL_ALPHA_TEST); 189void view::render (enum pass_type p, pass &data)
161 glDisable (GL_BLEND); 190{
191 pass_type = p;
192 pass_data = &data;
162 193
163 if (mode == view::DEPTH) 194 switch (pass_type)
164 { 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
165 glEnable (GL_POLYGON_OFFSET_FILL); 206 //glEnable (GL_POLYGON_OFFSET_FILL);
166 glPolygonOffset (0, 1); 207 //glPolygonOffset (0, 5);
208 glDisable (GL_MINMAX);
209 glDepthRange (DEPTH_OFFSET, 1.);
167 glDepthFunc (GL_LESS); 210 glDepthFunc (GL_LESS);
168 glDisable (GL_LIGHTING); 211 glEnable (GL_DEPTH_TEST);
169 glColorMask (0, 0, 0, 0); 212 glColorMask (0, 0, 0, 0);
170 cgGLDisableProfile (vsh_profile);// z-fighting?? 213 glDisable (GL_DEPTH_CLAMP_NV);
171 cgGLDisableProfile (fsh_profile); 214
172 } 215 world.detect_visibility (*this);
173 else 216
174 { 217 break;
175 glEnable (GL_MINMAX); 218
176 glDisable (GL_POLYGON_OFFSET_FILL); 219 case POSTDEPTH:
220 glDepthRange (0., 1. - DEPTH_OFFSET);
177 glDepthFunc (GL_LESS); 221 glDepthFunc (GL_LESS);
222 glColorMask (0, 0, 0, 0);
178 glDepthMask (1); 223 glDepthMask (0);
179 cgGLEnableProfile (vsh_profile); 224 glEnable (GL_DEPTH_CLAMP_NV);
180 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;
181 } 249 }
182 250
183 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) 251 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i)
184 (*i)->display (*this); 252 (*i)->display (*this);
185 253
254 if (pass_type == DEPTH)
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
256
186 drawn.clear (); 257 drawn.clear ();
187 258
259#if 0
188 if (mode == view::DEPTH || 1) 260 if (pass == view::DEPTH)
189 { 261 {
190 glEnable (GL_DEPTH_CLAMP_NV); 262 glEnable (GL_DEPTH_CLAMP_NV);
263 glDepthMask (0);
191 glDepthFunc (GL_LESS); 264 glDepthFunc (GL_LESS);
192 if (mode == view::LIGHTED) glDepthFunc (GL_LEQUAL);
193 glDisable (GL_LIGHTING);
194 cgGLDisableProfile (vsh_profile);
195 cgGLDisableProfile (fsh_profile);
196 //glShadeModel (GL_FLAT);
197
198#if 0
199 static int count; count++;
200 if (farlist.size ())
201 printf ("%d: size %d\n", count, farlist.size ());
202#endif
203 265
204 for (vector<octant *>::iterator i = farlist.begin (); i != farlist.end (); ++i) 266 for (vector<octant *>::iterator i = farlist.begin (); i != farlist.end (); ++i)
205 { 267 {
206 if (mode == view::DEPTH) begin_occ_query (**i); 268 begin_occ_query (**i);
207 if (mode == view::LIGHTED) glColor3f ((((long)*i >> 6) & 15) / 31. + 0.5,0,0);
208 (*i)->draw_bbox (*this); 269 (*i)->draw_bbox (*this);
209 if (mode == view::DEPTH) end_occ_query (); 270 end_occ_query ();
210 } 271 }
211 272
212 glEnable (GL_DEPTH_TEST);
213 glDisable (GL_DEPTH_CLAMP_NV); 273 glDisable (GL_DEPTH_CLAMP_NV);
214 //glShadeModel (GL_SMOOTH);
215 } 274 }
216 275#endif
217 glColorMask (1, 1, 1, 0);
218 glDepthMask (1);
219
220} 276}
221 277
278void light::enable ()
279{
280 lightpos->set (p);
281}
222 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