Transparency problems

hi i have a texture and the mask… i can made the thing looks transparent but only when a light hit it… what can i do??? i want that even in the dark it look transparent

this is the portion of code


glEnable(GL_BLEND);
if (lol) dibujaParedes();

glDisable(GL_BLEND);  

.
.
.

void dibujaParedes(){    
    int i,j;
    glBlendFunc(GL_DST_COLOR,GL_ZERO);
    glBindTexture(GL_TEXTURE_2D, wood[2]);
    // Pared Norte    
    for (i=-100;i<100;i++){
        for (j=-40;j<60;j++){
            glBegin(GL_QUADS);
                glTexCoord2f((100+i)*0.005f+0.000f,(40+j)*0.01f+0.00f); glVertex3f(i*5.0f, j*5.0f, -500);	// Bottom Right Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.005f,(40+j)*0.01f+0.00f); glVertex3f((i+1)*5.0f, j*5.0f, -500);	// Top Right Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.005f,(40+j)*0.01f+0.01f); glVertex3f((i+1)*5.0f, (j+1)*5.0f, -500) ;	// Top Left Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.000f,(40+j)*0.01f+0.01f); glVertex3f( i*5.0f,    (j+1)*5.0f, -500);	// Bottom Left Of The Texture and Quad
            glEnd();
        }
    }
    // Pared Sur
    for (i=-100;i<100;i++){
        for (j=-40;j<60;j++){
            glBegin(GL_QUADS);
                glTexCoord2f((100+i)*0.005f+0.000f,(40+j)*0.01f+0.00f); glVertex3f(i*5.0f, j*5.0f, 500);	// Bottom Right Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.005f,(40+j)*0.01f+0.00f); glVertex3f((i+1)*5.0f, j*5.0f, 500);	// Top Right Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.005f,(40+j)*0.01f+0.01f); glVertex3f((i+1)*5.0f, (j+1)*5.0f, 500) ;	// Top Left Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.000f,(40+j)*0.01f+0.01f); glVertex3f( i*5.0f,    (j+1)*5.0f, 500);	// Bottom Left Of The Texture and Quad
            glEnd();
        }
    }
    // Pared oeste
    for (i=-100;i<100;i++){
        for (j=-40;j<60;j++){
            glBegin(GL_QUADS);
                glTexCoord2f((100+i)*0.005f+0.000f,(40+j)*0.01f+0.00f); glVertex3f(-500, j*5.0f, i*5.0f);	// Bottom Right Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.005f,(40+j)*0.01f+0.00f); glVertex3f(-500, j*5.0f, (i+1)*5.0f);	// Top Right Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.005f,(40+j)*0.01f+0.01f); glVertex3f(-500, (j+1)*5.0f, (i+1)*5.0f) ;	// Top Left Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.000f,(40+j)*0.01f+0.01f); glVertex3f(-500, (j+1)*5.0f,  i*5.0f);	// Bottom Left Of The Texture and Quad
            glEnd();
        }
    }
    // Pared este
    for (i=-100;i<100;i++){
        for (j=-40;j<60;j++){
            glBegin(GL_QUADS);
                glTexCoord2f((100+i)*0.005f+0.000f,(40+j)*0.01f+0.00f); glVertex3f(500, j*5.0f, i*5.0f);	// Bottom Right Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.005f,(40+j)*0.01f+0.00f); glVertex3f(500, j*5.0f, (i+1)*5.0f);	// Top Right Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.005f,(40+j)*0.01f+0.01f); glVertex3f(500, (j+1)*5.0f, (i+1)*5.0f) ;	// Top Left Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.000f,(40+j)*0.01f+0.01f); glVertex3f(500, (j+1)*5.0f,  i*5.0f);	// Bottom Left Of The Texture and Quad
            glEnd();
        }
    }
    glBlendFunc(GL_ONE, GL_ONE);    
    glBindTexture(GL_TEXTURE_2D, wood[1]);
    // Pared Norte    
    for (i=-100;i<100;i++){
        for (j=-40;j<60;j++){
            glBegin(GL_QUADS);
                glTexCoord2f((100+i)*0.005f+0.000f,(40+j)*0.01f+0.00f); glVertex3f(i*5.0f, j*5.0f, -500);	// Bottom Right Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.005f,(40+j)*0.01f+0.00f); glVertex3f((i+1)*5.0f, j*5.0f, -500);	// Top Right Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.005f,(40+j)*0.01f+0.01f); glVertex3f((i+1)*5.0f, (j+1)*5.0f, -500) ;	// Top Left Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.000f,(40+j)*0.01f+0.01f); glVertex3f( i*5.0f,    (j+1)*5.0f, -500);	// Bottom Left Of The Texture and Quad
            glEnd();
        }
    }
    // Pared Sur
    for (i=-100;i<100;i++){
        for (j=-40;j<60;j++){
            glBegin(GL_QUADS);
                glTexCoord2f((100+i)*0.005f+0.000f,(40+j)*0.01f+0.00f); glVertex3f(i*5.0f, j*5.0f, 500);	// Bottom Right Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.005f,(40+j)*0.01f+0.00f); glVertex3f((i+1)*5.0f, j*5.0f, 500);	// Top Right Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.005f,(40+j)*0.01f+0.01f); glVertex3f((i+1)*5.0f, (j+1)*5.0f, 500) ;	// Top Left Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.000f,(40+j)*0.01f+0.01f); glVertex3f( i*5.0f,    (j+1)*5.0f, 500);	// Bottom Left Of The Texture and Quad
            glEnd();
        }
    }
    // Pared oeste
    for (i=-100;i<100;i++){
        for (j=-40;j<60;j++){
            glBegin(GL_QUADS);
                glTexCoord2f((100+i)*0.005f+0.000f,(40+j)*0.01f+0.00f); glVertex3f(-500, j*5.0f, i*5.0f);	// Bottom Right Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.005f,(40+j)*0.01f+0.00f); glVertex3f(-500, j*5.0f, (i+1)*5.0f);	// Top Right Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.005f,(40+j)*0.01f+0.01f); glVertex3f(-500, (j+1)*5.0f, (i+1)*5.0f) ;	// Top Left Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.000f,(40+j)*0.01f+0.01f); glVertex3f(-500, (j+1)*5.0f,  i*5.0f);	// Bottom Left Of The Texture and Quad
            glEnd();
        }
    }
    // Pared este
    for (i=-100;i<100;i++){
        for (j=-40;j<60;j++){
            glBegin(GL_QUADS);
                glTexCoord2f((100+i)*0.005f+0.000f,(40+j)*0.01f+0.00f); glVertex3f(500, j*5.0f, i*5.0f);	// Bottom Right Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.005f,(40+j)*0.01f+0.00f); glVertex3f(500, j*5.0f, (i+1)*5.0f);	// Top Right Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.005f,(40+j)*0.01f+0.01f); glVertex3f(500, (j+1)*5.0f, (i+1)*5.0f) ;	// Top Left Of The Texture and Quad
                glTexCoord2f((100+i)*0.005f+0.000f,(40+j)*0.01f+0.01f); glVertex3f(500, (j+1)*5.0f,  i*5.0f);	// Bottom Left Of The Texture and Quad
            glEnd();
        }
    }
}


