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.37 |
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 |
root |
1.17 |
view::view () |
42 |
root |
1.18 |
: gamma(1.0) |
43 |
root |
1.1 |
{ |
44 |
|
|
} |
45 |
|
|
|
46 |
root |
1.17 |
view::~view () |
47 |
root |
1.1 |
{ |
48 |
|
|
} |
49 |
|
|
|
50 |
root |
1.37 |
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 |
root |
1.34 |
bool view::may_draw (const entity *e) |
61 |
root |
1.1 |
{ |
62 |
|
|
if (drawn.find (e) != drawn.end ()) |
63 |
|
|
return false; |
64 |
|
|
|
65 |
|
|
drawn.insert (e); |
66 |
|
|
return true; |
67 |
|
|
} |
68 |
|
|
|
69 |
root |
1.18 |
void view::reset_projection () |
70 |
root |
1.1 |
{ |
71 |
root |
1.42 |
renormalize (orig, p); |
72 |
root |
1.3 |
|
73 |
root |
1.1 |
glViewport (0, 0, w, h); |
74 |
|
|
|
75 |
|
|
glMatrixMode (GL_PROJECTION); |
76 |
|
|
glLoadIdentity (); |
77 |
|
|
|
78 |
|
|
GLdouble aspect = (GLdouble)w/h; |
79 |
root |
1.42 |
GLdouble ymax = z_near * tan (fov * (M_PI / 360.0)); |
80 |
root |
1.1 |
|
81 |
root |
1.42 |
glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, z_near, z_far); |
82 |
root |
1.1 |
|
83 |
root |
1.6 |
d = normalize (d);//D |
84 |
|
|
u = normalize (u);//D |
85 |
root |
1.41 |
|
86 |
root |
1.1 |
vec3 rz = -d; |
87 |
|
|
vec3 rx = cross (u, rz); |
88 |
|
|
vec3 ry = cross (rz, rx); |
89 |
|
|
|
90 |
root |
1.17 |
matrix &m = projection; |
91 |
root |
1.16 |
m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0; |
92 |
|
|
m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0; |
93 |
|
|
m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0; |
94 |
root |
1.3 |
m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; |
95 |
root |
1.37 |
|
96 |
root |
1.38 |
diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z); |
97 |
root |
1.41 |
diagfact = sqrtf (3.);//D WHY??? |
98 |
|
|
//printf ("diagfact = %f\n", diagfact); |
99 |
root |
1.3 |
|
100 |
root |
1.38 |
glMultMatrixf (m); |
101 |
|
|
glTranslatef (-p.x, -p.y, -p.z); |
102 |
root |
1.6 |
|
103 |
root |
1.38 |
glGetFloatv (GL_PROJECTION_MATRIX, m); |
104 |
root |
1.5 |
|
105 |
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) ); |
106 |
|
|
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) ); |
107 |
|
|
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) ); |
108 |
|
|
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) ); |
109 |
|
|
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) ); |
110 |
|
|
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) ); |
111 |
root |
1.1 |
|
112 |
root |
1.13 |
glMatrixMode (GL_MODELVIEW); |
113 |
root |
1.12 |
glLoadIdentity (); |
114 |
root |
1.18 |
} |
115 |
|
|
|
116 |
|
|
void view::begin () |
117 |
|
|
{ |
118 |
root |
1.30 |
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
119 |
|
|
glDepthRange (0, 1. - (1. / pow (2., 16.))); |
120 |
|
|
|
121 |
root |
1.19 |
vislist.clear (); |
122 |
root |
1.18 |
|
123 |
|
|
generation++; |
124 |
root |
1.12 |
|
125 |
root |
1.31 |
farlist.clear (); |
126 |
|
|
|
127 |
root |
1.37 |
// check occlusion queries |
128 |
|
|
for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i) |
129 |
|
|
{ |
130 |
|
|
occ_query oq(*this, i->second, occ_query_result (i->second)); |
131 |
|
|
i->first->event (oq); |
132 |
|
|
} |
133 |
root |
1.20 |
|
134 |
root |
1.37 |
occ_queries.clear (); |
135 |
root |
1.18 |
|
136 |
root |
1.43 |
z_far = nz_far; |
137 |
|
|
c_far = nc_far; |
138 |
|
|
reset_projection (); |
139 |
|
|
|
140 |
root |
1.42 |
nc_far = nz_far = z_near + 1.F; |
141 |
root |
1.17 |
world.detect_visibility (*this); |
142 |
root |
1.42 |
|
143 |
|
|
printf ("far %f cf %f RCP %f,%f,%f +%f\n", z_far, c_far, |
144 |
root |
1.38 |
frustum.r.n.x, |
145 |
|
|
frustum.r.n.y, |
146 |
|
|
frustum.r.n.z, |
147 |
|
|
frustum.r.d |
148 |
|
|
);//D |
149 |
root |
1.18 |
} |
150 |
|
|
|
151 |
|
|
void view::end () |
152 |
|
|
{ |
153 |
root |
1.40 |
vislist.clear (); |
154 |
root |
1.18 |
} |
155 |
|
|
|
156 |
|
|
void view::pass (enum mode m) |
157 |
|
|
{ |
158 |
|
|
mode = m; |
159 |
|
|
|
160 |
|
|
glDisable (GL_ALPHA_TEST); |
161 |
|
|
glDisable (GL_BLEND); |
162 |
root |
1.4 |
|
163 |
root |
1.18 |
if (mode == view::DEPTH) |
164 |
|
|
{ |
165 |
|
|
glEnable (GL_POLYGON_OFFSET_FILL); |
166 |
|
|
glPolygonOffset (0, 1); |
167 |
|
|
glDepthFunc (GL_LESS); |
168 |
|
|
glDisable (GL_LIGHTING); |
169 |
|
|
glColorMask (0, 0, 0, 0); |
170 |
root |
1.38 |
cgGLDisableProfile (vsh_profile);// z-fighting?? |
171 |
root |
1.23 |
cgGLDisableProfile (fsh_profile); |
172 |
root |
1.18 |
} |
173 |
|
|
else |
174 |
|
|
{ |
175 |
|
|
glEnable (GL_MINMAX); |
176 |
|
|
glDisable (GL_POLYGON_OFFSET_FILL); |
177 |
|
|
glDepthFunc (GL_LESS); |
178 |
root |
1.38 |
glDepthMask (1); |
179 |
root |
1.23 |
cgGLEnableProfile (vsh_profile); |
180 |
|
|
cgGLEnableProfile (fsh_profile); |
181 |
root |
1.18 |
} |
182 |
|
|
|
183 |
root |
1.17 |
for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) |
184 |
root |
1.40 |
{ |
185 |
|
|
visibility_state &vs = vismap[*i]; |
186 |
root |
1.44 |
bool oq = mode == LIGHTED |
187 |
|
|
&& vs.last + 1. < timer.now |
188 |
|
|
&& (vs.visibility == visibility_state::PARTIAL |
189 |
|
|
|| vs.visibility == visibility_state::FULL); |
190 |
root |
1.40 |
|
191 |
|
|
if (oq) |
192 |
|
|
begin_occ_query (**i); |
193 |
|
|
|
194 |
|
|
(*i)->display (*this); |
195 |
|
|
|
196 |
|
|
if (oq) |
197 |
|
|
end_occ_query (); |
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.42 |
glDepthMask (0); |
206 |
root |
1.44 |
glColorMask (0, 0, 0, 0); |
207 |
root |
1.30 |
glDepthFunc (GL_LESS); |
208 |
root |
1.37 |
if (mode == view::LIGHTED) glDepthFunc (GL_LEQUAL); |
209 |
root |
1.44 |
if (mode == view::LIGHTED) glColorMask (1, 1, 1, 0); |
210 |
root |
1.30 |
glDisable (GL_LIGHTING); |
211 |
root |
1.27 |
cgGLDisableProfile (vsh_profile); |
212 |
|
|
cgGLDisableProfile (fsh_profile); |
213 |
|
|
|
214 |
root |
1.33 |
#if 0 |
215 |
root |
1.25 |
static int count; count++; |
216 |
|
|
if (farlist.size ()) |
217 |
|
|
printf ("%d: size %d\n", count, farlist.size ()); |
218 |
root |
1.33 |
#endif |
219 |
root |
1.25 |
|
220 |
root |
1.37 |
for (vector<octant *>::iterator i = farlist.begin (); i != farlist.end (); ++i) |
221 |
|
|
{ |
222 |
|
|
if (mode == view::DEPTH) begin_occ_query (**i); |
223 |
root |
1.38 |
if (mode == view::LIGHTED) glColor3f ((((long)*i >> 6) & 15) / 31. + 0.5,0,0); |
224 |
root |
1.37 |
(*i)->draw_bbox (*this); |
225 |
|
|
if (mode == view::DEPTH) end_occ_query (); |
226 |
root |
1.30 |
} |
227 |
root |
1.27 |
|
228 |
|
|
glDisable (GL_DEPTH_CLAMP_NV); |
229 |
root |
1.42 |
glDepthMask (1); |
230 |
root |
1.25 |
} |
231 |
root |
1.4 |
|
232 |
|
|
glColorMask (1, 1, 1, 0); |
233 |
|
|
glDepthMask (1); |
234 |
root |
1.27 |
|
235 |
root |
1.1 |
} |
236 |
root |
1.6 |
|
237 |
root |
1.1 |
|