1 | #include <cmath> |
1 | #include <cmath> |
|
|
2 | |
|
|
3 | #include <GL/gl.h> |
|
|
4 | #include <GL/glext.h> |
2 | |
5 | |
3 | #include "oct.h" |
6 | #include "oct.h" |
4 | #include "view.h" |
7 | #include "view.h" |
5 | |
8 | |
6 | draw_context::draw_context (view &v) |
9 | static GLenum gl_error; |
7 | : v(v), l(0), mode(LIGHTED) |
10 | view::view () |
|
|
11 | : gamma(1.0) |
8 | { |
12 | { |
9 | } |
13 | } |
10 | |
14 | |
11 | draw_context::~draw_context () |
15 | view::~view () |
12 | { |
16 | { |
13 | } |
17 | } |
14 | |
18 | |
15 | bool draw_context::may_draw (entity_base *e) |
19 | bool view::may_draw (entity_base *e) |
16 | { |
20 | { |
17 | if (drawn.find (e) != drawn.end ()) |
21 | if (drawn.find (e) != drawn.end ()) |
18 | return false; |
22 | return false; |
19 | |
23 | |
20 | drawn.insert (e); |
24 | drawn.insert (e); |
21 | return true; |
25 | return true; |
22 | } |
26 | } |
23 | |
27 | |
24 | void view::draw (draw_context &ctx) |
28 | void view::reset_projection () |
25 | { |
29 | { |
|
|
30 | renormalize (orig, p); |
26 | |
31 | |
27 | if (ctx.mode == draw_context::DEPTH) |
|
|
28 | { |
|
|
29 | glEnable (GL_POLYGON_OFFSET_FILL); |
|
|
30 | glPolygonOffset (0, 1); |
|
|
31 | glDrawBuffer (GL_NONE); |
|
|
32 | glDepthFunc (GL_LESS); |
|
|
33 | glDisable (GL_LIGHTING); |
|
|
34 | } |
|
|
35 | else |
|
|
36 | { |
|
|
37 | glDisable (GL_POLYGON_OFFSET_FILL); |
|
|
38 | glDrawBuffer (GL_BACK); |
|
|
39 | glDepthFunc (GL_LEQUAL); |
|
|
40 | glEnable (GL_LIGHTING); |
|
|
41 | } |
|
|
42 | |
|
|
43 | glViewport (0, 0, w, h); |
32 | glViewport (0, 0, w, h); |
44 | |
33 | |
45 | glMatrixMode (GL_PROJECTION); |
34 | glMatrixMode (GL_PROJECTION); |
46 | glLoadIdentity (); |
35 | glLoadIdentity (); |
47 | |
36 | |
|
|
37 | gl_error = glGetError (); |
48 | GLdouble aspect = (GLdouble)w/h; |
38 | GLdouble aspect = (GLdouble)w/h; |
49 | GLdouble zNear = 0.001; |
39 | GLdouble zNear = 0.1; |
50 | GLdouble zFar = 100.; |
40 | GLdouble zFar = 50.; |
51 | |
41 | |
|
|
42 | gl_error = glGetError (); |
52 | GLdouble ymax = zNear * tan (fov * (M_PI / 360.0)); |
43 | GLdouble ymax = zNear * tan (fov * (M_PI / 360.0)); |
53 | glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, zNear, zFar); |
44 | glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, zNear, zFar); |
54 | |
45 | |
|
|
46 | gl_error = glGetError (); |
|
|
47 | d = normalize (d);//D |
|
|
48 | u = normalize (u);//D |
55 | vec3 rz = -d; |
49 | vec3 rz = -d; |
56 | vec3 rx = cross (u, rz); |
50 | vec3 rx = cross (u, rz); |
57 | vec3 ry = cross (rz, rx); |
51 | vec3 ry = cross (rz, rx); |
|
|
52 | gl_error = glGetError (); |
58 | |
53 | |
59 | gl_matrix &m = ctx.projection; |
54 | matrix &m = projection; |
60 | m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0; |
55 | m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0; |
61 | m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0; |
56 | m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0; |
62 | m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0; |
57 | m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0; |
63 | m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; |
58 | m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; |
|
|
59 | gl_error = glGetError (); |
64 | |
60 | |
65 | glMultMatrixf ((GLfloat *)m.data); |
61 | glMultMatrixf ((GLfloat *)m.data); |
66 | |
62 | |
|
|
63 | gl_error = glGetError (); |
|
|
64 | glGetFloatv (GL_PROJECTION_MATRIX, (GLfloat *)&m); |
|
|
65 | |
|
|
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) ); |
|
|
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) ); |
|
|
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) ); |
|
|
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) ); |
|
|
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) ); |
|
|
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) ); |
|
|
73 | |
|
|
74 | gl_error = glGetError (); |
67 | glMatrixMode (GL_MODELVIEW); |
75 | glMatrixMode (GL_MODELVIEW); |
68 | glLoadIdentity (); |
76 | glLoadIdentity (); |
69 | glTranslatef (-p.x, -p.y, -p.z); |
77 | glTranslatef (-p.x, -p.y, -p.z); |
70 | |
78 | gl_error = glGetError (); |
71 | world.draw (ctx); |
|
|
72 | } |
79 | } |
73 | |
80 | |
|
|
81 | void view::begin () |
|
|
82 | { |
|
|
83 | glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
|
84 | glDepthRange (0, 1. - (1. / pow (2., 16.))); |
|
|
85 | |
|
|
86 | vismap.clear (); |
|
|
87 | vislist.clear (); |
|
|
88 | |
|
|
89 | generation++; |
|
|
90 | |
|
|
91 | reset_projection (); |
|
|
92 | |
|
|
93 | for (vector<octant *>::iterator i = farlist.begin (); i != farlist.end (); ++i) |
|
|
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); |
|
|
107 | } |
|
|
108 | |
|
|
109 | void view::end () |
|
|
110 | { |
|
|
111 | vismap.clear (); |
|
|
112 | } |
|
|
113 | |
|
|
114 | void view::pass (enum mode m) |
|
|
115 | { |
|
|
116 | mode = m; |
|
|
117 | |
|
|
118 | glDisable (GL_ALPHA_TEST); |
|
|
119 | glDisable (GL_BLEND); |
|
|
120 | |
|
|
121 | if (mode == view::DEPTH) |
|
|
122 | { |
|
|
123 | glEnable (GL_POLYGON_OFFSET_FILL); |
|
|
124 | glPolygonOffset (0, 1); |
|
|
125 | glDepthFunc (GL_LESS); |
|
|
126 | glDisable (GL_LIGHTING); |
|
|
127 | glColorMask (0, 0, 0, 0); |
|
|
128 | cgGLEnableProfile (vsh_profile);// z-fighting?? |
|
|
129 | cgGLDisableProfile (fsh_profile); |
|
|
130 | } |
|
|
131 | else |
|
|
132 | { |
|
|
133 | glEnable (GL_MINMAX); |
|
|
134 | glDisable (GL_POLYGON_OFFSET_FILL); |
|
|
135 | glDepthFunc (GL_LESS); |
|
|
136 | glDepthMask (0); |
|
|
137 | cgGLEnableProfile (vsh_profile); |
|
|
138 | cgGLEnableProfile (fsh_profile); |
|
|
139 | } |
|
|
140 | |
|
|
141 | for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) |
|
|
142 | (*i)->display (*this); |
|
|
143 | |
|
|
144 | drawn.clear (); |
|
|
145 | |
|
|
146 | if (mode == view::LIGHTED) |
|
|
147 | { |
|
|
148 | glEnable (GL_DEPTH_CLAMP_NV); |
|
|
149 | 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 | |
|
|
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) |
|
|
163 | { |
|
|
164 | glColor3f (1 / (GLfloat)(i - farlist.begin()), 1, 1 / (GLfloat)(i - farlist.begin())); |
|
|
165 | (*i)->draw_bbox (*this); |
|
|
166 | } |
|
|
167 | |
|
|
168 | glEnable (GL_DEPTH_TEST); |
|
|
169 | glDisable (GL_DEPTH_CLAMP_NV); |
|
|
170 | glShadeModel (GL_SMOOTH); |
|
|
171 | } |
|
|
172 | |
|
|
173 | glColorMask (1, 1, 1, 0); |
|
|
174 | glDepthMask (1); |
|
|
175 | |
|
|
176 | } |
|
|
177 | |
|
|
178 | |