0

我正在制作一个 openGL 引擎,我在片段着色器中的纹理返回黑色,我很确定问题出在纹理类(可能是 .cpp 文件)中,我知道它以前工作过。
我不确定我做了什么让它生气,但我很确定这不是一个大问题,
也很确定路径很好,我应该把它改成不采用镜面纹理吗?

纹理.h

#pragma once

#include <glad/glad.h>
#include <stb/stb_image.h>

#include "../shader/shader.h"
#include <tools/gldebugging.h>

namespace graphics
{
    class Texture
    {
    public:
        GLuint ID;
        const char* type;
        GLuint unit;
    public:
        Texture(const char* image, const char* texType = "diffuse", bool pixelArt = false, GLuint slot = 31);
        ~Texture() { this->remove(); }
        
        void use();
        void unuse();
        void remove();
    };
}



纹理.cpp

#include "texture.h"

namespace graphics
{
    unsigned char nrOfextures = 0;

    Texture::Texture(const char* image, const char* texType, bool pixelArt, GLuint slot)
        : type(texType)
    {
        if (slot == 31)
        {
            slot = nrOfextures;
            nrOfextures++;
        }

        int widthImg, heightImg, numColCh;
        stbi_set_flip_vertically_on_load(true);
        unsigned char* bytes = stbi_load(image, &widthImg, &heightImg, &numColCh, STBI_rgb_alpha);

        glGenTextures(1, &ID);
        glActiveTexture(GL_TEXTURE0 + slot);
        unit = slot;
        glBindTexture(GL_TEXTURE_2D, ID);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, pixelArt ? GL_NEAREST : GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, pixelArt ? GL_NEAREST : GL_LINEAR);

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

        if (numColCh == 4)
            glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,widthImg,heightImg,0,GL_RGBA,GL_UNSIGNED_BYTE,bytes);
        else if (numColCh == 3)
            glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,widthImg,heightImg,0,GL_RGB,GL_UNSIGNED_BYTE,bytes);
        else if (numColCh == 1)
            glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,widthImg,heightImg,0,GL_RED,GL_UNSIGNED_BYTE,bytes);
        else
            throw std::invalid_argument("Automatic Texture type recognition failed");

        glGenerateMipmap(GL_TEXTURE_2D);

        stbi_image_free(bytes);

        glBindTexture(GL_TEXTURE_2D, 0);
    }

    void Texture::use()
    {
        glActiveTexture(GL_TEXTURE0 + unit);
        glBindTexture(GL_TEXTURE_2D, ID);
    }

    void Texture::unuse()
    {
        glBindTexture(GL_TEXTURE_2D, 0);
    }

    void Texture::remove()
    {
        glDeleteTextures(1, &ID);
    }
}



创建纹理-

std::vector<Texture> QuadTexs
    {
        Texture("res/textures/planks.png", "diffuse"),
        Texture("res/textures/planksSpec.png", "specular")
    };

GitHub 回购

4

0 回答 0