2017-11-19 4 views
0

Ich habe untersucht, wie man die Bubble-Textur transparent machen kann, aber nichts gefunden habe, das in meinen Code passen würde. Ich habe mit glColor4f() versucht, aber ich könnte es an der falschen Stelle setzen. Ich bin ein absoluter Anfänger in openGL und ich habe einen Basiscode erhalten, zu dem ich andere Objekte hinzufügen muss, um eine 2D-Szene zu erstellen. Ich weiß auch nicht genau, was jede einzelne Zeile macht. Dies sind die relevanten Teile des Codes:OpenGL machen Textur transparent VBO

// 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); 

} 
+1

Wenn ein Antwort auf Ihre Frage hat Ihr Problem vollständig gelöst, dann sollten Sie die Antwort akzeptieren. (Häkchen links neben der Antwort). – Rabbid76

Antwort

1

Der Code OK so aussieht:

  • Hat Ihr Texture einen Alpha-Kanal haben?
  • Hat Ihr OpenGL Kontextpixelformat Alphakanalpuffer?
  • Welche Art von Transparenz möchten Sie (ganze Textur haben die gleiche Transparenz, oder die Transparenz sollte moduliert werden)?

Die glColor4f(1.0,1.0,1.0,alpha) funktioniert nur, wenn Ihre Texturen konfiguriert werden durch glColor moduliert werden und haben von Null verschiedene Alpha-Kanal-Set, so dass Sie hinzufügen müssen:

glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE); 

Anruf nach der Textur verbindlich zu machen, arbeiten.

Falls Sie Alphakanal nicht (in der Textur oder in Pixelformat) haben, können Sie verwenden:

glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR); 

Werfen Sie auch einen Blick auf: