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

时间:2019-04-02 19:39:12

标签: c++ opengl glsl shader

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

纹理

Original texture

结果

Resulting triangle

这是我的相关代码:

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

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

片段着色器

#version 330 core

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

void main() {
    FragColor = texture(diffuse, texCoord0);
}

顶点着色器

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

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

1 个答案:

答案 0 :(得分: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);

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

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

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

通过将4显式传递给最后一个参数,可以强制

stbi_load生成具有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 ...
     

// ... 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)

在这种情况下,加载图像时必须将格式参数从GL_RGB更改为GL_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);