34 #include <libFreeWRL.h>
36 #include "../vrml_parser/Structs.h"
37 #include "../main/headers.h"
38 #include "../opengl/OpenGL_Utils.h"
39 #include "../opengl/Textures.h"
40 #include "../scenegraph/RenderFuncs.h"
41 #include "../scenegraph/Component_Shape.h"
45 #define NO_TEXCOORD_NODE (r->tcoordtype==0)
88 void *StreamPoly_constructor(){
93 void StreamPoly_init(
struct tStreamPoly *t){
96 t->prv = StreamPoly_constructor();
114 static void do_glColor4fv(
struct SFColorRGBA *dest, GLfloat *param,
int isRGBA, GLfloat thisTransparency) {
118 if (isRGBA) pc = 4;
else pc = 3;
123 for (i=0; i<pc; i++) {
124 if ((param[i] < 0.0) || (param[i] >1.0)) {
128 dest->c[0] = param[0];
129 dest->c[1] = param[1];
130 dest->c[2] = param[2];
134 dest->c[3] = param[3];
137 dest->c[3] = 1.0f - thisTransparency;
143 void stream_polyrep(
void *innode,
void *coord,
void *fogCoord,
void *color,
void *normal,
struct X3D_TextureCoordinate *texCoordNode) {
147 int i, j, k, nmtexcoord;
151 struct SFVec3f *points= NULL;
int npoints=0;
152 struct SFColor *colors= NULL;
int ncolors=0;
153 struct SFVec3f *normals= NULL;
int nnormals=0;
154 float *fogpoints = NULL;
163 GLuint *newcindex = NULL;
164 GLuint *newtcindex = NULL;
165 struct SFVec3f *newpoints = NULL;
166 float *newfog = NULL;
167 struct SFVec3f *newnorms = NULL;
170 float *newTexCoords[MAX_MULTITEXTURE];
171 bool temp_points = FALSE;
172 struct Multi_Vec2f *textureCoordPoint[MAX_MULTITEXTURE];
173 int ntexdim[MAX_MULTITEXTURE];
174 for(k=0;k<MAX_MULTITEXTURE;k++){
175 textureCoordPoint[k] = NULL;
176 newTexCoords[k] = NULL;
182 node = X3D_NODE(innode);
186 #ifdef STREAM_POLY_VERBOSE
187 printf (
"start spv for %p extents %lf %lf, %lf %lf, %lf %lf\n",node,
201 printf (
"stream IFS, at start, this guy is empty, just returning \n");
208 if (xc->_nodeType != NODE_Coordinate && xc->_nodeType != NODE_GeoCoordinate ) {
209 printf (
"stream_polyrep, coord expected %d, got %d\n",NODE_Coordinate, xc->_nodeType);
212 }
else if(xc->_nodeType == NODE_GeoCoordinate){
218 points = MALLOC(
struct SFVec3f *,
sizeof(
struct SFVec3f)*(xgc->point.n));
219 npoints = xgc->point.n;
220 for(i=0;i<npoints;i++)
223 points[i].c[j] = (
float) xgc->point.p[i].c[j];
226 points = xgc->__movedCoords.p;
227 npoints = xgc->__movedCoords.n;
229 }
else { points = xc->point.p; npoints = xc->point.n; }
232 #ifdef STREAM_POLY_VERBOSE
233 printf (
"so, points is %p, npoints is %d ntri %d\n",points, npoints,r->ntri);
236 if (((
struct X3D_Node*)fogCoord)->_nodeType == NODE_FogCoordinate){
238 fogpoints = xfc->depth.p;
243 if ((cc->_nodeType != NODE_Color) && (cc->_nodeType != NODE_ColorRGBA)) {
244 ConsoleMessage (
"stream_polyrep, expected %d got %d\n", NODE_Color, cc->_nodeType);
248 colors = cc->color.p;
249 ncolors = cc->color.n;
250 isRGBA = (cc->_nodeType == NODE_ColorRGBA);
256 if (nc->_nodeType != NODE_Normal) {
257 ConsoleMessage (
"stream_polyrep, normal expected %d, got %d\n",NODE_Normal, nc->_nodeType);
260 }
else { normals = nc->vector.p; nnormals = nc->vector.n; }
264 if ((r->tcoordtype != NODE_TextureCoordinate) &&
265 (r->tcoordtype != NODE_TextureCoordinate3D) &&
266 (r->tcoordtype != NODE_TextureCoordinate4D) &&
267 (r->tcoordtype != NODE_MultiTextureCoordinate) &&
268 (r->tcoordtype != NODE_TextureCoordinateGenerator )) {
269 ConsoleMessage (
"stream_polyrep, TexCoord expected %d, got %d\n",NODE_TextureCoordinate, r->tcoordtype);
274 if (r->tcoordtype == NODE_TextureCoordinate) {
276 textureCoordPoint[0] = &(texCoordNode->point);
280 if (r->tcoordtype == NODE_TextureCoordinate3D) {
284 textureCoordPoint[0] = (
struct Multi_Vec2f*) &(tcn->point);
288 if (r->tcoordtype == NODE_TextureCoordinate4D) {
292 textureCoordPoint[0] =(
struct Multi_Vec2f*) &(tcn->point);
297 if (r->tcoordtype == NODE_MultiTextureCoordinate) {
300 if(mtc->texCoord.n && mtc->texCoord.p){
301 for(k=0;k<min(mtc->texCoord.n,MAX_MULTITEXTURE);k++){
302 textureCoordPoint[k] = NULL;
303 if( mtc->texCoord.p[k]->_nodeType == NODE_TextureCoordinate){
305 textureCoordPoint[k] = &(ttcc->point);
315 if (r->tcoordtype == NODE_TextureCoordinateGenerator) {
316 r->texgentype = findFieldInARR(((
struct X3D_TextureCoordinateGenerator *)texCoordNode)->mode->strptr, TEXTURECOORDINATEGENERATOR, TEXTURECOORDINATEGENERATOR_COUNT);
321 #ifdef STREAM_POLY_VERBOSE
322 printf (
"\nstart stream_polyrep ncoords %d ncolors %d nnormals %d ntri %d\n",
323 npoints, ncolors, nnormals, r->ntri);
327 #ifdef STREAM_POLY_VERBOSE
328 printf (
"stream polyrep, have an intern type of %d GeneratedTexCoords %p tcindex %p\n",r->tcoordtype, r->GeneratedTexCoords,r->tcindex);
329 printf (
"polyv, points %p coord %p ntri %d rnormal %p nnormal %d\n",points,r->actualCoord,r->ntri,r->normal, nnormals);
333 hasc = ((ncolors || r->color) && (gglobal()->RenderFuncs.last_texture_type!=TEXTURE_NO_ALPHA));
338 #ifdef STREAM_POLY_VERBOSE
339 printf (
"mustGenerateTextures, MALLOCing newtc\n");
343 if (!r->GeneratedTexCoords[0]) {
344 newTexCoords[0] = MALLOC (
float *,
sizeof (
float)*ntexdim[0]*r->ntri*3);
345 for(k=1;k<nmtexcoord;k++)
346 newTexCoords[k] = MALLOC (
float *,
sizeof (
float)*ntexdim[k]*r->ntri*3);
352 newcindex = MALLOC (GLuint *,
sizeof (GLuint)*r->ntri*3);
353 newtcindex = MALLOC (GLuint *,
sizeof (GLuint)*r->ntri*3);
355 newpoints = MALLOC (
struct SFVec3f *,
sizeof (
struct SFVec3f)*r->ntri*3);
357 newfog = MALLOC (
float *,
sizeof(
float)*r->ntri*3);
359 if ((nnormals) || (r->normal)) {
360 newnorms = MALLOC (
struct SFVec3f *,
sizeof (
struct SFVec3f)*r->ntri*3);
372 for (j=0; j<3; j++) {
374 r->minVals[j] = points[r->cindex[0]].c[j];
375 r->maxVals[j] = points[r->cindex[0]].c[j];
377 if (r->actualCoord!=NULL) {
378 r->minVals[j] = r->actualCoord[3*r->cindex[0]+j];
379 r->maxVals[j] = r->actualCoord[3*r->cindex[0]+j];
385 for(i=0; i<r->ntri*3; i++) {
386 int ind = r->cindex[i];
387 for (j=0; j<3; j++) {
389 if (ind >= npoints) {
393 printf (
"spv, warning, index %d >= npoints %d\n",ind,npoints);
395 if (r->minVals[j] > points[ind].c[j]) r->minVals[j] = points[ind].c[j];
396 if (r->maxVals[j] < points[ind].c[j]) r->maxVals[j] = points[ind].c[j];
398 }
else if(r->actualCoord) {
399 if (r->minVals[j] > r->actualCoord[3*ind+j]) r->minVals[j] = r->actualCoord[3*ind+j];
400 if (r->maxVals[j] < r->actualCoord[3*ind+j]) r->maxVals[j] = r->actualCoord[3*ind+j];
408 if (NO_TEXCOORD_NODE) {
409 defaultTextureMap(node, r);
425 if (node->_parentVector != NULL) {
426 if (vectorSize(node->_parentVector) != 0) {
427 parent = vector_get(
struct X3D_Shape *, node->_parentVector, 0);
429 if (parent->_nodeType == NODE_Shape) {
431 POSSIBLE_PROTO_EXPANSION(
struct X3D_Appearance *, parent->appearance,app)
434 if (app->_nodeType == NODE_Appearance) {
436 POSSIBLE_PROTO_EXPANSION(
struct X3D_Material *, app->material,mat)
439 if (mat->_nodeType == NODE_Material) {
440 thisTrans = mat->transparency;
454 #ifdef STREAM_POLY_VERBOSE
455 printf (
"before streaming for %p, extents %f %f, %f %f, %f %f\n",
467 for(i=0; i<r->ntri*3; i++) {
470 int ind = r->cindex[i];
476 #ifdef STREAM_POLY_VERBOSE
477 printf (
"rp, i, ntri*3 %d %d\n",i,r->ntri*3);
481 if(r->norindex) { nori = r->norindex[i];}
485 coli = r->colindex[i];
491 newtcindex[i] = r->tcindex[i];
492 #ifdef STREAM_POLY_VERBOSE
493 printf (
"have textures, and tcindex i %d tci %d\n",i,newtcindex[i]);
500 if(nori >= nnormals) {
504 #ifdef STREAM_POLY_VERBOSE
505 printf (
"nnormals at %d , nori %d ",(
int) &normals[nori].c,nori);
506 fwnorprint (normals[nori].c);
509 do_glNormal3fv(&newnorms[i], normals[nori].c);
510 }
else if(r->normal) {
511 #ifdef STREAM_POLY_VERBOSE
512 printf (
"r->normal nori %d ",nori);
513 fwnorprint(r->normal+3*nori);
516 do_glNormal3fv(&newnorms[i], r->normal+3*nori);
523 if (coli >= ncolors) {
527 #ifdef STREAM_POLY_VERBOSE
528 printf (
"coloUr ncolors %d, coli %d",ncolors,coli);
529 fwnorprint(colors[coli].c);
533 do_glColor4fv(&newcolors[i],oldColorsRGBA[coli].c,isRGBA,thisTrans);
535 do_glColor4fv(&newcolors[i],colors[coli].c,isRGBA,thisTrans);
536 }
else if(r->color) {
537 #ifdef STREAM_POLY_VERBOSE
539 fwnorprint(r->color+3*coli);
543 do_glColor4fv(&newcolors[i],r->color+4*coli,isRGBA,thisTrans);
545 do_glColor4fv(&newcolors[i],r->color+3*coli,isRGBA,thisTrans);
554 newpoints[i].c[0] = 0.0f;
555 newpoints[i].c[1] = 0.0f;
556 newpoints[i].c[2] = 0.0f;
559 memcpy (&newpoints[i], &points[ind].c[0],
sizeof (
struct SFColor));
560 if(newfog) memcpy(&newfog[i],&fogpoints[ind],
sizeof(
float));
561 #ifdef STREAM_POLY_VERBOSE
562 printf(
"Render (points) #%d = [%.5f, %.5f, %.5f] from [%.5f, %.5f, %.5f]\n",i,
563 newpoints[i].c[0],newpoints[i].c[1],newpoints[i].c[2],
564 points[ind].c[0], points[ind].c[1],points[ind].c[2]);
567 }
else if(r->actualCoord) {
568 memcpy (&newpoints[i].c[0], &r->actualCoord[3*ind],
sizeof(
struct SFColor));
569 #ifdef STREAM_POLY_VERBOSE
570 printf(
"Render (r->actualCoord) #%d = [%.5f, %.5f, %.5f]\n",i,
571 newpoints[i].c[0],newpoints[i].c[1],newpoints[i].c[2]);
574 #ifdef STREAM_POLY_VERBOSE
575 printf (
"spv, no points and no coords, setting to 0,0,0\n");
577 newpoints[i].c[0] = 0.0f; newpoints[i].c[1]=0.0f;newpoints[i].c[2]=0.0f;
584 if (!r->GeneratedTexCoords[0]) {
585 for(k=0;k<(max(1,nmtexcoord));k++){
586 if (textureCoordPoint[k] != NULL) {
588 int j = newtcindex[i];
594 if (j>=(textureCoordPoint[k]->n)) {
598 ConsoleMessage (
"stream_polyrep, have tcindex %d, tex coords %d, overflow",j,textureCoordPoint[k]->n);
603 jj = textureCoordPoint[k]->n / max(1,nmtexcoord);
604 j= j % textureCoordPoint[k]->n;
613 me = (
float*)textureCoordPoint[k]->p;
617 newTexCoords[k][i*ndim] = me[0]/(float)(jj);
618 newTexCoords[k][i*ndim+1] = me[1]/(float)(jj);
620 newTexCoords[k][i*ndim] = me[0];
621 newTexCoords[k][i*ndim+1] = me[1];
624 newTexCoords[k][i*ndim+2] = me[2];
626 newTexCoords[k][i*ndim+3] = me[3];
634 newTexCoords[k][i*ndim] = (newpoints[i].c[p->Sindex] - p->minVals[p->Sindex])/p->Ssize;
635 newTexCoords[k][i*ndim+1] = (newpoints[i].c[p->Tindex] - p->minVals[p->Tindex])/p->Ssize;
638 newTexCoords[k][i*ndim+2] = (newpoints[i].c[p->Tindex] - p->minVals[p->Tindex])/p->Ssize;;
641 newTexCoords[k][i*ndim+3] = 1.0f;
653 if (newpoints[i].c[0] > node->EXTENT_MAX_X) node->EXTENT_MAX_X = newpoints[i].c[0];
654 if (newpoints[i].c[0] < node->EXTENT_MIN_X) node->EXTENT_MIN_X = newpoints[i].c[0];
655 if (newpoints[i].c[1] > node->EXTENT_MAX_Y) node->EXTENT_MAX_Y = newpoints[i].c[1];
656 if (newpoints[i].c[1] < node->EXTENT_MIN_Y) node->EXTENT_MIN_Y = newpoints[i].c[1];
657 if (newpoints[i].c[2] > node->EXTENT_MAX_Z) node->EXTENT_MAX_Z = newpoints[i].c[2];
658 if (newpoints[i].c[2] < node->EXTENT_MIN_Z) node->EXTENT_MIN_Z = newpoints[i].c[2];
663 FREE_IF_NZ(r->actualCoord);
664 r->actualCoord = (
float *)newpoints;
665 FREE_IF_NZ(r->normal);
666 r->normal = (
float *)newnorms;
667 FREE_IF_NZ(r->flat_normal);
668 FREE_IF_NZ(r->cindex);
669 r->cindex = newcindex;
670 FREE_IF_NZ(r->actualFog);
671 r->actualFog = (
float*)newfog;
676 for(k=0;k<max(1,nmtexcoord);k++){
677 if (newTexCoords[k] != NULL) {
678 FREE_IF_NZ(r->GeneratedTexCoords[k]);
679 r->GeneratedTexCoords[k] = newTexCoords[k];
686 r->ntcoord = nmtexcoord;
687 memcpy(r->ntexdim,ntexdim,4*
sizeof(
int));
688 FREE_IF_NZ(r->color);
689 FREE_IF_NZ(r->colindex);
695 r->color = (
float *)newcolors;
698 FREE_IF_NZ(r->tcindex);
699 r->tcindex=newtcindex;
702 FREE_IF_NZ(r->norindex);
704 #ifdef STREAM_POLY_VERBOSE
705 printf (
"end stream_polyrep - ntri %d\n\n",r->ntri);
712 r->transparency = thisTrans;
713 r->isRGBAcolorNode = isRGBA;
718 if (r->VBO_buffers[NORMAL_VBO] == 0) glGenBuffers(1,&r->VBO_buffers[NORMAL_VBO]);
719 FW_GL_BINDBUFFER(GL_ARRAY_BUFFER,r->VBO_buffers[NORMAL_VBO]);
720 glBufferData(GL_ARRAY_BUFFER,r->ntri*
sizeof(
struct SFColor)*3,r->normal, GL_STATIC_DRAW);
725 if (r->VBO_buffers[COLOR_VBO] == 0) glGenBuffers(1,&r->VBO_buffers[COLOR_VBO]);
726 FW_GL_BINDBUFFER(GL_ARRAY_BUFFER,r->VBO_buffers[COLOR_VBO]);
727 glBufferData(GL_ARRAY_BUFFER,r->ntri*
sizeof(
struct SFColorRGBA)*3,r->color, GL_STATIC_DRAW);
731 if (r->VBO_buffers[FOG_VBO] == 0) glGenBuffers(1,&r->VBO_buffers[FOG_VBO]);
732 FW_GL_BINDBUFFER(GL_ARRAY_BUFFER,r->VBO_buffers[FOG_VBO]);
733 glBufferData(GL_ARRAY_BUFFER,r->ntri*
sizeof(
float)*3,r->actualFog, GL_STATIC_DRAW);
736 FW_GL_BINDBUFFER(GL_ARRAY_BUFFER,r->VBO_buffers[VERTEX_VBO]);
737 glBufferData(GL_ARRAY_BUFFER,r->ntri*
sizeof(
struct SFColor)*3,r->actualCoord, GL_STATIC_DRAW);
739 FW_GL_BINDBUFFER(GL_ELEMENT_ARRAY_BUFFER,r->VBO_buffers[INDEX_VBO]);
750 r->tri_indices = MALLOC(GLushort *,
sizeof(GLushort) * r->ntri*3);
755 for (i=0; i<r->ntri*3; i++) {
757 *to = (GLushort) *from; to++; from++;
760 glBufferData(GL_ELEMENT_ARRAY_BUFFER,
sizeof (GLushort)*r->ntri*3,r->tri_indices,GL_STATIC_DRAW);
767 FREE_IF_NZ(r->wire_indices);
768 lindex = MALLOC(GLushort *,
sizeof(GLushort) * r->ntri*3*2);
770 for(i=0;i<r->ntri;i++){
773 lindex[i6+0] = i3 + 0;
774 lindex[i6+1] = i3 + 1;
775 lindex[i6+2] = i3 + 1;
776 lindex[i6+3] = i3 + 2;
777 lindex[i6+4] = i3 + 2;
778 lindex[i6+5] = i3 + 0;
781 r->wire_indices = lindex;
790 FREE_IF_NZ(r->flat_normal);
791 r->flat_normal = MALLOC(GLfloat*,r->ntri*
sizeof(
struct SFColor)*3);
792 for(i=0;i<r->ntri;i++){
793 float a[3],b[3],c[3],d[3], e[3], f[3], g[3];
795 memcpy(a,&r->actualCoord[i9 +0],
sizeof(
struct SFColor));
796 memcpy(b,&r->actualCoord[i9 +3],
sizeof(
struct SFColor));
797 memcpy(c,&r->actualCoord[i9 +6],
sizeof(
struct SFColor));
802 memcpy(&r->flat_normal[i9 +0],g,
sizeof(
struct SFColor));
803 memcpy(&r->flat_normal[i9 +3],g,
sizeof(
struct SFColor));
804 memcpy(&r->flat_normal[i9 +6],g,
sizeof(
struct SFColor));
809 for(k=0;k<max(1,nmtexcoord);k++){
810 if (r->GeneratedTexCoords[k]) {
811 if (r->VBO_buffers[TEXTURE_VBO0+k] == 0) glGenBuffers(1,&r->VBO_buffers[TEXTURE_VBO0+k]);
812 FW_GL_BINDBUFFER(GL_ARRAY_BUFFER,r->VBO_buffers[TEXTURE_VBO0+k]);
813 glBufferData(GL_ARRAY_BUFFER,
sizeof (
float)*r->ntexdim[k]*r->ntri*3,r->GeneratedTexCoords[k], GL_STATIC_DRAW);
825 #ifdef STREAM_POLY_VERBOSE
826 printf (
"end spv for %p, extents %f %f, %f %f, %f %f\n",
842 GLfloat Tsize = 0.0f;
843 GLfloat Xsize = 0.0f;
844 GLfloat Ysize = 0.0f;
845 GLfloat Zsize = 0.0f;
848 psp->Sindex = 0; psp->Tindex = 0;
850 psp->minVals[0]=r->minVals[0];
851 psp->minVals[1]=r->minVals[1];
852 psp->minVals[2]=r->minVals[2];
854 #ifdef STREAM_POLY_VERBOSE
855 printf (
"have to gen default textures\n");
860 if (p->_nodeType == NODE_IndexedFaceSet || p->_nodeType == NODE_ElevationGrid) {
862 Xsize = r->maxVals[0]-psp->minVals[0];
863 Ysize = r->maxVals[1]-psp->minVals[1];
864 Zsize = r->maxVals[2]-psp->minVals[2];
868 if ((Xsize >= Ysize) && (Xsize >= Zsize)) {
870 psp->Ssize = Xsize; psp->Sindex = 0;
871 if (Ysize >= Zsize) {
872 Tsize = Ysize; psp->Tindex = 1;
874 Tsize = Zsize; psp->Tindex = 2;
876 }
else if ((Ysize >= Xsize) && (Ysize >= Zsize)) {
878 psp->Ssize = Ysize; psp->Sindex = 1;
879 if (Xsize >= Zsize) {
880 Tsize = Xsize; psp->Tindex = 0;
882 Tsize = Zsize; psp->Tindex = 2;
886 psp->Ssize = Zsize; psp->Sindex = 2;
887 if (Xsize >= Ysize) {
888 Tsize = Xsize; psp->Tindex = 0;
890 Tsize = Ysize; psp->Tindex = 1;