wood[2] is the mask;
wood[1] is the texture in bmp;

you can download the whole thing from this link

http://rapidshare.com/files/168725523/transp_problem.tar.gz.html

its for linux

thx!!

I think you can use multi-texture. Put the mask texture on texture unit 0 and the wood texture on texture unit 1. Multiply the mask texture with the wood texture. You can write a simple vertex and fragment shader to do this or use the fixed function pipeline multi-texture. This way you only need to draw the geometry one time and this should be faster.

Edit: But to be able to do that, the mask need an alpha component which you don’t have because you use a bmp (RGB) file format.

Ok got it working with multi-texture.

First you will need to get two functions pointers that I add in your final_lib.h. If you use freeglut replace the <GL/glut.h> header with <GL/freeglut> to be able to use glutGetProcAddress.


typedef void (APIENTRYP PFNGLACTIVETEXTUREPROC) (GLenum texture);
typedef void (APIENTRYP PFNGLMULTITEXCOORD2FPROC) (GLenum target, GLfloat s, GLfloat t);

static PFNGLACTIVETEXTUREPROC glActiveTexture;
static PFNGLMULTITEXCOORD2FPROC glMultiTexCoord2f;

Somewhere simply call:


 glActiveTexture = (PFNGLACTIVETEXTUREPROC)glutGetProcAddress("glActiveTexture");
    glMultiTexCoord2f = (PFNGLMULTITEXCOORD2FPROC)glutGetProcAddress("glMultiTexCoord2f");

