1 |
root |
1.1 |
#include <cmath> |
2 |
|
|
|
3 |
root |
1.37 |
#define GL_GLEXT_PROTOTYPES |
4 |
root |
1.26 |
#include <GL/gl.h> |
5 |
|
|
#include <GL/glext.h> |
6 |
|
|
|
7 |
root |
1.37 |
#include "view.h" |
8 |
root |
1.1 |
#include "oct.h" |
9 |
|
|
|
10 |
root |
1.37 |
vector<GLuint> occ_query_objects; |
11 |
|
|
|
12 |
|
|
static 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 |
|
|
|
28 |
|
|
inline void end_occ_query () |
29 |
|
|
{ |
30 |
|
|
glEndQueryARB (GL_SAMPLES_PASSED); |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
static 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 |
|
|
|
43 |
root |
1.17 |
view::view () |
44 |
root |
1.18 |
: gamma(1.0) |
45 |
root |
1.1 |
{ |
46 |
|
|
} |
47 |
|
|
|
48 |
root |
1.17 |
view::~view () |
49 |
root |
1.1 |
{ |
50 |
|
|
} |
51 |
|
|
|
52 |
root |
1.37 |
void view::begin_occ_query (recv_occ_query &recv) |
53 |
|
|
{ |
54 |
|
|
occ_queries.push_back (oq_data (&recv, ::begin_occ_query ())); |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
void view::end_occ_query () |
58 |
|
|
{ |
59 |
|
|
::end_occ_query (); |
60 |
|
|
} |
61 |
|
|
|
62 |
root |
1.34 |
bool view::may_draw (const entity *e) |
63 |
root |
1.1 |
{ |
64 |
|
|
if (drawn.find (e) != drawn.end ()) |
65 |
|
|
return false; |
66 |
|
|
|
67 |
|
|
drawn.insert (e); |
68 |
|
|
return true; |
69 |
|
|
} |
70 |
|
|
|
71 |
root |
1.18 |
void view::reset_projection () |
72 |
root |
1.1 |
{ |
73 |
root |
1.40 |
renormalize (orig, p); |
74 |
root |
1.3 |
|
75 |
root |
1.1 |
glViewport (0, 0, w, h); |
76 |
|
|
|
77 |
|
|
glMatrixMode (GL_PROJECTION); |
78 |
|
|
glLoadIdentity (); |
79 |
|
|
|
80 |
|
|
GLdouble aspect = (GLdouble)w/h; |
81 |
root |
1.38 |
GLdouble ymax = near * tan (fov * (M_PI / 360.0)); |
82 |
root |
1.1 |
|
83 |
root |
1.37 |
glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, near, far); |
84 |
root |
1.1 |
|
85 |
root |
1.6 |
d = normalize (d);//D |
86 |
|
|
u = normalize (u);//D |
87 |
root |
1.1 |
vec3 rz = -d; |
88 |
|
|
vec3 rx = cross (u, rz); |
89 |
|
|
vec3 ry = cross (rz, rx); |
90 |
|
|
|
91 |
root |
1.17 |
matrix &m = projection; |
92 |
root |
1.16 |
m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0; |
93 |
|
|
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; |
95 |
root |
1.3 |
m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; |
96 |
root |
1.37 |
|
97 |
root |
1.38 |
diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z); |
98 |
root |
1.3 |
|
99 |
root |
1.38 |
glMultMatrixf (m); |
100 |
|
|
glTranslatef (-p.x, -p.y, -p.z); |
101 |
root |
1.6 |
|
102 |
root |
1.38 |
glGetFloatv (GL_PROJECTION_MATRIX, m); |
103 |
root |
1.5 |
|
104 |
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) ); |
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) ); |
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) ); |
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) ); |
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) ); |
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) ); |
110 |
root |
1.1 |
|
111 |
root |
1.13 |
glMatrixMode (GL_MODELVIEW); |
112 |
root |
1.12 |
glLoadIdentity (); |
113 |
root |
1.18 |
} |
114 |
|
|
|
115 |
|
|
void view::begin () |
116 |
|
|
{ |
117 |
root |
1.30 |
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
118 |
|
|
glDepthRange (0, 1. - (1. / pow (2., 16.))); |
119 |
|
|
|
120 |
root |
1.19 |
vislist.clear (); |
121 |
root |
1.18 |
|
122 |
|
|
generation++; |
123 |
root |
1.12 |
|
124 |
root |
1.18 |
reset_projection (); |
125 |
|
|
|
126 |
root |
1.31 |
farlist.clear (); |
127 |
|
|
|
128 |
root |
1.37 |
// check occlusion queries |
129 |
|
|
for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i) |
130 |
|
|
{ |
131 |
|
|
occ_query oq(*this, i->second, occ_query_result (i->second)); |
132 |
|
|
i->first->event (oq); |
133 |
|
|
} |
134 |
root |
1.20 |
|
135 |
root |
1.37 |
occ_queries.clear (); |
136 |
root |
1.18 |
|
137 |
root |
1.38 |
nextfar = near; |
138 |
root |
1.17 |
world.detect_visibility (*this); |
139 |
root |
1.38 |
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 (); |
148 |
root |
1.18 |
} |
149 |
|
|
|
150 |
|
|
void view::end () |
151 |
|
|
{ |
152 |
root |
1.40 |
vislist.clear (); |
153 |
root |
1.18 |
} |
154 |
|
|
|
155 |
|
|
void view::pass (enum mode m) |
156 |
|
|
{ |
157 |
|
|
mode = m; |
158 |
|
|
|
159 |
|
|
glDisable (GL_ALPHA_TEST); |
160 |
|
|
glDisable (GL_BLEND); |
161 |
root |
1.4 |
|
162 |
root |
1.18 |
if (mode == view::DEPTH) |
163 |
|
|
{ |
164 |
|
|
glEnable (GL_POLYGON_OFFSET_FILL); |
165 |
|
|
glPolygonOffset (0, 1); |
166 |
|
|
glDepthFunc (GL_LESS); |
167 |
|
|
glDisable (GL_LIGHTING); |
168 |
|
|
glColorMask (0, 0, 0, 0); |
169 |
root |
1.38 |
cgGLDisableProfile (vsh_profile);// z-fighting?? |
170 |
root |
1.23 |
cgGLDisableProfile (fsh_profile); |
171 |
root |
1.18 |
} |
172 |
|
|
else |
173 |
|
|
{ |
174 |
|
|
glEnable (GL_MINMAX); |
175 |
|
|
glDisable (GL_POLYGON_OFFSET_FILL); |
176 |
|
|
glDepthFunc (GL_LESS); |
177 |
root |
1.38 |
glDepthMask (1); |
178 |
root |
1.23 |
cgGLEnableProfile (vsh_profile); |
179 |
|
|
cgGLEnableProfile (fsh_profile); |
180 |
root |
1.18 |
} |
181 |
|
|
|
182 |
root |
1.17 |
for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) |
183 |
root |
1.40 |
{ |
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 |
|
|
|
193 |
|
|
(*i)->display (*this); |
194 |
|
|
|
195 |
|
|
if (oq) |
196 |
|
|
end_occ_query (); |
197 |
|
|
|
198 |
|
|
} |
199 |
root |
1.17 |
|
200 |
|
|
drawn.clear (); |
201 |
root |
1.24 |
|
202 |
root |
1.39 |
if (mode == view::DEPTH) |
203 |
root |
1.25 |
{ |
204 |
root |
1.27 |
glEnable (GL_DEPTH_CLAMP_NV); |
205 |
root |
1.30 |
glDepthFunc (GL_LESS); |
206 |
root |
1.37 |
if (mode == view::LIGHTED) glDepthFunc (GL_LEQUAL); |
207 |
root |
1.30 |
glDisable (GL_LIGHTING); |
208 |
root |
1.27 |
cgGLDisableProfile (vsh_profile); |
209 |
|
|
cgGLDisableProfile (fsh_profile); |
210 |
root |
1.37 |
//glShadeModel (GL_FLAT); |
211 |
root |
1.27 |
|
212 |
root |
1.33 |
#if 0 |
213 |
root |
1.25 |
static int count; count++; |
214 |
|
|
if (farlist.size ()) |
215 |
|
|
printf ("%d: size %d\n", count, farlist.size ()); |
216 |
root |
1.33 |
#endif |
217 |
root |
1.25 |
|
218 |
root |
1.37 |
for (vector<octant *>::iterator i = farlist.begin (); i != farlist.end (); ++i) |
219 |
|
|
{ |
220 |
|
|
if (mode == view::DEPTH) begin_occ_query (**i); |
221 |
root |
1.38 |
if (mode == view::LIGHTED) glColor3f ((((long)*i >> 6) & 15) / 31. + 0.5,0,0); |
222 |
root |
1.37 |
(*i)->draw_bbox (*this); |
223 |
|
|
if (mode == view::DEPTH) end_occ_query (); |
224 |
root |
1.30 |
} |
225 |
root |
1.27 |
|
226 |
|
|
glEnable (GL_DEPTH_TEST); |
227 |
|
|
glDisable (GL_DEPTH_CLAMP_NV); |
228 |
root |
1.37 |
//glShadeModel (GL_SMOOTH); |
229 |
root |
1.25 |
} |
230 |
root |
1.4 |
|
231 |
|
|
glColorMask (1, 1, 1, 0); |
232 |
|
|
glDepthMask (1); |
233 |
root |
1.27 |
|
234 |
root |
1.1 |
} |
235 |
root |
1.6 |
|
236 |
root |
1.1 |
|