0

I've been looking into how to make the bubble texture transparent but haven't found anything that would fit in my code. I have tried with glColor4f() but I might be putting it in the wrong place. I'm a total beginner in openGL and I've been given a basic code to which I need to add other objects in order to make a 2D scene. I also don't exactly know what every single line does. These are the relevant pieces of code:

// Globals
GLuint locT; // location of "T" uniform variable in myShaderProgram
GLuint locT2;


// Textures
GLuint myBubblesTexture = 0;

// Shader program object for applying textures to our shapes
GLuint myShaderProgram;

// Vertex Buffer Object IDs for the ground texture object
GLuint bubblesPosVBO, bubblesColourVBO, bubblesTexCoordVBO, bubblesIndicesVBO;


// 1) Position Array - Store vertices as (x,y) pairs
static GLfloat bubblesVertices[] = {

    -0.2f, -0.2f,
    -0.2f, 0.2f,
    0.2f, -0.2f,
    0.2f, 0.2f
};
// 2) Colour Array - Store RGB values as unsigned bytes
static GLubyte bubblesColors[] = {

    255, 0, 0, 255,
    255, 255, 0, 255,
    0, 255, 0, 255,
    0, 255, 255, 255

};
// 3) Texture coordinate array (store uv coordinates as floating point values)
static float bubblesTextureCoords[] = {

    0.0f, 1.0f,
    0.0f, 0.0f,
    1.0f, 1.0f,
    1.0f, 0.0f

};
// 4) Index Array - Store indices to quad vertices - this determines the order the vertices are to be processed
static GLubyte bubblesVertexIndices[] = { 0, 1, 2, 3 };


void init(int argc, char* argv[]);
void setupBubblesTextureVBO(void);
void display(void);
void drawTexturedBubblesVBO(void);

int _tmain(int argc, char* argv[]) 
{

    init(argc, argv);

    glutMainLoop();

    // Shut down COM
    shutdownCOM();

    return 0;
}


void init(int argc, char* argv[]) {

    // Initialise COM so we can use Windows Imaging Component
    initCOM();

    // Initialise FreeGLUT
    glutInit(&argc, argv);

    glutInitContextVersion(3, 3);
    glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

    glutInitWindowSize(1200, 800);
    glutInitWindowPosition(64, 64);
    glutCreateWindow("Funky Fish");

    // Register callback functions
    glutDisplayFunc(display);

    // Initialise GLEW library
    GLenum err = glewInit();

    // Setup colour to clear the window
    glClearColor(0.2f, 0.2f, 0.8f, 0.0f);
    glLineWidth(9.0f);

    //Load textures
    myBubblesTexture = fiLoadTextureA("bubbles.png");

    //Shader setup 
    myShaderProgram = setupShaders(string("Shaders\\basic_vertex_shader.txt"), string("Shaders\\basic_fragment_shader.txt"));

    // Get uniform location of "T" variable in shader program (we'll use this in the play function to give the uniform variable "T" a value)
    locT = glGetUniformLocation(myShaderProgram, "T");

    //Setup the bubbles using VBO
    setupBubblesTextureVBO();
}

// our bubbles
void setupBubblesTextureVBO(void) {

    // setup VBO for the quad object position data
    glGenBuffers(1, &bubblesPosVBO);
    glBindBuffer(GL_ARRAY_BUFFER, bubblesPosVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(bubblesVertices), bubblesVertices, GL_STATIC_DRAW);

    // setup VBO for the quad object colour data
    glGenBuffers(1, &bubblesColourVBO);
    glBindBuffer(GL_ARRAY_BUFFER, bubblesColourVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(bubblesColors), bubblesColors, GL_STATIC_DRAW);

    // setup VBO for the quad object texture coord data
    glGenBuffers(1, &bubblesTexCoordVBO);
    glBindBuffer(GL_ARRAY_BUFFER, bubblesTexCoordVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(bubblesTextureCoords), bubblesTextureCoords, GL_STATIC_DRAW);

    // setup quad vertex index array
    glGenBuffers(1, &bubblesIndicesVBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bubblesIndicesVBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(bubblesVertexIndices), bubblesVertexIndices, GL_STATIC_DRAW);
}

void display(void) {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // draw our bubble
    drawTexturedBubblesVBO();
    glutSwapBuffers();
}

void drawTexturedBubblesVBO(void) {

    glUseProgram(myShaderProgram);

    glEnable(GL_TEXTURE_2D);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_BLEND);

    // Move our bubble to the centre of the screen
    GUMatrix4 T = GUMatrix4::translationMatrix(0.0f, 0.0f, 0.0f);
    glUniformMatrix4fv(locT, 1, GL_FALSE, (GLfloat*)&T);


    // Bind each vertex buffer and enable
    // The data is still stored in the GPU but we need to set it up (which also includes validation of the VBOs behind-the-scenes)

    // Bind texture
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, myBubblesTexture);
    glUniform1i(glGetUniformLocation(myShaderProgram, "texture"), 0);
    glEnable(GL_TEXTURE_2D);

    glBindBuffer(GL_ARRAY_BUFFER, bubblesPosVBO);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)0);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, bubblesColourVBO);
    glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, (const GLvoid*)0);
    glEnableVertexAttribArray(1);

    glBindBuffer(GL_ARRAY_BUFFER, bubblesTexCoordVBO);
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)0);
    glEnableVertexAttribArray(2);


    // Bind the index buffer
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bubblesIndicesVBO);

    // Draw the object - same function call as used for vertex arrays but the last parameter is interpreted as an offset into the currently bound index buffer (set to 0 so we start drawing from the beginning of the buffer).
    glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, (GLvoid*)0);
    //  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glDisable(GL_TEXTURE_2D);

    // use to force disable our shaderprogram
    // glUseProgram(0);

}
Spektre
  • 49,595
  • 11
  • 110
  • 380

1 Answers1

1

The code looks OK so:

  • Does your Texture have an alpha channel?
  • Does your OpenGL context pixel format has Alpha channel buffer?
  • What kind of transparency you want (whole texture have the same transparency, or the transparency should be modulated)?

The glColor4f(1.0,1.0,1.0,alpha) will work only if your textures are configured to be modulated by glColor and have nonzero alpha channel set so you need to add:

glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);

call after binding the texture to make it work.

In case you do not have alpha channel (in texture or in pixelformat) you can use:

glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);

Also take a look at:

Spektre
  • 49,595
  • 11
  • 110
  • 380