我的 openGL 纹理类中有什么愚蠢的错误?

问题描述

我正在制作一个 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,GL_RGBA,widthImg,GL_UNSIGNED_BYTE,bytes);
        else if (numColCh == 3)
            glTexImage2D(GL_TEXTURE_2D,GL_RGB,bytes);
        else if (numColCh == 1)
            glTexImage2D(GL_TEXTURE_2D,GL_RED,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 Repo

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)