GLFW GLAD 未绘制纹理,黑色方块

Posted

技术标签:

【中文标题】GLFW GLAD 未绘制纹理,黑色方块【英文标题】:GLFW GLAD Texture not drawn, black square 【发布时间】:2019-03-22 17:13:14 【问题描述】:

我正在尝试为由两个三角形组成的正方形绘制纹理。但是,它显示的不是带有纹理的正方形,而是黑色正方形。此外,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);

【问题讨论】:

Related: ***.com/questions/4041124/… 如果没有,您应该尝试使用renderdoc等工具进行捕获 为什么在绘制之前不重新绑定纹理对象? @genpfault 我该怎么做? @Borgleader 如何使用 renderdoc? 与上传纹理图像的方式相同:glBindTexture(GL_TEXTURE_2D, texture). 【参考方案1】:

几个问题:

stbi_load()desired_channels 设置为3,而不是仅仅希望图像是3 通道。 设置glPixelStorei(GL_UNPACK_ALIGNMENT, 1),以防加载的图片与默认的4不匹配。 您在加载纹理对象后立即解除绑定;请务必在绘制之前重新绑定它。 使用 stbi_set_flip_vertically_on_load(1)(或修改您的 texcoords)为 OpenGL 以“正确”方式翻转图像。 您的顶点/texcoords/indexes 有点不稳定;我把它们修好了,让我觉得有意义。

你真的应该设置你的着色器应该从哪个特定的纹理单元进行采样,而不是依赖于未分配的采样器制服默认为零的事实:

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

大家一起:

#include <glad/glad.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

void CheckStatus( GLuint obj, bool isShader )

    GLint status = GL_FALSE, log[ 1 << 11 ] =  0 ;
    ( isShader ? glGetShaderiv : glGetProgramiv )( obj, isShader ? GL_COMPILE_STATUS : GL_LINK_STATUS, &status );
    if( status == GL_TRUE ) return;
    ( isShader ? glGetShaderInfoLog : glGetProgramInfoLog )( obj, sizeof( log ), NULL, (GLchar*)log );
    std::cerr << (GLchar*)log << "\n";
    std::exit( EXIT_FAILURE );


void AttachShader( GLuint program, GLenum type, const char* src )

    GLuint shader = glCreateShader( type );
    glShaderSource( shader, 1, &src, NULL );
    glCompileShader( shader );
    CheckStatus( shader, true );
    glAttachShader( program, shader );
    glDeleteShader( shader );


const char* vert = 1 + R"GLSL(
#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;

)GLSL";

const char* frag = 1 + R"GLSL(
#version 330 core
out vec4 FragColor;

in vec3 ourColor;
in vec2 TexCoord;

uniform sampler2D ourTexture;

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

)GLSL";

int main( int, char** )

    glfwSetErrorCallback( []( int, const char* desc )  std::cerr << desc << "\n"; std::exit( EXIT_FAILURE );  );
    glfwInit();
    glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
    glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
    glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE );
    glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
    GLFWwindow* window = glfwCreateWindow( 640, 480, "GLFW", NULL, NULL );
    glfwMakeContextCurrent( window );
    gladLoadGLLoader( (GLADloadproc)glfwGetProcAddress );

    GLuint prog = glCreateProgram();
    AttachShader( prog, GL_VERTEX_SHADER, vert );
    AttachShader( prog, GL_FRAGMENT_SHADER, frag );
    glLinkProgram( prog );
    CheckStatus( prog, false );

    float vertices[] =
    
        -0.5f, -0.5f, 0.0f,  1.0f,1.0f,0.0f,  0.0f,0.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,0.0f,0.0f,  1.0f,1.0f,
        -0.5f,  0.5f, 0.0f,  1.0f,1.0f,0.0f,  0.0f,1.0f
    ;

    unsigned int indices[] =
    
        0,1,2,
        2,3,0
    ;
    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;

    stbi_set_flip_vertically_on_load( 1 );
    // https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Brick_wall_close-up_view.jpg/800px-Brick_wall_close-up_view.jpg
    unsigned char *data = stbi_load("wall.jpg", &w, &h, NULL, 3);
    if (data)
    
        std::cout << "LOADING TEXTURE: " << w << "x" << h << std::endl;
        glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
        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))
    
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // Render stuff
        glUseProgram( prog );
        unsigned int texture_unit = 0;
        glActiveTexture( GL_TEXTURE0 + texture_unit );
        glUniform1i( glGetUniformLocation( prog, "ourTexture" ), texture_unit );
        glBindTexture(GL_TEXTURE_2D, texture);
        glBindVertexArray(VAO);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);

        glfwSwapBuffers(window);
        glfwPollEvents();
    

【讨论】:

以上是关于GLFW GLAD 未绘制纹理,黑色方块的主要内容,如果未能解决你的问题,请参考以下文章

iPhone App - 为啥我的 CGContext 圆圈看起来像黑色方块?

three.js 纹理未显示且为黑色

openGL FBO 复制到纹理导致黑色/深色图像

现代OpenGL 3.3核心黑色纹理

使用OpenGL和GLFW的简单三角形[重复]

关于word标题出现的黑色方块