ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/oct.C
Revision: 1.18
Committed: Wed Oct 6 00:27:00 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.17: +2 -20 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include <cstdlib>
2    
3 root 1.8 #include <vector>
4    
5     using namespace std;
6    
7     #define GL_GLEXT_PROTOTYPES
8     #include <GL/gl.h>
9    
10 root 1.1 #include "oct.h"
11 root 1.8 #include "view.h"
12 root 1.2 #include "entity.h"
13    
14 root 1.8 vector<GLuint> occ_query_objects;
15 root 1.2
16 root 1.8 static GLuint begin_occ_query ()
17 root 1.2 {
18 root 1.8 GLuint id;
19 root 1.2
20 root 1.8 if (occ_query_objects.size ())
21 root 1.3 {
22 root 1.8 id = *(occ_query_objects.end () - 1);
23     occ_query_objects.pop_back ();
24 root 1.3 }
25     else
26 root 1.11 glGenQueries (1, &id);
27 root 1.8
28 root 1.11 glBeginQuery (GL_SAMPLES_PASSED, id);
29 root 1.8 return id;
30     }
31    
32 root 1.11 #define end_occ_query() glEndQuery (GL_SAMPLES_PASSED);
33 root 1.8
34     static GLuint occ_query_result (GLuint id)
35     {
36     GLuint count;
37    
38 root 1.11 glGetQueryObjectuiv (id, GL_QUERY_RESULT, &count);
39 root 1.8 occ_query_objects.push_back (id);
40    
41     return count;
42     }
43    
44     octant world(0, sector (SOFFS_MIN, SOFFS_MIN, SOFFS_MIN), MAXEXTENT);
45    
46     octant::octant (octant *parent, const sector &orig, uoffs extent)
47     : parent(parent), orig(orig), extent(extent)
48     {
49     for (fill = 8; fill--; )
50     sub[fill] = 0;
51 root 1.2 }
52    
53     octant::~octant ()
54     {
55     for (fill = 8; fill--; )
56     delete sub[fill];
57     }
58    
59 root 1.8 static bool overlap (const sector &o1, uoffs ea, const box &bbox)
60 root 1.4 {
61 root 1.8 sector a2, b2;
62 root 1.4
63     ea /= 2;
64    
65     a2.x = o1.x + ea;
66     a2.y = o1.y + ea;
67     a2.z = o1.z + ea;
68    
69 root 1.8 b2.x = (bbox.a.x + bbox.b.x) / 2;
70     b2.y = (bbox.a.y + bbox.b.y) / 2;
71     b2.z = (bbox.a.z + bbox.b.z) / 2;
72    
73     return abs (a2.x - b2.x) <= ea + (bbox.b.x - bbox.a.x)
74     && abs (a2.y - b2.y) <= ea + (bbox.b.y - bbox.a.y)
75     && abs (a2.z - b2.z) <= ea + (bbox.b.z - bbox.a.z);
76 root 1.4 }
77    
78 root 1.6 void octant::add (entity_base *e)
79 root 1.2 {
80 root 1.8 box bbox = translate (e->bbox, e->orig, sector (0, 0, 0));
81 root 1.7
82     uoffs size = max (abs (bbox.b.x - bbox.a.x),
83 root 1.8 max (abs (bbox.b.y - bbox.a.y),
84     abs (bbox.b.z - bbox.a.z)));
85 root 1.3
86 root 1.8 if (overlap (orig, extent, bbox))
87 root 1.4 {
88 root 1.8 uoffs extent2 = extent / 2;
89    
90     if (size > extent2 || !extent2)
91     {
92     push_back (e);
93     e->o.push_back (this);
94     return;
95     }
96 root 1.5
97 root 1.8 for (int i = 8; i--; )
98 root 1.5 {
99 root 1.8 sector s = orig;
100     s.offset (i, extent2);
101     if (overlap (s, extent2, bbox))
102 root 1.5 {
103 root 1.8 if (!sub[i])
104     sub[i] = new octant (this, s, extent2);
105    
106     sub[i]->add (e);
107 root 1.5 }
108 root 1.8 }
109 root 1.4 }
110 root 1.2 }
111 root 1.1
112     void octant::remove (entity_base *e)
113     {
114     }
115    
116 root 1.4 void octant::draw (draw_context &ctx)
117 root 1.1 {
118 root 1.8 visibility_state &vs = ctx.vismap[this];
119    
120     if (vs.generation != ctx.generation)
121 root 1.10 vs.visibility = visibility_state::UNKNOWN;
122    
123     const sector &cam = ctx.v.orig;
124     if (cam.x >= orig.x && cam.x <= orig.x + extent
125     && cam.y >= orig.y && cam.y <= orig.y + extent
126     && cam.z >= orig.z && cam.z <= orig.z + extent)
127     {
128     vs.visibility = visibility_state::PARTIAL;
129     vs.generation = ctx.generation;
130     }
131     else
132     {
133     point center (
134 root 1.18 orig.x + (soffs)extent / 2 - cam.x,
135     orig.y + (soffs)extent / 2 - cam.y,
136 root 1.14 orig.z + (soffs)extent / 2 - cam.z
137 root 1.10 );
138    
139 root 1.14 GLfloat dia = (0.5 * sqrtf (3))*(GLfloat)extent;
140    
141     if (ctx.frustum.t.distance (center) < -dia) return;
142     if (ctx.frustum.b.distance (center) < -dia) return;
143     if (ctx.frustum.l.distance (center) < -dia) return;
144     if (ctx.frustum.r.distance (center) < -dia) return;
145     if (ctx.frustum.n.distance (center) < -dia) return;
146     if (ctx.frustum.f.distance (center) < -dia) return;
147 root 1.10 }
148 root 1.8
149 root 1.17 #if 0
150 root 1.8 glBegin (GL_LINES);
151     sector s = orig;
152     s.x -= ctx.v.orig.x;
153     s.y -= ctx.v.orig.y;
154     s.z -= ctx.v.orig.z;
155 root 1.13 vec3 clr(0, 0.8, 0);
156     glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat*)&clr);
157 root 1.8
158     for (int i = 8; i--; )
159     for (int ji = 3; ji--; )
160     {
161     int j = i | (1 << ji);
162     if (i != j)
163     {
164     glVertex3i (s.x + !!(i & 1) * extent,
165     s.y + !!(i & 2) * extent,
166     s.z + !!(i & 4) * extent);
167     glVertex3i (s.x + !!(j & 1) * extent,
168     s.y + !!(j & 2) * extent,
169     s.z + !!(j & 4) * extent);
170     }
171     }
172    
173     glEnd ();
174 root 1.13 glDisable(GL_COLOR_MATERIAL);
175 root 1.8 #endif
176    
177 root 1.10 #if 0
178     if (vs.visibility == visibility_state::PARTIAL
179 root 1.8 && vs.generation == ctx.generation)
180 root 1.10 #endif
181 root 1.8 {
182     for (iterator i = end (); i-- != begin (); )
183 root 1.10 #if 0
184     printf ("draw %p %d,%d,%d (%d,%d,%d - %d,%d,%d)\n", *i,
185     (*i)->orig.x,
186     (*i)->orig.y,
187     (*i)->orig.z,
188     (*i)->bbox.a.x,
189     (*i)->bbox.a.y,
190     (*i)->bbox.a.z,
191     (*i)->bbox.b.x,
192     (*i)->bbox.b.y,
193     (*i)->bbox.b.z
194     ),
195     #endif
196 root 1.8 (*i)->display (ctx);
197    
198 root 1.10 // node to start with
199     unsigned char si = ctx.v.d.x < 0 ? 1 : 0
200     | ctx.v.d.y < 0 ? 2 : 0
201     | ctx.v.d.z < 0 ? 4 : 0;
202    
203     // bit-toggle to find next child for front-to-back order
204     static unsigned char next[8]
205     = { 0, 0^1, 1^2, 2^4, 4^3, 3^5, 5^6, 6^7 };
206    
207     for (int i = 0; i < 8; i++)
208     {
209     si ^= next[i];
210    
211     if (sub[si])
212     sub[si]->draw (ctx);
213     }
214 root 1.8 }
215    
216     vs.generation = ctx.generation;
217 root 1.1 }
218 root 1.8
219    
220 root 1.2