ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/view.C
Revision: 1.75
Committed: Sun Nov 7 02:28:18 2004 UTC (19 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.74: +14 -7 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include <cmath>
2    
3 root 1.45 #include "opengl.h"
4 root 1.26
5 root 1.37 #include "view.h"
6 root 1.1 #include "oct.h"
7    
8 root 1.55 using namespace gl;
9    
10 root 1.68 pass pass_depth (0);
11    
12 root 1.37 vector<GLuint> occ_query_objects;
13    
14     static GLuint begin_occ_query ()
15     {
16     GLuint id;
17    
18     if (occ_query_objects.size ())
19     {
20     id = *(occ_query_objects.end () - 1);
21     occ_query_objects.pop_back ();
22     }
23     else
24 root 1.66 glGenQueries (1, &id);
25 root 1.37
26 root 1.66 glBeginQuery (GL_SAMPLES_PASSED, id);
27 root 1.37 return id;
28     }
29    
30     inline void end_occ_query ()
31     {
32 root 1.66 glEndQuery (GL_SAMPLES_PASSED);
33 root 1.37 }
34    
35     static GLuint occ_query_result (GLuint id)
36     {
37     GLuint count;
38    
39 root 1.66 glGetQueryObjectuiv (id, GL_QUERY_RESULT, &count);
40 root 1.37 occ_query_objects.push_back (id);
41    
42     return count;
43     }
44    
45 root 1.17 view::view ()
46 root 1.60 : gamma(1.0), nz_near (1.F), nz_far (2.F)
47 root 1.1 {
48     }
49    
50 root 1.17 view::~view ()
51 root 1.1 {
52     }
53    
54 root 1.51 void view::begin_occ_query (recv_occ_query &recv, void *id)
55 root 1.37 {
56 root 1.51 occ_queries.push_back (oq_data (&recv, ::begin_occ_query (), id));
57 root 1.37 }
58    
59     void view::end_occ_query ()
60     {
61     ::end_occ_query ();
62     }
63    
64 root 1.34 bool view::may_draw (const entity *e)
65 root 1.1 {
66     if (drawn.find (e) != drawn.end ())
67     return false;
68    
69     drawn.insert (e);
70     return true;
71     }
72    
73 root 1.51 visibility_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    
99 root 1.18 void view::reset_projection ()
100 root 1.1 {
101 root 1.42 renormalize (orig, p);
102 root 1.3
103 root 1.1 glViewport (0, 0, w, h);
104    
105     glMatrixMode (GL_PROJECTION);
106     glLoadIdentity ();
107    
108     GLdouble aspect = (GLdouble)w/h;
109 root 1.59 GLdouble ftan = tan (fov * (.5 * M_PI / 180.));
110     GLdouble ymax = z_near * ftan;
111     GLdouble xmax = ymax * aspect;
112 root 1.1
113 root 1.72 matrix perspective;
114 root 1.74 #if 1
115 root 1.72 {
116     matrix &m = perspective;
117    
118 root 1.75 m(0,0) = 1.F / xmax; m(0,1) = 0; m(0,2) = 0; m(0,3) = 0;
119     m(1,0) = 0; m(1,1) = 1.F / ymax; m(1,2) = 0; m(1,3) = 0;
120     m(2,0) = 0; m(2,1) = 0; m(2,2) = -1e10F / z_far; m(2,3) = 1;
121     m(3,0) = 0; m(3,1) = 0; m(3,2) = -1; m(3,3) = 0;
122 root 1.74
123     glLoadMatrixf (perspective);
124 root 1.72 }
125     #else
126 root 1.59 glFrustum (-xmax, xmax, -ymax, ymax, z_near, z_far);
127 root 1.72 #endif
128    
129     glGetFloatv (GL_PROJECTION_MATRIX, perspective);
130 root 1.1
131 root 1.59 perspfact = z_near / ymax * 0.5F * h;
132 root 1.48
133 root 1.72 glMatrixMode (GL_MODELVIEW);
134     glLoadIdentity ();
135     matrix &m = projection;
136    
137 root 1.6 d = normalize (d);//D
138     u = normalize (u);//D
139 root 1.41
140 root 1.1 vec3 rz = -d;
141     vec3 rx = cross (u, rz);
142     vec3 ry = cross (rz, rx);
143    
144 root 1.16 m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0;
145     m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0;
146     m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0;
147 root 1.3 m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1;
148 root 1.37
149 root 1.38 diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z);
150 root 1.48 //printf ("diagfact = %f\n", diagfact);
151 root 1.41 diagfact = sqrtf (3.);//D WHY???
152 root 1.3
153 root 1.38 glMultMatrixf (m);
154 root 1.63
155 root 1.72 glGetFloatv (GL_MODELVIEW_MATRIX, m);
156    
157     m = perspective * m;
158 root 1.5
159 root 1.17 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) );
160     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) );
161     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) );
162     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) );
163     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) );
164     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) );
165 root 1.1
166 root 1.59 {
167     GLdouble frustlen = z_far - z_near;
168     GLdouble fheight = frustlen * ftan;
169     GLdouble fwidth = fheight * w / h;
170     point half (0, 0, z_near + frustlen * .5);
171     point corner (fwidth, fheight, frustlen);
172    
173     frustum.s = sphere (p + d * (.5 * frustlen), length (corner - half));
174     }
175    
176     {
177     GLdouble depth = h / ftan;
178    
179     frustum.c = cone (p, d, atan (sqrt (GLdouble (w * w + h * h)) * ftan / h));
180     }
181    
182 root 1.67 glTranslatef (-p.x, -p.y, -p.z);
183 root 1.18 }
184    
185     void view::begin ()
186     {
187 root 1.53 generation++;
188    
189 root 1.19 vislist.clear ();
190 root 1.18
191 root 1.60 z_near = max (nz_near, 1.F);
192     z_far = max (nz_far, z_near * 2.F);
193 root 1.43 c_far = nc_far;
194 root 1.48
195 root 1.43 reset_projection ();
196    
197 root 1.65 nz_near = 100.;
198 root 1.60 nc_far = nz_far = 1.F;
199 root 1.18 }
200    
201     void view::end ()
202     {
203 root 1.40 vislist.clear ();
204 root 1.18 }
205    
206 root 1.50 #define DEPTH_OFFSET (1. / (GLdouble)(1L << 16))
207    
208 root 1.68 void view::render (enum pass_type p, pass &data)
209 root 1.18 {
210 root 1.68 pass_type = p;
211     pass_data = &data;
212 root 1.18
213 root 1.68 switch (pass_type)
214 root 1.18 {
215 root 1.52 case DEPTH:
216 root 1.63 glColorMask (1, 1, 1, 1);
217     glDepthMask (1);
218 root 1.75 glDisable (GL_BLEND);
219 root 1.63
220 root 1.75 //glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
221     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
222 root 1.63
223     //glEnable (GL_STENCIL_TEST); // for depth-passes
224     //glStencilOp (GL_KEEP, GL_KEEP, GL_REPLACE);
225     //glStencilFunc (GL_LESS, 1, 255);
226    
227 root 1.52 //glEnable (GL_POLYGON_OFFSET_FILL);
228     //glPolygonOffset (0, 5);
229 root 1.75 glEnable (GL_CULL_FACE);
230 root 1.52 glDisable (GL_MINMAX);
231     glDepthRange (DEPTH_OFFSET, 1.);
232     glDepthFunc (GL_LESS);
233     glEnable (GL_DEPTH_TEST);
234     glColorMask (0, 0, 0, 0);
235 root 1.59 glDisable (GL_DEPTH_CLAMP_NV);
236 root 1.54
237 root 1.61 world.detect_visibility (*this);
238 root 1.54
239 root 1.52 break;
240    
241     case POSTDEPTH:
242 root 1.59 glDepthRange (0., 1. - DEPTH_OFFSET);
243 root 1.57 glDepthFunc (GL_LESS);
244 root 1.52 glColorMask (0, 0, 0, 0);
245     glDepthMask (0);
246 root 1.59 glEnable (GL_DEPTH_CLAMP_NV);
247 root 1.63 glDisable (GL_STENCIL_TEST);
248 root 1.75 glDisable (GL_CULL_FACE);
249 root 1.53
250     // check occlusion queries
251     for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i)
252     {
253     occ_query oq(*this, i->data, ::occ_query_result (i->id));
254     i->recv->event (oq);
255     }
256    
257     occ_queries.clear ();
258    
259 root 1.52 break;
260    
261     case LIGHTED:
262 root 1.75 glEnable (GL_CULL_FACE);
263     //glEnable (GL_BLEND);//TODO, only when not first lighted pass
264     glBlendFunc (GL_ONE, GL_ONE);
265     //glClear (GL_STENCIL_BUFFER_BIT);
266 root 1.63 //glEnable (GL_STENCIL_TEST);
267 root 1.52 glEnable (GL_MINMAX);
268     glDepthRange (0., 1. - DEPTH_OFFSET);
269     glDepthFunc (GL_LESS);
270 root 1.53 glColorMask (1, 1, 1, 1);
271 root 1.52 glDepthMask (0);
272 root 1.59 glDisable (GL_DEPTH_CLAMP_NV);
273 root 1.54
274 root 1.52 break;
275 root 1.18 }
276    
277 root 1.61 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i)
278     (*i)->display (*this);
279 root 1.62
280 root 1.68 if (pass_type == DEPTH)
281 root 1.62 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
282 root 1.61
283 root 1.56 drawn.clear ();
284    
285 root 1.52 #if 0
286 root 1.56 if (pass == view::DEPTH)
287 root 1.25 {
288 root 1.27 glEnable (GL_DEPTH_CLAMP_NV);
289 root 1.42 glDepthMask (0);
290 root 1.30 glDepthFunc (GL_LESS);
291 root 1.25
292 root 1.37 for (vector<octant *>::iterator i = farlist.begin (); i != farlist.end (); ++i)
293     {
294 root 1.48 begin_occ_query (**i);
295 root 1.37 (*i)->draw_bbox (*this);
296 root 1.48 end_occ_query ();
297 root 1.30 }
298 root 1.27
299     glDisable (GL_DEPTH_CLAMP_NV);
300 root 1.25 }
301 root 1.52 #endif
302 root 1.1 }
303 root 1.6
304 root 1.69 void light::enable ()
305     {
306     lightpos->set (p);
307     }
308    
309     void light::disable ()
310     {
311     }
312    
313     static shader::varying_1f camdist;
314     static shader::varying_3f lightvec;
315    
316     void linear_light::vsh ()
317     {
318     using namespace shader::compile;
319    
320     lightvec = xyz (lightpos - model_view_matrix * vin.vertex);
321     camdist = max (1 - length (lightvec) / radius, 0);
322 root 1.73
323     fsh ();
324 root 1.69 }
325    
326 root 1.73 void linear_light::fsh ()
327 root 1.69 {
328     using namespace shader::compile;
329    
330 root 1.73 sh_lightvec = lightvec;
331 root 1.75 sh_colour = float3 (c.r / 255.F, c.g / 255.F, c.b / 255.F) * (min (intensity * camdist * 100.F + 0.9F, 1.F));
332 root 1.69 }
333    
334 root 1.1