36 #include <libFreeWRL.h>
38 #include "../vrml_parser/Structs.h"
39 #include "../main/headers.h"
40 #include "../opengl/OpenGL_Utils.h"
41 #include "../scenegraph/Component_Shape.h"
42 #include "../scenegraph/RenderFuncs.h"
43 #include "../scenegraph/LinearAlgebra.h"
51 int multitex_source[2];
52 int multitex_function;
59 void *RenderTextures_constructor(){
64 void RenderTextures_init(
struct tRenderTextures *t){
65 t->prv = RenderTextures_constructor();
69 t->textureParameterStack = (
void *)p->textureParameterStack;
80 static int setActiveTexture (
int c, GLfloat thisTransparency, GLint *texUnit, GLint *texMode)
95 if (getAppearanceProperties()->currentShaderProperties != NULL) {
96 printf (
"setActiveTexture %d, boundTextureStack is %d, sending to uniform %d\n",c,
97 tg->RenderFuncs.boundTextureStack[c],
98 getAppearanceProperties()->currentShaderProperties->TextureUnit[c]);
100 printf (
"setActiveTexture %d, boundTextureStack is %d, sending to uniform [NULL--No Shader]\n",c,
101 tg->RenderFuncs.boundTextureStack[c]);
109 if (p->textureParameterStack[c].multitex_mode[0] == INT_ID_UNDEFINED) {
112 printf (
"setActiveTexture - simple texture NOT a MultiTexture \n");
120 printf (
"setActiveTexture - firsttexture \n");
122 texMode[c]= GL_MODULATE;
129 if (p->textureParameterStack[c].multitex_source[0] != MTMODE_OFF) {
138 PRINT_GL_ERROR_IF_ANY(
"");
145 void textureTransform_end(
void) {
150 printf (
"start of textureDraw_end\n");
156 FW_GL_MATRIX_MODE(GL_TEXTURE);
157 for(j=0;j<tg->RenderFuncs.textureStackTop;j++)
160 tg->RenderFuncs.textureStackTop = 0;
161 tg->RenderFuncs.texturenode = NULL;
162 FW_GL_MATRIX_MODE(GL_MODELVIEW);
166 void do_textureTransform (
struct X3D_Node *textureNode,
int ttnum) {
169 if (textureNode->_nodeType == NODE_TextureTransform) {
175 FW_GL_TRANSLATE_F(-((ttt->center).c[0]),-((ttt->center).c[1]), 0);
176 FW_GL_SCALE_F(((ttt->scale).c[0]),((ttt->scale).c[1]),1);
177 FW_GL_ROTATE_RADIANS(ttt->rotation,0,0,1);
178 FW_GL_TRANSLATE_F(((ttt->center).c[0]),((ttt->center).c[1]), 0);
179 FW_GL_TRANSLATE_F(((ttt->translation).c[0]), ((ttt->translation).c[1]), 0);
181 }
else if (textureNode->_nodeType == NODE_MultiTextureTransform) {
183 if (ttnum < mtt->textureTransform.n) {
186 if (ttt->_nodeType == NODE_TextureTransform) {
188 FW_GL_TRANSLATE_F(-((ttt->center).c[0]),-((ttt->center).c[1]), 0);
189 FW_GL_SCALE_F(((ttt->scale).c[0]),((ttt->scale).c[1]),1);
190 FW_GL_ROTATE_RADIANS(ttt->rotation,0,0,1);
191 FW_GL_TRANSLATE_F(((ttt->center).c[0]),((ttt->center).c[1]), 0);
192 FW_GL_TRANSLATE_F(((ttt->translation).c[0]), ((ttt->translation).c[1]), 0);
196 printf (
"MultiTextureTransform expected a textureTransform for texture %d, got %d \n",
197 ttnum, ttt->_nodeType);
204 printf (
"not enough transforms in MultiTextureTransform -will fill with Identity matrix\n");
208 }
else if (textureNode->_nodeType == NODE_TextureTransform3D) {
212 FW_GL_TRANSLATE_F(-((ttt->center).c[0]),-((ttt->center).c[1]), -((ttt->center).c[2]));
213 FW_GL_SCALE_F(((ttt->scale).c[0]),((ttt->scale).c[1]),((ttt->scale).c[2]));
214 FW_GL_ROTATE_RADIANS(ttt->rotation.c[3], ttt->rotation.c[0],ttt->rotation.c[1],ttt->rotation.c[2]);
215 FW_GL_TRANSLATE_F(((ttt->center).c[0]),((ttt->center).c[1]), ((ttt->center).c[2]));
216 FW_GL_TRANSLATE_F(((ttt->translation).c[0]), ((ttt->translation).c[1]), ((ttt->translation).c[2]));
217 }
else if (textureNode->_nodeType == NODE_TextureTransformMatrix3D) {
223 mat[i] = (
double)ttt->matrix.c[i];
224 FW_GL_SETDOUBLEV(GL_TEXTURE_MATRIX,mat);
228 printf (
"expected a textureTransform node, got %d\n",textureNode->_nodeType);
237 int isMultiTexture(
struct X3D_Node *node){
239 if(node && node->_nodeType == NODE_MultiTexture)
249 OLDCODE
int i, isStrict, isMulti, isIdentity;
250 OLDCODE GLint texUnit[MAX_MULTITEXTURE];
251 OLDCODE GLint texMode[MAX_MULTITEXTURE];
259 OLDCODE tnode = tg->RenderFuncs.texturenode;
261 OLDCODE me = getAppearanceProperties()->currentShaderProperties;
263 OLDCODE #ifdef TEXVERBOSE
264 OLDCODE printf (
"passedInGenTex, using passed in genTex, textureStackTop %d\n",tg->RenderFuncs.textureStackTop);
265 OLDCODE printf (
"passedInGenTex, cubeFace %d\n",getAppearanceProperties()->cubeFace);
268 OLDCODE FW_GL_MATRIX_MODE(GL_TEXTURE);
271 OLDCODE isStrict = 1;
275 OLDCODE genTexPtr = genTex;
276 OLDCODE isIdentity = TRUE;
277 OLDCODE
for (c=0; c<tg->RenderFuncs.textureStackTop; c++) {
278 OLDCODE FW_GL_PUSH_MATRIX();
279 OLDCODE FW_GL_LOAD_IDENTITY();
282 OLDCODE
if (tg->RenderFuncs.boundTextureStack[c]!=0) {
283 OLDCODE isMulti = isMultiTexture(tg->RenderFuncs.texturenode);
285 OLDCODE
if (setActiveTexture(c,getAppearanceProperties()->transparency,texUnit,texMode)) {
287 OLDCODE GLuint texture;
288 OLDCODE
struct X3D_Node *tt = getThis_textureTransform();
289 OLDCODE
if (tt!=NULL) {
290 OLDCODE
int match = FALSE;
291 OLDCODE match = isMulti && (tt->_nodeType == NODE_MultiTextureTransform);
292 OLDCODE match = match || (!isMulti && (tt->_nodeType != NODE_MultiTextureTransform));
293 OLDCODE
if(isStrict){
295 OLDCODE do_textureTransform(tt,c);
296 OLDCODE isIdentity = FALSE;
299 OLDCODE do_textureTransform(tt,c);
300 OLDCODE isIdentity = FALSE;
304 OLDCODE
if(isTex3D(tnode)){
306 OLDCODE
if(tnode->_nodeType != NODE_ComposedTexture3D){
310 OLDCODE glUniform1iv(me->tex3dTiles,3,tti->tiles);
315 OLDCODE
if(tg->RenderFuncs.shapenode && isIdentity && genTexPtr->TC_size < 3){
320 OLDCODE
float bbox[6], *bmin, *bmax;
323 OLDCODE POSSIBLE_PROTO_EXPANSION(
struct X3D_Node *,sn->geometry,gn);
327 OLDCODE bmax = &bbox[3];
328 OLDCODE
for(i=0;i<3;i++){
329 OLDCODE bmin[i] = gn->_extent[i*2 + 1];
330 OLDCODE bmax[i] = gn->_extent[i*2];
333 OLDCODE vecdif3f(bmax,bmax,bmin);
334 OLDCODE
for(i=0;i<3;i++){
335 OLDCODE
if(bmax[i] != 0.0f)
336 OLDCODE bmax[i] = 1.0f/bmax[i];
338 OLDCODE bmax[i] = 1.0f;
351 OLDCODE FW_GL_SCALE_F(bmax[0],bmax[1],bmax[2]);
352 OLDCODE FW_GL_TRANSLATE_F(-bmin[0],-bmin[1],-bmin[2]);
356 OLDCODE
if(tg->RenderFuncs.shapenode && genTexPtr->TC_size < 3){
358 OLDCODE glUniform1i(me->tex3dUseVertex,1);
360 OLDCODE glUniform1i(me->tex3dUseVertex,0);
363 OLDCODE
if(me->repeatSTR > -1)
364 OLDCODE glUniform1iv(me->repeatSTR,3,tti->repeatSTR);
365 OLDCODE
if(me->magFilter > -1)
366 OLDCODE glUniform1i(me->magFilter,tti->magFilter);
371 OLDCODE texture = tg->RenderFuncs.boundTextureStack[c];
374 OLDCODE glActiveTexture(GL_TEXTURE0+c);
376 OLDCODE
if (getAppearanceProperties()->cubeFace==0) {
377 OLDCODE glBindTexture(GL_TEXTURE_2D,texture);
379 OLDCODE glBindTexture(GL_TEXTURE_CUBE_MAP,texture);
381 OLDCODE
if(genTexPtr->VBO)
382 OLDCODE FW_GL_BINDBUFFER(GL_ARRAY_BUFFER,genTexPtr->VBO);
384 OLDCODE
if (genTexPtr->pre_canned_textureCoords != NULL) {
386 OLDCODE FW_GL_TEXCOORD_POINTER (2,GL_FLOAT,0,genTexPtr->pre_canned_textureCoords,c);
388 OLDCODE FW_GL_TEXCOORD_POINTER (genTexPtr->TC_size,
389 OLDCODE genTexPtr->TC_type,
390 OLDCODE genTexPtr->TC_stride,
391 OLDCODE genTexPtr->TC_pointer,c);
395 OLDCODE genTexPtr = genTexPtr->next ? genTexPtr->next : genTexPtr;
398 OLDCODE
if (me != NULL) {
399 OLDCODE tnode = tg->RenderFuncs.texturenode;
402 OLDCODE
if (me->textureCount != -1) {
403 OLDCODE glUniform1i(me->textureCount, tg->RenderFuncs.textureStackTop);
405 OLDCODE
if(tg->RenderFuncs.textureStackTop){
406 OLDCODE
if(isMultiTexture(tg->RenderFuncs.texturenode)){
408 OLDCODE glUniform4f(me->multitextureColor,mtnode->color.c[0],mtnode->color.c[1],mtnode->color.c[2],mtnode->alpha);
411 OLDCODE
for (i=0; i<tg->RenderFuncs.textureStackTop; i++) {
420 OLDCODE glUniform1i(me->TextureUnit[i],i);
422 OLDCODE glUniform2i(me->TextureMode[i],p->textureParameterStack[i].multitex_mode[0], p->textureParameterStack[i].multitex_mode[1]);
423 OLDCODE glUniform2i(me->TextureSource[i],p->textureParameterStack[i].multitex_source[0], p->textureParameterStack[i].multitex_source[1]);
426 OLDCODE glUniform1i(me->TextureFunction[i],p->textureParameterStack[i].multitex_function);
428 OLDCODE #ifdef TEXVERBOSE
430 OLDCODE printf (
" NOT sending in %d i+tu+mode because currentShaderProperties is NULL\n",tg->RenderFuncs.textureStackTop);
434 OLDCODE FW_GL_MATRIX_MODE(GL_MODELVIEW);
436 OLDCODE PRINT_GL_ERROR_IF_ANY(
"");
459 for (c=0; c<tg->RenderFuncs.textureStackTop; c++) {
461 FW_GL_BINDBUFFER(GL_ARRAY_BUFFER,genTexPtr->VBO);
463 if (genTexPtr->pre_canned_textureCoords != NULL) {
465 FW_GL_TEXCOORD_POINTER (2,GL_FLOAT,0,genTexPtr->pre_canned_textureCoords,c);
467 FW_GL_TEXCOORD_POINTER (genTexPtr->TC_size,
469 genTexPtr->TC_stride,
470 genTexPtr->TC_pointer,c);
472 genTexPtr = genTexPtr->next ? genTexPtr->next : genTexPtr;
477 void textureTransform_start() {
479 int i, isStrict, isMulti, isIdentity;
480 GLint texUnit[MAX_MULTITEXTURE];
481 GLint texMode[MAX_MULTITEXTURE];
488 tnode = tg->RenderFuncs.texturenode;
490 me = getAppearanceProperties()->currentShaderProperties;
493 printf (
"passedInGenTex, using passed in genTex, textureStackTop %d\n",tg->RenderFuncs.textureStackTop);
494 printf (
"passedInGenTex, cubeFace %d\n",getAppearanceProperties()->cubeFace);
497 FW_GL_MATRIX_MODE(GL_TEXTURE);
505 for (c=0; c<tg->RenderFuncs.textureStackTop; c++) {
507 FW_GL_LOAD_IDENTITY();
510 if (tg->RenderFuncs.boundTextureStack[c]!=0) {
511 isMulti = isMultiTexture(tg->RenderFuncs.texturenode);
513 if (setActiveTexture(c,getAppearanceProperties()->transparency,texUnit,texMode)) {
516 struct X3D_Node *tt = getThis_textureTransform();
519 match = isMulti && (tt->_nodeType == NODE_MultiTextureTransform);
520 match = match || (!isMulti && (tt->_nodeType != NODE_MultiTextureTransform));
523 do_textureTransform(tt,c);
527 do_textureTransform(tt,c);
534 if(tnode->_nodeType != NODE_ComposedTexture3D){
538 glUniform1iv(me->tex3dTiles,3,tti->tiles);
543 if(tg->RenderFuncs.shapenode && isIdentity ) {
548 float bbox[6], *bmin, *bmax;
551 POSSIBLE_PROTO_EXPANSION(
struct X3D_Node *,sn->geometry,gn);
557 bmin[i] = gn->_extent[i*2 + 1];
558 bmax[i] = gn->_extent[i*2];
561 vecdif3f(bmax,bmax,bmin);
564 bmax[i] = 1.0f/bmax[i];
579 FW_GL_SCALE_F(bmax[0],bmax[1],bmax[2]);
580 FW_GL_TRANSLATE_F(-bmin[0],-bmin[1],-bmin[2]);
584 if(tg->RenderFuncs.shapenode ) {
586 glUniform1i(me->tex3dUseVertex,1);
588 glUniform1i(me->tex3dUseVertex,0);
591 if(me->repeatSTR > -1)
592 glUniform1iv(me->repeatSTR,3,tti->repeatSTR);
593 if(me->magFilter > -1)
594 glUniform1i(me->magFilter,tti->magFilter);
599 texture = tg->RenderFuncs.boundTextureStack[c];
602 glActiveTexture(GL_TEXTURE0+c);
604 if (getAppearanceProperties()->cubeFace==0) {
605 glBindTexture(GL_TEXTURE_2D,texture);
607 glBindTexture(GL_TEXTURE_CUBE_MAP,texture);
614 tnode = tg->RenderFuncs.texturenode;
617 if (me->textureCount != -1) {
618 glUniform1i(me->textureCount, tg->RenderFuncs.textureStackTop);
620 if(tg->RenderFuncs.textureStackTop){
621 if(isMultiTexture(tg->RenderFuncs.texturenode)){
623 glUniform4f(me->multitextureColor,mtnode->color.c[0],mtnode->color.c[1],mtnode->color.c[2],mtnode->alpha);
626 for (i=0; i<tg->RenderFuncs.textureStackTop; i++) {
635 glUniform1i(me->TextureUnit[i],i);
637 glUniform2i(me->TextureMode[i],p->textureParameterStack[i].multitex_mode[0], p->textureParameterStack[i].multitex_mode[1]);
638 glUniform2i(me->TextureSource[i],p->textureParameterStack[i].multitex_source[0], p->textureParameterStack[i].multitex_source[1]);
641 glUniform1i(me->TextureFunction[i],p->textureParameterStack[i].multitex_function);
645 printf (
" NOT sending in %d i+tu+mode because currentShaderProperties is NULL\n",tg->RenderFuncs.textureStackTop);
649 FW_GL_MATRIX_MODE(GL_MODELVIEW);
651 PRINT_GL_ERROR_IF_ANY(
"");