PDA

View Full Version : Multi texture



isato
05-18-2010, 06:30 AM
I have created a scene graph and are now trying to implement multi texturing. It works in some way when but the problem is when I use multi texture and regular texture in at different nodes the multi texture works correctly but single texture only gets the background color of the texture I am using. Both single and multi works fine if its the only node using texturing in the scene. Any ideas where to look?


This is the way I am binding and unbinding the textures when using multi texture.


19 void Texture::multiBind(int index){
20 if(!index){
21 glActiveTextureARB(GL_TEXTURE0_ARB);
22 } else if (index){
23 glActiveTextureARB(GL_TEXTURE1_ARB);
24 }
25 else {
26 cout <<"Index of texture has to be 0 or 1" <<endl;
27 }
28 glEnable(GL_TEXTURE_2D);
29 glBindTexture(GL_TEXTURE_2D, id);
30 }
31
32 void Texture::multiUnbind(int index){
33 if(!index){
34 glActiveTextureARB(GL_TEXTURE0_ARB);
35 } else if(index) {
36 glActiveTextureARB(GL_TEXTURE1_ARB);
37 }else {
38 cout << "Could not unbind texture " <<endl;
39 }
40 glBindTexture(GL_TEXTURE_2D,0); //Tried both with and without this one

41 glDisable(GL_TEXTURE_2D);
42
43 }



This is the way I am binding and unbinding the textures when using single texture.

18 void bind(){glBindTexture(GL_TEXTURE_2D, id);}
19 void unBind(){glBindTexture(GL_TEXTURE_2D,0);}



Here is what I do before rendering.


//Bind texture
67 Texture *tex = NULL;
68 vector<Texture*> textures;
69 if(accState.textured.isTexture()){
70 tex = geo->getTexture();
71 if(tex != NULL){
72 tex->bind();
73 }
74 } else if(accState.textured.isMulti() ){
75 textures = geo->getTextures();
76 if(textures.size() < 2){
77 cout << "Using only one texture in multi texture mode" <<endl;
78 }
79 for(unsigned int i = 0; i < textures.size() ; i++ ){
80 if(textures[i] != NULL){
81 textures[i]->multiBind(i);
82 }
83 }
84 }
85
86 if(accState.polygoneMode.isWire()) {
87 drawWire(faceToDraw);
88 } else if(accState.polygoneMode.isFilled()){
89 drawFilled(faceToDraw);
90 } else if (accState.polygoneMode.isPoint()){
91 drawPoints(faceToDraw);
92 }
93
94 if(accState.textured.isTexture()){
95 if(tex != NULL){
96 tex->unBind();
97 }
98 }else if(accState.textured.isMulti()){
99 for(unsigned int i = 0; i < textures.size() ;i++){
100 textures[i]->multiUnbind(i);
101 }
102 }


Here is where I do my rendering.


107 void RenderNode::drawFilled(Face &amp;f){
108 glBegin(GL_TRIANGLES);
109
110 vector<Material> faceMaterials = geo->getMaterial();
111 if(faceMaterials.size()!= 0){
112 Material material = faceMaterials[f.vert[0].material];
113 const float* dif = material.getDiffuse();
114 float color[4];
115 color[0] = dif[0];
116 color[1] = dif[1];
117 color[2] = dif[2];
118 color[3] = dif[3];
119 glColor4fv(color);
120 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT);
121 }
122
123 for(unsigned int i = 0; i < f.vert.size(); i++){
124 if(accState.textured.isTexture()){
125 // cout << "MY place to be " <<endl;
126
127 glTexCoord2f (f.vert[i].texture.u,
128 f.vert[i].texture.v);
129 } else if(accState.textured.isMulti()){
130 //cout << "Rendering with multitexture " <<endl;
131 glMultiTexCoord2fARB(GL_TEXTURE0_ARB,
132 f.vert[i].texture.u,
133 f.vert[i].texture.v);
134 glMultiTexCoord2fARB(GL_TEXTURE1_ARB,
135 f.vert[i].texture.u,
136 f.vert[i].texture.v);
137 }
138
139 glNormal3f(f.vert[i].normal.x,
140 f.vert[i].normal.y,
141 f.vert[i].normal.z);
142 glVertex3f(f.vert[i].coordinate.x,
143 f.vert[i].coordinate.y,
144 f.vert[i].coordinate.z);
145 }
146 glEnd();
147 }


Any ideas where to look? The states are correct and I am entering the correct statements.
Regards Isato

isato
05-20-2010, 09:10 AM
I have solved it.

If you do:

bind texture 0;
bind texture 1;

you have to unbind them in this order.

unbind texture 1;
unbind texture 0;

I did it in the wrong order.

Dark Photon
05-20-2010, 06:02 PM
you have to unbind them in this order.

unbind texture 1;
unbind texture 0;

I did it in the wrong order.
What?! That doesn't make any sense.

I mean I understand what you're saying, but that that would be it doesn't make any sense.

I think what's going on is that your single texture bind/unbinds are just "assuming" that the active texture unit is 0, and it's not -- unless the your multi-texture binds/unbinds are called in reverse order, which guarantees that 0 is still active. Your code even working is an accident.

So I suggest you call glActiveTexture in your single texture bind/unbind calls. Then it's no accident.

isato
05-21-2010, 05:41 AM
Ok thanks for your replay.

I wonder how I can access these
GL_TEXTURE0_ARB
GL_TEXTURE1_ARB

The code below is from my texture. How do I do
glActiveTextureARB(MySelf);
?


32 void Texture::multiUnbind(int index){
33 if(!index){
34 glActiveTextureARB(GL_TEXTURE0_ARB);
35 } else if(index) {
36 glActiveTextureARB(GL_TEXTURE1_ARB);
37 }else {
38 cout << "Could not unbind texture " <<endl;
39 }
40
41 glDisable(GL_TEXTURE_2D);
42 glBindTexture(GL_TEXTURE_2D,0);
43 }
44