现代OpenGL 3.3核心黑色纹理

时间:2017-12-16 20:22:14

标签: c++ opengl textures

我正在尝试使用OpenGL绘制一个png文件,似乎对我如何设置纹理有疑问。

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

如果我交换片段着色器中注释掉的行,我会得到一个全白色三角形。然而,当线试图使用纹理时,三角形全部变黑。我刚刚开始使用OpenGL,所以我不必将所有开始的概念都放下来。纹理可能超出我的技能水平,但我认为尝试在这里询问是不是有害,因为有人可能会告诉我在哪里我有困惑。

1 个答案:

答案 0 :(得分:1)

您在renderloop中调用了glBindTexture(GL_TEXTURE_2D, 0)而不是glBindTexture(GL_TEXTURE_2D, m_texture)。使用此命令,纹理将绑定到当前活动的纹理单元。