For the following to function properly I have to change the data type of the field data of the struct Imagen from char to unsigned char. Since your mask (the bushesm.bmp) is black where we want to draw and white where we don’t want to draw + don’t have an alpha component, I write this algo in the cargarTextura function:


//Création de l'image RGBA
    GLubyte *bushes_image = (GLubyte*)malloc(w2->sizeX*w2->sizeY*4*sizeof(GLubyte));
    int i,j,k,l;
    
    //Convertir l'image RGB en format RGBA
    for( j = 0, k = 0,l = 0  ; j < w2->sizeY ; j++)
    {
      for( i = 0 ; i < w2->sizeX ; i++)
      {
        bushes_image[l++] = w2->data[k++];
        bushes_image[l++] = w2->data[k++];
        bushes_image[l++] = w2->data[k++];
        bushes_image[l++] = 255;
      }
    }
    glActiveTexture(GL_TEXTURE1);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,wood[1]);//bushes
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,w2->sizeX,w2->sizeY,0,GL_RGBA,GL_UNSIGNED_BYTE,bushes_image);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture
    
    glDisable(GL_TEXTURE_2D);
    
    free(bushes_image);
    
    GLubyte *bushes_image_mask = (GLubyte*)malloc(w3->sizeX*w3->sizeY*4*sizeof(GLubyte));
    
     //Convertir l'image RGB en format RGBA
    for( j = 0, k = 0,l = 0  ; j < w3->sizeY ; j++)
    {
      for( i = 0 ; i < w3->sizeX ; i++)
      {
        if( w3->data[k] == 255 )
        {
          bushes_image_mask[l++] = 0/*w3->data[k++]*/;
          bushes_image_mask[l++] = 0/*w3->data[k++]*/;
          bushes_image_mask[l++] = 0/*w3->data[k++]*/;
          bushes_image_mask[l++] = 0;
          k+=3;
        }
        else
        {
          bushes_image_mask[l++] = 255/*w3->data[k++]*/;
          bushes_image_mask[l++] = 255/*w3->data[k++]*/;
          bushes_image_mask[l++] = 255/*w3->data[k++]*/;
          bushes_image_mask[l++] = 255;
          k+=3;
        }
      }
    }
    
    glActiveTexture(GL_TEXTURE0);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,wood[2]);
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,w3->sizeX,w3->sizeY,0,GL_RGBA,GL_UNSIGNED_BYTE,bushes_image_mask);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture
    //glDisable(GL_TEXTURE_2D);
    free(bushes_image_mask);
}

The function dibujaParedes become:


