31#include <system_threads.h>
35#include <libFreeWRL.h>
40#include "../vrml_parser/Structs.h"
41#include "../main/headers.h"
43#include "../scenegraph/readpng.h"
44#include "../input/InputFunctions.h"
45#include "../opengl/Material.h"
46#include "../opengl/OpenGL_Utils.h"
48#include "../world_script/fieldSet.h"
49#include "../scenegraph/Component_Shape.h"
50#include "../scenegraph/Component_CubeMapTexturing.h"
51#include "../scenegraph/RenderFuncs.h"
52#include "LoadTextures.h"
72int multitex_source[2];
77#ifndef GL_EXT_texture_cube_map
80# define GL_TEXTURE_CUBE_MAP_EXT 0x8513
82# define GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT 0x8515
97 struct Vector *activeTextureTable;
101 int currentlyWorkingOn;
102 int textureInProcess;
108void *Textures_constructor(){
109 void *v = MALLOCV(
sizeof(
struct pTextures));
113void Textures_init(
struct tTextures *t){
117 t->prv = Textures_constructor();
120 p->activeTextureTable = NULL;
123 p->currentlyWorkingOn = -1;
125 p->textureInProcess = -1;
128void Textures_clear(
struct tTextures *t){
130 glDeleteBuffers (1,&t->defaultBlankTexture);
148#if defined(_MSC_VER) || defined(AQUA)
152#if !defined(_ANDROID) && !defined(ANDROIDNDK) && !defined(GLES2)
153GLXContext textureContext = NULL;
163struct Uni_String *newASCIIString(
const char *str);
165int readpng_init(FILE *infile, ulg *pWidth, ulg *pHeight);
166void readpng_cleanup(
int free_image_data);
169static void myTexImage2D (
int generateMipMaps, GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, GLubyte *pixels);
179# define uint32 uint32_t
184static void myScaleImage(
int srcX,
int srcY,
int destX,
int destY,
unsigned char *src,
unsigned char *dest) {
188 uint32 *src32 = (uint32 *)src;
189 uint32 *dest32 = (uint32 *)dest;
191 if ((srcY<=0) || (destY<=0) || (srcX<=0) || (destX<=0))
return;
192 if (src == NULL)
return;
193 if (dest == NULL)
return;
195 if ((srcY==destY) && (srcX==destX)) {
197 memcpy (dest,src,srcY*srcX*4);
201 YscaleFactor = ((float)srcY) / ((float)destY);
202 XscaleFactor = ((float)srcX) / ((float)destX);
204 for (wye=0; wye<destY; wye++) {
205 for (yex=0; yex<destX; yex++) {
208 size_t oldIndex, newIndex;
210 fx = YscaleFactor * ((float) wye);
211 fy = XscaleFactor * ((float) yex);
214 oldIndex = (size_t)row * (
size_t)srcX + (size_t)column;
215 newIndex = (size_t)wye * (
size_t)destX + (size_t)yex;
216 dest32[newIndex] = src32[oldIndex];
221static void myScaleImage3D(
int srcX,
int srcY,
int srcZ,
int destX,
int destY,
int destZ,
unsigned char *src,
unsigned char *dest) {
227 uint32 *src32 = (uint32 *)src;
228 uint32 *dest32 = (uint32 *)dest;
230 if ((srcY<=0) || (destY<=0) || (srcX<=0) || (destX<=0) || (srcZ<=0) || (destZ<=0))
return;
231 if (src == NULL)
return;
232 if (dest == NULL)
return;
234 if ((srcY==destY) && (srcX==destX) && (srcZ==destZ)) {
236 memcpy (dest,src,srcY*srcX*srcZ*4);
240 YscaleFactor = ((float)srcY) / ((float)destY);
241 XscaleFactor = ((float)srcX) / ((float)destX);
242 ZscaleFactor = ((float)srcZ) / ((float)destZ);
244 for (iz=0; iz<destZ; iz++) {
246 fz = ZscaleFactor * ((float) iz);
248 for (iy=0; iy<destY; iy++) {
250 fy = YscaleFactor * ((float) iy);
252 for (ix=0; ix<destX; ix++) {
256 fx = XscaleFactor * ((float) ix);
258 oldIndex = (page * srcY + row) * srcX + column;
259 dest32[(iz*destY + iy)*destX+ix] = src32[oldIndex];
264int iclamp(
int ival,
int istart,
int iend) {
266 iret = ival > iend? iend : ival;
267 iret = iret < istart ? istart : iret;
270void compute_3D_alpha_gradient_store_rgb(
char *dest,
int x,
int y,
int z){
275 int iz,iy,ix, jz,jy,jx,jzz,jyy,jxx, k;
277 int gradient[3], maxgradient[3], mingradient[3];
278 unsigned char *urgba;
279 uint32 *pixels = (uint32 *)dest;
291 for(k=0;k<3;k++) gradient[k] = 0;
292 rgba0 = (
char *) &pixels[(iz*y +iy)*x + ix];
293 urgba = (
unsigned char *)rgba0;
298 if(iz == z-1) jzz = -1;
299 if(iy == y-1) jyy = -1;
300 if(ix == x-1) jxx = -1;
301 jx = jxx; jy = jyy; jz = jzz;
303 rgba1 = (
char *) &pixels[((iz+jz)*y +(iy+jy))*x + (ix+jx)];
304 gradient[0] = (int)rgba1[3] - (
int)rgba0[3];
307 rgba1 = (
char *) &pixels[((iz+jz)*y +(iy+jy))*x + (ix+jx)];
308 gradient[1] = (int)rgba1[3] - (
int)rgba0[3];
311 rgba1 = (
char *) &pixels[((iz+jz)*y +(iy+jy))*x + (ix+jx)];
312 gradient[2] = (int)rgba1[3] - (
int)rgba0[3];
316 int cube[3][3][3], i,j,ii,jj,kk;
318 ii = iclamp(ix+i,0,x-1);
320 jj= iclamp(iy+j,0,y-1);
322 kk = iclamp(iz+k,0,z-1);
323 cube[i+1][j+1][k+1] = ((
unsigned char *)&pixels[(kk*y +jj)*x + ii])[3];
334 gradient[0] += cube[0][0][1] + 2*cube[0][1][1] + cube[0][2][1];
335 gradient[0] -= cube[2][0][1] + 2*cube[2][1][1] + cube[2][2][1];
336 gradient[0] += cube[0][1][0] + 2*cube[0][1][1] + cube[0][1][2];
337 gradient[0] -= cube[2][1][0] + 2*cube[2][1][1] + cube[2][1][2];
340 gradient[1] += cube[1][0][0] + 2*cube[1][0][1] + cube[1][0][2];
341 gradient[1] -= cube[1][2][0] + 2*cube[1][2][1] + cube[1][2][2];
342 gradient[1] += cube[0][0][1] + 2*cube[1][0][1] + cube[2][0][1];
343 gradient[1] -= cube[9][2][1] + 2*cube[1][2][1] + cube[2][2][1];
346 gradient[2] += cube[0][1][0] + 2*cube[1][1][0] + cube[2][1][0];
347 gradient[2] -= cube[0][1][2] + 2*cube[1][1][2] + cube[2][1][2];
348 gradient[2] += cube[1][0][0] + 2*cube[1][1][0] + cube[1][2][0];
349 gradient[2] -= cube[1][9][2] + 2*cube[1][1][2] + cube[1][2][2];
359 for(k=0;k<3;k++) gradient[k] /= 2;
361 maxgradient[k] = max(maxgradient[k],gradient[k]);
362 mingradient[k] = min(mingradient[k],gradient[k]);
368 for(k=0;k<3;k++) gradient[k] += 127;
371 for(k=0;k<3;k++) urgba[k] = (
unsigned char)gradient[k];
380 printf(
"mingradient %d %d %d\n",mingradient[0],mingradient[1],mingradient[2]);
381 printf(
"maxgradient %d %d %d\n",maxgradient[0],maxgradient[1],maxgradient[2]);
390 tti2->texdata = (
unsigned char *)dest;
392 saveImage_web3dit(tti2,
"gradientRGB.web3dit");
394 saveImage_web3dit(tti2,
"gradientRGBA.web3dit");
399static void GenMipMap2D( GLubyte *src, GLubyte **dst,
int srcWidth,
int srcHeight,
int *dstWidth,
int *dstHeight )
408 *dstWidth = srcWidth / 2;
409 if ( *dstWidth <= 0 )
412 *dstHeight = srcHeight / 2;
413 if ( *dstHeight <= 0 )
416 total_size =
sizeof(GLubyte) * (
size_t)texelSize * (size_t)(*dstWidth) * (size_t)(*dstHeight);
417 *dst = MALLOC(
void *, total_size );
422 for ( y = 0; y < *dstHeight; y++ )
424 for( x = 0; x < *dstWidth; x++ )
426 size_t srcIndex[4], x2, y2, swidth, texsize, kd;
449 srcIndex[0] = (y2*swidth + x2) * texsize;
450 srcIndex[1] = (y2*swidth + x2 + (size_t)1L) * texsize;
451 srcIndex[2] = ((y2 + 1L)*swidth + x2) * texsize;
452 srcIndex[3] = ((y2 + 1L)*swidth + x2 + (
size_t)1L) * texsize;
455 for ( sample = 0; sample < 4; sample++ )
457 r += src[srcIndex[sample] + (size_t)0L];
458 g += src[srcIndex[sample] + (size_t)1L];
459 b += src[srcIndex[sample] + (size_t)2L];
460 a += src[srcIndex[sample] + (size_t)3L];
470 kd = ((size_t)y * (
size_t)(*dstWidth) + (size_t)x ) * (size_t)texelSize;
472 pix[0] = (GLubyte)( r );
473 pix[1] = (GLubyte)( g );
474 pix[2] = (GLubyte)( b );
475 pix[3] = (GLubyte)( a );
485static void myTexImage2D (
int generateMipMaps, GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, GLubyte *pixels) {
486 GLubyte *prevImage = NULL;
487 GLubyte *newImage = NULL;
492 FW_GL_TEXIMAGE2D(target,level,internalformat,width,height,border,format,type,pixels);
497 case GL_NO_ERROR:
break;
499 ConsoleMessage(
"glError %d in glTexImage2D\n",(
int)err);
502 if (!generateMipMaps)
return;
503 if ((width <=1) && (height <=1))
return;
507 total_size = (size_t)4L * width * height;
508 prevImage = MALLOC(GLubyte *, total_size);
509 memcpy (prevImage, pixels, total_size);
514 while ((width > 1) && (height > 1)) {
515 GLint newWidth, newHeight;
517 GenMipMap2D(prevImage,&newImage, width, height, &newWidth, &newHeight);
519 FW_GL_TEXIMAGE2D(target,level,internalformat,newWidth,newHeight,0,format,GL_UNSIGNED_BYTE,newImage);
521 FREE_IF_NZ(prevImage);
522 prevImage = newImage;
528 FREE_IF_NZ(newImage);
535const char *texst(
int num)
537 if (num == TEX_NOTLOADED)
return "TEX_NOTLOADED";
538 if (num == TEX_LOADING)
return "TEX_LOADING";
539 if (num == TEX_NEEDSBINDING)
return "TEX_NEEDSBINDING";
540 if (num == TEX_LOADED)
return "TEX_LOADED";
541 if (num == TEX_UNSQUASHED)
return "UNSQUASHED";
547int isTextureAlpha(
int texno) {
553 ti = getTableIndex(texno);
554 if (ti==NULL)
return FALSE;
556 if (ti->status==TEX_LOADED) {
564int fwl_isTextureinitialized() {
565 return gglobal()->threads.TextureThreadRunning;
569int fwl_isTextureLoaded(
int texno) {
575 ti = getTableIndex(texno);
576 return (ti->status==TEX_LOADED);
580int fwl_isTextureParsing() {
585 if (p->textureInProcess > 0) {
586 printf(
"call to fwl_isTextureParsing %d, returning %d\n",
587 p->textureInProcess,p->textureInProcess > 0);
590 return p->textureInProcess >0;
594void releaseTexture(
struct X3D_Node *node) {
599 if (node->_nodeType == NODE_ImageTexture) {
601 }
else if (node->_nodeType == NODE_PixelTexture) {
603 }
else if (node->_nodeType == NODE_MovieTexture) {
605 }
else if (node->_nodeType == NODE_PixelTexture3D) {
607 }
else if (node->_nodeType == NODE_ImageTexture3D) {
609 }
else if (node->_nodeType == NODE_ComposedTexture3D) {
615 printf (
"releaseTexture, calling getTableIndex\n");
616 ti = getTableIndex(tableIndex);
617 printf (
"releaseTexture, ti %p, ti->status %d\n",ti,ti->status);
618 ti->status = TEX_NOTLOADED;
620 if (ti->OpenGLTexture != TEXTURE_INVALID) {
621 printf (
"deleting %d textures, starting at %u\n",ti->frames, ti->OpenGLTexture);
622 ti->OpenGLTexture = TEXTURE_INVALID;
627 ti = getTableIndex(tableIndex);
629 ti->status = TEX_NOTLOADED;
630 if (ti->OpenGLTexture != TEXTURE_INVALID) {
631 FW_GL_DELETETEXTURES(1, &ti->OpenGLTexture);
632 ti->OpenGLTexture = TEXTURE_INVALID;
645sprintf (line,
"getTableIndex, looking for %d",indx);
646ConsoleMessage (line);}
649 ConsoleMessage (
"getTableIndex, texture <0 requested");
653 if (p->activeTextureTable ==NULL ) {
654 ConsoleMessage (
"NULL sizing errror in getTableIndex");
659 if (indx >= vectorSize(p->activeTextureTable)) {
660 ConsoleMessage (
"sizing errror in getTableIndex");
667printf (
"getTableIndex - valid request\n");
668sprintf (line,
"getTableIndex, for %d, size %d",indx, vectorSize(p->activeTextureTable));
669ConsoleMessage (line);}
674int getTextureTableIndexFromFromTextureNode(
struct X3D_Node *node){
675 int thisTexture = -1;
676 int thisTextureType = node->_nodeType;
677 if (thisTextureType==NODE_ImageTexture){
679 thisTexture = it->__textureTableIndex;
680 }
else if (thisTextureType==NODE_PixelTexture){
682 thisTexture = pt->__textureTableIndex;
683 }
else if (thisTextureType==NODE_MovieTexture){
685 thisTexture = mt->__textureTableIndex;
686 }
else if (thisTextureType==NODE_ImageCubeMapTexture){
688 thisTexture = ict->__textureTableIndex;
689 }
else if (thisTextureType==NODE_GeneratedCubeMapTexture){
691 thisTexture = ict->__textureTableIndex;
692 }
else if (thisTextureType==NODE_PixelTexture3D){
694 thisTexture = pt->__textureTableIndex;
695 }
else if (thisTextureType==NODE_ImageTexture3D){
697 thisTexture = pt->__textureTableIndex;
698 }
else if (thisTextureType==NODE_ComposedTexture3D){
700 thisTexture = pt->__textureTableIndex;
701 }
else if (thisTextureType==NODE_MultiTexture){
703 thisTexture = getTextureTableIndexFromFromTextureNode(X3D_NODE(pt->texture.p[0]));
705 ConsoleMessage (
"Invalid type for texture, %s\n",stringNodeType(thisTextureType));
711 int index = getTextureTableIndexFromFromTextureNode(textureNode);
713 ret = getTableIndex(index);
716int getGlTextureNumberFromTextureNode(
struct X3D_Node *textureNode){
718 if(tts == NULL)
return 0;
719 return tts->OpenGLTexture;
722int getTextureSizeFromTextureNode(
struct X3D_Node *textureNode,
int *ixyz){
725 ixyz[0] = ixyz[1] = ixyz[2] = 0;
739void registerTexture0(
int iaction,
struct X3D_Node *tmp) {
747 if ((it->_nodeType == NODE_ImageTexture) ||
748 (it->_nodeType == NODE_PixelTexture) ||
749 (it->_nodeType == NODE_ImageCubeMapTexture) ||
750 (it->_nodeType == NODE_GeneratedCubeMapTexture) ||
751 (it->_nodeType == NODE_PixelTexture3D) ||
752 (it->_nodeType == NODE_ImageTexture3D) ||
753 (it->_nodeType == NODE_ComposedTexture3D) ||
754 (it->_nodeType == NODE_MovieTexture)
767 if (p->activeTextureTable == NULL) {
773 textureNumber = vectorSize(p->activeTextureTable);
777 DEBUG_TEX(
"CREATING TEXTURE NODE: type %d\n", it->_nodeType);
780 switch (it->_nodeType) {
782 case NODE_ImageTexture:
783 it->__textureTableIndex = textureNumber;
785 case NODE_PixelTexture: {
788 pt->__textureTableIndex = textureNumber;
790 case NODE_PixelTexture3D: {
793 pt->__textureTableIndex = textureNumber;
795 case NODE_ImageTexture3D: {
798 pt->__textureTableIndex = textureNumber;
800 case NODE_ComposedTexture3D: {
803 pt->__textureTableIndex = textureNumber;
805 case NODE_MovieTexture: {
808 mt->__textureTableIndex = textureNumber;
811 case NODE_ImageCubeMapTexture: {
814 v1t->__textureTableIndex = textureNumber;
818 case NODE_GeneratedCubeMapTexture: {
821 v1t->__textureTableIndex = textureNumber;
828 newTexture->nodeType = it->_nodeType;
829 newTexture->scenegraphNode = X3D_NODE(tmp);
830 newTexture->textureNumber = textureNumber;
840 int *textureNumber = NULL;
845 switch (it->_nodeType) {
847 case NODE_ImageTexture:
848 textureNumber = &it->__textureTableIndex;
850 case NODE_PixelTexture: {
853 textureNumber = &pt->__textureTableIndex;
855 case NODE_PixelTexture3D: {
858 textureNumber = &pt->__textureTableIndex;
860 case NODE_ImageTexture3D: {
863 textureNumber = &pt->__textureTableIndex;
865 case NODE_ComposedTexture3D: {
868 textureNumber = &pt->__textureTableIndex;
870 case NODE_MovieTexture: {
873 textureNumber = &mt->__textureTableIndex;
876 case NODE_ImageCubeMapTexture: {
879 textureNumber = &v1t->__textureTableIndex;
882 case NODE_GeneratedCubeMapTexture: {
885 textureNumber = &v1t->__textureTableIndex;
889 tti = getTableIndex(*textureNumber);
904 FREE_IF_NZ(tti->texdata);
914void registerTexture(
struct X3D_Node *tmp) {
915 registerTexture0(1,tmp);
917void unRegisterTexture(
struct X3D_Node *tmp) {
918 registerTexture0(0,tmp);
923void unRegisterPolyRep(
struct X3D_Node *tmp)
927 free_polyrep(tmp->_intern);
932void add_node_to_broto_context(
struct X3D_Proto *currentContext,
struct X3D_Node *node);
935void push_render_geom(
int igeom);
936void pop_render_geom();
945 thisurl.n = 0; thisurl.p = NULL;
948 for (count=0; count<6; count++) {
951 case 0: {thistex = X3D_IMAGETEXTURE(node->__frontTexture); thisurl = node->frontUrl;
break;}
952 case 1: {thistex = X3D_IMAGETEXTURE(node->__backTexture); thisurl = node->backUrl;
break;}
953 case 2: {thistex = X3D_IMAGETEXTURE(node->__topTexture); thisurl = node->topUrl;
break;}
954 case 3: {thistex = X3D_IMAGETEXTURE(node->__bottomTexture); thisurl = node->bottomUrl;
break;}
955 case 4: {thistex = X3D_IMAGETEXTURE(node->__rightTexture); thisurl = node->rightUrl;
break;}
956 case 5: {thistex = X3D_IMAGETEXTURE(node->__leftTexture); thisurl = node->leftUrl;
break;}
958 if (thisurl.n != 0 ) {
960 if (thistex == NULL) {
962 thistex = createNewX3DNode(NODE_ImageTexture);
963 thistp = createNewX3DNode (NODE_TextureProperties);
964 if(node->_executionContext){
965 add_node_to_broto_context(X3D_PROTO(node->_executionContext),X3D_NODE(thistex));
966 add_node_to_broto_context(X3D_PROTO(node->_executionContext),X3D_NODE(thistp));
972 thistp->generateMipMaps = GL_FALSE;
975 thistex->textureProperties = X3D_NODE(thistp);
976 ADD_PARENT(X3D_NODE(thistp), X3D_NODE(thistex));
979 printf (
"bg, creating shadow texture node url.n = %d\n",thisurl.n);
984 for (i=0; i<thisurl.n; i++) {
985 thistex->url.p[i] = newASCIIString (thisurl.p[i]->strptr);
987 thistex->url.n = thisurl.n;
990 case 0: {node->__frontTexture = X3D_NODE(thistex);
break;}
991 case 1: {node->__backTexture = X3D_NODE(thistex);
break;}
992 case 2: {node->__topTexture = X3D_NODE(thistex);
break;}
993 case 3: {node->__bottomTexture = X3D_NODE(thistex);
break;}
994 case 4: {node->__rightTexture = X3D_NODE(thistex);
break;}
995 case 5: {node->__leftTexture = X3D_NODE(thistex);
break;}
1000 gglobal()->RenderFuncs.textureStackTop = 0;
1002 push_render_geom(1);
1003 render_node(X3D_NODE(thistex));
1006 textureTransform_start();
1009 textureCoord_send(&mtf);
1010 FW_GL_VERTEX_POINTER(3,GL_FLOAT,0,BackgroundVert);
1011 FW_GL_NORMAL_POINTER(GL_FLOAT,0,Backnorms);
1013 sendArraysToGPU (GL_TRIANGLES, count*6, 6);
1015 textureTransform_end();
1027 for (count=0; count<6; count++) {
1030 case 0: {POSSIBLE_PROTO_EXPANSION(
struct X3D_Node *, node->frontTexture,thistex);
break;}
1031 case 1: {POSSIBLE_PROTO_EXPANSION(
struct X3D_Node *, node->backTexture,thistex);
break;}
1032 case 2: {POSSIBLE_PROTO_EXPANSION(
struct X3D_Node *, node->topTexture,thistex);
break;}
1033 case 3: {POSSIBLE_PROTO_EXPANSION(
struct X3D_Node *, node->bottomTexture,thistex);
break;}
1034 case 4: {POSSIBLE_PROTO_EXPANSION(
struct X3D_Node *, node->rightTexture,thistex);
break;}
1035 case 5: {POSSIBLE_PROTO_EXPANSION(
struct X3D_Node *, node->leftTexture,thistex);
break;}
1040 if ((thistex->_nodeType == NODE_ImageTexture) ||
1041 (thistex->_nodeType == NODE_PixelTexture) ||
1042 (thistex->_nodeType == NODE_MovieTexture) ||
1043 (thistex->_nodeType == NODE_MultiTexture)) {
1046 switch (thistex->_nodeType) {
1047 case NODE_ImageTexture: {
1048 if (X3D_IMAGETEXTURE(thistex)->textureProperties == NULL) {
1049 thistp = createNewX3DNode (NODE_TextureProperties);
1050 if(node->_executionContext){
1051 add_node_to_broto_context(X3D_PROTO(node->_executionContext),X3D_NODE(thistp));
1053 X3D_IMAGETEXTURE(thistex)->textureProperties = X3D_NODE(thistp);
1054 ADD_PARENT(X3D_NODE(thistp),thistex);
1058 case NODE_PixelTexture: {
1059 if (X3D_PIXELTEXTURE(thistex)->textureProperties == NULL) {
1060 thistp = createNewX3DNode (NODE_TextureProperties);
1061 if(node->_executionContext){
1062 add_node_to_broto_context(X3D_PROTO(node->_executionContext),X3D_NODE(thistp));
1065 X3D_PIXELTEXTURE(thistex)->textureProperties = X3D_NODE(thistp);
1066 ADD_PARENT(X3D_NODE(thistp),thistex);
1071 case NODE_MovieTexture:
1072 case NODE_MultiTexture:
1076 gglobal()->RenderFuncs.textureStackTop = 0;
1078 render_node((
void *)thistex);
1081 textureCoord_send(&mtf);
1082 FW_GL_VERTEX_POINTER(3,GL_FLOAT,0,BackgroundVert);
1083 FW_GL_NORMAL_POINTER(GL_FLOAT,0,Backnorms);
1085 sendArraysToGPU (GL_TRIANGLES, count*6, 6);
1094void loadTextureNode (
struct X3D_Node *node,
void *vparam)
1097 if (NODE_NEEDS_COMPILING) {
1099 DEBUG_TEX (
"FORCE NODE RELOAD: %p %s\n", node, stringNodeType(node->_nodeType));
1111 switch (node->_nodeType) {
1113 case NODE_MovieTexture: {
1118 case NODE_PixelTexture:
1119 releaseTexture(node);
1122 case NODE_ImageTexture:
1123 releaseTexture(node);
1126 case NODE_ImageCubeMapTexture:
1127 releaseTexture(node);
1130 case NODE_GeneratedCubeMapTexture:
1134 case NODE_PixelTexture3D:
1135 releaseTexture(node);
1137 case NODE_ImageTexture3D:
1138 releaseTexture(node);
1140 case NODE_ComposedTexture3D:
1141 releaseTexture(node);
1145 printf (
"loadTextureNode, unknown node type %s\n",stringNodeType(node->_nodeType));
1152 new_bind_image (X3D_NODE(node), (
struct multiTexParams *)vparam);
1162 rdr_caps = tg->display.rdr_caps;
1168 if (node->__xparams == 0) {
1170 node->__xparams = MALLOC (
void *,
sizeof (
struct multiTexParams) * rdr_caps->texture_units);
1179 for (count = 0; count < rdr_caps->texture_units; count++) {
1180 paramPtr->multitex_mode[0]= MTMODE_MODULATE;
1181 paramPtr->multitex_mode[1]= 0;
1182 paramPtr->multitex_source[0]=INT_ID_UNDEFINED;
1183 paramPtr->multitex_source[1]=0;
1184 paramPtr->multitex_function=INT_ID_UNDEFINED;
1191 max = node->texture.n;
1192 if (max > rdr_caps->texture_units) max = rdr_caps->texture_units;
1200 for (count = 0; count < max; count++) {
1201 char *smode, *ssource, *sfunc;
1202 smode = ssource = sfunc = NULL;
1203 if(node->mode.n>count){
1205 smode = node->mode.p[count]->strptr;
1207 mode = findFieldInMULTITEXTUREMODE(smode);
1208 if(mode > -1) mode += 1;
1211 if(strchr(smode,
'/') || strchr(smode,
',')){
1213 char *srgb, *salpha, *b1,*b2, *splittable;
1214 int modergb, modealpha;
1215 splittable = strdup(smode);
1216 b1 = strchr(splittable,
' ');
1217 b2 = strrchr(splittable,
' ');
1219 splittable[b1 - splittable] =
'\0';
1221 modergb = findFieldInMULTITEXTUREMODE(srgb);
1223 modergb = MTMODE_MODULATE;
1226 modealpha = findFieldInMULTITEXTUREMODE(salpha);
1228 modealpha = MTMODE_MODULATE;
1237 paramPtr->multitex_mode[0] = mode;
1238 paramPtr->multitex_mode[1] = modea;
1242 if(node->source.n > count) {
1243 int source, sourcea;
1244 ssource = node->source.p[count]->strptr;
1245 source = findFieldInMULTITEXTURESOURCE(ssource);
1246 if(source > -1) source += 1;
1250 if(strchr(ssource,
'/') || strchr(ssource,
',')){
1252 char *srgb, *salpha, *b1,*b2, *splittable;
1253 int sourcergb, sourcealpha;
1254 splittable = strdup(ssource);
1255 b1 = strchr(splittable,
' ');
1256 b2 = strrchr(splittable,
' ');
1258 splittable[b1 - splittable] =
'\0';
1260 sourcergb = findFieldInMULTITEXTURESOURCE(srgb);
1262 sourcergb = INT_ID_UNDEFINED;
1265 sourcealpha = findFieldInMULTITEXTURESOURCE(salpha);
1268 sourcea = sourcealpha;
1272 paramPtr->multitex_source[0] = source;
1273 paramPtr->multitex_source[1] = sourcea;
1278 if (node->function.n>count) {
1280 sfunc = node->function.p[count]->strptr;
1282 ifunc = findFieldInMULTITEXTUREFUNCTION(sfunc);
1285 paramPtr->multitex_function = ifunc;
1290printf (
"compile_MultiTexture, %d of %d, mode %d %d source %d %d function %d m %s s %s f %s\n",
1291count,max,paramPtr->multitex_mode[0],paramPtr->multitex_mode[1],paramPtr->multitex_source[0],paramPtr->multitex_source[1],paramPtr->multitex_function,smode,ssource,sfunc);
1306 rdr_caps = tg->display.rdr_caps;
1309 printf (
"loadMultiTexture, this %s has %d textures %x %x\n",stringNodeType(node->_nodeType),
1311 (
int) node->texture.p[0], (
int) node->texture.p[1]);
1312 printf (
" change %d ichange %d\n",node->_change, node->_ichange);
1316 if (NODE_NEEDS_COMPILING) {
1317 compileMultiTexture(node);
1327 max = node->texture.n;
1330 if (max > rdr_caps->texture_units) max = rdr_caps->texture_units;
1331 if (max > MAX_MULTITEXTURE) max = MAX_MULTITEXTURE;
1338 printf (
"loadMultiTExture, param stack:\n");
1339 for (count=0; count<max; count++) {
1340 printf (
" tex %d source %d mode %d\n",count,paramPtr[count].multitex_source,paramPtr[count].multitex_mode);
1344 for (count=0; count < max; count++) {
1346 printf (
"loadMultiTexture, working on texture %d\n",count);
1350 nt = X3D_IMAGETEXTURE(node->texture.p[count]);
1352 switch (nt->_nodeType) {
1353 case NODE_PixelTexture:
1354 case NODE_ImageTexture :
1356 loadTextureNode (X3D_NODE(nt),paramPtr);
1358 case NODE_MultiTexture:
1359 printf (
"MultiTexture texture %d is a MULTITEXTURE!!\n",count);
1362 printf (
"MultiTexture - unknown sub texture type %d\n",
1370 tg->RenderFuncs.textureStackTop++;
1377 printf (
"loadMultiTexture, textureStackTop %d\n",gglobal()->RenderFuncs.textureStackTop);
1378 printf (
"loadMultiTexture, finished with texture %d\n",count);
1384int getTextureDescriptors(
struct X3D_Node *textureNode,
int *textures,
int *modes,
int *sources,
int *funcs,
int *width,
int *height){
1386 if( textureNode == NULL)
return 0;
1387 if(textureNode->_nodeType == NODE_MultiTexture){
1391 ntexture = pt->texture.n;
1392 for(
int i=0;i<ntexture;i++){
1395 textures[i] = getGlTextureNumberFromTextureNode(pt->texture.p[i]);
1396 iret = getTextureSizeFromTextureNode(pt->texture.p[i],ixyz);
1398 modes[i] = xparam[i].multitex_mode[0] + + 100*xparam[i].multitex_mode[1];
1399 sources[i] = xparam[i].multitex_source[0] + 100*xparam[i].multitex_source[1];
1400 funcs[i] = xparam[i].multitex_function;
1406 textures[0] = getGlTextureNumberFromTextureNode(textureNode);
1408 modes[0] = MTMODE_REPLACE;
1409 sources[0] = INT_ID_UNDEFINED;
1410 funcs[0] = INT_ID_UNDEFINED;
1411 iret = getTextureSizeFromTextureNode(textureNode,ixyz);
1413 height[0] = ixyz[1];
1419#define BOUNDARY_TO_GL(direct) \
1420 switch (findFieldInTEXTUREBOUNDARYKEYWORDS(tpNode->boundaryMode##direct->strptr)) { \
1421 case TB_CLAMP: direct##rc=GL_CLAMP; break; \
1422 case TB_CLAMP_TO_EDGE: direct##rc=GL_CLAMP_TO_EDGE; break; \
1423 case TB_CLAMP_TO_BOUNDARY: direct##rc=GL_CLAMP_TO_BORDER; break; \
1424 case TB_MIRRORED_REPEAT: direct##rc=GL_MIRRORED_REPEAT; break; \
1425 case TB_REPEAT: direct##rc=GL_REPEAT; break; \
1426 default: direct##rc = GL_REPEAT; \
1431#define DEF_FINDFIELD(arr) \
1432 static int findFieldIn##arr(const char* field) \
1434 return findFieldInARR(field, arr, arr##_COUNT); \
1437DEF_FINDFIELD(TEXTUREMINIFICATIONKEYWORDS)
1438DEF_FINDFIELD(TEXTUREMAGNIFICATIONKEYWORDS)
1439DEF_FINDFIELD(TEXTUREBOUNDARYKEYWORDS)
1440DEF_FINDFIELD(TEXTURECOMPRESSIONKEYWORDS)
1444 int rx,ry,rz,sx,sy,sz;
1450 float anisotropicDegree=1.0f;
1454 GLint minFilter, magFilter;
1456 int generateMipMaps;
1458 unsigned char *mytexdata;
1464 pthread_mutex_t gl_mutex1 = PTHREAD_MUTEX_INITIALIZER;
1474 int haveValidTexturePropertiesNode;
1480 pthread_mutex_lock( &gl_mutex1 );
1484 rdr_caps = tg->display.rdr_caps;
1488 Src = FALSE; Trc = FALSE; Rrc = FALSE;
1490 haveValidTexturePropertiesNode = FALSE;
1492 borderColour.c[0]=0.0f;borderColour.c[1]=0.0f;borderColour.c[2]=0.0f;borderColour.c[3]=0.0f;
1493 compression = GL_FALSE;
1498 if (!checkNode(me->scenegraphNode, __FILE__,__LINE__)) {
1499 ConsoleMessage (
"main node disappeared, ignoring texture\n");
1500 me->status = TEXTURE_INVALID;
1503 pthread_mutex_unlock( &gl_mutex1 );
1512 if (me->texdata == NULL) {
1513 char buff[] = {0x70, 0x70, 0x70, 0xff} ;
1517 me->hasAlpha = FALSE;
1518 me->texdata = MALLOC(
unsigned char *, 4);
1519 memcpy (me->texdata, buff, 4);
1527 if (me->OpenGLTexture == TEXTURE_INVALID) {
1529 if ((getAppearanceProperties()->cubeFace==0) || (getAppearanceProperties()->cubeFace == GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT)) {
1530 FW_GL_GENTEXTURES (1, &me->OpenGLTexture);
1534 printf (
"just glGend texture for block %p is %u, type %s\n",
1535 me, me->OpenGLTexture,stringNodeType(me->nodeType));
1541 if (me->nodeType == NODE_ImageTexture) {
1543 Src = it->repeatS; Trc = it->repeatT;
1544 tpNode = X3D_TEXTUREPROPERTIES(it->textureProperties);
1545 }
else if (me->nodeType == NODE_PixelTexture) {
1547 Src = pt->repeatS; Trc = pt->repeatT;
1548 tpNode = X3D_TEXTUREPROPERTIES(pt->textureProperties);
1549 }
else if (me->nodeType == NODE_MovieTexture) {
1551 Src = mt->repeatS; Trc = mt->repeatT;
1552 tpNode =X3D_TEXTUREPROPERTIES(mt->textureProperties);
1553 }
else if (me->nodeType == NODE_PixelTexture3D) {
1555 Src = pt3d->repeatS; Trc = pt3d->repeatT; Rrc = pt3d->repeatR;
1556 tpNode = X3D_TEXTUREPROPERTIES(pt3d->textureProperties);
1557 }
else if (me->nodeType == NODE_ImageTexture3D) {
1560 Src = it3d->repeatS; Trc = it3d->repeatT; Rrc = it3d->repeatR;
1561 tpNode = X3D_TEXTUREPROPERTIES(it3d->textureProperties);
1562 }
else if (me->nodeType == NODE_ComposedTexture3D) {
1565 Src = ct3d->repeatS; Trc = ct3d->repeatT; Rrc = ct3d->repeatR;
1566 tpNode = X3D_TEXTUREPROPERTIES(ct3d->textureProperties);
1567 }
else if (me->nodeType == NODE_ImageCubeMapTexture) {
1569 tpNode = X3D_TEXTUREPROPERTIES(mi->textureProperties);
1570 }
else if (me->nodeType == NODE_GeneratedCubeMapTexture) {
1572 tpNode = X3D_TEXTUREPROPERTIES(mi->textureProperties);
1577 me->repeatSTR[0] = Src;
1578 me->repeatSTR[1] = Trc;
1579 me->repeatSTR[2] = Rrc;
1584 if (tpNode->_nodeType != NODE_TextureProperties) {
1585 ConsoleMessage (
"have a %s as a textureProperties node",stringNodeType(tpNode->_nodeType));
1587 haveValidTexturePropertiesNode = TRUE;
1588 generateMipMaps = tpNode->generateMipMaps?GL_TRUE:GL_FALSE;
1589 texPri = tpNode->texturePriority;
1590 if ((texPri < 0.0) || (texPri>1.0)) {
1592 ConsoleMessage (
"invalid texturePriority of %f",tpNode->texturePriority);
1594 memcpy(&borderColour,&(tpNode->borderColor),
sizeof(
struct SFColorRGBA));
1596 anisotropicDegree = tpNode->anisotropicDegree;
1597 if ((anisotropicDegree < 1.0) || (anisotropicDegree>rdr_caps->anisotropicDegree)) {
1601 anisotropicDegree = rdr_caps->anisotropicDegree;
1604 borderWidth = tpNode->borderWidth;
1605 if (borderWidth < 0) borderWidth=0;
1606 if (borderWidth>1) borderWidth = 1;
1610 switch (findFieldInTEXTUREMAGNIFICATIONKEYWORDS(tpNode->magnificationFilter->strptr)) {
1611 case TMAG_AVG_PIXEL:
1612 magFilter = GL_LINEAR;
break;
1613 case TMAG_DEFAULT: magFilter = GL_LINEAR;
break;
1614 case TMAG_FASTEST: magFilter = GL_LINEAR;
break;
1615 case TMAG_NEAREST_PIXEL: magFilter = GL_NEAREST;
break;
1616 case TMAG_NICEST: magFilter = GL_NEAREST;
break;
1617 default: magFilter = GL_NEAREST; ConsoleMessage (
"unknown magnification filter %s",
1618 tpNode->magnificationFilter->strptr);
1622 if (generateMipMaps)
switch (findFieldInTEXTUREMINIFICATIONKEYWORDS(tpNode->minificationFilter->strptr)) {
1623 case TMIN_AVG_PIXEL: minFilter = GL_NEAREST;
break;
1624 case TMIN_AVG_PIXEL_AVG_MIPMAP: minFilter = GL_NEAREST_MIPMAP_NEAREST;
break;
1625 case TMIN_AVG_PIXEL_NEAREST_MIPMAP: minFilter = GL_NEAREST_MIPMAP_NEAREST;
break;
1626 case TMIN_DEFAULT: minFilter = GL_NEAREST_MIPMAP_LINEAR;
break;
1627 case TMIN_FASTEST: minFilter = GL_NEAREST_MIPMAP_LINEAR;
break;
1628 case TMIN_NICEST: minFilter = GL_NEAREST_MIPMAP_NEAREST;
break;
1629 case TMIN_NEAREST_PIXEL: minFilter = GL_NEAREST;
break;
1630 case TMIN_NEAREST_PIXEL_NEAREST_MIPMAP: minFilter = GL_NEAREST_MIPMAP_LINEAR;
break;
1631 default: minFilter = GL_NEAREST_MIPMAP_NEAREST;
1632 ConsoleMessage (
"unknown minificationFilter of %s",
1633 tpNode->minificationFilter->strptr);
1635 else switch (findFieldInTEXTUREMINIFICATIONKEYWORDS(tpNode->minificationFilter->strptr)) {
1636 case TMIN_AVG_PIXEL:
1637 case TMIN_AVG_PIXEL_AVG_MIPMAP:
1638 case TMIN_AVG_PIXEL_NEAREST_MIPMAP:
1642 case TMIN_NEAREST_PIXEL: minFilter = GL_NEAREST;
break;
1643 case TMIN_NEAREST_PIXEL_NEAREST_MIPMAP: minFilter = GL_LINEAR;
break;
1644 default: minFilter = GL_NEAREST;
1645 ConsoleMessage (
"unknown minificationFilter of %s",
1646 tpNode->minificationFilter->strptr);
1649 switch (findFieldInTEXTURECOMPRESSIONKEYWORDS(tpNode->textureCompression->strptr)) {
1650 case TC_DEFAULT: compression = GL_FASTEST;
break;
1651 case TC_FASTEST: compression = GL_NONE;
break;
1652 case TC_HIGH: compression = GL_FASTEST;
break;
1653 case TC_LOW: compression = GL_NONE;
break;
1654 case TC_MEDIUM: compression = GL_NICEST;
break;
1655 case TC_NICEST: compression = GL_NICEST;
break;
1657 default: compression = GL_NEAREST_MIPMAP_NEAREST;
1658 ConsoleMessage (
"unknown textureCompression of %s",
1659 tpNode->textureCompression->strptr);
1670 if (!haveValidTexturePropertiesNode) {
1673 Src = Src ? GL_REPEAT : GL_CLAMP_TO_EDGE;
1674 Trc = Trc ? GL_REPEAT : GL_CLAMP_TO_EDGE;
1675 Rrc = Rrc ? GL_REPEAT : GL_CLAMP_TO_EDGE;
1677 generateMipMaps = GL_TRUE;
1679 if(me->x > 0 && me->y > 0)
1693 if(me->x < me->y*me->z) ratio = (float)(me->y*me->z) / (float)me->x;
1694 else ratio = (float)me->x / (
float)(me->y*me->z);
1695 if(ratio > 2.0f) generateMipMaps = GL_FALSE;
1699 if ((me->x<=256) || ((me->y*me->z)<=256)) {
1700 minFilter = GL_NEAREST_MIPMAP_NEAREST;
1701 if(!generateMipMaps) minFilter = GL_NEAREST;
1702 magFilter = GL_NEAREST;
1704 minFilter = GL_LINEAR_MIPMAP_NEAREST;
1705 if(!generateMipMaps) minFilter = GL_LINEAR;
1706 magFilter = GL_LINEAR;
1718 if (getAppearanceProperties()->cubeFace != 0) {
1720 unsigned char *dest = me->texdata;
1729 iformat = GL_RGBA; format = GL_RGBA;
1734 if (getAppearanceProperties()->cubeFace == GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT) {
1735 FW_GL_TEXPARAMETERI(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1736 FW_GL_TEXPARAMETERI(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1737 FW_GL_TEXPARAMETERI(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1738 FW_GL_TEXPARAMETERI(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1739 FW_GL_TEXPARAMETERI(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
1750 int cy, cyy, icsize;
1751 sp = (uint32 *) me->texdata;
1752 for (cy=0; cy<ry/2; cy++) {
1754 for(cx=0;cx<rx;cx+=512){
1755 icsize = min(512,rx-cx-1)*4;
1756 memcpy(tp,&sp[cy*rx + cx],icsize);
1757 memcpy(&sp[cy*rx + cx],&sp[cyy*rx + cx],icsize);
1758 memcpy(&sp[cyy*rx + cx],tp,icsize);
1763 generateMipMaps = 0;
1764 myTexImage2D(generateMipMaps, getAppearanceProperties()->cubeFace, 0, iformat, rx, ry, 0, format, GL_UNSIGNED_BYTE, dest);
1767 if (getAppearanceProperties()->cubeFace == GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
1768 glEnable(GL_TEXTURE_CUBE_MAP);
1769 glEnable(GL_TEXTURE_GEN_S);
1770 glEnable(GL_TEXTURE_GEN_T);
1771 glEnable(GL_TEXTURE_GEN_R);
1776 if (me->nodeType == NODE_ImageCubeMapTexture) {
1793 unpackImageCubeMap(me);
1794 me->status = TEX_LOADED;
1795 }
else if(me->z == 6){
1798 unpackImageCubeMap6(me);
1799 me->status = TEX_LOADED;
1802 }
else if(me->nodeType == NODE_GeneratedCubeMapTexture){
1804 me->status = TEX_LOADED;
1808 mytexdata = me->texdata;
1809 if (mytexdata == NULL) {
1810 printf (
"mytexdata is null, texture failed, put something here\n");
1813 glBindTexture (GL_TEXTURE_2D, me->OpenGLTexture);
1834 FW_GL_TEXPARAMETERI( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, Src);
1835 FW_GL_TEXPARAMETERI( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, Trc);
1836#if !defined(GL_ES_VERSION_2_0)
1837 FW_GL_TEXPARAMETERI( GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, Rrc);
1838 FW_GL_TEXPARAMETERF(GL_TEXTURE_2D,GL_TEXTURE_PRIORITY, texPri);
1839 FW_GL_TEXPARAMETERFV(GL_TEXTURE_2D,GL_TEXTURE_BORDER_COLOR,(GLfloat *)&borderColour);
1842#ifdef GL_TEXTURE_MAX_ANISOTROPY_EXT
1843 FW_GL_TEXPARAMETERF(GL_TEXTURE_2D,GL_TEXTURE_MAX_ANISOTROPY_EXT,anisotropicDegree);
1846 if (compression != GL_NONE) {
1847 FW_GL_TEXPARAMETERI(GL_TEXTURE_2D, GL_TEXTURE_INTERNAL_FORMAT, GL_COMPRESSED_RGBA);
1848 glHint(GL_TEXTURE_COMPRESSION_HINT, compression);
1850 npot = rdr_caps->av_npot_texture;
1855 FW_GL_TEXPARAMETERI( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
1856 FW_GL_TEXPARAMETERI( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
1857 me->magFilter = magFilter == GL_LINEAR ? 1 : 0;
1863 iformat = GL_RGBA; format = GL_RGBA;
1868 unsigned char *dest = mytexdata;
1872 rx = x; ry = y; rz = z;
1875 static int round_down = 1;
1876 static int trunc_down = 0;
1879 while(sx) {sx /= 2; rx *= 2;}
1880 if(rx/2 == x) {rx /= 2;}
1883 if(x < rx && (
float)(x - rx/2)/(
float)(rx - rx/2) < .25) rx = rx/2;
1886 if(x < rx) rx = rx/2;
1889 while(sy) {sy /= 2; ry *= 2;}
1890 if(ry/2 == y) {ry /= 2;}
1892 if(y < ry && (
float)(y - ry/2)/(
float)(ry - ry/2) < .25) ry = ry/2;
1894 if(y < ry) ry = ry/2;
1898 while(sz) {sz /= 2; rz *= 2;}
1899 if(rz/2 == z) {rz /= 2;}
1901 if(z < rz && (
float)(z - rz/2)/(
float)(rz - rz/2) < .25) rz = rz/2;
1903 if(z < rz) rz = rz/2;
1906 if (gglobal()->internalc.global_print_opengl_errors) {
1907 DEBUG_MSG(
"initial texture scale to %d %d\n",rx,ry);
1913 int emulating3D_TILED;
1914 emulating3D_TILED = TRUE;
1915 generateMipMaps = FALSE;
1916 if(emulating3D_TILED){
1937 int rc,sc,c,cube_root, max_size;
1938 unsigned char *texdataTiles = NULL;
1940 int nx, ny, ix, iy, nxx, nyy;
1943 max_size = rdr_caps->runtime_max_texture_size;
1946 if(x * y * z > 256 * 256 * 256)
1947 max_size = min(max_size,4096);
1949 cube_root = (int)pow( max_size * max_size + 3, 1.0/3.0);
1954 while(sc) {sc /= 2; rc *= 2;}
1955 if(rc > c) {rc /= 2;}
1958 if(rx != x || ry != y || rz != z || rx > cube_root || ry > cube_root || rz > cube_root) {
1965 if (rx > cube_root) rx = cube_root;
1966 if (ry > cube_root) ry = cube_root;
1967 if (rz > cube_root) rz = cube_root;
1970 if (gglobal()->internalc.global_print_opengl_errors) {
1971 DEBUG_MSG(
"texture size after maxTextureSize taken into account: %d %d, from %d %d\n",rx,ry,x,y);
1979 if(x > rx || y > ry || z > rz){
1984 dest = MALLOC(
unsigned char *, 4 * rx * ry * rz);
1985 myScaleImage3D(x,y,z,mx,my,mz,mytexdata,dest);
1989 FREE_IF_NZ(me->texdata);
1994 if(me->channels == 1){
1996 compute_3D_alpha_gradient_store_rgb((
char *)dest,x,y,z);
1999 ny = (int) sqrt(z+1);
2001 nx = z - nx*ny > 0 ? nx+1 : nx;
2011 texdataTiles = MALLOC(
unsigned char *,nxx * nyy * 4);
2012 p2 = (uint32 *)texdataTiles;
2013 p1 = (uint32 *)dest;
2014 for(iz=0;iz<z;iz++){
2021 ifrom = (iz*y + j)*x + k;
2022 ito = (iy*y + j)*nxx + (ix*x) + k;
2036 tti3->texdata = texdataTiles;
2037 saveImage_web3dit(tti3,
"test_tiled_texture.web3dit");
2040 myTexImage2D(generateMipMaps, GL_TEXTURE_2D, 0, iformat, nxx, nyy, 0, format, GL_UNSIGNED_BYTE, texdataTiles);
2041 ConsoleMessage(
"final texture2D size %d %d\n",nxx,nyy);
2042 FREE_IF_NZ(texdataTiles);
2043 if(dest != me->texdata) FREE_IF_NZ(dest);
2049 if(rx != x || ry != y || rx > rdr_caps->runtime_max_texture_size || ry > rdr_caps->runtime_max_texture_size) {
2056 rx = min(rx,rdr_caps->runtime_max_texture_size);
2057 ry = min(ry,rdr_caps->runtime_max_texture_size);
2060 if (gglobal()->internalc.global_print_opengl_errors) {
2061 DEBUG_MSG(
"texture size after maxTextureSize taken into account: %d %d, from %d %d\n",rx,ry,x,y);
2073 if(generateMipMaps){
2079 if ((x==rx) && (y==ry)) {
2085 size_t total_size = (size_t)4L * rx * ry;
2086 dest = MALLOC(
unsigned char *, total_size);
2088 myScaleImage(x,y,rx,ry,mytexdata,dest);
2090 if(rx > 8192 || ry > 8192) ConsoleMessage(
"texture size rx %d ry %d\n",rx,ry);
2091 myTexImage2D(generateMipMaps, GL_TEXTURE_2D, 0, iformat, rx, ry, 0, format, GL_UNSIGNED_BYTE, dest);
2093 if(mytexdata != dest) {
2099 FREE_IF_NZ (me->texdata);
2108 me->status = TEX_LOADED;
2111 pthread_mutex_unlock( &gl_mutex1 );
2136 int thisTextureType;
2153 thisTextureType = node->_nodeType;
2154 if (thisTextureType==NODE_ImageTexture){
2157 thisTexture = it->__textureTableIndex;
2158 }
else if (thisTextureType==NODE_PixelTexture){
2160 thisTexture = pt->__textureTableIndex;
2161 }
else if (thisTextureType==NODE_MovieTexture){
2163 thisTexture = mt->__textureTableIndex;
2165 }
else if (thisTextureType==NODE_ImageCubeMapTexture){
2167 thisTexture = ict->__textureTableIndex;
2169 }
else if (thisTextureType==NODE_GeneratedCubeMapTexture){
2171 thisTexture = gct->__textureTableIndex;
2172 }
else if (thisTextureType==NODE_PixelTexture3D){
2175 thisTexture = pt3d->__textureTableIndex;
2176 }
else if (thisTextureType==NODE_ImageTexture3D){
2179 thisTexture = pt3d->__textureTableIndex;
2181 }
else if (thisTextureType==NODE_ComposedTexture3D){
2184 thisTexture = pt3d->__textureTableIndex;
2186 ConsoleMessage (
"Invalid type for texture, %s\n",stringNodeType(thisTextureType));
2190 myTableIndex = getTableIndex(thisTexture);
2191 if (myTableIndex->status != TEX_LOADED) {
2192 DEBUG_TEX(
"new_bind_image, I am %p, textureStackTop %d, thisTexture is %d myTableIndex %p status %s\n",
2193 node,tg->RenderFuncs.textureStackTop,thisTexture,myTableIndex, texst(myTableIndex->status));
2199 tg->RenderFuncs.boundTextureStack[tg->RenderFuncs.textureStackTop] = tg->Textures.defaultBlankTexture;
2200 switch (myTableIndex->status) {
2202 DEBUG_TEX(
"feeding texture %p to texture thread...\n", myTableIndex);
2203 if(mfurl && mfurl->n == 0) {
2205 myTableIndex->status = TEX_NEEDSBINDING;
2207 myTableIndex->status = TEX_LOADING;
2208 send_texture_to_loader(myTableIndex);
2214 DEBUG_TEX(
"I've to wait for %p...\n", myTableIndex);
2217 case TEX_NEEDSBINDING:
2218 DEBUG_TEX(
"texture loaded into memory... now lets load it into OpenGL...\n");
2219 move_texture_to_opengl(myTableIndex);
2224 if (myTableIndex->status != TEX_LOADED) {
2225 printf (
"issue going from TEX_NEEDSBINDING to TEX_LOADED, is %s\n",
2226 texst(myTableIndex->status));
2237 if (myTableIndex->hasAlpha) tg->RenderFuncs.last_texture_type = TEXTURE_ALPHA;
2238 else tg->RenderFuncs.last_texture_type = TEXTURE_NO_ALPHA;
2242 if (myTableIndex->OpenGLTexture == TEXTURE_INVALID) {
2244 DEBUG_TEX(
"no openGLtexture here status %s\n", texst(myTableIndex->status));
2248 tg->RenderFuncs.boundTextureStack[tg->RenderFuncs.textureStackTop] = myTableIndex->OpenGLTexture;
2255 if (param != NULL) {
2257 memcpy(&(textureParameterStack[tg->RenderFuncs.textureStackTop]), param,sizeof (
struct multiTexParams));
2260 p->textureInProcess = -1;
2263 case TEX_UNSQUASHED:
2265 printf (
"unknown texture status %d\n",myTableIndex->status);
2271int get_bound_image(
struct X3D_Node *node) {
2273 int thisTextureType;
2290 thisTextureType = node->_nodeType;
2291 if (thisTextureType==NODE_ImageTexture){
2294 thisTexture = it->__textureTableIndex;
2295 }
else if (thisTextureType==NODE_PixelTexture){
2297 thisTexture = pt->__textureTableIndex;
2298 }
else if (thisTextureType==NODE_MovieTexture){
2300 thisTexture = mt->__textureTableIndex;
2302 }
else if (thisTextureType==NODE_ImageCubeMapTexture){
2304 thisTexture = ict->__textureTableIndex;
2306 }
else if (thisTextureType==NODE_GeneratedCubeMapTexture){
2308 thisTexture = gct->__textureTableIndex;
2309 }
else if (thisTextureType==NODE_PixelTexture3D){
2312 thisTexture = pt3d->__textureTableIndex;
2313 }
else if (thisTextureType==NODE_ImageTexture3D){
2316 thisTexture = pt3d->__textureTableIndex;
2318 }
else if (thisTextureType==NODE_ComposedTexture3D){
2321 thisTexture = pt3d->__textureTableIndex;
2323 ConsoleMessage (
"Invalid type for texture, %s\n",stringNodeType(thisTextureType));
2327 myTableIndex = getTableIndex(thisTexture);
2328 return myTableIndex->OpenGLTexture;