34#include <libFreeWRL.h>
35#include "../vrml_parser/Structs.h"
36#include "../main/headers.h"
37#include "../opengl/OpenGL_Utils.h"
38#include "../opengl/Textures.h"
39#include "../scenegraph/Component_Shape.h"
40#include "../scenegraph/LinearAlgebra.h"
41#include "../scenegraph/Component_CubeMapTexturing.h"
42#include "../input/EAIHelpers.h"
43#include "../vrml_parser/CParseGeneral.h"
46# define uint32 uint32_t
272int generate_color_cubemap_gl_texture(
int size) {
275 glGenTextures(1, &tex);
276 glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
277 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
278 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
279 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
280 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
281 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
284 for (
size_t i = 0; i < 6; ++i) {
285 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA, size, size, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
288 glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
300static int cubetextureID = 0;
302void texture_flipy(
int width,
int height,
int bytesperpixel,
unsigned char* data){
304 int ipixi, ipixo, ibytei, ibyteo;
305 unsigned char* row = malloc(width * bytesperpixel);
306 for (
int y = 0; y < height/2; y++) {
307 int y2 = height - 1 - y;
309 ibyteo = ipixo * bytesperpixel;
311 ibytei = ipixi * bytesperpixel;
312 memcpy(row, &data[ibyteo], width * bytesperpixel);
313 memcpy(&data[ibyteo], &data[ibytei], width * bytesperpixel);
314 memcpy(&data[ibytei], row, width * bytesperpixel);
323 textureTableIndexStruct_s* tti;
324 tti = getTableTableFromTextureNode(X3D_NODE(node));
325 if (tti && tti->status != TEX_LOADED)
327 if (tti->status == TEX_NOTLOADED) {
328 tti->OpenGLTexture = generate_color_cubemap_gl_texture(0);
329 glBindTexture(GL_TEXTURE_CUBE_MAP, tti->OpenGLTexture);
330 tti->status = TEX_LOADING;
332 if (tti->status == TEX_LOADING) {
333 int loaded_faces = 0;
334 textureTableIndexStruct_s* ttiface;
336 for (
int iface = 0; iface < 6; iface++) {
342 case 0: {POSSIBLE_PROTO_EXPANSION(
struct X3D_Node*, node->right, texface);
break; }
343 case 1: {POSSIBLE_PROTO_EXPANSION(
struct X3D_Node*, node->left, texface);
break; }
345 case 2: {POSSIBLE_PROTO_EXPANSION(
struct X3D_Node*, node->top, texface);
break; }
346 case 3: {POSSIBLE_PROTO_EXPANSION(
struct X3D_Node*, node->bottom, texface);
break; }
348 case 4: {POSSIBLE_PROTO_EXPANSION(
struct X3D_Node*, node->front, texface);
break; }
349 case 5: {POSSIBLE_PROTO_EXPANSION(
struct X3D_Node*, node->back, texface);
break; }
352 if (texface != NULL) {
355 if ((texface->_nodeType == NODE_ImageTexture) ||
356 (texface->_nodeType == NODE_PixelTexture) ||
357 (texface->_nodeType == NODE_MovieTexture) ||
358 (texface->_nodeType == NODE_MultiTexture)) {
360 ttiface = getTableTableFromTextureNode(X3D_NODE(texface));
361 if (ttiface->status == TEX_LOADED) {
362 glBindTexture(GL_TEXTURE_2D, ttiface->OpenGLTexture);
363 unsigned char* texdata = malloc(ttiface->x * ttiface->y * 4);
365 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, texdata);
366 texture_flipy(ttiface->x, ttiface->y, 4, texdata);
367 glBindTexture(GL_TEXTURE_2D, 0);
368 glBindTexture(GL_TEXTURE_CUBE_MAP, tti->OpenGLTexture);
369 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + iface, 0, GL_RGBA, ttiface->x, ttiface->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texdata);
370 glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
375 gglobal()->RenderFuncs.textureStackTop = 0;
377 render_node(X3D_NODE(texface));
378 gglobal()->RenderFuncs.textureStackTop = 0;
383 if (loaded_faces > 0 && loaded_faces < 6)
384 tti->status = TEX_LOADING;
385 else if (loaded_faces == 6) {
386 tti->status = TEX_LOADED;
397 for (
int iface = 0; iface < 6; iface++) {
398 textureTableIndexStruct_s* ttiface = getTableTableFromTextureNode(p[iface]);
402 channels = max(channels, ttiface->channels);
403 imgalpha = max(ttiface->hasAlpha, imgalpha);
405 textureTableIndexStruct_s* tti = getTableTableFromTextureNode(X3D_NODE(node));
406 tti->channels = channels;
407 tti->hasAlpha = imgalpha;
411 if (tti && tti->status >= TEX_LOADING) {
412 gglobal()->RenderFuncs.textureStackTop = 1;
413 gglobal()->RenderFuncs.texturenode = node;
416 gglobal()->RenderFuncs.textureStackTop = 0;
435#if !defined( mydds_h )
438#define DDS_MAGIC 0x20534444
442 #define DDSD_CAPS 0x00000001
446 #define DDSD_PIXELFORMAT 0x00001000
449 #define DDSD_DEPTH 0x00800000
452 #define DDPF_ALPHAPIXELS 0x00000001
453 #define DDPF_FOURCC 0x00000004
454 #define DDPF_INDEXED 0x00000020
455 #define DDPF_RGB 0x00000040
458 #define DDSCAPS_COMPLEX 0x00000008
463 #define DDSCAPS2_CUBEMAP 0x00000200
464 #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
465 #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
466 #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
467 #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
468 #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
469 #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
470 #define DDSCAPS2_VOLUME 0x00200000
480 #define D3DFMT_DXT1 0x31545844
482 #define D3DFMT_DXT3 0x33545844
484 #define D3DFMT_DXT5 0x35545844
487#define PF_IS_DXT1(pf) \
488 ((pf.dwFlags & DDPF_FOURCC) && \
489 (pf.dwFourCC == (unsigned int) D3DFMT_DXT1))
491#define PF_IS_DXT3(pf) \
492 ((pf.dwFlags & DDPF_FOURCC) && \
493 (pf.dwFourCC == (unsigned int) D3DFMT_DXT3))
495#define PF_IS_DXT5(pf) \
496 ((pf.dwFlags & DDPF_FOURCC) && \
497 (pf.dwFourCC == (unsigned int) D3DFMT_DXT5))
499#define PF_IS_BGRA8(pf) \
500 ((pf.dwFlags & DDPF_RGB) && \
501 (pf.dwFlags & DDPF_ALPHAPIXELS) && \
502 (pf.dwRGBBitCount == 32) && \
503 (pf.dwRBitMask == 0xff0000) && \
504 (pf.dwGBitMask == 0xff00) && \
505 (pf.dwBBitMask == 0xff) && \
506 (pf.dwAlphaBitMask == 0xff000000U))
508#define PF_IS_RGB8(pf) \
509 ((pf.dwFlags & DDPF_RGB) && \
510 !(pf.dwFlags & DDPF_ALPHAPIXELS) && \
511 (pf.dwRGBBitCount == 24) && \
512 (pf.dwRBitMask == 0xff) && \
513 (pf.dwGBitMask == 0xff00) && \
514 (pf.dwBBitMask == 0xff0000))
516#define PF_IS_BGR8(pf) \
517 ((pf.dwFlags & DDPF_RGB) && \
518 !(pf.dwFlags & DDPF_ALPHAPIXELS) && \
519 (pf.dwRGBBitCount == 24) && \
520 (pf.dwRBitMask == 0xff0000) && \
521 (pf.dwGBitMask == 0xff00) && \
522 (pf.dwBBitMask == 0xff))
524#define PF_IS_BGR5A1(pf) \
525 ((pf.dwFlags & DDPF_RGB) && \
526 (pf.dwFlags & DDPF_ALPHAPIXELS) && \
527 (pf.dwRGBBitCount == 16) && \
528 (pf.dwRBitMask == 0x00007c00) && \
529 (pf.dwGBitMask == 0x000003e0) && \
530 (pf.dwBBitMask == 0x0000001f) && \
531 (pf.dwAlphaBitMask == 0x00008000))
533#define PF_IS_BGR565(pf) \
534 ((pf.dwFlags & DDPF_RGB) && \
535 !(pf.dwFlags & DDPF_ALPHAPIXELS) && \
536 (pf.dwRGBBitCount == 16) && \
537 (pf.dwRBitMask == 0x0000f800) && \
538 (pf.dwGBitMask == 0x000007e0) && \
539 (pf.dwBBitMask == 0x0000001f))
541#define PF_IS_INDEX8(pf) \
542 ((pf.dwFlags & DDPF_INDEXED) && \
543 (pf.dwRGBBitCount == 8))
545#define PF_IS_VOLUME(pf) \
546 ((pf.dwFlags & DDSD_DEPTH))
555 unsigned int dwMagic;
557 unsigned int dwFlags;
558 unsigned int dwHeight;
559 unsigned int dwWidth;
560 unsigned int dwPitchOrLinearSize;
561 unsigned int dwDepth;
562 unsigned int dwMipMapCount;
563 unsigned int dwReserved1[ 11 ];
568 unsigned int dwFlags;
569 unsigned int dwFourCC;
570 unsigned int dwRGBBitCount;
571 unsigned int dwRBitMask;
572 unsigned int dwGBitMask;
573 unsigned int dwBBitMask;
574 unsigned int dwAlphaBitMask;
579 unsigned int dwCaps1;
580 unsigned int dwCaps2;
582 unsigned int dwReserved;
584 unsigned int dwReserved2;
596 unsigned int divSize;
597 unsigned int blockBytes;
598 GLenum internalFormat;
599 GLenum externalFormat;
604 true,
false,
false, 4, 8, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
607 true,
false,
false, 4, 16, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
610 true,
false,
false, 4, 16, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
616 false,
false,
false, 1, 4, GL_RGBA8, GL_BGRA, GL_UNSIGNED_BYTE
619 false,
true,
false, 1, 2, GL_RGB5_A1, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV
622 false,
false,
true, 1, 1, GL_RGB8, GL_BGRA, GL_UNSIGNED_BYTE
627 false,
false,
false, 1, 3, GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE
630 false,
false,
false, 1, 3, GL_RGB8, GL_BGR, GL_UNSIGNED_BYTE
633 false,
true,
false, 1, 2, GL_RGB5, GL_RGB, GL_UNSIGNED_SHORT_5_6_5
637int textureIsDDS(textureTableIndexStruct_s* this_tex,
char *filename) {
639 unsigned char *buffer, *bdata;
641 unsigned long fileLen;
647 int nchan, idoFrontBackSwap;
649 unsigned int xSize, ySize,zSize;
662 file = fopen(filename,
"rb");
667 xx=fread(sniffbuf, 4, 1, file);
669 if(strncmp(sniffbuf,
"DDS ",4)){
675 file = fopen(filename,
"rb");
680 fseek(file, 0, SEEK_END);
682 fseek(file, 0, SEEK_SET);
685 buffer=MALLOC(
unsigned char *, fileLen+1);
692 xx=fread(buffer, fileLen, 1, file);
696 if (fileLen <
sizeof(hdr))
700 memcpy( &hdr, buffer,
sizeof(hdr));
703 if ((hdr.dwMagic == DDS_MAGIC) && (hdr.dwSize == 124) &&
704 (hdr.dwFlags & DDSD_PIXELFORMAT) && (hdr.dwFlags & DDSD_CAPS)) {
710 ySize = hdr.dwHeight;
720 printf (
"looking to see what it is...\n");
721 printf (
"DDPF_FOURCC dwFlags %x mask %x, final %x\n",hdr.sPixelFormat.dwFlags,DDPF_FOURCC,hdr.sPixelFormat.dwFlags & DDPF_FOURCC);
723 printf (
"if it is a dwFourCC, %x and %x\n", hdr.sPixelFormat.dwFourCC ,D3DFMT_DXT1);
725 printf (
"dwFlags %x\n",hdr.sPixelFormat.dwFlags);
726 printf (
"dwRGBBitCount %d\n",hdr.sPixelFormat.dwRGBBitCount);
727 printf (
"dwRBitMask %x\n",hdr.sPixelFormat.dwRBitMask);
728 printf (
"dwGBitMask %x\n",hdr.sPixelFormat.dwGBitMask);
729 printf (
"dwBBitMask %x\n",hdr.sPixelFormat.dwBBitMask);
730 printf (
"dwAlphaBitMask %x\n",hdr.sPixelFormat.dwAlphaBitMask);
731 printf (
"dwFlags and DDPF_ALPHAPIXELS... %x\n",DDPF_ALPHAPIXELS & hdr.sPixelFormat.dwFlags);
732 printf (
"dwflags & DDPF_RGB %x\n,",hdr.sPixelFormat.dwFlags & DDPF_RGB);
734 printf (
"dwFlags and DEPTH %x\n",hdr.dwFlags & DDSD_DEPTH);
735 printf (
"dwCaps1 and complex %x\n", (hdr.sCaps.dwCaps1 & DDSCAPS_COMPLEX));
736 printf (
"dwCaps1 and VOLUME %x\n", (hdr.sCaps.dwCaps1 & DDSCAPS2_VOLUME));
743 if(hdr.sPixelFormat.dwFlags & DDPF_FOURCC){
744 if( PF_IS_DXT1( hdr.sPixelFormat ) ) {
747 else if( PF_IS_DXT3( hdr.sPixelFormat ) ) {
750 else if( PF_IS_DXT5( hdr.sPixelFormat ) ) {
754 #if defined (GL_BGRA)
755 else if( PF_IS_BGRA8( hdr.sPixelFormat ) ) {
758 else if( PF_IS_BGR5A1( hdr.sPixelFormat ) ) {
759 li = &loadInfoBGR5A1;
761 else if( PF_IS_INDEX8( hdr.sPixelFormat ) ) {
762 li = &loadInfoIndex8;
766 else if( PF_IS_RGB8( hdr.sPixelFormat ) ) {
769 else if( PF_IS_BGR8( hdr.sPixelFormat ) ) {
772 else if( PF_IS_BGR565( hdr.sPixelFormat ) ) {
773 li = &loadInfoBGR565;
786 x = xSize = hdr.dwWidth;
787 y = ySize = hdr.dwHeight;
789 idoFrontBackSwap = 0;
790 if( PF_IS_VOLUME(hdr) )
791 z = zSize = hdr.dwDepth;
792 if( hdr.sCaps.dwCaps2 & DDSCAPS2_CUBEMAP){
794 if(hdr.sCaps.dwCaps2 & DDSCAPS2_CUBEMAP_POSITIVEX) facecount++;
795 if(hdr.sCaps.dwCaps2 & DDSCAPS2_CUBEMAP_NEGATIVEX) facecount++;
796 if(hdr.sCaps.dwCaps2 & DDSCAPS2_CUBEMAP_POSITIVEY) facecount++;
797 if(hdr.sCaps.dwCaps2 & DDSCAPS2_CUBEMAP_NEGATIVEY) facecount++;
798 if(hdr.sCaps.dwCaps2 & DDSCAPS2_CUBEMAP_POSITIVEZ) facecount++;
799 if(hdr.sCaps.dwCaps2 & DDSCAPS2_CUBEMAP_NEGATIVEZ) facecount++;
800 z = zSize = facecount;
802 idoFrontBackSwap = 1;
805 if(DDPF_ALPHAPIXELS & hdr.sPixelFormat.dwFlags) nchan = 4;
811 int ipix,jpix,bpp, ir, ig, ib;
813 unsigned char * rgbablob = malloc(x*y*z *4);
814 bpp = hdr.sPixelFormat.dwRGBBitCount / 8;
815 ir = 0; ig = 1; ib = 2;
816 if(hdr.sPixelFormat.dwRBitMask > hdr.sPixelFormat.dwBBitMask){
827 unsigned char *pixel,*
rgba;
829 ii = idoFrontBackSwap && i == 4? 5 : i;
830 ii = idoFrontBackSwap && i == 5? 4 : i;
832 ipix = (i*y +j)*x +k;
833 jpix = (ii*y +(y-1-j))*x + k;
834 pixel = &bdata[ipix * bpp];
835 rgba = &rgbablob[jpix *4];
855 this_tex->channels = nchan;
859 this_tex->texdata = rgbablob;
868 if( li->compressed ) {
889 }
else if( li->palette ) {
986 void add_node_to_broto_context(
struct X3D_Proto *currentContext,
struct X3D_Node *node);
994static int offsets[]={
1015void unpackImageCubeMap (textureTableIndexStruct_s* me) {
1022 ERROR_MSG(
"problem unpacking single image ImageCubeMap\n");
1026 if (node->_nodeType != NODE_ImageCubeMapTexture) {
1027 ERROR_MSG(
"internal error - expected ImageCubeMapTexture here");
1033 if ((me->x * 3) != (me->y*4)) {
1034 ERROR_MSG (
"expect an ImageCubeMap to be in a 4:3 ratio");
1042 if (node->__subTextures.n != 6) {
1043 ERROR_MSG(
"unpackImageCubeMap, there should be 6 PixelTexture nodes here\n");
1048 for (count=0; count <6; count++) {
1051 uint32 *tex = (uint32 *) me->texdata;
1052 struct X3D_PixelTexture *pt = X3D_PIXELTEXTURE(node->__subTextures.p[count]);
1053 int xSubIndex, ySubIndex;
1056 ySubIndex=offsets[count*2]*size; xSubIndex=offsets[count*2+1]*size;
1059 FREE_IF_NZ(pt->image.arr.p);
1060 pt->image.arr.n = size*size;
1061 pt->image.arr.p = MALLOC(
int *, pt->image.arr.n * sizeof (
int));
1062 pt->image.whc[0] = size;
1063 pt->image.whc[1] = size;
1064 pt->image.whc[2] = 4;
1067 for (y=ySubIndex; y<ySubIndex+size; y++) {
1068 for (x=xSubIndex; x<xSubIndex+size; x++) {
1070 unsigned char *
rgba;
1075 pt->image.arr.p[index] = ((val & 0xffffff) << 8) | ((val & 0xff000000) >> 24);
1077 rgba = (
unsigned char *)&tex[ipix];
1079 pt->image.arr.p[index] = (
rgba[0] << 24) + (
rgba[1] << 16) + (
rgba[2] << 8) + (
rgba[3] << 0);
1089 node->__regenSubTextures = FALSE;
1092 FREE_IF_NZ(me->texdata);
1096void unpackImageCubeMap6 (textureTableIndexStruct_s* me) {
1105 ERROR_MSG(
"problem unpacking single image ImageCubeMap\n");
1109 if (node->_nodeType != NODE_ImageCubeMapTexture) {
1110 ERROR_MSG(
"internal error - expected ImageCubeMapTexture here");
1115 if (node->__subTextures.n != 6) {
1116 ERROR_MSG(
"unpackImageCubeMap, there should be 6 PixelTexture nodes here\n");
1124 uint32 imlookup[] = {0,1,2,3,4,5};
1125 for (count=0; count <6; count++) {
1129 struct X3D_PixelTexture *pt = X3D_PIXELTEXTURE(node->__subTextures.p[count]);
1132 FREE_IF_NZ(pt->image.arr.p);
1133 pt->image.arr.n = me->x*me->y;
1134 pt->image.arr.p = MALLOC(
int *, pt->image.arr.n * sizeof (uint32));
1135 pt->image.whc[0] = me->x;
1136 pt->image.whc[1] = me->y;
1137 pt->image.whc[2] = 4;
1138 ioff = imlookup[count] * me->x * me->y;
1143 tex = (uint32 *) me->texdata;
1145 for(j=0;j<me->y;j++){
1146 for(i=0;i<me->x;i++){
1149 unsigned char*
rgba;
1153 rgba = (
unsigned char*)&tex[ipix];
1155 pt->image.arr.p[ipix] = pixint;
1162 node->__regenSubTextures = FALSE;
1165 FREE_IF_NZ(me->texdata);
1178int unpackImageCubeMapB(textureTableIndexStruct_s* tti,
int pattern,
unsigned char** facetextures) {
1182 if (pattern == ICM_DDS) {
1184 int oo0[] = { 0,0,0,0, 1,0,0,0, 2,0,0,0, 3,0,0,0, 4,0,0,0, 5,0,0,0 };
1186 }
else if (pattern == ICM_T) {
1199 }
else if (pattern == ICM_3X4) {
1221 int oo1[] = { 5,0,0,0, 4,0,0,0, 3,0,0,0, 2,0,0,0, 1,0,0,0, 0,0,0,0, };
1226 int oo2[] = { 0,0,0,0, 0,1,0,0, 0,2,0,0, 0,3,0,0, 0,4,0,0, 0,5,0,0, };
1231 int oo3[] = { 2,0,0,0, 2,1,0,0, 1,0,0,0, 1,1,0,0, 0,0,0,0, 0,1,0,0, };
1236 int oo4[] = { 0,0,0,0, 0,1,0,0, 0,2,0,0, 1,0,0,0, 1,2,0,0, 1,2,0,0, };
1239 default: nx = 0; ny = 0; isize = 0;
1246 for (
int iface = 0; iface < 6; iface++) {
1247 int x, y, xx,ix, yy, iy, index;
1248 unsigned char* tex = tti->texdata;
1249 unsigned char rgba[4];
1251 int xSubIndex, ySubIndex, iflipx, inoflipy;
1255 if (tti->hasAlpha && nci == 3) nci = 4;
1256 ySubIndex = offs[iface * 4] * isize; xSubIndex = offs[iface * 4 + 1] * isize;
1257 iflipx = offs[iface * 4 + 2] * isize; inoflipy = offs[iface * 4 + 3] * isize;
1260 FREE_IF_NZ(facetextures[iface]);
1261 facetextures[iface] = malloc(isize * isize * nco);
1263 for (yy = ySubIndex, iy=0; yy < ySubIndex+isize; yy++,iy++) {
1266 y = (ySubIndex + isize - 1) - iy;
1267 if (inoflipy) y = yy;
1268 for (xx = xSubIndex,ix=0; xx < xSubIndex + isize; xx++,ix++) {
1272 x = (xSubIndex + isize - 1) - ix;
1273 ipix = y * tti->x + x;
1275 memset(
rgba, 255, 4);
1276 memcpy(
rgba, &tex[ibyte], nci);
1277 memcpy(&facetextures[iface][index],
rgba,nco);
1287int unpack_image_2D_into_cube_faces(textureTableIndexStruct_s* tti,
unsigned char** facetextures) {
1303 int pattern = tti->x * 3 == tti->y * 4 ? ICM_T : tti->x * 4 == tti->y * 3 ? ICM_3X4 : tti->x * 3 == tti->y * 2 ? ICM_2X3 : tti->x * 2 == tti->y * 3 ? ICM_3X2 : tti->x * 6 == tti->y ? ICM_1X6 : tti->x == tti->y * 6 ? ICM_6X1 : ICM_UNK;
1304 isize = unpackImageCubeMapB(tti,pattern,facetextures);
1306 else if (tti->z == 6) {
1309 isize = unpackImageCubeMapB(tti,ICM_DDS,facetextures);
1319void set_debug_quad(
int which_debug_shader,
int textureID);
1325 int refresh = FALSE;
1329 if (node->autoRefresh > 0.0) {
1330 double dtime = TickTime();
1331 double elapsedTime = dtime - node->__lasttime;
1332 double runtime = dtime - BrowserStartTime();
1333 if (elapsedTime > node->autoRefresh && runtime < node->autoRefreshTimeLimit ) {
1334 node->__lasttime = dtime;
1338 textureTableIndexStruct_s * tti;
1339 tti = getTableTableFromTextureNode(X3D_NODE(node));
1340 if (refresh) tti->status = TEX_NOTLOADED;
1341 if (tti && tti->status != TEX_LOADED)
1345 if (node->__subTextures.n == 0) {
1346 tti->OpenGLTexture = generate_color_cubemap_gl_texture(0);
1347 node->__subTextures.p = malloc(
sizeof(
struct X3D_Node*));
1348 node->__subTextures.n = 1;
1350 tex2d->url.p = malloc(
sizeof(
struct Uni_String*) * node->url.n);
1351 for (
int i = 0; i < node->url.n; i++) {
1352 tex2d->url.p[i] = newASCIIString(node->url.p[i]->strptr);
1354 tex2d->url.n = node->url.n;
1355 tex2d->_parentResource = node->_parentResource;
1357 if (node->_executionContext)
1358 add_node_to_broto_context(X3D_PROTO(node->_executionContext), X3D_NODE(tex2d));
1360 textureTableIndexStruct_s* tti2d = getTableTableFromTextureNode(X3D_NODE(tex2d));
1361 tti2d->scenegraphNode = X3D_NODE(tex2d);
1362 tti2d->no_gl = TRUE;
1363 node->__subTextures.p[0] = X3D_NODE(tex2d);
1364 node->_ichange = node->_change;
1365 render_node(X3D_NODE(tex2d));
1370 textureTableIndexStruct_s* tti2d = getTableTableFromTextureNode(X3D_NODE(tex2d));
1371 if (refresh) tti2d->status = TEX_NOTLOADED;
1372 if (tti2d->status < TEX_NEEDSBINDING) {
1373 render_node(X3D_NODE(tex2d));
1383 unsigned char* facetextures[6];
1384 memset(facetextures, 0, 6 *
sizeof(
unsigned char*));
1387 int isize = unpack_image_2D_into_cube_faces(tti2d,facetextures);
1388 glBindTexture(GL_TEXTURE_CUBE_MAP, tti->OpenGLTexture);
1389 for (
int iface = 0; iface < 6; iface++)
1390 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + iface, 0, GL_RGBA, isize, isize, 0, GL_RGBA, GL_UNSIGNED_BYTE, facetextures[iface]);
1391 tti->status = TEX_LOADED;
1392 for (
int iface = 0; iface < 6; iface++)
1393 FREE_IF_NZ(facetextures[iface]);
1394 tti->channels = tti2d->channels;
1395 tti->hasAlpha = tti2d->hasAlpha;
1396 glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
1402 if (tti && tti->status == TEX_LOADED) {
1403 gglobal()->RenderFuncs.textureStackTop = 1;
1404 gglobal()->RenderFuncs.texturenode = node;
1407 gglobal()->RenderFuncs.textureStackTop = 0;
1410 set_debug_quad(5, tti->OpenGLTexture);
1421 #include "RenderFuncs.h"
1423 Stack * gencube_stack;
1424}* ppComponent_CubeMapTexturing;
1426static void *Component_CubeMapTexturing_constructor(){
1433void Component_CubeMapTexturing_init(
struct tComponent_CubeMapTexturing *t){
1436 t->prv = Component_CubeMapTexturing_constructor();
1438 ppComponent_CubeMapTexturing p = (ppComponent_CubeMapTexturing)t->prv;
1439 p->gencube_stack = newStack(
usehit);
1444void Component_CubeMapTexturing_clear(
struct tComponent_CubeMapTexturing *t){
1448 ppComponent_CubeMapTexturing p = (ppComponent_CubeMapTexturing)t->prv;
1449 deleteVector(
usehit,p->gencube_stack);
1458void pushnset_framebuffer(
int ibuffer);
1459void popnset_framebuffer();
1461#ifdef GL_DEPTH_COMPONENT32
1462#define FW_GL_DEPTH_COMPONENT GL_DEPTH_COMPONENT32
1464#define FW_GL_DEPTH_COMPONENT GL_DEPTH_COMPONENT16
1466int haveFrameBufferObject();
1467void printFramebufferStatusIfNotComplete(
int status) {
1470 if (status != GL_FRAMEBUFFER_COMPLETE) {
1471 printf(
"make_depth_buffer_cube: framebuffer not complete\n");
1473 case GL_FRAMEBUFFER_UNDEFINED:
1474 printf(
"GL_FRAMEBUFFER_UNDEFINED\n");
break;
1475 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
1476 printf(
"GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT\n");
break;
1477 case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
1478 printf(
"GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT\n");
break;
1479 case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
1480 printf(
"GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER\n");
break;
1481 case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
1482 printf(
"GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER\n");
break;
1483 case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
1484 printf(
"GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE\n");
break;
1485 case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS:
1486 printf(
"GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS\n");
break;
1487 case GL_FRAMEBUFFER_UNSUPPORTED:
1488 printf(
"GL_FRAMEBUFFER_UNSUPPORTED\n");
break;
1490 printf(
"unknown GL error %u\n", (
unsigned int)status);
break;
1499 tti = getTableIndex(node->__textureTableIndex);
1500 if (tti->OpenGLTexture == 0) {
1501 tti->x = tti->y = node->size;
1502 tti->status = TEX_LOADED;
1503 glGenTextures(1, &tti->OpenGLTexture);
1504 glBindTexture(GL_TEXTURE_CUBE_MAP, tti->OpenGLTexture);
1505 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1506 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1507 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1508 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1509 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
1511 tti->hasAlpha = TRUE;
1513 for (
size_t i = 0; i < 6; ++i) {
1514 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA, tti->x, tti->y, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
1516 glGenFramebuffers(1, &tti->ifbobuffer);
1517 pushnset_framebuffer(tti->ifbobuffer);
1518 PRINT_GL_ERROR_IF_ANY(
"make_depth_buffer_cube 1");
1521 glViewport(0, 0, tti->x, tti->y);
1524 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + 0, tti->OpenGLTexture, 0);
1525 int status = glCheckNamedFramebufferStatus(tti->ifbobuffer, GL_FRAMEBUFFER);
1526 printFramebufferStatusIfNotComplete(status);
1527 popnset_framebuffer();
1528 glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
1539void get_view_matrix(
double *savePosOri,
double *saveView);
1548 if(!strcmp(node->update->strptr,
"ALWAYS") || !strcmp(node->update->strptr,
"NEXT_FRAME_ONLY")){
1551 if(rs->render_geom && !rs->render_cube){
1556 ppComponent_CubeMapTexturing p = (ppComponent_CubeMapTexturing)gglobal()->Component_CubeMapTexturing.prv;
1559 for(i=0;i<vectorSize(p->gencube_stack);i++){
1560 uhit = vector_get(
usehit,p->gencube_stack,i);
1561 if(uhit.node == X3D_NODE(node)){
1567 double modelviewMatrix[16], mvmInverse[16];
1568 double worldmatrix[16], viewmatrix[16], saveView[16], savePosOri[16];
1571 FW_GL_GETDOUBLEV(GL_MODELVIEW_MATRIX, modelviewMatrix);
1572 get_view_matrix(savePosOri,saveView);
1573 matmultiplyAFFINE(viewmatrix,saveView,savePosOri);
1575 matinverseAFFINE(mvmInverse,modelviewMatrix);
1580 matmultiplyAFFINE(worldmatrix,viewmatrix,mvmInverse);
1583 uhit.node = X3D_NODE(node);
1585 memcpy(uhit.mvm,worldmatrix,16*
sizeof(
double));
1586 vector_pushBack(
usehit,p->gencube_stack,uhit);
1587 if(!strcmp(node->update->strptr,
"NEXT_FRAME_ONLY")){
1589 freeASCIIString(node->update);
1590 node->update = newASCIIString(
"NONE");
1600 gglobal()->RenderFuncs.textureStackTop = 1;
1601 gglobal()->RenderFuncs.texturenode = node;
1627void fw_gluPerspective_2(GLDOUBLE xcenter, GLDOUBLE fovy, GLDOUBLE aspect, GLDOUBLE zNear, GLDOUBLE zFar);
1628void pushnset_viewport(
float *vpFraction);
1629void popnset_viewport();
1630void render_bound_background();
1631void lightTable_clear();
1634#include "../x3d_parser/Bindable.h"
1636void generate_GeneratedCubeMapTextures(){
1644 double savebackmat[16];
1645 Stack *gencube_stack;
1646 ttglobal tg = gglobal();
1647 ppComponent_CubeMapTexturing p = (ppComponent_CubeMapTexturing)tg->Component_CubeMapTexturing.prv;
1648 static int iframe = 0;
1650 bstack = getActiveBindableStacks(tg);
1655 memcpy(savebackmat,bstack->backgroundmatrix,16*
sizeof(
double));
1657 gencube_stack = p->gencube_stack;
1658 if(vectorSize(gencube_stack)){
1661 n = vectorSize(gencube_stack);
1665 double modelviewmatrix[16];
1666 textureTableIndexStruct_s* tti;
1667 float vp[4] = {0.0f,1.0f,0.0f,1.0f};
1670 uhit = vector_get(
usehit,gencube_stack,i);
1672 memcpy(modelviewmatrix,uhit.mvm,16*
sizeof(
double));
1675 tti = getTableIndex(node->__textureTableIndex);
1678 pushnset_framebuffer(tti->ifbobuffer);
1679 pushnset_viewport(vp);
1680 glViewport(0,0,isize,isize);
1681 glEnable(GL_TEXTURE_GEN_S);
1682 glEnable(GL_TEXTURE_GEN_T);
1683 glEnable(GL_TEXTURE_GEN_R);
1688 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + j, tti->OpenGLTexture, 0);
1692 glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
1693 FW_GL_CLEAR(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1697 FW_GL_MATRIX_MODE(GL_PROJECTION);
1698 FW_GL_LOAD_IDENTITY();
1700 fw_gluPerspective_2(0.0,90.0, 1.0, .1,10000.0);
1702 FW_GL_MATRIX_MODE(GL_MODELVIEW);
1703 FW_GL_LOAD_IDENTITY();
1704 fw_glSetDoublev(GL_MODELVIEW_MATRIX, modelviewmatrix);
1705 fw_glRotated(sideangle[j].angle,sideangle[j].x,sideangle[j].y,sideangle[j].z);
1706 fw_glScaled(1.0, -1.0, 1.0);
1707 fw_glGetDoublev(GL_MODELVIEW_MATRIX, bstack->viewmatrix);
1711 render_bound_background();
1714 PRINT_GL_ERROR_IF_ANY(
"XEvents::render, before render_hier");
1716 render_hier(rootNode(), VF_globalLight );
1717 PRINT_GL_ERROR_IF_ANY(
"XEvents::render, render_hier(VF_globalLight)");
1718 render_hier(rootNode(), VF_Other );
1721 profile_start(
"hier_geom");
1722 render_hier(rootNode(), VF_Geom | VF_Cube);
1723 profile_end(
"hier_geom");
1724 PRINT_GL_ERROR_IF_ANY(
"XEvents::render, render_hier(VF_Geom)");
1727 if (tg->RenderFuncs.have_transparency) {
1729 render_hier(rootNode(), VF_Geom | VF_Blend | VF_Cube);
1730 PRINT_GL_ERROR_IF_ANY(
"XEvents::render, render_hier(VF_Geom)");
1735 int status = glCheckNamedFramebufferStatus(tti->ifbobuffer, GL_FRAMEBUFFER);
1736 printFramebufferStatusIfNotComplete(status);
1737 popnset_framebuffer();
1739 set_debug_quad(5, tti->OpenGLTexture);
1744 gencube_stack->n = 0;
1745 memcpy(bstack->backgroundmatrix,savebackmat,16*
sizeof(
double));