ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libgender/oct.C
Revision: 1.8
Committed: Mon Oct 4 07:04:58 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.7: +126 -51 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include <cstdlib>
2
3 #include <vector>
4
5 using namespace std;
6
7 #define GL_GLEXT_PROTOTYPES
8 #include <GL/gl.h>
9
10 #include "oct.h"
11 #include "view.h"
12 #include "entity.h"
13
14 vector<GLuint> occ_query_objects;
15
16 static GLuint begin_occ_query ()
17 {
18 GLuint id;
19
20 if (occ_query_objects.size ())
21 {
22 id = *(occ_query_objects.end () - 1);
23 occ_query_objects.pop_back ();
24 }
25 else
26 glGenQueriesARB (1, &id);
27
28 glBeginQuery (GL_SAMPLES_PASSED_ARB, id);
29 return id;
30 }
31
32 #define end_occ_query() glEndQuery (GL_SAMPLES_PASSED_ARB);
33
34 static GLuint occ_query_result (GLuint id)
35 {
36 GLuint count;
37
38 glGetQueryObjectuivARB (id, GL_QUERY_RESULT_ARB, &count);
39 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 }
52
53 octant::~octant ()
54 {
55 for (fill = 8; fill--; )
56 delete sub[fill];
57 }
58
59 static bool overlap (const sector &o1, uoffs ea, const box &bbox)
60 {
61 sector a2, b2;
62
63 ea /= 2;
64
65 a2.x = o1.x + ea;
66 a2.y = o1.y + ea;
67 a2.z = o1.z + ea;
68
69 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 }
77
78 void octant::add (entity_base *e)
79 {
80 printf ("OCTANT %d,%d,%d+%d\n", orig.x, orig.y, orig.z, extent);
81
82 box bbox = translate (e->bbox, e->orig, sector (0, 0, 0));
83
84 uoffs size = max (abs (bbox.b.x - bbox.a.x),
85 max (abs (bbox.b.y - bbox.a.y),
86 abs (bbox.b.z - bbox.a.z)));
87
88 if (overlap (orig, extent, bbox))
89 {
90 uoffs extent2 = extent / 2;
91
92 if (size > extent2 || !extent2)
93 {
94 printf ("overlap, add\n");
95 push_back (e);
96 e->o.push_back (this);
97 return;
98 }
99
100 for (int i = 8; i--; )
101 {
102 sector s = orig;
103 s.offset (i, extent2);
104 if (overlap (s, extent2, bbox))
105 {
106 if (!sub[i])
107 sub[i] = new octant (this, s, extent2);
108
109 sub[i]->add (e);
110 }
111 }
112 }
113 else
114 printf ("no overlap\n");
115 }
116
117 void octant::remove (entity_base *e)
118 {
119 }
120
121 void octant::draw (draw_context &ctx)
122 {
123 visibility_state &vs = ctx.vismap[this];
124
125 if (vs.generation != ctx.generation)
126 vs.visibility = visibility_state::OCCLUDED;
127
128 #if 1
129 glBegin (GL_LINES);
130 sector s = orig;
131 s.x -= ctx.v.orig.x;
132 s.y -= ctx.v.orig.y;
133 s.z -= ctx.v.orig.z;
134 glColor3f (1, 1, 1);
135
136 for (int i = 8; i--; )
137 for (int ji = 3; ji--; )
138 {
139 int j = i | (1 << ji);
140 if (i != j)
141 {
142 glVertex3i (s.x + !!(i & 1) * extent,
143 s.y + !!(i & 2) * extent,
144 s.z + !!(i & 4) * extent);
145 glVertex3i (s.x + !!(j & 1) * extent,
146 s.y + !!(j & 2) * extent,
147 s.z + !!(j & 4) * extent);
148 }
149 }
150
151 glEnd ();
152 #endif
153
154 const sector &cam = ctx.v.orig;
155 if (cam.x >= orig.x && cam.x <= orig.x + extent
156 && cam.y >= orig.y && cam.y <= orig.y + extent
157 && cam.z >= orig.z && cam.z <= orig.z + extent)
158 {
159 vs.visibility = visibility_state::VISIBLE;
160 vs.generation = ctx.generation;
161 }
162
163 #if 0
164 if (vs.visibility == visibility_state::VISIBLE
165 && vs.generation == ctx.generation)
166 #endif
167 {
168 for (iterator i = end (); i-- != begin (); )
169 (*i)->display (ctx);
170
171 for (int i = 8; i--; )
172 if (sub[i])
173 sub[i]->draw (ctx);
174 }
175
176 vs.generation = ctx.generation;
177 }
178
179
180