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