16 |
|
#include <GL/glu.h> |
17 |
|
#include <glut.h> |
18 |
|
#endif |
19 |
+ |
#include "sm_flag.h" |
20 |
|
#include "sm_list.h" |
21 |
|
#include "sm_geom.h" |
22 |
+ |
#include "sm_qtree.h" |
23 |
+ |
#include "sm_stree.h" |
24 |
|
#include "sm.h" |
25 |
|
|
26 |
|
#ifdef TEST_DRIVER |
47 |
|
static int qt_hsiz = 0; /* quadtree cache size */ |
48 |
|
|
49 |
|
|
50 |
< |
int |
51 |
< |
mark_active_tris(qtptr,fptr,arg1,arg2) |
50 |
> |
|
51 |
> |
mark_active_tris(qtptr,fptr) |
52 |
|
QUADTREE *qtptr; |
53 |
< |
int *fptr,arg1,*arg2; |
53 |
> |
int *fptr; |
54 |
|
{ |
55 |
|
QUADTREE qt = *qtptr; |
56 |
|
OBJECT *os,*optr; |
57 |
|
register int i,t_id; |
58 |
+ |
TRI *tri; |
59 |
|
|
60 |
< |
if (!QT_IS_LEAF(qt)) |
57 |
< |
return(TRUE); |
58 |
< |
|
60 |
> |
|
61 |
|
if(!QT_FLAG_FILL_TRI(*fptr)) |
62 |
|
(*fptr)++; |
61 |
– |
/* For each triangle in the set, set the which flag*/ |
62 |
– |
os = qtqueryset(qt); |
63 |
|
|
64 |
< |
for (i = QT_SET_CNT(os), optr = QT_SET_PTR(os); i > 0; i--) |
65 |
< |
{ |
66 |
< |
t_id = QT_SET_NEXT_ELEM(optr); |
67 |
< |
/* Set the render flag */ |
68 |
< |
if(SM_IS_NTH_T_BASE(smMesh,t_id)) |
69 |
< |
continue; |
70 |
< |
SM_SET_NTH_T_ACTIVE(smMesh,t_id); |
71 |
< |
/* NOTE:Also set the LRU clock bit: MAY WANT TO CHANGE: */ |
72 |
< |
SM_SET_NTH_T_LRU(smMesh,t_id); |
73 |
< |
} |
74 |
< |
return(TRUE); |
75 |
< |
} |
76 |
< |
|
77 |
< |
int |
78 |
< |
mark_active_interior(qtptr,q0,q1,q2,t0,t1,t2,n,arg1,arg2,arg3) |
79 |
< |
QUADTREE *qtptr; |
80 |
< |
FVECT q0,q1,q2; |
81 |
< |
FVECT t0,t1,t2; |
82 |
< |
int n; |
83 |
< |
int *arg1,arg2,*arg3; |
84 |
< |
{ |
85 |
< |
QUADTREE qt = *qtptr; |
86 |
< |
OBJECT *os,*optr; |
87 |
< |
register int i,t_id; |
88 |
< |
|
89 |
< |
if (!QT_IS_LEAF(qt)) |
90 |
< |
return(TRUE); |
64 |
> |
if(QT_IS_EMPTY(qt) || QT_LEAF_IS_FLAG(qt)) |
65 |
> |
return; |
66 |
|
/* For each triangle in the set, set the which flag*/ |
67 |
|
os = qtqueryset(qt); |
68 |
|
|
70 |
|
{ |
71 |
|
t_id = QT_SET_NEXT_ELEM(optr); |
72 |
|
/* Set the render flag */ |
73 |
< |
if(SM_IS_NTH_T_BASE(smMesh,t_id)) |
73 |
> |
tri = SM_NTH_TRI(smMesh,t_id); |
74 |
> |
if(!T_IS_VALID(tri) || SM_IS_NTH_T_BASE(smMesh,t_id)) |
75 |
|
continue; |
76 |
|
SM_SET_NTH_T_ACTIVE(smMesh,t_id); |
77 |
< |
/* NOTE:Also set the LRU clock bit: MAY WANT TO CHANGE: */ |
78 |
< |
SM_SET_NTH_T_LRU(smMesh,t_id); |
77 |
> |
/* Set the Active bits of the Vertices */ |
78 |
> |
S_SET_FLAG(T_NTH_V(tri,0)); |
79 |
> |
S_SET_FLAG(T_NTH_V(tri,1)); |
80 |
> |
S_SET_FLAG(T_NTH_V(tri,2)); |
81 |
> |
|
82 |
|
} |
104 |
– |
return(TRUE); |
83 |
|
} |
84 |
|
|
85 |
+ |
#define mark_active_interior mark_active_tris |
86 |
+ |
|
87 |
|
mark_tris_in_frustum(view) |
88 |
|
VIEW *view; |
89 |
|
{ |
90 |
|
FVECT nr[4],far[4]; |
91 |
< |
|
91 |
> |
FPEQ peq; |
92 |
> |
int debug=0; |
93 |
|
/* Mark triangles in approx. view frustum as being active:set |
94 |
|
LRU counter: for use in discarding samples when out |
95 |
|
of space |
100 |
|
/* First clear all the quadtree node and triangle active flags */ |
101 |
|
qtClearAllFlags(); |
102 |
|
smClear_flags(smMesh,T_ACTIVE_FLAG); |
103 |
+ |
/* Clear all of the active sample flags*/ |
104 |
+ |
sClear_all_flags(smMesh->samples); |
105 |
|
|
106 |
+ |
|
107 |
|
/* calculate the world space coordinates of the view frustum */ |
108 |
|
calculate_view_frustum(view->vp,view->hvec,view->vvec,view->horiz, |
109 |
|
view->vert, dev_zmin,dev_zmax,nr,far); |
110 |
|
|
127 |
– |
/* Project the view frustum onto the spherical quadtree */ |
128 |
– |
/* For every cell intersected by the projection of the faces |
129 |
– |
of the frustum: mark all triangles in the cell as ACTIVE- |
130 |
– |
Also set the triangles LRU clock counter |
131 |
– |
*/ |
132 |
– |
/* Near face triangles */ |
133 |
– |
smLocator_apply_func(smMesh,nr[0],nr[2],nr[3],mark_active_tris, |
134 |
– |
mark_active_interior,NULL,NULL); |
135 |
– |
smLocator_apply_func(smMesh,nr[2],nr[0],nr[1],mark_active_tris, |
136 |
– |
mark_active_interior,NULL,NULL); |
137 |
– |
/* Right face triangles */ |
138 |
– |
smLocator_apply_func(smMesh,nr[0],far[3],far[0],mark_active_tris, |
139 |
– |
mark_active_interior,NULL,NULL); |
140 |
– |
smLocator_apply_func(smMesh,far[3],nr[0],nr[3],mark_active_tris, |
141 |
– |
mark_active_interior,NULL,NULL); |
142 |
– |
/* Left face triangles */ |
143 |
– |
smLocator_apply_func(smMesh,nr[1],far[2],nr[2],mark_active_tris, |
144 |
– |
mark_active_interior,NULL,NULL); |
145 |
– |
smLocator_apply_func(smMesh,far[2],nr[1],far[1],mark_active_tris, |
146 |
– |
mark_active_interior,NULL,NULL); |
147 |
– |
/* Top face triangles */ |
148 |
– |
smLocator_apply_func(smMesh,nr[0],far[0],nr[1],mark_active_tris, |
149 |
– |
mark_active_interior,NULL,NULL); |
150 |
– |
smLocator_apply_func(smMesh,nr[1],far[0],far[1],mark_active_tris, |
151 |
– |
mark_active_interior,NULL,NULL); |
152 |
– |
/* Bottom face triangles */ |
153 |
– |
smLocator_apply_func(smMesh,nr[3],nr[2],far[3],mark_active_tris, |
154 |
– |
mark_active_interior,NULL,NULL); |
155 |
– |
smLocator_apply_func(smMesh,nr[2],far[2],far[3],mark_active_tris, |
156 |
– |
mark_active_interior,NULL,NULL); |
157 |
– |
/* Far face triangles */ |
158 |
– |
smLocator_apply_func(smMesh,far[0],far[2],far[1],mark_active_tris, |
159 |
– |
mark_active_interior,NULL,NULL); |
160 |
– |
|
161 |
– |
smLocator_apply_func(smMesh,far[2],far[0],far[3],mark_active_tris, |
162 |
– |
mark_active_interior,NULL,NULL); |
111 |
|
#ifdef TEST_DRIVER |
112 |
|
VCOPY(FrustumFar[0],far[0]); |
113 |
|
VCOPY(FrustumFar[1],far[1]); |
118 |
|
VCOPY(FrustumNear[2],nr[2]); |
119 |
|
VCOPY(FrustumNear[3],nr[3]); |
120 |
|
#endif |
121 |
+ |
/* Project the view frustum onto the spherical quadtree */ |
122 |
+ |
/* For every cell intersected by the projection of the faces |
123 |
+ |
|
124 |
+ |
of the frustum: mark all triangles in the cell as ACTIVE- |
125 |
+ |
Also set the triangles LRU clock counter |
126 |
+ |
*/ |
127 |
+ |
|
128 |
+ |
if(EQUAL_VEC3(view->vp,SM_VIEW_CENTER(smMesh))) |
129 |
+ |
{/* Near face triangles */ |
130 |
+ |
smLocator_apply_func(smMesh,nr[3],nr[2],nr[0],mark_active_tris, |
131 |
+ |
mark_active_interior,NULL); |
132 |
+ |
smLocator_apply_func(smMesh,nr[1],nr[0],nr[2],mark_active_tris, |
133 |
+ |
mark_active_interior,NULL); |
134 |
+ |
return; |
135 |
+ |
} |
136 |
+ |
|
137 |
+ |
/* Test the view against the planes: and swap orientation if inside:*/ |
138 |
+ |
tri_plane_equation(nr[0],nr[2],nr[3], &peq,FALSE); |
139 |
+ |
if(PT_ON_PLANE(SM_VIEW_CENTER(smMesh),peq) > 0.0) |
140 |
+ |
{/* Near face triangles */ |
141 |
+ |
smLocator_apply_func(smMesh,nr[3],nr[2],nr[0],mark_active_tris, |
142 |
+ |
mark_active_interior,NULL); |
143 |
+ |
smLocator_apply_func(smMesh,nr[1],nr[0],nr[2],mark_active_tris, |
144 |
+ |
mark_active_interior,NULL); |
145 |
+ |
} |
146 |
+ |
else |
147 |
+ |
{/* Near face triangles */ |
148 |
+ |
smLocator_apply_func(smMesh,nr[0],nr[2],nr[3],mark_active_tris, |
149 |
+ |
mark_active_interior,NULL); |
150 |
+ |
smLocator_apply_func(smMesh,nr[2],nr[0],nr[1],mark_active_tris, |
151 |
+ |
mark_active_interior,NULL); |
152 |
+ |
} |
153 |
+ |
tri_plane_equation(nr[0],far[3],far[0], &peq,FALSE); |
154 |
+ |
if(PT_ON_PLANE(SM_VIEW_CENTER(smMesh),peq) > 0.0) |
155 |
+ |
{ /* Right face triangles */ |
156 |
+ |
smLocator_apply_func(smMesh,far[0],far[3],nr[0],mark_active_tris, |
157 |
+ |
mark_active_interior,NULL); |
158 |
+ |
smLocator_apply_func(smMesh,nr[3],nr[0],far[3],mark_active_tris, |
159 |
+ |
mark_active_interior,NULL); |
160 |
+ |
} |
161 |
+ |
else |
162 |
+ |
{/* Right face triangles */ |
163 |
+ |
smLocator_apply_func(smMesh,nr[0],far[3],far[0],mark_active_tris, |
164 |
+ |
mark_active_interior,NULL); |
165 |
+ |
smLocator_apply_func(smMesh,far[3],nr[0],nr[3],mark_active_tris, |
166 |
+ |
mark_active_interior,NULL); |
167 |
+ |
} |
168 |
+ |
|
169 |
+ |
tri_plane_equation(nr[1],far[2],nr[2], &peq,FALSE); |
170 |
+ |
if(PT_ON_PLANE(SM_VIEW_CENTER(smMesh),peq) > 0.0) |
171 |
+ |
{ /* Left face triangles */ |
172 |
+ |
smLocator_apply_func(smMesh,nr[2],far[2],nr[1],mark_active_tris, |
173 |
+ |
mark_active_interior,NULL); |
174 |
+ |
smLocator_apply_func(smMesh,far[1],nr[1],far[2],mark_active_tris, |
175 |
+ |
mark_active_interior,NULL); |
176 |
+ |
} |
177 |
+ |
else |
178 |
+ |
{ /* Left face triangles */ |
179 |
+ |
smLocator_apply_func(smMesh,nr[1],far[2],nr[2],mark_active_tris, |
180 |
+ |
mark_active_interior,NULL); |
181 |
+ |
smLocator_apply_func(smMesh,far[2],nr[1],far[1],mark_active_tris, |
182 |
+ |
mark_active_interior,NULL); |
183 |
+ |
|
184 |
+ |
} |
185 |
+ |
tri_plane_equation(nr[0],far[0],nr[1], &peq,FALSE); |
186 |
+ |
if(PT_ON_PLANE(SM_VIEW_CENTER(smMesh),peq) > 0.0) |
187 |
+ |
{/* Top face triangles */ |
188 |
+ |
smLocator_apply_func(smMesh,nr[1],far[0],nr[0],mark_active_tris, |
189 |
+ |
mark_active_interior,NULL); |
190 |
+ |
smLocator_apply_func(smMesh,far[1],far[0],nr[1],mark_active_tris, |
191 |
+ |
mark_active_interior,NULL); |
192 |
+ |
} |
193 |
+ |
else |
194 |
+ |
{/* Top face triangles */ |
195 |
+ |
smLocator_apply_func(smMesh,nr[0],far[0],nr[1],mark_active_tris, |
196 |
+ |
mark_active_interior,NULL); |
197 |
+ |
smLocator_apply_func(smMesh,nr[1],far[0],far[1],mark_active_tris, |
198 |
+ |
mark_active_interior,NULL); |
199 |
+ |
} |
200 |
+ |
tri_plane_equation(nr[3],nr[2],far[3], &peq,FALSE); |
201 |
+ |
if(PT_ON_PLANE(SM_VIEW_CENTER(smMesh),peq) > 0.0) |
202 |
+ |
{/* Bottom face triangles */ |
203 |
+ |
smLocator_apply_func(smMesh,far[3],nr[2],nr[3],mark_active_tris, |
204 |
+ |
mark_active_interior,NULL); |
205 |
+ |
smLocator_apply_func(smMesh,far[3],far[2],nr[2],mark_active_tris, |
206 |
+ |
mark_active_interior,NULL); |
207 |
+ |
} |
208 |
+ |
else |
209 |
+ |
{ /* Bottom face triangles */ |
210 |
+ |
smLocator_apply_func(smMesh,nr[3],nr[2],far[3],mark_active_tris, |
211 |
+ |
mark_active_interior,NULL); |
212 |
+ |
smLocator_apply_func(smMesh,nr[2],far[2],far[3],mark_active_tris, |
213 |
+ |
mark_active_interior,NULL); |
214 |
+ |
} |
215 |
+ |
tri_plane_equation(far[2],far[0],far[1], &peq,FALSE); |
216 |
+ |
if(PT_ON_PLANE(SM_VIEW_CENTER(smMesh),peq) > 0.0) |
217 |
+ |
{/* Far face triangles */ |
218 |
+ |
smLocator_apply_func(smMesh,far[0],far[2],far[1],mark_active_tris, |
219 |
+ |
mark_active_interior,NULL); |
220 |
+ |
smLocator_apply_func(smMesh,far[2],far[0],far[3],mark_active_tris, |
221 |
+ |
mark_active_interior,NULL); |
222 |
+ |
} |
223 |
+ |
else |
224 |
+ |
{/* Far face triangles */ |
225 |
+ |
smLocator_apply_func(smMesh,far[1],far[2],far[0],mark_active_tris, |
226 |
+ |
mark_active_interior,NULL); |
227 |
+ |
smLocator_apply_func(smMesh,far[3],far[0],far[2],mark_active_tris, |
228 |
+ |
mark_active_interior,NULL); |
229 |
+ |
} |
230 |
+ |
|
231 |
|
} |
232 |
|
|
233 |
|
/* |
379 |
|
for (n = os[0]; n; n--) |
380 |
|
{ |
381 |
|
tri = SM_NTH_TRI(sm,os[n]); |
382 |
+ |
if(!T_IS_VALID(tri)) |
383 |
+ |
continue; |
384 |
+ |
|
385 |
|
s0 = T_NTH_V(tri,0); |
386 |
|
s1 = T_NTH_V(tri,1); |
387 |
|
s2 = T_NTH_V(tri,2); |
435 |
|
QUADTREE root; |
436 |
|
int i; |
437 |
|
FVECT t0,t1,t2; |
438 |
+ |
STREE *st; |
439 |
|
|
440 |
+ |
|
441 |
|
if (lvl < 1) |
442 |
|
return; |
443 |
+ |
st = SM_LOCATOR(sm); |
444 |
|
glPushAttrib(GL_LIGHTING_BIT); |
445 |
|
glShadeModel(GL_FLAT); |
446 |
|
glBegin(GL_TRIANGLES); |
447 |
< |
for(i=0; i < 4; i++) |
447 |
> |
for(i=0; i < ST_NUM_ROOT_NODES; i++) |
448 |
|
{ |
449 |
< |
root = ST_NTH_ROOT(SM_LOCATOR(sm),i); |
450 |
< |
stNth_base_verts(SM_LOCATOR(sm),i,t0,t1,t2); |
451 |
< |
qtRender_level(root,t0,t1,t2,sm,lvl-1); |
449 |
> |
root = ST_NTH_ROOT(st,i); |
450 |
> |
qtRender_level(root,ST_NTH_V(st,i,0),ST_NTH_V(st,i,1),ST_NTH_V(st,i,2), |
451 |
> |
sm,lvl-1); |
452 |
|
} |
453 |
|
glEnd(); |
454 |
|
glPopAttrib(); |
473 |
|
return; |
474 |
|
for (i = QT_MAX_LEVELS; i--; ) |
475 |
|
lvlcnt[i] = 0; |
476 |
< |
stCount_level_leaves(lvlcnt, SM_LOCATOR(sm)->root); |
476 |
> |
stCount_level_leaves(lvlcnt, ST_TOP_ROOT(SM_LOCATOR(sm))); |
477 |
> |
stCount_level_leaves(lvlcnt, ST_BOTTOM_ROOT(SM_LOCATOR(sm))); |
478 |
|
for (ntarget -= lvlcnt[i=0]; i < QT_MAX_LEVELS-1; ntarget -= lvlcnt[++i]) |
479 |
|
if (ntarget < lvlcnt[i+1]) |
480 |
|
break; |
494 |
|
int j; |
495 |
|
|
496 |
|
tri = SM_NTH_TRI(sm,i); |
497 |
< |
if (clr) SM_CLEAR_NTH_T_NEW(sm,i); |
497 |
> |
if (clr) SM_CLR_NTH_T_NEW(sm,i); |
498 |
|
|
499 |
|
/* NOTE:Triangles are defined clockwise:historical relative to spherical |
500 |
|
tris: could change |
525 |
|
int rgb[3]; |
526 |
|
|
527 |
|
tri = SM_NTH_TRI(sm,i); |
528 |
< |
if (clr) SM_CLEAR_NTH_T_NEW(sm,i); |
528 |
> |
if (clr) SM_CLR_NTH_T_NEW(sm,i); |
529 |
|
|
530 |
|
/* NOTE:Triangles are defined clockwise:historical relative to spherical |
531 |
|
tris: could change |
583 |
|
TRI *tri; |
584 |
|
|
585 |
|
tri = SM_NTH_TRI(sm,i); |
586 |
< |
if (clr) SM_CLEAR_NTH_T_NEW(sm,i); |
586 |
> |
if (clr) SM_CLR_NTH_T_NEW(sm,i); |
587 |
|
|
588 |
|
/* NOTE:Triangles are defined clockwise:historical relative to spherical |
589 |
|
tris: could change |
619 |
|
glPushAttrib(GL_DEPTH_BUFFER_BIT); |
620 |
|
|
621 |
|
/* First draw background polygons */ |
557 |
– |
|
622 |
|
glDisable(GL_DEPTH_TEST); |
623 |
|
glBegin(GL_TRIANGLES); |
624 |
|
SM_FOR_ALL_ACTIVE_BG_TRIS(sm,i) |
625 |
|
smRender_bg_tri(sm,i,vp,d,clr); |
626 |
|
glEnd(); |
563 |
– |
|
627 |
|
glEnable(GL_DEPTH_TEST); |
628 |
|
glBegin(GL_TRIANGLES); |
629 |
|
SM_FOR_ALL_ACTIVE_FG_TRIS(sm,i) |
630 |
|
{ |
631 |
+ |
if(SM_BG_TRI(sm,i)) |
632 |
+ |
continue; |
633 |
|
if(!SM_MIXED_TRI(sm,i)) |
634 |
|
smRender_tri(sm,i,vp,clr); |
635 |
< |
else |
635 |
> |
else |
636 |
|
smRender_mixed_tri(sm,i,vp,clr); |
637 |
|
} |
638 |
|
glEnd(); |
805 |
|
epsilon is calculated as running avg of distance of sample points |
806 |
|
from canonical view: m = 1/(AVG(1/r)): some fraction of this |
807 |
|
*/ |
808 |
+ |
|
809 |
+ |
if(!smMesh) |
810 |
+ |
return; |
811 |
|
d = DIST(view->vp,SM_VIEW_CENTER(smMesh)); |
812 |
|
if(qual >= 100 && d > SM_ALLOWED_VIEW_CHANGE(smMesh)) |
813 |
|
{ |
814 |
|
/* Re-build the mesh */ |
815 |
|
#ifdef TEST_DRIVER |
816 |
|
odev.v = *view; |
817 |
< |
#endif |
818 |
< |
smRebuild_mesh(smMesh,view->vp); |
817 |
> |
#endif |
818 |
> |
mark_tris_in_frustum(view); |
819 |
> |
smRebuild_mesh(smMesh,view); |
820 |
|
} |
821 |
|
/* This is our final update iff qual==100 and view==&odev.v */ |
822 |
|
last_update = qual>=100 && view==&(odev.v); |
823 |
|
/* Check if we should draw ALL triangles in current frustum */ |
824 |
< |
if(smClean_notify || smNew_tri_cnt > SM_NUM_TRIS(smMesh)*SM_INC_PERCENT) |
824 |
> |
if(smClean_notify || smNew_tri_cnt > SM_SAMPLE_TRIS(smMesh)*SM_INC_PERCENT) |
825 |
|
{ |
826 |
|
#ifdef TEST_DRIVER |
827 |
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
828 |
|
#else |
829 |
< |
if (SM_TONE_MAP(smMesh) < SM_NUM_SAMP(smMesh)) |
829 |
> |
if ( SM_TONE_MAP(smMesh) < SM_NUM_SAMP(smMesh)) |
830 |
|
{ |
831 |
|
tmClearHisto(); |
832 |
|
tmAddHisto(SM_BRT(smMesh),SM_NUM_SAMP(smMesh),1); |
848 |
|
} |
849 |
|
/* Do an incremental update instead */ |
850 |
|
else |
851 |
< |
{ |
852 |
< |
if(!smNew_tri_cnt) |
851 |
> |
{ |
852 |
> |
if(!smNew_tri_cnt) |
853 |
|
return; |
854 |
|
#ifdef TEST_DRIVER |
855 |
|
glDrawBuffer(GL_FRONT); |