1 | #include <cmath> |
1 | #include <cmath> |
2 | |
2 | |
3 | #include "oct.h" |
3 | #include "oct.h" |
4 | #include "view.h" |
4 | #include "view.h" |
5 | |
5 | |
6 | draw_context::draw_context (view &v) |
6 | view::view () |
7 | : v(v), l(0), mode(LIGHTED) |
7 | : gamma(1.0) |
8 | { |
8 | { |
9 | } |
9 | } |
10 | |
10 | |
11 | draw_context::~draw_context () |
11 | view::~view () |
12 | { |
12 | { |
13 | } |
13 | } |
14 | |
14 | |
15 | bool draw_context::may_draw (entity_base *e) |
15 | bool view::may_draw (entity_base *e) |
16 | { |
16 | { |
17 | if (drawn.find (e) != drawn.end ()) |
17 | if (drawn.find (e) != drawn.end ()) |
18 | return false; |
18 | return false; |
19 | |
19 | |
20 | drawn.insert (e); |
20 | drawn.insert (e); |
21 | return true; |
21 | return true; |
22 | } |
22 | } |
23 | |
23 | |
24 | void view::draw (draw_context &ctx) |
24 | void view::reset_projection () |
25 | { |
25 | { |
26 | // check occlusion queries here |
26 | renormalize (orig, p); |
27 | |
27 | |
28 | ctx.generation++; |
|
|
29 | |
|
|
30 | //renormalize (orig, p); |
|
|
31 | |
|
|
32 | if (ctx.mode == draw_context::DEPTH) |
|
|
33 | { |
|
|
34 | glEnable (GL_POLYGON_OFFSET_FILL); |
|
|
35 | glPolygonOffset (0, 1); |
|
|
36 | glDepthFunc (GL_LESS); |
|
|
37 | glDisable (GL_LIGHTING); |
|
|
38 | glColorMask (0, 0, 0, 0); |
|
|
39 | } |
|
|
40 | else |
|
|
41 | { |
|
|
42 | glDisable (GL_POLYGON_OFFSET_FILL); |
|
|
43 | glDrawBuffer (GL_BACK); |
|
|
44 | glDepthFunc (GL_LEQUAL); |
|
|
45 | glEnable (GL_LIGHTING); |
|
|
46 | glDepthMask (0); |
|
|
47 | } |
|
|
48 | |
|
|
49 | glViewport (0, 0, w, h); |
28 | glViewport (0, 0, w, h); |
50 | |
29 | |
51 | glMatrixMode (GL_PROJECTION); |
30 | glMatrixMode (GL_PROJECTION); |
52 | glLoadIdentity (); |
31 | glLoadIdentity (); |
53 | |
32 | |
54 | GLdouble aspect = (GLdouble)w/h; |
33 | GLdouble aspect = (GLdouble)w/h; |
55 | GLdouble zNear = 0.001; |
34 | GLdouble zNear = 0.1; |
56 | GLdouble zFar = 100.; |
35 | GLdouble zFar = 50.; |
57 | |
36 | |
58 | GLdouble ymax = zNear * tan (fov * (M_PI / 360.0)); |
37 | GLdouble ymax = zNear * tan (fov * (M_PI / 360.0)); |
59 | glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, zNear, zFar); |
38 | glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, zNear, zFar); |
60 | |
39 | |
|
|
40 | d = normalize (d);//D |
|
|
41 | u = normalize (u);//D |
61 | vec3 rz = -d; |
42 | vec3 rz = -d; |
62 | vec3 rx = cross (u, rz); |
43 | vec3 rx = cross (u, rz); |
63 | vec3 ry = cross (rz, rx); |
44 | vec3 ry = cross (rz, rx); |
64 | |
45 | |
65 | gl_matrix &m = ctx.projection; |
46 | matrix &m = projection; |
66 | m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0; |
47 | m(0,0) = rx.x; m(0,1) = rx.y; m(0,2) = rx.z; m(0,3) = 0; |
67 | m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0; |
48 | m(1,0) = ry.x; m(1,1) = ry.y; m(1,2) = ry.z; m(1,3) = 0; |
68 | m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0; |
49 | m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0; |
69 | m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; |
50 | m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1; |
70 | |
51 | |
71 | glMultMatrixf ((GLfloat *)m.data); |
52 | glMultMatrixf ((GLfloat *)m.data); |
72 | |
53 | |
|
|
54 | glGetFloatv (GL_PROJECTION_MATRIX, (GLfloat *)&m); |
|
|
55 | |
|
|
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) ); |
|
|
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) ); |
|
|
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) ); |
|
|
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) ); |
|
|
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) ); |
|
|
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) ); |
|
|
62 | |
73 | glMatrixMode (GL_MODELVIEW); |
63 | glMatrixMode (GL_MODELVIEW); |
74 | glLoadIdentity (); |
64 | glLoadIdentity (); |
75 | glTranslatef (-p.x, -p.y, -p.z); |
65 | glTranslatef (-p.x, -p.y, -p.z); |
|
|
66 | } |
76 | |
67 | |
77 | world.draw (ctx); |
68 | void view::begin () |
|
|
69 | { |
|
|
70 | vismap.clear (); |
78 | |
71 | |
|
|
72 | generation++; |
|
|
73 | |
|
|
74 | reset_projection (); |
|
|
75 | |
|
|
76 | // check occlusion queries here |
|
|
77 | checklist.clear (); |
|
|
78 | |
|
|
79 | world.detect_visibility (*this); |
|
|
80 | } |
|
|
81 | |
|
|
82 | void view::end () |
|
|
83 | { |
|
|
84 | vismap.clear (); |
|
|
85 | } |
|
|
86 | |
|
|
87 | void view::pass (enum mode m) |
|
|
88 | { |
|
|
89 | mode = m; |
|
|
90 | |
|
|
91 | glDisable (GL_ALPHA_TEST); |
|
|
92 | glDisable (GL_BLEND); |
|
|
93 | |
|
|
94 | if (mode == view::DEPTH) |
|
|
95 | { |
|
|
96 | glEnable (GL_POLYGON_OFFSET_FILL); |
|
|
97 | glPolygonOffset (0, 1); |
|
|
98 | glDepthFunc (GL_LESS); |
|
|
99 | glDisable (GL_LIGHTING); |
|
|
100 | glColorMask (0, 0, 0, 0); |
|
|
101 | //cgGLDisableProfile (CG_PROFILE_ARBVP1); |
|
|
102 | cgGLDisableProfile (CG_PROFILE_ARBFP1); |
|
|
103 | } |
|
|
104 | else |
|
|
105 | { |
|
|
106 | glEnable (GL_MINMAX); |
|
|
107 | glDisable (GL_POLYGON_OFFSET_FILL); |
|
|
108 | glDepthFunc (GL_LESS); |
|
|
109 | glDepthMask (0); |
|
|
110 | cgGLEnableProfile (CG_PROFILE_ARBVP1); |
|
|
111 | cgGLEnableProfile (CG_PROFILE_ARBFP1); |
|
|
112 | } |
|
|
113 | |
|
|
114 | for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i) |
|
|
115 | (*i)->display (*this); |
|
|
116 | |
79 | ctx.drawn.clear (); |
117 | drawn.clear (); |
80 | |
118 | |
81 | glColorMask (1, 1, 1, 0); |
119 | glColorMask (1, 1, 1, 0); |
82 | glDepthMask (1); |
120 | glDepthMask (1); |
83 | } |
121 | } |
84 | |
122 | |
|
|
123 | |