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); |
|
|
82 | |
|
|
83 | perspfact = z_near / ymax * 0.5 * (GLdouble)h; |
39 | |
84 | |
40 | d = normalize (d);//D |
85 | d = normalize (d);//D |
41 | u = normalize (u);//D |
86 | u = normalize (u);//D |
|
|
87 | |
42 | vec3 rz = -d; |
88 | vec3 rz = -d; |
43 | vec3 rx = cross (u, rz); |
89 | vec3 rx = cross (u, rz); |
44 | vec3 ry = cross (rz, rx); |
90 | vec3 ry = cross (rz, rx); |
45 | |
91 | |
46 | matrix &m = projection; |
92 | matrix &m = projection; |
47 | m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0; |
93 | 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; |
94 | 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; |
95 | 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; |
96 | m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; |
51 | |
97 | |
52 | glMultMatrixf ((GLfloat *)m.data); |
98 | diagfact = abs (rz.x) + abs (rz.y) + abs (rz.z); |
|
|
99 | //printf ("diagfact = %f\n", diagfact); |
|
|
100 | diagfact = sqrtf (3.);//D WHY??? |
53 | |
101 | |
|
|
102 | glMultMatrixf (m); |
|
|
103 | glTranslatef (-p.x, -p.y, -p.z); |
|
|
104 | |
54 | glGetFloatv (GL_PROJECTION_MATRIX, (GLfloat *)&m); |
105 | glGetFloatv (GL_PROJECTION_MATRIX, m); |
55 | |
106 | |
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) ); |
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) ); |
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) ); |
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) ); |
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) ); |
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) ); |
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) ); |
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) ); |
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) ); |
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) ); |
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) ); |
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) ); |
62 | |
113 | |
63 | glMatrixMode (GL_MODELVIEW); |
114 | glMatrixMode (GL_MODELVIEW); |
64 | glLoadIdentity (); |
115 | glLoadIdentity (); |
65 | glTranslatef (-p.x, -p.y, -p.z); |
|
|
66 | } |
116 | } |
67 | |
117 | |
68 | void view::begin () |
118 | void view::begin () |
69 | { |
119 | { |
|
|
120 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
121 | glDepthRange (0, 1. - (1. / pow (2., 16.))); |
|
|
122 | |
70 | vismap.clear (); |
123 | vislist.clear (); |
71 | |
124 | |
72 | generation++; |
125 | generation++; |
73 | |
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 | |
74 | reset_projection (); |
144 | reset_projection (); |
75 | |
145 | |
76 | // check occlusion queries here |
146 | nc_far = nz_far = z_near + 1.F; |
77 | checklist.clear (); |
|
|
78 | |
|
|
79 | world.detect_visibility (*this); |
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 |
80 | } |
150 | } |
81 | |
151 | |
82 | void view::end () |
152 | void view::end () |
83 | { |
153 | { |
84 | vismap.clear (); |
154 | vislist.clear (); |
85 | } |
155 | } |
86 | |
156 | |
87 | void view::pass (enum mode m) |
157 | void view::pass (enum mode m) |
88 | { |
158 | { |
89 | mode = m; |
159 | mode = m; |
90 | |
160 | |
91 | glDisable (GL_ALPHA_TEST); |
161 | glDisable (GL_ALPHA_TEST); |
92 | glDisable (GL_BLEND); |
162 | glDisable (GL_BLEND); |
|
|
163 | //glDisable (GL_DEPTH_TEST); |
93 | |
164 | |
94 | if (mode == view::DEPTH) |
165 | if (mode == view::DEPTH) |
95 | { |
166 | { |
96 | glEnable (GL_POLYGON_OFFSET_FILL); |
167 | glEnable (GL_POLYGON_OFFSET_FILL); |
97 | glPolygonOffset (0, 1); |
168 | glPolygonOffset (0, 5); |
98 | glDepthFunc (GL_LESS); |
169 | glDepthFunc (GL_LESS); |
99 | glDisable (GL_LIGHTING); |
170 | glDisable (GL_LIGHTING); |
100 | glColorMask (0, 0, 0, 0); |
171 | glColorMask (0, 0, 0, 0); |
101 | //cgGLDisableProfile (CG_PROFILE_ARBVP1); |
172 | //cgGLDisableProfile (vsh_profile);// z-fighting?? |
102 | cgGLDisableProfile (CG_PROFILE_ARBFP1); |
173 | //cgGLDisableProfile (fsh_profile); |
103 | } |
174 | } |
104 | else |
175 | else |
105 | { |
176 | { |
106 | glEnable (GL_MINMAX); |
177 | glEnable (GL_MINMAX); |
107 | glDisable (GL_POLYGON_OFFSET_FILL); |
178 | glDisable (GL_POLYGON_OFFSET_FILL); |
108 | glDepthFunc (GL_LESS); |
179 | glDepthFunc (GL_LESS); |
109 | glDepthMask (0); |
180 | glDepthMask (0); |
110 | cgGLEnableProfile (CG_PROFILE_ARBVP1); |
181 | //cgGLEnableProfile (vsh_profile); |
111 | cgGLEnableProfile (CG_PROFILE_ARBFP1); |
182 | //cgGLEnableProfile (fsh_profile); |
112 | } |
183 | } |
113 | |
184 | |
114 | for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) |
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 | |
115 | (*i)->display (*this); |
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 | } |
116 | |
210 | |
117 | drawn.clear (); |
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 | } |
118 | |
228 | |
119 | glColorMask (1, 1, 1, 0); |
229 | glColorMask (1, 1, 1, 0); |
120 | glDepthMask (1); |
230 | glDepthMask (1); |
121 | } |
|
|
122 | |
231 | |
|
|
232 | } |
123 | |
233 | |
|
|
234 | |