void dibujaParedes(){    
    int i,j;
   
   glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA);    
    //glDisable(GL_BLEND);
    //glEnable(GL_TEXTURE_2D);
    
    glActiveTexture(GL_TEXTURE0);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, wood[2]);
    //glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL);
    glActiveTexture(GL_TEXTURE1);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, wood[1]);
   // glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
    // Pared Norte    
    for (i=-100;i<100;i++){
        for (j=-40;j<60;j++){
            glBegin(GL_QUADS);
            glMultiTexCoord2f(GL_TEXTURE1,(100+i)*0.005f+0.000f,(40+j)*0.01f+0.00f); 
                glMultiTexCoord2f(GL_TEXTURE0,(100+i)*0.005f+0.000f,(40+j)*0.01f+0.00f); glVertex3f(i*5.0f, j*5.0f, -500);	// Bottom Right Of The Texture and Quad
                glMultiTexCoord2f(GL_TEXTURE1,(100+i)*0.005f+0.005f,(40+j)*0.01f+0.00f);
                glMultiTexCoord2f(GL_TEXTURE0,(100+i)*0.005f+0.005f,(40+j)*0.01f+0.00f); glVertex3f((i+1)*5.0f, j*5.0f, -500);	// Top Right Of The Texture and Quad
                glMultiTexCoord2f(GL_TEXTURE1,(100+i)*0.005f+0.005f,(40+j)*0.01f+0.01f);
                glMultiTexCoord2f(GL_TEXTURE0,(100+i)*0.005f+0.005f,(40+j)*0.01f+0.01f); glVertex3f((i+1)*5.0f, (j+1)*5.0f, -500) ;	// Top Left Of The Texture and Quad
                glMultiTexCoord2f(GL_TEXTURE1,(100+i)*0.005f+0.000f,(40+j)*0.01f+0.01f);
                glMultiTexCoord2f(GL_TEXTURE0,(100+i)*0.005f+0.000f,(40+j)*0.01f+0.01f); glVertex3f( i*5.0f,    (j+1)*5.0f, -500);	// Bottom Left Of The Texture and Quad
            glEnd();
        }
    }
    // Pared Sur
    for (i=-100;i<100;i++){
        for (j=-40;j<60;j++){
            glBegin(GL_QUADS);
            glMultiTexCoord2f(GL_TEXTURE1,(100+i)*0.005f+0.000f,(40+j)*0.01f+0.00f);
                glMultiTexCoord2f(GL_TEXTURE0,(100+i)*0.005f+0.000f,(40+j)*0.01f+0.00f); glVertex3f(i*5.0f, j*5.0f, 500);	// Bottom Right Of The Texture and Quad
                glMultiTexCoord2f(GL_TEXTURE1,(100+i)*0.005f+0.005f,(40+j)*0.01f+0.00f);
                glMultiTexCoord2f(GL_TEXTURE0,(100+i)*0.005f+0.005f,(40+j)*0.01f+0.00f); glVertex3f((i+1)*5.0f, j*5.0f, 500);	// Top Right Of The Texture and Quad
                glMultiTexCoord2f(GL_TEXTURE1,(100+i)*0.005f+0.005f,(40+j)*0.01f+0.01f);
                glMultiTexCoord2f(GL_TEXTURE0,(100+i)*0.005f+0.005f,(40+j)*0.01f+0.01f); glVertex3f((i+1)*5.0f, (j+1)*5.0f, 500) ;	// Top Left Of The Texture and Quad
                glMultiTexCoord2f(GL_TEXTURE1,(100+i)*0.005f+0.000f,(40+j)*0.01f+0.01f);
                glMultiTexCoord2f(GL_TEXTURE0,(100+i)*0.005f+0.000f,(40+j)*0.01f+0.01f); glVertex3f( i*5.0f,    (j+1)*5.0f, 500);	// Bottom Left Of The Texture and Quad
            glEnd();
        }
    }
    // Pared oeste
    for (i=-100;i<100;i++){
        for (j=-40;j<60;j++){
            glBegin(GL_QUADS);
            glMultiTexCoord2f(GL_TEXTURE1,(100+i)*0.005f+0.000f,(40+j)*0.01f+0.00f);
                glMultiTexCoord2f(GL_TEXTURE0,(100+i)*0.005f+0.000f,(40+j)*0.01f+0.00f); glVertex3f(-500, j*5.0f, i*5.0f);	// Bottom Right Of The Texture and Quad
                glMultiTexCoord2f(GL_TEXTURE1,(100+i)*0.005f+0.005f,(40+j)*0.01f+0.00f);
                glMultiTexCoord2f(GL_TEXTURE0,(100+i)*0.005f+0.005f,(40+j)*0.01f+0.00f); glVertex3f(-500, j*5.0f, (i+1)*5.0f);	// Top Right Of The Texture and Quad
                glMultiTexCoord2f(GL_TEXTURE1,(100+i)*0.005f+0.005f,(40+j)*0.01f+0.01f);
                glMultiTexCoord2f(GL_TEXTURE0,(100+i)*0.005f+0.005f,(40+j)*0.01f+0.01f); glVertex3f(-500, (j+1)*5.0f, (i+1)*5.0f) ;	// Top Left Of The Texture and Quad
                glMultiTexCoord2f(GL_TEXTURE1,(100+i)*0.005f+0.000f,(40+j)*0.01f+0.01f);
                glMultiTexCoord2f(GL_TEXTURE0,(100+i)*0.005f+0.000f,(40+j)*0.01f+0.01f); glVertex3f(-500, (j+1)*5.0f,  i*5.0f);	// Bottom Left Of The Texture and Quad
            glEnd();
        }
    }
    // Pared este
    for (i=-100;i<100;i++){
        for (j=-40;j<60;j++){
            glBegin(GL_QUADS);
            glMultiTexCoord2f(GL_TEXTURE1,(100+i)*0.005f+0.000f,(40+j)*0.01f+0.00f);
                glMultiTexCoord2f(GL_TEXTURE0,(100+i)*0.005f+0.000f,(40+j)*0.01f+0.00f); glVertex3f(500, j*5.0f, i*5.0f);	// Bottom Right Of The Texture and Quad
                glMultiTexCoord2f(GL_TEXTURE1,(100+i)*0.005f+0.005f,(40+j)*0.01f+0.00f); 
                glMultiTexCoord2f(GL_TEXTURE0,(100+i)*0.005f+0.005f,(40+j)*0.01f+0.00f); glVertex3f(500, j*5.0f, (i+1)*5.0f);	// Top Right Of The Texture and Quad
                glMultiTexCoord2f(GL_TEXTURE1,(100+i)*0.005f+0.005f,(40+j)*0.01f+0.01f);
                glMultiTexCoord2f(GL_TEXTURE0,(100+i)*0.005f+0.005f,(40+j)*0.01f+0.01f); glVertex3f(500, (j+1)*5.0f, (i+1)*5.0f) ;	// Top Left Of The Texture and Quad
                glMultiTexCoord2f(GL_TEXTURE1,(100+i)*0.005f+0.000f,(40+j)*0.01f+0.01f);
                glMultiTexCoord2f(GL_TEXTURE0,(100+i)*0.005f+0.000f,(40+j)*0.01f+0.01f); glVertex3f(500, (j+1)*5.0f,  i*5.0f);	// Bottom Left Of The Texture and Quad
            glEnd();
        }
    }
    //glDisable(GL_TEXTURE_2D);
    
    glActiveTexture(GL_TEXTURE1);
    glDisable(GL_TEXTURE_2D);
    
    glActiveTexture(GL_TEXTURE0);
 //   glEnable(GL_BLEND);
}

With some other minor change here and there, you will be able to make it work. Notice here I have used the fixed function pipeline to do texture because my video card is too old to use shader.

Screenshot:
[img=http://img368.imageshack.us/img368/8294/image1tg3.th.png]
[img=http://img101.imageshack.us/img101/5278/image2um1.th.png]

I noticed that the default texture wrap mode cause some artifacts at texture boundaries. If you look the image of the first link, you will see a horizontal line appear of nowhere (top left quadrant). In the second image you will see a vertical line (corner of the field at the left). To make them disappear, add the parameters glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
and glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE); for texture wrap mode.