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"
51int multitex_source[2];
59void *RenderTextures_constructor(){
64void RenderTextures_init(
struct tRenderTextures *t){
65 t->prv = RenderTextures_constructor();
69 t->textureParameterStack = (
void *)p->textureParameterStack;
80static 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(
"");
145void 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);
166void 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);
237int isMultiTexture(
struct X3D_Node *node){
239 if(node && node->_nodeType == NODE_MultiTexture)
249OLDCODE
int i, isStrict, isMulti, isIdentity;
250OLDCODE GLint texUnit[MAX_MULTITEXTURE];
251OLDCODE GLint texMode[MAX_MULTITEXTURE];
259OLDCODE tnode = tg->RenderFuncs.texturenode;
261OLDCODE me = getAppearanceProperties()->currentShaderProperties;
263OLDCODE #ifdef TEXVERBOSE
264OLDCODE printf (
"passedInGenTex, using passed in genTex, textureStackTop %d\n",tg->RenderFuncs.textureStackTop);
265OLDCODE printf (
"passedInGenTex, cubeFace %d\n",getAppearanceProperties()->cubeFace);
268OLDCODE FW_GL_MATRIX_MODE(GL_TEXTURE);
275OLDCODE genTexPtr = genTex;
276OLDCODE isIdentity = TRUE;
277OLDCODE
for (c=0; c<tg->RenderFuncs.textureStackTop; c++) {
278OLDCODE FW_GL_PUSH_MATRIX();
279OLDCODE FW_GL_LOAD_IDENTITY();
282OLDCODE
if (tg->RenderFuncs.boundTextureStack[c]!=0) {
283OLDCODE isMulti = isMultiTexture(tg->RenderFuncs.texturenode);
285OLDCODE
if (setActiveTexture(c,getAppearanceProperties()->transparency,texUnit,texMode)) {
287OLDCODE GLuint texture;
288OLDCODE
struct X3D_Node *tt = getThis_textureTransform();
289OLDCODE
if (tt!=NULL) {
290OLDCODE
int match = FALSE;
291OLDCODE match = isMulti && (tt->_nodeType == NODE_MultiTextureTransform);
292OLDCODE match = match || (!isMulti && (tt->_nodeType != NODE_MultiTextureTransform));
295OLDCODE do_textureTransform(tt,c);
296OLDCODE isIdentity = FALSE;
299OLDCODE do_textureTransform(tt,c);
300OLDCODE isIdentity = FALSE;
304OLDCODE
if(isTex3D(tnode)){
306OLDCODE
if(tnode->_nodeType != NODE_ComposedTexture3D){
310OLDCODE glUniform1iv(me->tex3dTiles,3,tti->tiles);
315OLDCODE
if(tg->RenderFuncs.shapenode && isIdentity && genTexPtr->TC_size < 3){
320OLDCODE
float bbox[6], *bmin, *bmax;
323OLDCODE POSSIBLE_PROTO_EXPANSION(
struct X3D_Node *,sn->geometry,gn);
327OLDCODE bmax = &bbox[3];
328OLDCODE
for(i=0;i<3;i++){
329OLDCODE bmin[i] = gn->_extent[i*2 + 1];
330OLDCODE bmax[i] = gn->_extent[i*2];
333OLDCODE vecdif3f(bmax,bmax,bmin);
334OLDCODE
for(i=0;i<3;i++){
335OLDCODE
if(bmax[i] != 0.0f)
336OLDCODE bmax[i] = 1.0f/bmax[i];
338OLDCODE bmax[i] = 1.0f;
351OLDCODE FW_GL_SCALE_F(bmax[0],bmax[1],bmax[2]);
352OLDCODE FW_GL_TRANSLATE_F(-bmin[0],-bmin[1],-bmin[2]);
356OLDCODE
if(tg->RenderFuncs.shapenode && genTexPtr->TC_size < 3){
358OLDCODE glUniform1i(me->tex3dUseVertex,1);
360OLDCODE glUniform1i(me->tex3dUseVertex,0);
363OLDCODE
if(me->repeatSTR > -1)
364OLDCODE glUniform1iv(me->repeatSTR,3,tti->repeatSTR);
365OLDCODE
if(me->magFilter > -1)
366OLDCODE glUniform1i(me->magFilter,tti->magFilter);
371OLDCODE texture = tg->RenderFuncs.boundTextureStack[c];
374OLDCODE glActiveTexture(GL_TEXTURE0+c);
376OLDCODE
if (getAppearanceProperties()->cubeFace==0) {
377OLDCODE glBindTexture(GL_TEXTURE_2D,texture);
379OLDCODE glBindTexture(GL_TEXTURE_CUBE_MAP,texture);
381OLDCODE
if(genTexPtr->VBO)
382OLDCODE FW_GL_BINDBUFFER(GL_ARRAY_BUFFER,genTexPtr->VBO);
384OLDCODE
if (genTexPtr->pre_canned_textureCoords != NULL) {
386OLDCODE FW_GL_TEXCOORD_POINTER (2,GL_FLOAT,0,genTexPtr->pre_canned_textureCoords,c);
388OLDCODE FW_GL_TEXCOORD_POINTER (genTexPtr->TC_size,
389OLDCODE genTexPtr->TC_type,
390OLDCODE genTexPtr->TC_stride,
391OLDCODE genTexPtr->TC_pointer,c);
395OLDCODE genTexPtr = genTexPtr->next ? genTexPtr->next : genTexPtr;
398OLDCODE
if (me != NULL) {
399OLDCODE tnode = tg->RenderFuncs.texturenode;
402OLDCODE
if (me->textureCount != -1) {
403OLDCODE glUniform1i(me->textureCount, tg->RenderFuncs.textureStackTop);
405OLDCODE
if(tg->RenderFuncs.textureStackTop){
406OLDCODE
if(isMultiTexture(tg->RenderFuncs.texturenode)){
408OLDCODE glUniform4f(me->multitextureColor,mtnode->color.c[0],mtnode->color.c[1],mtnode->color.c[2],mtnode->alpha);
411OLDCODE
for (i=0; i<tg->RenderFuncs.textureStackTop; i++) {
420OLDCODE glUniform1i(me->TextureUnit[i],i);
422OLDCODE glUniform2i(me->TextureMode[i],p->textureParameterStack[i].multitex_mode[0], p->textureParameterStack[i].multitex_mode[1]);
423OLDCODE glUniform2i(me->TextureSource[i],p->textureParameterStack[i].multitex_source[0], p->textureParameterStack[i].multitex_source[1]);
426OLDCODE glUniform1i(me->TextureFunction[i],p->textureParameterStack[i].multitex_function);
428OLDCODE #ifdef TEXVERBOSE
430OLDCODE printf (
" NOT sending in %d i+tu+mode because currentShaderProperties is NULL\n",tg->RenderFuncs.textureStackTop);
434OLDCODE FW_GL_MATRIX_MODE(GL_MODELVIEW);
436OLDCODE 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;
477void 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);
646 printf (
" NOT sending in %d i+tu+mode because currentShaderProperties is NULL\n",tg->RenderFuncs.textureStackTop);
650 FW_GL_MATRIX_MODE(GL_MODELVIEW);
652 PRINT_GL_ERROR_IF_ANY(
"");