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