ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/view.C
Revision: 1.49
Committed: Mon Oct 11 15:26:34 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.48: +2 -1 lines
Log Message:
*** empty log message ***

File Contents

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