GLFW GLAD未绘制纹理,黑色正方形

时间:2019-03-22 17:13:15

标签: c++ opengl glsl glfw glad

我正在尝试将纹理绘制到由两个三角形组成的正方形。但是,不是显示带有纹理的正方形,而是显示黑色正方形。另外,shader.h与颜色完美配合。这不是main.cpp中的所有代码,仅是纹理代码。

Main.cpp

    Shader defShader("resources/shaders/vertex.txt", "resources/shaders/fragment.txt");

    float vertices[] = {
        0.5f,  0.5f, 0.0f,  1.0f,0.0f,0.0f,  1.0f,1.0f,
        0.5f, -0.5f, 0.0f,  0.0f,1.0f,0.0f,  1.0f,0.0f,
        -0.5f,-0.5f, 0.0f,  1.0f,1.0f,0.0f,  0.0f,1.0f,
        -0.5f, 0.5f, 0.0f,  1.0f,1.0f,0.0f,  0.0f,1.0f
    };

    unsigned int indices[] = {
        0,1,3,
        1,2,3
    };
    unsigned int VBO, VAO, EBO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);

    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

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

    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
    glEnableVertexAttribArray(1);

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

    unsigned int texture;
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    // Set texture wrap parameters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    // Set texture filtering paremeters
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    // Load image, create texture and generate mipmaps
    int w, h, nrChannels;

    unsigned char *data = stbi_load("resources/textures/wall.jpg", &w, &h, &nrChannels, 0);
    if (data) {
        std::cout << "LOADING TEXTURE\n" << std::endl;
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
        glGenerateMipmap(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, 0);
    }
    else {
        std::cout << "FAILED TO LOAD TEXTURE\n" << std::endl;
    }
    stbi_image_free(data);

    while (!glfwWindowShouldClose(window)) {

        processInput(window);

        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // Render stuff
        defShader.Use();
        glBindVertexArray(VAO);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);

        glfwSwapBuffers(window);
        glfwPollEvents();

    }

    glfwTerminate();
    return 0;
}

这里是着色器。 vertShader.txt

#version 330 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aColor;
layout(location = 2) in vec2 aTexCoord;

out vec3 ourColor;
out vec2 TexCoord;

void main() {
    gl_Position = vec4(aPos, 1.0);
    ourColor = aColor;
    TexCoord = aTexCoord;
}

fragShader.txt

#version 330 core
out vec4 FragColor;

in vec3 ourColor;
in vec2 TexCoord;

uniform sampler2D ourTexture;

void main() {
    FragColor = texture(ourTexture, TexCoord);
}

1 个答案:

答案 0 :(得分:2)

一些问题:

  • stbi_load()的{​​{1}}设置为desired_channels,而不仅仅是希望图像是3通道。
  • 设置3,以防加载的图像与默认的glPixelStorei(GL_UNPACK_ALIGNMENT, 1)不匹配。
  • 您正在加载纹理对象后立即解除绑定;确保在绘制之前将其重新绑定。
  • 使用4(或修改您的texcoords)以“正确”的方式翻转OpenGL图片。
  • 您的顶点/ texcoords /索引有点奇怪;我把它们固定起来对我来说很有意义。
  • 您真的应该设置着色器应该从哪个特定纹理单元采样,而不要依赖未分配的采样器统一默认为零的事实:

    stbi_set_flip_vertically_on_load(1)

一起:

screenshot

unsigned int texture_unit = 0;
glActiveTexture( GL_TEXTURE0 + texture_unit );
glUniform1i( glGetUniformLocation( prog, "ourTexture" ), texture_unit );