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

Comparing libgender/view.C (file contents):
Revision 1.36 by root, Sat Oct 9 02:08:29 2004 UTC vs.
Revision 1.40 by root, Sat Oct 9 22:43:31 2004 UTC

1#include <cmath> 1#include <cmath>
2 2
3#define GL_GLEXT_PROTOTYPES
3#include <GL/gl.h> 4#include <GL/gl.h>
4#include <GL/glext.h> 5#include <GL/glext.h>
5 6
7#include "view.h"
6#include "oct.h" 8#include "oct.h"
7#include "view.h"
8 9
9 static GLenum gl_error; 10vector<GLuint> occ_query_objects;
11
12static GLuint begin_occ_query ()
13{
14 GLuint id;
15
16 if (occ_query_objects.size ())
17 {
18 id = *(occ_query_objects.end () - 1);
19 occ_query_objects.pop_back ();
20 }
21 else
22 glGenQueriesARB (1, &id);
23
24 glBeginQueryARB (GL_SAMPLES_PASSED, id);
25 return id;
26}
27
28inline void end_occ_query ()
29{
30 glEndQueryARB (GL_SAMPLES_PASSED);
31}
32
33static GLuint occ_query_result (GLuint id)
34{
35 GLuint count;
36
37 glGetQueryObjectuivARB (id, GL_QUERY_RESULT, &count);
38 occ_query_objects.push_back (id);
39
40 return count;
41}
42
10view::view () 43view::view ()
11: gamma(1.0) 44: gamma(1.0)
12{ 45{
13} 46}
14 47
15view::~view () 48view::~view ()
16{ 49{
17} 50}
18 51
52void view::begin_occ_query (recv_occ_query &recv)
53{
54 occ_queries.push_back (oq_data (&recv, ::begin_occ_query ()));
55}
56
57void view::end_occ_query ()
58{
59 ::end_occ_query ();
60}
61
19bool view::may_draw (const entity *e) 62bool view::may_draw (const entity *e)
20{ 63{
21 if (drawn.find (e) != drawn.end ()) 64 if (drawn.find (e) != drawn.end ())
22 return false; 65 return false;
23 66
32 glViewport (0, 0, w, h); 75 glViewport (0, 0, w, h);
33 76
34 glMatrixMode (GL_PROJECTION); 77 glMatrixMode (GL_PROJECTION);
35 glLoadIdentity (); 78 glLoadIdentity ();
36 79
37 gl_error = glGetError ();
38 GLdouble aspect = (GLdouble)w/h; 80 GLdouble aspect = (GLdouble)w/h;
39 GLdouble zNear = 1.;
40 GLdouble zFar = 500.;
41
42 gl_error = glGetError ();
43 GLdouble ymax = zNear * tan (fov * (M_PI / 360.0)); 81 GLdouble ymax = near * tan (fov * (M_PI / 360.0));
82
44 glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, zNear, zFar); 83 glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, near, far);
45 84
46 gl_error = glGetError ();
47 d = normalize (d);//D 85 d = normalize (d);//D
48 u = normalize (u);//D 86 u = normalize (u);//D
49 vec3 rz = -d; 87 vec3 rz = -d;
50 vec3 rx = cross (u, rz); 88 vec3 rx = cross (u, rz);
51 vec3 ry = cross (rz, rx); 89 vec3 ry = cross (rz, rx);
52 gl_error = glGetError ();
53 90
54 matrix &m = projection; 91 matrix &m = projection;
55 m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0; 92 m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0;
56 m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0; 93 m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0;
57 m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0; 94 m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0;
58 m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; 95 m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1;
59 gl_error = glGetError ();
60 96
61 glMultMatrixf ((GLfloat *)m.data); 97 diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z);
62 98
63 gl_error = glGetError (); 99 glMultMatrixf (m);
100 glTranslatef (-p.x, -p.y, -p.z);
101
64 glGetFloatv (GL_PROJECTION_MATRIX, (GLfloat *)&m); 102 glGetFloatv (GL_PROJECTION_MATRIX, m);
65 103
66 gl_error = glGetError ();
67 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) ); 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) );
68 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) ); 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) );
69 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) ); 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) );
70 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) ); 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) );
71 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) ); 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) );
72 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) ); 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) );
73 110
74 gl_error = glGetError ();
75 glMatrixMode (GL_MODELVIEW); 111 glMatrixMode (GL_MODELVIEW);
76 glLoadIdentity (); 112 glLoadIdentity ();
77 glTranslatef (-p.x, -p.y, -p.z);
78 gl_error = glGetError ();
79} 113}
80 114
81void view::begin () 115void view::begin ()
82{ 116{
83 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 117 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
84 glDepthRange (0, 1. - (1. / pow (2., 16.))); 118 glDepthRange (0, 1. - (1. / pow (2., 16.)));
85 119
86 vismap.clear ();
87 vislist.clear (); 120 vislist.clear ();
88 121
89 generation++; 122 generation++;
90 123
91 reset_projection (); 124 reset_projection ();
92 125
93 for (vector<octant *>::iterator i = farlist.begin (); i != farlist.end (); ++i)
94 ;
95
96 farlist.clear (); 126 farlist.clear ();
97 127
98 // check occlusion queries here 128 // check occlusion queries
99#if 0
100 for (vector<octant *>::iterator i = checklist.begin (); i != checklist.end (); ++i) 129 for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i)
101 //printf ("%p %d\n", *i, occ_query_result ((*i)->occ_query)); 130 {
102 occ_query_result ((*i)->occ_query); 131 occ_query oq(*this, i->second, occ_query_result (i->second));
132 i->first->event (oq);
133 }
103 134
104 checklist.clear (); 135 occ_queries.clear ();
105#endif
106 136
137 nextfar = near;
107 world.detect_visibility (*this); 138 world.detect_visibility (*this);
139 printf ("far %f nf %f RCP %f,%f,%f +%f\n", far, nextfar,
140 frustum.r.n.x,
141 frustum.r.n.y,
142 frustum.r.n.z,
143 frustum.r.d
144 );//D
145 far = nextfar;
146
147 reset_projection ();
108} 148}
109 149
110void view::end () 150void view::end ()
111{ 151{
112 vismap.clear (); 152 vislist.clear ();
113} 153}
114 154
115void view::pass (enum mode m) 155void view::pass (enum mode m)
116{ 156{
117 mode = m; 157 mode = m;
124 glEnable (GL_POLYGON_OFFSET_FILL); 164 glEnable (GL_POLYGON_OFFSET_FILL);
125 glPolygonOffset (0, 1); 165 glPolygonOffset (0, 1);
126 glDepthFunc (GL_LESS); 166 glDepthFunc (GL_LESS);
127 glDisable (GL_LIGHTING); 167 glDisable (GL_LIGHTING);
128 glColorMask (0, 0, 0, 0); 168 glColorMask (0, 0, 0, 0);
129 cgGLEnableProfile (vsh_profile);// z-fighting?? 169 cgGLDisableProfile (vsh_profile);// z-fighting??
130 cgGLDisableProfile (fsh_profile); 170 cgGLDisableProfile (fsh_profile);
131 } 171 }
132 else 172 else
133 { 173 {
134 glEnable (GL_MINMAX); 174 glEnable (GL_MINMAX);
135 glDisable (GL_POLYGON_OFFSET_FILL); 175 glDisable (GL_POLYGON_OFFSET_FILL);
136 glDepthFunc (GL_LESS); 176 glDepthFunc (GL_LESS);
137 glDepthMask (0); 177 glDepthMask (1);
138 cgGLEnableProfile (vsh_profile); 178 cgGLEnableProfile (vsh_profile);
139 cgGLEnableProfile (fsh_profile); 179 cgGLEnableProfile (fsh_profile);
140 } 180 }
141 181
142 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) 182 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i)
183 {
184 visibility_state &vs = vismap[*i];
185 bool oq = (vs.visibility == visibility_state::PARTIAL
186 || vs.visibility == visibility_state::FULL)
187 && (vs.last + 1. < timer.now)
188 && (mode == LIGHTED);
189
190 if (oq)
191 begin_occ_query (**i);
192
143 (*i)->display (*this); 193 (*i)->display (*this);
194
195 if (oq)
196 end_occ_query ();
197
198 }
144 199
145 drawn.clear (); 200 drawn.clear ();
146 201
147 if (mode == view::DEPTH || 1) 202 if (mode == view::DEPTH)
148 { 203 {
149 glEnable (GL_DEPTH_CLAMP_NV); 204 glEnable (GL_DEPTH_CLAMP_NV);
150 glDepthFunc (GL_LESS); 205 glDepthFunc (GL_LESS);
151 if (1) glDepthFunc (GL_LEQUAL); 206 if (mode == view::LIGHTED) glDepthFunc (GL_LEQUAL);
152 glEnable (GL_COLOR_MATERIAL);
153 glDisable (GL_LIGHTING); 207 glDisable (GL_LIGHTING);
154 cgGLDisableProfile (vsh_profile); 208 cgGLDisableProfile (vsh_profile);
155 cgGLDisableProfile (fsh_profile); 209 cgGLDisableProfile (fsh_profile);
156 glShadeModel (GL_FLAT); 210 //glShadeModel (GL_FLAT);
157 211
158#if 0 212#if 0
159 static int count; count++; 213 static int count; count++;
160 if (farlist.size ()) 214 if (farlist.size ())
161 printf ("%d: size %d\n", count, farlist.size ()); 215 printf ("%d: size %d\n", count, farlist.size ());
162#endif 216#endif
163 217
164 for (vector<octant *>::iterator i = farlist.begin (); i != farlist.end (); ++i) 218 for (vector<octant *>::iterator i = farlist.begin (); i != farlist.end (); ++i)
165 { 219 {
166 glColor3f (1,0,0); 220 if (mode == view::DEPTH) begin_occ_query (**i);
221 if (mode == view::LIGHTED) glColor3f ((((long)*i >> 6) & 15) / 31. + 0.5,0,0);
167 (*i)->draw_bbox (*this); 222 (*i)->draw_bbox (*this);
223 if (mode == view::DEPTH) end_occ_query ();
168 } 224 }
169 225
170 glEnable (GL_DEPTH_TEST); 226 glEnable (GL_DEPTH_TEST);
171 glDisable (GL_DEPTH_CLAMP_NV); 227 glDisable (GL_DEPTH_CLAMP_NV);
172 glShadeModel (GL_SMOOTH); 228 //glShadeModel (GL_SMOOTH);
173 } 229 }
174 230
175 glColorMask (1, 1, 1, 0); 231 glColorMask (1, 1, 1, 0);
176 glDepthMask (1); 232 glDepthMask (1);
177 233

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines