1 | #include <cmath> |
1 | #include <cmath> |
2 | |
2 | |
|
|
3 | #define GL_GLEXT_PROTOTYPES |
|
|
4 | #include <GL/gl.h> |
|
|
5 | #include <GL/glext.h> |
|
|
6 | |
|
|
7 | #include "view.h" |
3 | #include "oct.h" |
8 | #include "oct.h" |
4 | #include "view.h" |
|
|
5 | |
9 | |
6 | draw_context::draw_context (view &v) |
10 | vector<GLuint> occ_query_objects; |
7 | : v(v), l(0), mode(LIGHTED) |
|
|
8 | { |
|
|
9 | } |
|
|
10 | |
11 | |
11 | draw_context::~draw_context () |
12 | static GLuint begin_occ_query () |
12 | { |
13 | { |
13 | } |
14 | GLuint id; |
14 | |
15 | |
15 | void draw_context::transform (const matrix &m) |
16 | if (occ_query_objects.size ()) |
16 | { |
17 | { |
17 | modelview = modelview * m; |
18 | id = *(occ_query_objects.end () - 1); |
18 | mvp = projection * modelview; |
19 | occ_query_objects.pop_back (); |
19 | } |
20 | } |
|
|
21 | else |
|
|
22 | glGenQueriesARB (1, &id); |
20 | |
23 | |
21 | bool draw_context::may_draw (entity_base *e) |
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 | view::view () |
|
|
44 | : gamma(1.0) |
|
|
45 | { |
|
|
46 | } |
|
|
47 | |
|
|
48 | view::~view () |
|
|
49 | { |
|
|
50 | } |
|
|
51 | |
|
|
52 | 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 | bool view::may_draw (const entity *e) |
22 | { |
63 | { |
23 | if (drawn.find (e) != drawn.end ()) |
64 | if (drawn.find (e) != drawn.end ()) |
24 | return false; |
65 | return false; |
25 | |
66 | |
26 | drawn.insert (e); |
67 | drawn.insert (e); |
27 | return true; |
68 | return true; |
28 | } |
69 | } |
29 | |
70 | |
30 | void view::draw (draw_context &ctx) |
71 | void view::reset_projection () |
31 | { |
72 | { |
32 | // check occlusion queries here |
|
|
33 | |
|
|
34 | ctx.generation++; |
|
|
35 | |
|
|
36 | renormalize (orig, p); |
73 | renormalize (orig, p); |
37 | |
74 | |
38 | if (ctx.mode == draw_context::DEPTH) |
75 | glViewport (0, 0, w, h); |
|
|
76 | |
|
|
77 | glMatrixMode (GL_PROJECTION); |
|
|
78 | glLoadIdentity (); |
|
|
79 | |
|
|
80 | GLdouble aspect = (GLdouble)w/h; |
|
|
81 | GLdouble ymax = z_near * tan (fov * (M_PI / 360.0)); |
|
|
82 | |
|
|
83 | glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, z_near, z_far); |
|
|
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 | diagfact = sqrtf (3.);//D WHY??? |
|
|
100 | //printf ("diagfact = %f\n", diagfact); |
|
|
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 | reset_projection (); |
|
|
128 | |
|
|
129 | farlist.clear (); |
|
|
130 | |
|
|
131 | // check occlusion queries |
|
|
132 | for (vector<oq_data>::iterator i = occ_queries.begin (); i != occ_queries.end (); ++i) |
|
|
133 | { |
|
|
134 | occ_query oq(*this, i->second, occ_query_result (i->second)); |
|
|
135 | i->first->event (oq); |
|
|
136 | } |
|
|
137 | |
|
|
138 | occ_queries.clear (); |
|
|
139 | |
|
|
140 | nc_far = nz_far = z_near + 1.F; |
|
|
141 | world.detect_visibility (*this); |
|
|
142 | z_far = nz_far; |
|
|
143 | c_far = nc_far; |
|
|
144 | z_far = c_far;//D |
|
|
145 | |
|
|
146 | printf ("far %f cf %f RCP %f,%f,%f +%f\n", z_far, c_far, |
|
|
147 | frustum.r.n.x, |
|
|
148 | frustum.r.n.y, |
|
|
149 | frustum.r.n.z, |
|
|
150 | frustum.r.d |
|
|
151 | );//D |
|
|
152 | } |
|
|
153 | |
|
|
154 | void view::end () |
|
|
155 | { |
|
|
156 | vislist.clear (); |
|
|
157 | } |
|
|
158 | |
|
|
159 | void view::pass (enum mode m) |
|
|
160 | { |
|
|
161 | mode = m; |
|
|
162 | |
|
|
163 | glDisable (GL_ALPHA_TEST); |
|
|
164 | glDisable (GL_BLEND); |
|
|
165 | |
|
|
166 | if (mode == view::DEPTH) |
39 | { |
167 | { |
40 | glEnable (GL_POLYGON_OFFSET_FILL); |
168 | glEnable (GL_POLYGON_OFFSET_FILL); |
41 | glPolygonOffset (0, 1); |
169 | glPolygonOffset (0, 1); |
42 | glDepthFunc (GL_LESS); |
170 | glDepthFunc (GL_LESS); |
43 | glDisable (GL_LIGHTING); |
171 | glDisable (GL_LIGHTING); |
44 | glColorMask (0, 0, 0, 0); |
172 | glColorMask (0, 0, 0, 0); |
|
|
173 | cgGLDisableProfile (vsh_profile);// z-fighting?? |
|
|
174 | cgGLDisableProfile (fsh_profile); |
45 | } |
175 | } |
46 | else |
176 | else |
47 | { |
177 | { |
|
|
178 | glEnable (GL_MINMAX); |
48 | glDisable (GL_POLYGON_OFFSET_FILL); |
179 | glDisable (GL_POLYGON_OFFSET_FILL); |
49 | glDrawBuffer (GL_BACK); |
|
|
50 | glDepthFunc (GL_LEQUAL); |
180 | glDepthFunc (GL_LESS); |
51 | glEnable (GL_LIGHTING); |
181 | glDepthMask (1); |
|
|
182 | cgGLEnableProfile (vsh_profile); |
|
|
183 | cgGLEnableProfile (fsh_profile); |
|
|
184 | } |
|
|
185 | |
|
|
186 | for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) |
|
|
187 | { |
|
|
188 | visibility_state &vs = vismap[*i]; |
|
|
189 | bool oq = (vs.visibility == visibility_state::PARTIAL |
|
|
190 | || vs.visibility == visibility_state::FULL) |
|
|
191 | && (vs.last + 1. < timer.now) |
|
|
192 | && (mode == LIGHTED); |
|
|
193 | |
|
|
194 | if (oq) |
|
|
195 | begin_occ_query (**i); |
|
|
196 | |
|
|
197 | (*i)->display (*this); |
|
|
198 | |
|
|
199 | if (oq) |
|
|
200 | end_occ_query (); |
|
|
201 | |
|
|
202 | } |
|
|
203 | |
|
|
204 | drawn.clear (); |
|
|
205 | |
|
|
206 | if (mode == view::DEPTH) |
|
|
207 | { |
|
|
208 | glEnable (GL_DEPTH_CLAMP_NV); |
52 | glDepthMask (0); |
209 | glDepthMask (0); |
|
|
210 | glDepthFunc (GL_LESS); |
|
|
211 | if (mode == view::LIGHTED) glDepthFunc (GL_LEQUAL); |
|
|
212 | glDisable (GL_LIGHTING); |
|
|
213 | cgGLDisableProfile (vsh_profile); |
|
|
214 | cgGLDisableProfile (fsh_profile); |
|
|
215 | |
|
|
216 | #if 0 |
|
|
217 | static int count; count++; |
|
|
218 | if (farlist.size ()) |
|
|
219 | printf ("%d: size %d\n", count, farlist.size ()); |
|
|
220 | #endif |
|
|
221 | |
|
|
222 | for (vector<octant *>::iterator i = farlist.begin (); i != farlist.end (); ++i) |
|
|
223 | { |
|
|
224 | if (mode == view::DEPTH) begin_occ_query (**i); |
|
|
225 | if (mode == view::LIGHTED) glColor3f ((((long)*i >> 6) & 15) / 31. + 0.5,0,0); |
|
|
226 | (*i)->draw_bbox (*this); |
|
|
227 | if (mode == view::DEPTH) end_occ_query (); |
53 | } |
228 | } |
54 | |
|
|
55 | glViewport (0, 0, w, h); |
|
|
56 | |
229 | |
57 | glMatrixMode (GL_PROJECTION); |
230 | glDisable (GL_DEPTH_CLAMP_NV); |
58 | glLoadIdentity (); |
231 | glDepthMask (1); |
59 | |
232 | } |
60 | GLdouble aspect = (GLdouble)w/h; |
|
|
61 | GLdouble zNear = 0.1; |
|
|
62 | GLdouble zFar = 50.; |
|
|
63 | |
|
|
64 | GLdouble ymax = zNear * tan (fov * (M_PI / 360.0)); |
|
|
65 | glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, zNear, zFar); |
|
|
66 | |
|
|
67 | d = normalize (d);//D |
|
|
68 | u = normalize (u);//D |
|
|
69 | vec3 rz = -d; |
|
|
70 | vec3 rx = cross (u, rz); |
|
|
71 | vec3 ry = cross (rz, rx); |
|
|
72 | |
|
|
73 | matrix &m = ctx.projection; |
|
|
74 | m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = -p.x; |
|
|
75 | m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = -p.y; |
|
|
76 | m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = -p.z; |
|
|
77 | m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; |
|
|
78 | |
|
|
79 | glMultMatrixf ((GLfloat *)m.data); |
|
|
80 | |
|
|
81 | glGetFloatv (GL_PROJECTION_MATRIX, (GLfloat *)&m); |
|
|
82 | |
|
|
83 | ctx.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) ); |
|
|
84 | ctx.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) ); |
|
|
85 | ctx.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) ); |
|
|
86 | ctx.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) ); |
|
|
87 | ctx.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) ); |
|
|
88 | ctx.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) ); |
|
|
89 | |
|
|
90 | for (int i = 0; i < 4; i++) |
|
|
91 | for (int j = 0; j < 4 ; j++) |
|
|
92 | mvp[i][j] = m(i,j); |
|
|
93 | |
|
|
94 | ctx.modelview.identity (); |
|
|
95 | |
|
|
96 | world.draw (ctx); |
|
|
97 | |
|
|
98 | ctx.drawn.clear (); |
|
|
99 | |
233 | |
100 | glColorMask (1, 1, 1, 0); |
234 | glColorMask (1, 1, 1, 0); |
101 | glDepthMask (1); |
235 | glDepthMask (1); |
102 | } |
|
|
103 | |
236 | |
|
|
237 | } |
104 | |
238 | |
|
|
239 | |