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