2017-12-16 6 views
0

Ich versuche, eine PNG-Datei mit OpenGL zu zeichnen und scheint ein Problem mit, wie ich die Texturen einrichten.Moderne OpenGL 3.3 Core Black Textur

Main.cpp:

float positions[] = { 
    -1.0f, -1.0f, 
    0.0f, 1.0f, 
    1.0f, 1.0f, 

}; 

float texCoords[] = { 
    -1.0f, -1.0f, 
    -1.0f, 1.0f, 
    1.0f, 1.0f, 
}; 


unsigned int buffer; 
glGenBuffers(1, &buffer); 
glBindBuffer(GL_ARRAY_BUFFER, buffer); 
glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW); 

glEnableVertexAttribArray(0); 
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0); 

glBindBuffer(GL_ARRAY_BUFFER, 0); 

unsigned int texBuffer; 
glGenBuffers(1, &texBuffer); 
glBindBuffer(GL_ARRAY_BUFFER, texBuffer); 
glBufferData(GL_ARRAY_BUFFER, sizeof(texCoords), texCoords, GL_STATIC_DRAW); 

glEnableVertexAttribArray(1); 
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0); 

glBindBuffer(GL_ARRAY_BUFFER, 0); 

int w; 
int h; 
int comp; 
unsigned char* image = stbi_load("res/images/background_level.png", &w, &h, &comp, STBI_rgb_alpha); 

if (image == nullptr) 
    throw(std::string("Failed to load texture")); 

//std::cout << image << std::endl; 

unsigned int m_texture; 

glGenTextures(1, &m_texture); 

glBindTexture(GL_TEXTURE_2D, m_texture); 

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 

if (comp == 3) 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, image); 
else if (comp == 4) 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); 

glBindTexture(GL_TEXTURE_2D, 0); 

stbi_image_free(image); 


/* Loop until the user closes the window */ 
while (!glfwWindowShouldClose(window)) 
{ 
    /* Render here */ 
    glClearColor(1.0, 1.0, 1.0, 1.0); 
    glClear(GL_COLOR_BUFFER_BIT); 

    // draw our first triangle 
    glUseProgram(programID); 
    glActiveTexture(GL_TEXTURE0); 
    glBindTexture(GL_TEXTURE_2D, 0); 

    int uniformLoc = glGetUniformLocation(programID, "tex"); 
    glUniform1i(uniformLoc, 0); 
    glDrawArrays(GL_TRIANGLES, 0, 3); 


    /* Swap front and back buffers */ 
    glfwSwapBuffers(window); 

    /* Poll for and process events */ 
    glfwPollEvents(); 
} 

vertex.shader:

#shader VERTEX 
#version 330 core 

layout (location=0) in vec2 vert; 
layout (location=1) in vec2 vertTexCoord; 
out vec2 fragTexCoord; 

void main() { 
    // Pass the tex coord straight through to the fragment shader 
    fragTexCoord = vertTexCoord; 

    gl_Position = vec4(vert, 0, 1); 
} 

fragment.shader:

#shader FRAGMENT 

#version 330 core 
uniform sampler2D tex; //this is the texture 
in vec2 fragTexCoord; //this is the texture coord 
out vec4 finalColor; //this is the output color of the pixel 

void main() { 
    //finalColor = vec4(1.0, 1.0, 1.0, 1.0); 
    finalColor = texture(tex, fragTexCoord); 
} 

Wenn ich die Linie tauschen im Fragment-Shader auf Kommentar, ich bekomme ein ganz weißes Dreieck gerendert. Wenn die Linie jedoch versucht, die Textur zu verwenden, wird das Dreieck schwarz dargestellt. Ich fange gerade mit OpenGL an, also habe ich nicht unbedingt alle Anfangskonzepte unten pat. Die Texturen könnten meinem Qualifikationsniveau voraus sein, aber ich dachte mir, dass es nicht schaden könnte, hier zu fragen, denn jemand könnte mir zeigen, wo ich Dinge verwirrt habe.

+1

Gibt es einen Grund, warum Sie innerhalb des Renderloops 'glBindTexture (GL_TEXTURE_2D, 0);' aufrufen? Dies scheint zu vermuten, dass 'm_texture == 0' ist. – BDL

+0

@BDL Ich habe das in 'glBindTexture (GL_TEXTURE_2D, m_texture) geändert;' und es funktioniert jetzt wie erwartet. Vielen Dank! Der Grund, warum ich dachte, dass ich '0 'binden musste, war der Aufruf von' glActiveTexture (GL_TEXTURE0); ' –

Antwort

1

Sie rufen innerhalb des Renderloops glBindTexture(GL_TEXTURE_2D, 0) anstelle von glBindTexture(GL_TEXTURE_2D, m_texture) an. Mit diesem Befehl wird eine Textur an die aktuell aktive Textureinheit gebunden.