ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/view.C
Revision: 1.32
Committed: Wed Oct 6 17:55:40 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.31: +10 -0 lines
Log Message:
i

File Contents

# User Rev Content
1 root 1.1 #include <cmath>
2    
3 root 1.26 #include <GL/gl.h>
4     #include <GL/glext.h>
5    
6 root 1.1 #include "oct.h"
7     #include "view.h"
8    
9 root 1.32 static GLenum gl_error;
10 root 1.17 view::view ()
11 root 1.18 : gamma(1.0)
12 root 1.1 {
13     }
14    
15 root 1.17 view::~view ()
16 root 1.1 {
17     }
18    
19 root 1.17 bool view::may_draw (entity_base *e)
20 root 1.1 {
21     if (drawn.find (e) != drawn.end ())
22     return false;
23    
24     drawn.insert (e);
25     return true;
26     }
27    
28 root 1.18 void view::reset_projection ()
29 root 1.1 {
30 root 1.5 renormalize (orig, p);
31 root 1.3
32 root 1.1 glViewport (0, 0, w, h);
33    
34     glMatrixMode (GL_PROJECTION);
35     glLoadIdentity ();
36    
37 root 1.32 gl_error = glGetError ();
38 root 1.1 GLdouble aspect = (GLdouble)w/h;
39 root 1.6 GLdouble zNear = 0.1;
40     GLdouble zFar = 50.;
41 root 1.1
42 root 1.32 gl_error = glGetError ();
43 root 1.1 GLdouble ymax = zNear * tan (fov * (M_PI / 360.0));
44     glFrustum (-ymax * aspect, ymax * aspect, -ymax, ymax, zNear, zFar);
45    
46 root 1.32 gl_error = glGetError ();
47 root 1.6 d = normalize (d);//D
48     u = normalize (u);//D
49 root 1.1 vec3 rz = -d;
50     vec3 rx = cross (u, rz);
51     vec3 ry = cross (rz, rx);
52 root 1.32 gl_error = glGetError ();
53 root 1.1
54 root 1.17 matrix &m = projection;
55 root 1.16 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;
57     m(2,0) = rz.x; m(2,1) = rz.y; m(2,2) = rz.z; m(2,3) = 0;
58 root 1.3 m(3,0) = 0; m(3,1) = 0; m(3,2) = 0; m(3,3) = 1;
59 root 1.32 gl_error = glGetError ();
60 root 1.3
61     glMultMatrixf ((GLfloat *)m.data);
62 root 1.6
63 root 1.32 gl_error = glGetError ();
64 root 1.5 glGetFloatv (GL_PROJECTION_MATRIX, (GLfloat *)&m);
65    
66 root 1.32 gl_error = glGetError ();
67 root 1.17 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 root 1.1
74 root 1.32 gl_error = glGetError ();
75 root 1.13 glMatrixMode (GL_MODELVIEW);
76 root 1.12 glLoadIdentity ();
77 root 1.16 glTranslatef (-p.x, -p.y, -p.z);
78 root 1.32 gl_error = glGetError ();
79 root 1.18 }
80    
81     void view::begin ()
82     {
83 root 1.30 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
84     glDepthRange (0, 1. - (1. / pow (2., 16.)));
85    
86 root 1.18 vismap.clear ();
87 root 1.19 vislist.clear ();
88 root 1.18
89     generation++;
90 root 1.12
91 root 1.18 reset_projection ();
92    
93 root 1.31 for (vector<octant *>::iterator i = farlist.begin (); i != farlist.end (); ++i)
94     ;
95     farlist.clear ();
96    
97 root 1.18 // check occlusion queries here
98 root 1.24 #if 0
99 root 1.20 for (vector<octant *>::iterator i = checklist.begin (); i != checklist.end (); ++i)
100 root 1.22 //printf ("%p %d\n", *i, occ_query_result ((*i)->occ_query));
101     occ_query_result ((*i)->occ_query);
102 root 1.20
103 root 1.17 checklist.clear ();
104 root 1.24 #endif
105 root 1.18
106 root 1.17 world.detect_visibility (*this);
107 root 1.18 }
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 root 1.4
121 root 1.18 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 root 1.24 cgGLEnableProfile (vsh_profile);// z-fighting??
129 root 1.23 cgGLDisableProfile (fsh_profile);
130 root 1.18 }
131     else
132     {
133     glEnable (GL_MINMAX);
134     glDisable (GL_POLYGON_OFFSET_FILL);
135     glDepthFunc (GL_LESS);
136     glDepthMask (0);
137 root 1.23 cgGLEnableProfile (vsh_profile);
138     cgGLEnableProfile (fsh_profile);
139 root 1.18 }
140    
141 root 1.17 for (vector<octant *>::iterator i = vislist.begin (); i != vislist.end (); ++i)
142     (*i)->display (*this);
143    
144     drawn.clear ();
145 root 1.24
146 root 1.27 if (mode == view::LIGHTED)
147 root 1.25 {
148 root 1.27 glEnable (GL_DEPTH_CLAMP_NV);
149 root 1.30 glDepthFunc (GL_LESS);
150 root 1.27 glEnable (GL_COLOR_MATERIAL);
151 root 1.30 glDisable (GL_LIGHTING);
152     #if 1
153 root 1.27 cgGLDisableProfile (vsh_profile);
154     cgGLDisableProfile (fsh_profile);
155     #endif
156 root 1.28 glShadeModel (GL_FLAT);
157 root 1.27
158 root 1.25 static int count; count++;
159     if (farlist.size ())
160     printf ("%d: size %d\n", count, farlist.size ());
161    
162 root 1.30 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 root 1.25 (*i)->draw_bbox (*this);
166 root 1.30 }
167 root 1.27
168     glEnable (GL_DEPTH_TEST);
169     glDisable (GL_DEPTH_CLAMP_NV);
170 root 1.28 glShadeModel (GL_SMOOTH);
171 root 1.25 }
172 root 1.4
173     glColorMask (1, 1, 1, 0);
174     glDepthMask (1);
175 root 1.27
176 root 1.1 }
177 root 1.6
178 root 1.1