OpenGL 2D纹理无法正确显示C ++

Mos*_*mmo 2 c++ opengl shader glsl

我是OpenGL的新手,遇到了一个似乎无法破解的问题。我正在尝试向三角形添加简单的2D纹理,该纹理似乎可以很好地加载,但是正在以黑色和白色显示粒状。

质地

原始质感

结果

产生的三角形

这是我的相关代码:

main.cxx

#include <iostream>
#include "display.h"
#include "shader.h"
#include "texture.h"
#include "mesh.h"
#include <glm/glm.hpp>
#include <GLFW/glfw3.h>

int main() 
{

    Display display(800, 600, "project2");

    Vertex vertices[] = {
        Vertex(glm::vec3(-0.5f, -0.5f, 0.0f), glm::vec2(0.0f, 0.0f)),
        Vertex(glm::vec3(0.5f, -0.5f, 0.0f), glm::vec2(1.0f, 0.0f)),
        Vertex(glm::vec3(0.0f, 0.5f, 0.0f), glm::vec2(0.5f, 1.0f)),
    };

    Mesh mesh(vertices, sizeof(vertices)/sizeof(vertices[0]));

    Shader shader("./shaders/shader");

    Texture texture("./textures/container.jpg");

    while (!display.IsClosed())
    {
        display.ProcessInput();

        display.Clear(0.0f, 0.15f, 0.3f, 1.0f);

        texture.Bind(0);
        shader.Bind();

        mesh.Draw();

        display.Update();
    } 

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

texture.cxx

#include "texture.h"

Texture::Texture(const std::string &filePath)
{
    int width, height, numComponents;
    unsigned char* imageData = stbi_load(filePath.c_str(), &width, &height, &numComponents, 4);

    if (!imageData) {
        std::cerr << "Failed to load texture: " << filePath << std::endl;
        return;
    }

    stbi_set_flip_vertically_on_load(true);

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

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

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

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
    glGenerateMipmap(GL_TEXTURE_2D);

    stbi_image_free(imageData);
}

Texture::~Texture()
{
    glDeleteTextures(1, &texture); 
}

void Texture::Bind(unsigned int unit)
{
    glActiveTexture(GL_TEXTURE0 + unit); 
    glBindTexture(GL_TEXTURE_2D, texture);
}
Run Code Online (Sandbox Code Playgroud)

片段着色器

#version 330 core

out vec4 FragColor;
uniform sampler2D diffuse;
in vec2 texCoord0;

void main() {
    FragColor = texture(diffuse, texCoord0);
}
Run Code Online (Sandbox Code Playgroud)

顶点着色器

#version 330 core
layout (location=0) in vec3 position;
layout (location=1) in vec2 texCoord;

out vec2 texCoord0;

void main() {
    gl_Position = vec4(position, 1.0);
    texCoord0 = texCoord;
}
Run Code Online (Sandbox Code Playgroud)

我之前在另一个项目中使用过纹理,并且如果有人可以帮助我,我的代码看起来几乎相同,将不胜感激。

Rab*_*d76 5

当将具有3个颜色通道的RGB图像加载到纹理对象时,GL_UNPACK_ALIGNMENT必须将其设置为1:

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB,
             GL_UNSIGNED_BYTE, imageData);
Run Code Online (Sandbox Code Playgroud)

GL_UNPACK_ALIGNMENT指定内存中每个像素行开始的对齐要求。默认情况下GL_UNPACK_ALIGNMENT设置为4。这意味着图像的每一行的开始都假定为与4的倍数对齐的地址。由于图像数据被紧紧包装,并且每个像素的大小为3字节,因此对齐方式必须更改。

要正确读取图像,最后一个参数stbi_load必须为0(因为jpg格式提供3种颜色通道)或3(强制3种颜色通道):

unsigned char* imageData = stbi_load(filePath.c_str(),
     &width, &height, &numComponents, 0);
Run Code Online (Sandbox Code Playgroud)

stbi_load 通过将4显式传递给最后一个参数,可以强制生成具有4个颜色通道的图像:

参见stb_image.h

Basic usage (see HDR discussion below for HDR usage):
      int x,y,n;
      unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
      // ... process data if not NULL ...
      // ... x = width, y = height, n = # 8-bit components per pixel ...
Run Code Online (Sandbox Code Playgroud)

// ... replace '0' with '1'..'4' to force that many components per pixel

      // ... but 'n' will always be the number that it would have been if you said 0
      stbi_image_free(data)
Run Code Online (Sandbox Code Playgroud)

在这种情况下,加载图像时必须将格式参数从更改GL_RGBGL_RGBA

unsigned char* imageData = stbi_load(filePath.c_str(),
     &width, &height, &numComponents, 0);

// [...]

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA,
             GL_UNSIGNED_BYTE, imageData);
Run Code Online (Sandbox Code Playgroud)