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