2017-11-19 5 views
0

버블 텍스처를 투명하게 만드는 방법을 살펴 보았지만 코드에 맞지 않는 것을 찾지 못했습니다. 나는 glColor4f()으로 시도했지만 잘못된 장소에 넣을 수도 있습니다. 저는 OpenGL에서 전체 초보자이고 2D 장면을 만들기 위해 다른 객체를 추가해야하는 기본 코드를 받았습니다. 또한 모든 단일 회선이 정확히 무엇인지 모릅니다. 이 코드는 OK 그렇게 보이는OpenGL 텍스처를 투명하게 만듭니다. 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

:

glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR); 

는 또한 살펴 귀하의 질문에 대한 답변이 귀하의 문제를 완전히 해결했으면 대답을 받아 들여야합니다. (답의 왼쪽에 체크 표시). – Rabbid76

답변

1

:

  • 이 텍스처가 알파 채널을 가지고 있습니까

    이 코드의 관련 부분은 무엇입니까?
  • OpenGL 컨텍스트 픽셀 형식에 알파 채널 버퍼가 있습니까?
  • 원하는 투명도 (전체 질감이 동일한 투명도를 갖거나 투명도가 조절되어야 함)?

텍스쳐가 glColor에 의해 조절되도록 구성하고 추가 할 수 있도록 제로 알파 채널 세트를 가지고하는 경우 glColor4f(1.0,1.0,1.0,alpha)에만 작동합니다 :

glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE); 

전화를 작동 할 수있는 텍스처 바인딩 후.

당신이 (pixelformat 질감 또는에서) 알파 채널이없는 경우

당신은 사용할 수 있습니다에 If