ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/entity.C
Revision: 1.2
Committed: Sun Oct 3 01:14:40 2004 UTC (21 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.1: +138 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include <algorithm>
2 root 1.2 #include <cmath>
3 root 1.1
4     using namespace std;
5    
6     #include "entity.h"
7    
8 root 1.2 const vec3 cross (const vec3 &a, const vec3 &b)
9     {
10     return vec3 (
11     a.y * b.z - a.z * b.y,
12     a.z * b.x - a.x * b.z,
13     a.x * b.y - a.y * b.x
14     );
15     }
16    
17     GLfloat dot (const vec3 &a, const vec3 &b)
18     {
19     return a.x * b.x + a.y * b.y + a.z * b.z;
20     }
21    
22 root 1.1 void box::add (const box &o)
23     {
24     a.x = min (a.x, o.a.x);
25     a.y = min (a.y, o.a.y);
26     a.z = min (a.z, o.a.z);
27     b.x = max (b.x, o.b.x);
28     b.y = max (b.y, o.b.y);
29     b.z = max (b.z, o.b.z);
30     }
31    
32     void entity_base::update_bbox ()
33     {
34     if (parent)
35     parent->update_bbox ();
36     }
37    
38     void entity_base::hide ()
39     {
40     for (vector<octant *>::iterator i = o.end (); i-- != o.begin (); )
41     (*i)->remove (this);
42    
43     o.clear ();
44     }
45    
46     void entity_filter::update_bbox ()
47     {
48     bbox = e->bbox;
49     entity_base::update_bbox ();
50     }
51    
52     void entity_filter::show (const sector &sec)
53     {
54     e->show (sec);
55     }
56    
57     void entity_filter::draw ()
58     {
59     e->draw ();
60     }
61    
62     entity_filter::~entity_filter ()
63     {
64     delete e;
65     }
66    
67     void entity::show (const sector &sec)
68     {
69 root 1.2 show ();
70     }
71    
72     void entity::show ()
73     {
74 root 1.1 e->show (this->sec);
75     }
76    
77     void entity::draw ()
78     {
79 root 1.2 glPushMatrix ();
80 root 1.1 //TODO setup matrix etc.
81     e->draw ();
82 root 1.2 glPopMatrix ();
83 root 1.1 }
84    
85     void entity_container::update_bbox ()
86     {
87     bbox.reset ();
88    
89     for (iterator i = end (); i-- != begin (); )
90     {
91     (*i)->update_bbox ();
92     bbox.add ((*i)->bbox);
93     }
94     }
95    
96     void entity_container::show (const sector &sec)
97     {
98     for (iterator i = end (); i-- != begin (); )
99     (*i)->show (sec);
100     }
101    
102     void entity_container::draw ()
103     {
104     for (iterator i = end (); i-- != begin (); )
105     (*i)->draw ();
106     }
107    
108     entity_container::~entity_container ()
109     {
110     hide ();
111    
112     for (iterator i = end (); i-- != begin (); )
113     delete *i;
114    
115     clear ();
116 root 1.2 }
117    
118     template entity_opengl1d<GL_POINTS>;
119     template entity_opengl1d<GL_LINES>;
120     template entity_opengl1d<GL_LINE_STRIP>;
121     template entity_opengl1d<GL_LINE_LOOP>;
122     template entity_opengl2d<GL_TRIANGLES>;
123     template entity_opengl2d<GL_TRIANGLE_STRIP>;
124     template entity_opengl2d<GL_TRIANGLE_FAN>;
125     template entity_opengl2d<GL_QUADS>;
126     template entity_opengl2d<GL_QUAD_STRIP>;
127     template entity_opengl2d<GL_POLYGON>;
128    
129     template<GLenum type>
130     void entity_opengl1d<type>::update_bbox ()
131     {
132     entity_base::update_bbox ();
133     }
134    
135     template<GLenum type>
136     void entity_opengl2d<type>::update_bbox ()
137     {
138     entity_base::update_bbox ();
139     }
140    
141     template<GLenum type>
142     void entity_opengl1d<type>::draw ()
143     {
144     glBegin (type);
145    
146     for (iterator i = begin (); i < end (); ++i)
147     {
148     glColor3fv ((GLfloat *)&i->c);
149     glVertex3fv ((GLfloat *)&i->p);
150     }
151    
152     glEnd ();
153     }
154    
155     template<GLenum type>
156     void entity_opengl2d<type>::draw ()
157     {
158     glBegin (type);
159    
160     for (iterator i = begin (); i < end (); ++i)
161     {
162     glColor3fv ((GLfloat *)&i->c);
163     glNormal3fv ((GLfloat *)&i->n);
164     glTexCoord2fv ((GLfloat *)&i->t);
165     glVertex3fv ((GLfloat *)&i->p);
166     }
167    
168     glEnd ();
169     }
170    
171     template<GLenum type>
172     void entity_opengl1d<type>::show (const sector &sec)
173     {
174     }
175    
176     template<GLenum type>
177     void entity_opengl2d<type>::show (const sector &sec)
178     {
179     }
180    
181     void view::draw (const draw_context &c)
182     {
183     glViewport (0, 0, w, h);
184    
185     glMatrixMode (GL_PROJECTION);
186     glLoadIdentity ();
187    
188     double aspect = (double)w/h;
189     double zNear = 5;
190     double zFar = 1000.;
191    
192     {
193     GLdouble xmin, xmax, ymin, ymax;
194    
195     ymax = zNear * tan (fov * (M_PI / 360.0));
196     ymin = -ymax;
197     xmin = ymin * aspect;
198     xmax = ymax * aspect;
199    
200     glFrustum(xmin, xmax, ymin, ymax, zNear, zFar);
201     }
202    
203     vec3 rz = -d;
204     vec3 rx = cross (u, rz);
205     vec3 ry = cross (rz, rx);
206    
207     GLfloat m[4][4];
208     m[0][0] = rx.x; m[0][1] = rx.y; m[0][2] = rx.z; m[0][3] = 0;
209     m[1][0] = ry.x; m[1][1] = ry.y; m[1][2] = ry.z; m[1][3] = 0;
210     m[2][0] = rz.x; m[2][1] = rz.y; m[2][2] = rz.z; m[2][3] = 0;
211     m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
212     glMultMatrixf ((GLfloat *)m);
213    
214     glTranslatef (-p.x, -p.y, -p.z);
215    
216     glMatrixMode (GL_MODELVIEW);
217     glLoadIdentity ();
218    
219     entity *e = new entity;
220     e->sec.x = e->sec.y = e->sec.z = 0;
221    
222     entity_triangles *tri = new entity_triangles;
223     tri->push_back (vertex2d (colour (1, 0, 0), point (-9, 0, -9), vec3 (0, 0, 1)));
224     tri->push_back (vertex2d (colour (0, 1, 1), point ( 0, 9, -9), vec3 (0, 0, 1)));
225     tri->push_back (vertex2d (colour (1, 0, 1), point ( 9, 0, -9), vec3 (0, 0, 1)));
226     e->set (tri);
227    
228     e->show ();
229     e->draw ();
230    
231     delete e;
232 root 1.1 }
233    
234