ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libgender/entity.C
Revision: 1.21
Committed: Sun Oct 3 20:14:33 2004 UTC (22 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.20: +34 -36 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include <algorithm>
2    
3     using namespace std;
4    
5     #include "entity.h"
6 root 1.4 #include "oct.h"
7 root 1.13 #include "view.h"
8 root 1.1
9 root 1.18 #include <GL/glext.h>
10    
11 root 1.21 entity_base::entity_base ()
12     {
13     orig.x = orig.y = orig.z = 0;
14     }
15    
16     entity_base::~entity_base ()
17     {
18     hide ();
19     }
20    
21 root 1.1 void entity_base::update_bbox ()
22     {
23     if (parent)
24     parent->update_bbox ();
25     }
26    
27     void entity_base::hide ()
28     {
29     for (vector<octant *>::iterator i = o.end (); i-- != o.begin (); )
30     (*i)->remove (this);
31    
32     o.clear ();
33     }
34    
35 root 1.21 void entity_base::display (draw_context &ctx)
36     {
37     if (ctx.may_draw (this))
38     {
39     glPushMatrix ();
40     glTranslated (-orig.x, -orig.y, -orig.z);
41     draw (ctx);
42     glPopMatrix ();
43     }
44     }
45    
46     /////////////////////////////////////////////////////////////////////////////
47    
48 root 1.1 void entity_filter::update_bbox ()
49     {
50     bbox = e->bbox;
51     entity_base::update_bbox ();
52     }
53    
54 root 1.21 void entity_filter::show ()
55 root 1.1 {
56 root 1.21 world.add (orig, this);
57 root 1.8
58 root 1.21 e->show ();
59 root 1.1 }
60    
61 root 1.13 void entity_filter::draw (draw_context &ctx)
62 root 1.1 {
63 root 1.21 e->display (ctx);
64 root 1.1 }
65    
66     entity_filter::~entity_filter ()
67     {
68     delete e;
69     }
70    
71 root 1.21 /////////////////////////////////////////////////////////////////////////////
72 root 1.1
73     void entity_container::update_bbox ()
74     {
75     bbox.reset ();
76    
77     for (iterator i = end (); i-- != begin (); )
78     {
79     (*i)->update_bbox ();
80     bbox.add ((*i)->bbox);
81     }
82     }
83    
84 root 1.21 void entity_container::show ()
85 root 1.1 {
86 root 1.21 world.add (orig, this);
87 root 1.8
88 root 1.1 for (iterator i = end (); i-- != begin (); )
89 root 1.21 (*i)->show ();
90 root 1.1 }
91    
92 root 1.13 void entity_container::draw (draw_context &ctx)
93 root 1.1 {
94     for (iterator i = end (); i-- != begin (); )
95 root 1.21 (*i)->display (ctx);
96 root 1.1 }
97    
98     entity_container::~entity_container ()
99     {
100     hide ();
101    
102     for (iterator i = end (); i-- != begin (); )
103     delete *i;
104    
105     clear ();
106 root 1.2 }
107    
108 root 1.21 /////////////////////////////////////////////////////////////////////////////
109    
110 root 1.12 template class entity_opengl1d<GL_POINTS>;
111     template class entity_opengl1d<GL_LINES>;
112     template class entity_opengl1d<GL_LINE_STRIP>;
113     template class entity_opengl1d<GL_LINE_LOOP>;
114     template class entity_opengl2d<GL_TRIANGLES>;
115     template class entity_opengl2d<GL_TRIANGLE_STRIP>;
116     template class entity_opengl2d<GL_TRIANGLE_FAN>;
117     template class entity_opengl2d<GL_QUADS>;
118     template class entity_opengl2d<GL_QUAD_STRIP>;
119     template class entity_opengl2d<GL_POLYGON>;
120 root 1.2
121     template<GLenum type>
122     void entity_opengl1d<type>::update_bbox ()
123     {
124 root 1.6 bbox.reset ();
125    
126     for (vector<vertex1d>::iterator i = end (); i-- != begin (); )
127     bbox.add (i->p);
128    
129 root 1.2 entity_base::update_bbox ();
130     }
131    
132     template<GLenum type>
133     void entity_opengl2d<type>::update_bbox ()
134     {
135 root 1.7 for (vector<vertex2d>::iterator i = end (); i-- != begin (); )
136 root 1.6 bbox.add (i->p);
137    
138 root 1.2 entity_base::update_bbox ();
139     }
140    
141     template<GLenum type>
142 root 1.13 void entity_opengl1d<type>::draw (draw_context &ctx)
143 root 1.2 {
144     glBegin (type);
145    
146     for (iterator i = begin (); i < end (); ++i)
147     {
148 root 1.4 if (ctx.mode == draw_context::LIGHTED)
149     glColor3fv ((GLfloat *)&i->c);
150    
151 root 1.2 glVertex3fv ((GLfloat *)&i->p);
152     }
153    
154     glEnd ();
155     }
156    
157     template<GLenum type>
158 root 1.13 void entity_opengl2d<type>::draw (draw_context &ctx)
159 root 1.2 {
160 root 1.14 if (!list)
161     {
162 root 1.17 list = glGenLists (1);
163     glNewList (list, GL_COMPILE);
164 root 1.14
165 root 1.5 glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *)&m.diffuse);
166     glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *)&m.specular);
167     glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *)&m.emission);
168 root 1.9 glMaterialf (GL_FRONT, GL_SHININESS, m.shininess);
169 root 1.15
170 root 1.20 #if 0
171 root 1.15 glBegin (type);
172 root 1.14
173     for (iterator i = begin (); i < end (); ++i)
174 root 1.4 {
175     glTexCoord2fv ((GLfloat *)&i->t);
176     glNormal3fv ((GLfloat *)&i->n);
177 root 1.14 glVertex3fv ((GLfloat *)&i->p);
178 root 1.4 }
179    
180 root 1.14 glEnd ();
181 root 1.19 #else
182     glEnableClientState (GL_VERTEX_ARRAY);
183     glVertexPointer (3, GL_FLOAT, sizeof (vertex2d), (void *)&begin ()->p);
184     glEnableClientState (GL_NORMAL_ARRAY);
185     glNormalPointer (GL_FLOAT, sizeof (vertex2d), (void *)&begin ()->n);
186     glEnableClientState (GL_TEXTURE_COORD_ARRAY);
187     glTexCoordPointer (2, GL_FLOAT, sizeof (vertex2d), (void *)&begin ()->t);
188    
189     glDrawArrays (type, 0, size ());
190     #endif
191 root 1.17 glEndList ();
192 root 1.19
193     clear ();
194 root 1.2 }
195    
196 root 1.17 glCallList (list);
197 root 1.2 }
198    
199 root 1.1
200