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) |
52 |
< |
QUADTREE *qtptr; |
53 |
< |
int *fptr,arg1,*arg2; |
54 |
< |
{ |
55 |
< |
QUADTREE qt = *qtptr; |
53 |
< |
OBJECT *os,*optr; |
54 |
< |
register int i,t_id; |
55 |
< |
|
56 |
< |
if (!QT_IS_LEAF(qt)) |
57 |
< |
return(TRUE); |
58 |
< |
|
59 |
< |
if(!QT_FLAG_FILL_TRI(*fptr)) |
60 |
< |
(*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); |
91 |
< |
/* For each triangle in the set, set the which flag*/ |
92 |
< |
os = qtqueryset(qt); |
93 |
< |
|
94 |
< |
for (i = QT_SET_CNT(os), optr = QT_SET_PTR(os); i > 0; i--) |
95 |
< |
{ |
96 |
< |
t_id = QT_SET_NEXT_ELEM(optr); |
97 |
< |
/* Set the render flag */ |
98 |
< |
if(SM_IS_NTH_T_BASE(smMesh,t_id)) |
99 |
< |
continue; |
100 |
< |
SM_SET_NTH_T_ACTIVE(smMesh,t_id); |
101 |
< |
/* NOTE:Also set the LRU clock bit: MAY WANT TO CHANGE: */ |
102 |
< |
SM_SET_NTH_T_LRU(smMesh,t_id); |
103 |
< |
} |
104 |
< |
return(TRUE); |
105 |
< |
} |
106 |
< |
|
107 |
< |
mark_tris_in_frustum(view) |
108 |
< |
VIEW *view; |
109 |
< |
{ |
110 |
< |
FVECT nr[4],far[4]; |
111 |
< |
|
112 |
< |
/* Mark triangles in approx. view frustum as being active:set |
113 |
< |
LRU counter: for use in discarding samples when out |
114 |
< |
of space |
115 |
< |
Radiance often has no far clipping plane: but driver will set |
116 |
< |
dev_zmin,dev_zmax to satisfy OGL |
117 |
< |
*/ |
118 |
< |
|
119 |
< |
/* First clear all the quadtree node and triangle active flags */ |
120 |
< |
qtClearAllFlags(); |
121 |
< |
smClear_flags(smMesh,T_ACTIVE_FLAG); |
122 |
< |
|
123 |
< |
/* calculate the world space coordinates of the view frustum */ |
124 |
< |
calculate_view_frustum(view->vp,view->hvec,view->vvec,view->horiz, |
125 |
< |
view->vert, dev_zmin,dev_zmax,nr,far); |
126 |
< |
|
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); |
163 |
< |
#ifdef TEST_DRIVER |
164 |
< |
VCOPY(FrustumFar[0],far[0]); |
165 |
< |
VCOPY(FrustumFar[1],far[1]); |
166 |
< |
VCOPY(FrustumFar[2],far[2]); |
167 |
< |
VCOPY(FrustumFar[3],far[3]); |
168 |
< |
VCOPY(FrustumNear[0],nr[0]); |
169 |
< |
VCOPY(FrustumNear[1],nr[1]); |
170 |
< |
VCOPY(FrustumNear[2],nr[2]); |
171 |
< |
VCOPY(FrustumNear[3],nr[3]); |
172 |
< |
#endif |
173 |
< |
} |
174 |
< |
|
175 |
< |
/* |
176 |
< |
* smClean() : display has been wiped clean |
177 |
< |
* |
178 |
< |
* Called after display has been effectively cleared, meaning that all |
179 |
< |
* geometry must be resent down the pipeline in the next call to smUpdate(). |
180 |
< |
*/ |
50 |
> |
/* |
51 |
> |
* smClean() : display has been wiped clean |
52 |
> |
* |
53 |
> |
* Called after display has been effectively cleared, meaning that all |
54 |
> |
* geometry must be resent down the pipeline in the next call to smUpdate(). |
55 |
> |
*/ |
56 |
|
smClean() |
57 |
|
{ |
58 |
|
smClean_notify = TRUE; |
139 |
|
stCount_level_leaves(lcnt+1, QT_NTH_CHILD(qt,3)); |
140 |
|
} |
141 |
|
else |
142 |
< |
lcnt[0]++; |
142 |
> |
if(QT_LEAF_IS_FLAG(qt)) |
143 |
> |
lcnt[0]++; |
144 |
|
} |
145 |
|
|
146 |
|
|
160 |
|
return(NULL); |
161 |
|
if (QT_IS_TREE(qt) && !QT_IS_FLAG(qt)) /* not in our frustum */ |
162 |
|
return(NULL); |
163 |
+ |
if(QT_IS_LEAF(qt) && !QT_LEAF_IS_FLAG(qt)) /* not in our frustum */ |
164 |
+ |
return(NULL); |
165 |
|
/* else look up node */ |
166 |
|
if ((le = qtCache_find(qt)) == NULL) |
167 |
|
error(SYSTEM, "out of memory in qtRender_level"); |
168 |
|
if (QT_IS_TREE(qt) && (QT_IS_EMPTY(le->qt) || lvl > 0)) |
169 |
|
{ /* compute children */ |
170 |
|
qtSubdivide_tri(v0,v1,v2,a,b,c); |
171 |
< |
rc[0] = qtRender_level(QT_NTH_CHILD(qt,0),v0,a,c,sm,lvl-1); |
172 |
< |
rc[1] = qtRender_level(QT_NTH_CHILD(qt,1),a,v1,b,sm,lvl-1); |
173 |
< |
rc[2] = qtRender_level(QT_NTH_CHILD(qt,2),c,b,v2,sm,lvl-1); |
174 |
< |
rc[3] = qtRender_level(QT_NTH_CHILD(qt,3),b,c,a,sm,lvl-1); |
171 |
> |
rc[0] = qtRender_level(QT_NTH_CHILD(qt,0),v0,c,b,sm,lvl-1); |
172 |
> |
rc[1] = qtRender_level(QT_NTH_CHILD(qt,1),c,v1,a,sm,lvl-1); |
173 |
> |
rc[2] = qtRender_level(QT_NTH_CHILD(qt,2),b,a,v2,sm,lvl-1); |
174 |
> |
rc[3] = qtRender_level(QT_NTH_CHILD(qt,3),a,b,c,sm,lvl-1); |
175 |
|
} |
176 |
|
if (QT_IS_EMPTY(le->qt)) |
177 |
|
{ /* let's make some data! */ |
194 |
|
{ /* from triangle set */ |
195 |
|
OBJECT *os; |
196 |
|
int s0, s1, s2; |
197 |
< |
|
197 |
> |
|
198 |
|
os = qtqueryset(qt); |
199 |
< |
for (n = os[0]; n; n--) |
199 |
> |
for (i = os[0]; i; i--) |
200 |
|
{ |
201 |
< |
tri = SM_NTH_TRI(sm,os[n]); |
201 |
> |
if(SM_IS_NTH_T_BASE(sm,os[i])) |
202 |
> |
continue; |
203 |
> |
tri = SM_NTH_TRI(sm,os[i]); |
204 |
> |
if(!T_IS_VALID(tri)) |
205 |
> |
continue; |
206 |
> |
n++; |
207 |
|
s0 = T_NTH_V(tri,0); |
208 |
|
s1 = T_NTH_V(tri,1); |
209 |
|
s2 = T_NTH_V(tri,2); |
223 |
|
rgbs[2] += SM_NTH_RGB(sm,s0)[2] + SM_NTH_RGB(sm,s1)[2] |
224 |
|
+ SM_NTH_RGB(sm,s2)[2]; |
225 |
|
} |
226 |
< |
n = 3*os[0]; |
226 |
> |
n *= 3; |
227 |
|
} |
228 |
|
if (!n) |
229 |
|
return(NULL); |
241 |
|
VSUM(c,SM_VIEW_CENTER(sm),c,le->av.dist); |
242 |
|
/* draw triangle */ |
243 |
|
glColor3ub(le->av.rgb[0],le->av.rgb[1],le->av.rgb[2]); |
361 |
– |
/* NOTE: Triangle vertex order may change */ |
362 |
– |
glVertex3d(c[0],c[1],c[2]); |
363 |
– |
glVertex3d(b[0],b[1],b[2]); |
244 |
|
glVertex3d(a[0],a[1],a[2]); |
245 |
+ |
glVertex3d(b[0],b[1],b[2]); |
246 |
+ |
glVertex3d(c[0],c[1],c[2]); |
247 |
+ |
|
248 |
|
} |
249 |
|
return(&le->av); |
250 |
|
} |
254 |
|
SM *sm; |
255 |
|
int lvl; |
256 |
|
{ |
257 |
< |
QUADTREE root; |
257 |
> |
QUADTREE qt; |
258 |
|
int i; |
259 |
|
FVECT t0,t1,t2; |
260 |
+ |
STREE *st; |
261 |
|
|
262 |
< |
if (lvl < 1) |
262 |
> |
|
263 |
> |
if (lvl < 0) |
264 |
|
return; |
265 |
+ |
st = SM_LOCATOR(sm); |
266 |
|
glPushAttrib(GL_LIGHTING_BIT); |
267 |
|
glShadeModel(GL_FLAT); |
268 |
|
glBegin(GL_TRIANGLES); |
269 |
< |
for(i=0; i < 4; i++) |
269 |
> |
for(i=0; i < ST_NUM_ROOT_NODES; i++) |
270 |
|
{ |
271 |
< |
root = ST_NTH_ROOT(SM_LOCATOR(sm),i); |
272 |
< |
stNth_base_verts(SM_LOCATOR(sm),i,t0,t1,t2); |
273 |
< |
qtRender_level(root,t0,t1,t2,sm,lvl-1); |
271 |
> |
qt = ST_ROOT_QT(st,i); |
272 |
> |
qtRender_level(qt,ST_NTH_V(st,i,0),ST_NTH_V(st,i,1),ST_NTH_V(st,i,2), |
273 |
> |
sm,lvl); |
274 |
|
} |
275 |
|
glEnd(); |
276 |
|
glPopAttrib(); |
283 |
|
{ |
284 |
|
int i, ntarget; |
285 |
|
int lvlcnt[QT_MAX_LEVELS]; |
286 |
+ |
STREE *st; |
287 |
|
|
288 |
|
if (qual <= 0) |
289 |
|
return; |
296 |
|
return; |
297 |
|
for (i = QT_MAX_LEVELS; i--; ) |
298 |
|
lvlcnt[i] = 0; |
299 |
< |
stCount_level_leaves(lvlcnt, SM_LOCATOR(sm)->root); |
299 |
> |
|
300 |
> |
st = SM_LOCATOR(sm); |
301 |
> |
for(i=0; i < ST_NUM_ROOT_NODES;i++) |
302 |
> |
stCount_level_leaves(lvlcnt, ST_ROOT_QT(st,i)); |
303 |
> |
|
304 |
|
for (ntarget -= lvlcnt[i=0]; i < QT_MAX_LEVELS-1; ntarget -= lvlcnt[++i]) |
305 |
|
if (ntarget < lvlcnt[i+1]) |
306 |
|
break; |
320 |
|
int j; |
321 |
|
|
322 |
|
tri = SM_NTH_TRI(sm,i); |
323 |
< |
if (clr) SM_CLEAR_NTH_T_NEW(sm,i); |
324 |
< |
|
434 |
< |
/* NOTE:Triangles are defined clockwise:historical relative to spherical |
435 |
< |
tris: could change |
436 |
< |
*/ |
437 |
< |
for(j=2; j>= 0; j--) |
323 |
> |
if (clr) SM_CLR_NTH_T_NEW(sm,i); |
324 |
> |
for(j=0; j <= 2; j++) |
325 |
|
{ |
326 |
|
#ifdef DEBUG |
327 |
|
if(SM_BG_SAMPLE(sm,T_NTH_V(tri,j))) |
347 |
|
int rgb[3]; |
348 |
|
|
349 |
|
tri = SM_NTH_TRI(sm,i); |
350 |
< |
if (clr) SM_CLEAR_NTH_T_NEW(sm,i); |
350 |
> |
if (clr) SM_CLR_NTH_T_NEW(sm,i); |
351 |
|
|
352 |
|
/* NOTE:Triangles are defined clockwise:historical relative to spherical |
353 |
|
tris: could change |
372 |
|
rgb[0]/=cnt; rgb[1]/=cnt; rgb[2]/=cnt; |
373 |
|
d /= (double)cnt; |
374 |
|
} |
375 |
< |
for(j=2; j>= 0; j--) |
375 |
> |
for(j=0; j <= 2; j++) |
376 |
|
{ |
377 |
|
if(SM_BG_SAMPLE(sm,ids[j])) |
378 |
|
{ |
405 |
|
TRI *tri; |
406 |
|
|
407 |
|
tri = SM_NTH_TRI(sm,i); |
408 |
< |
if (clr) SM_CLEAR_NTH_T_NEW(sm,i); |
408 |
> |
if (clr) SM_CLR_NTH_T_NEW(sm,i); |
409 |
|
|
410 |
|
/* NOTE:Triangles are defined clockwise:historical relative to spherical |
411 |
|
tris: could change |
412 |
|
*/ |
413 |
< |
for(j=2; j>= 0; j--) |
413 |
> |
for(j=0; j <= 2; j++) |
414 |
|
{ |
415 |
|
id = T_NTH_V(tri,j); |
416 |
|
glColor3ub(SM_NTH_RGB(sm,id)[0],SM_NTH_RGB(sm,id)[1], |
441 |
|
glPushAttrib(GL_DEPTH_BUFFER_BIT); |
442 |
|
|
443 |
|
/* First draw background polygons */ |
557 |
– |
|
444 |
|
glDisable(GL_DEPTH_TEST); |
445 |
|
glBegin(GL_TRIANGLES); |
446 |
|
SM_FOR_ALL_ACTIVE_BG_TRIS(sm,i) |
447 |
|
smRender_bg_tri(sm,i,vp,d,clr); |
448 |
|
glEnd(); |
563 |
– |
|
449 |
|
glEnable(GL_DEPTH_TEST); |
450 |
|
glBegin(GL_TRIANGLES); |
451 |
|
SM_FOR_ALL_ACTIVE_FG_TRIS(sm,i) |
452 |
|
{ |
453 |
|
if(!SM_MIXED_TRI(sm,i)) |
454 |
|
smRender_tri(sm,i,vp,clr); |
455 |
< |
else |
455 |
> |
else |
456 |
|
smRender_mixed_tri(sm,i,vp,clr); |
457 |
|
} |
458 |
|
glEnd(); |
471 |
|
|
472 |
|
tri = SM_NTH_TRI(sm,i); |
473 |
|
|
474 |
< |
/* Triangles are defined clockwise:historical relative to spherical |
590 |
< |
tris: could change |
591 |
< |
*/ |
592 |
< |
for(j=2; j >=0; j--) |
474 |
> |
for(j=0; j <= 2; j++) |
475 |
|
{ |
476 |
|
VCOPY(ptr,SM_NTH_WV(sm,T_NTH_V(tri,j))); |
477 |
|
glVertex3d(ptr[0],ptr[1],ptr[2]); |
575 |
|
|
576 |
|
/* Turn Depth Test off -- using Painter's algorithm */ |
577 |
|
glPushAttrib(GL_DEPTH_BUFFER_BIT); |
578 |
< |
glDisable(GL_DEPTH_TEST); |
578 |
> |
glDepthFunc(GL_ALWAYS); |
579 |
|
d = (dev_zmin+dev_zmax)/2.0; |
580 |
|
/* Now render back-to front */ |
581 |
|
/* First render bg triangles */ |
622 |
|
epsilon is calculated as running avg of distance of sample points |
623 |
|
from canonical view: m = 1/(AVG(1/r)): some fraction of this |
624 |
|
*/ |
625 |
+ |
|
626 |
+ |
if(!smMesh) |
627 |
+ |
return; |
628 |
+ |
|
629 |
+ |
|
630 |
+ |
|
631 |
|
d = DIST(view->vp,SM_VIEW_CENTER(smMesh)); |
632 |
|
if(qual >= 100 && d > SM_ALLOWED_VIEW_CHANGE(smMesh)) |
633 |
|
{ |
634 |
|
/* Re-build the mesh */ |
635 |
|
#ifdef TEST_DRIVER |
636 |
|
odev.v = *view; |
637 |
< |
#endif |
638 |
< |
smRebuild_mesh(smMesh,view->vp); |
637 |
> |
#endif |
638 |
> |
mark_tris_in_frustum(view); |
639 |
> |
smRebuild_mesh(smMesh,view); |
640 |
|
} |
641 |
|
/* This is our final update iff qual==100 and view==&odev.v */ |
642 |
|
last_update = qual>=100 && view==&(odev.v); |
643 |
|
/* Check if we should draw ALL triangles in current frustum */ |
644 |
< |
if(smClean_notify || smNew_tri_cnt > SM_NUM_TRIS(smMesh)*SM_INC_PERCENT) |
644 |
> |
if(smClean_notify || smNew_tri_cnt > SM_SAMPLE_TRIS(smMesh)*SM_INC_PERCENT) |
645 |
|
{ |
646 |
|
#ifdef TEST_DRIVER |
647 |
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
648 |
|
#else |
649 |
< |
if (SM_TONE_MAP(smMesh) < SM_NUM_SAMP(smMesh)) |
649 |
> |
if ( SM_TONE_MAP(smMesh) < SM_NUM_SAMP(smMesh)) |
650 |
|
{ |
651 |
|
tmClearHisto(); |
652 |
|
tmAddHisto(SM_BRT(smMesh),SM_NUM_SAMP(smMesh),1); |
653 |
< |
if(tmComputeMapping(0.,0.,0.) != TM_E_OK || |
654 |
< |
tmMapPixels(SM_RGB(smMesh),SM_BRT(smMesh),SM_CHR(smMesh), |
655 |
< |
SM_NUM_SAMP(smMesh)) != TM_E_OK) |
767 |
< |
return; |
653 |
> |
tmComputeMapping(0.,0.,0.); |
654 |
> |
tmMapPixels(SM_RGB(smMesh),SM_BRT(smMesh),SM_CHR(smMesh), |
655 |
> |
SM_NUM_SAMP(smMesh)); |
656 |
|
} |
657 |
|
#endif |
658 |
|
mark_tris_in_frustum(view); |
667 |
|
} |
668 |
|
/* Do an incremental update instead */ |
669 |
|
else |
670 |
< |
{ |
671 |
< |
if(!smNew_tri_cnt) |
670 |
> |
{ |
671 |
> |
if(!smNew_tri_cnt) |
672 |
|
return; |
673 |
|
#ifdef TEST_DRIVER |
674 |
|
glDrawBuffer(GL_FRONT); |
681 |
|
if(tmComputeMapping(0.,0.,0.) != TM_E_OK) |
682 |
|
return; |
683 |
|
} |
684 |
< |
if(tmMapPixels(SM_NTH_RGB(smMesh,t),&SM_NTH_BRT(smMesh,t), |
685 |
< |
SM_NTH_CHR(smMesh,t), SM_NUM_SAMP(smMesh)-t) != TM_E_OK) |
686 |
< |
return; |
684 |
> |
tmMapPixels(SM_NTH_RGB(smMesh,t),&SM_NTH_BRT(smMesh,t), |
685 |
> |
SM_NTH_CHR(smMesh,t), SM_NUM_SAMP(smMesh)-t); |
686 |
> |
|
687 |
|
#endif |
688 |
|
smUpdate_Rendered_mesh(smMesh,view->vp,last_update); |
689 |
|
|
691 |
|
glDrawBuffer(GL_BACK); |
692 |
|
#endif |
693 |
|
} |
694 |
+ |
|
695 |
|
SM_TONE_MAP(smMesh) = SM_NUM_SAMP(smMesh); |
696 |
+ |
|
697 |
|
if (last_update) |
698 |
|
{ |
699 |
|
smClean_notify = FALSE; |