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

问题描述 投票:2回答:1

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

我之前在另一个项目中有纹理工作,我的代码看起来几乎相同,如果有人可以帮助我,那将非常感激。

c++ opengl glsl shader
1个回答
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个字节,对齐必须改变。

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

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

可以强制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 ...

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

在这种情况下,在加载图像时,必须将format参数从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);
© www.soinside.com 2019 - 2024. All rights reserved.