着色器的调试在 opengl 中不起作用

debugging of shader is not working in opengl

我正在尝试抽象 OpenGL 着色器,但着色器编译失败,即使它是一个教程着色器。主要问题是,当我 运行 着色器编译失败时,它会在控制台中输出任意文本,而不是一些可以帮助我修复错误的错误消息。

这是我的Shader.h

#include "glm/vec2.hpp"
#include "glm/vec3.hpp"
#include "glm/vec4.hpp"
#include "glm/gtc/type_ptr.hpp"
#include <iostream>

class Shader
{
public:
    Shader(std::string vert, std::string frag);

    void setInt(const char* uniformName, int data);
    void set2Int(const char* uniformName, glm::ivec2 data);
    void set3Int(const char* uniformName, glm::ivec3 data);
    void set4Int(const char* uniformName, glm::ivec4 data);

    void setFloat(const char* uniformName, float data);
    void set2Float(const char* uniformName, glm::vec2 data);
    void set3Float(const char* uniformName, glm::vec3 data);
    void set4Float(const char* uniformName, glm::vec4 data);

    void setMat3x3(const char* uniformName, glm::mat3 data);
    void setMat4x4(const char* uniformName, glm::mat4 data);

    void Bind();
    void UnBind();

    ~Shader();
private:
    unsigned int makeShader(unsigned int type, std::string data);
    unsigned int id;
};

这是我的Shader.cpp

#include "Shader.h"
#include "glew/glew.h"

Shader::Shader(std::string vert, std::string frag)
{
    unsigned int vertexShader = makeShader(GL_VERTEX_SHADER, vert);
    unsigned int fragmentShader = makeShader(GL_FRAGMENT_SHADER, frag);

    id = glCreateProgram();

    glAttachShader(id, vertexShader);
    glAttachShader(id, fragmentShader);
    glLinkProgram(id);
    glValidateProgram(id);

    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);
}

void Shader::setInt(const char* uniformName, int data)
{
    int loc = glGetUniformLocation(id, uniformName);
    glUniform1iv(loc, 1, &data);
}

void Shader::set2Int(const char* uniformName, glm::ivec2 data)
{
    int loc = glGetUniformLocation(id, uniformName);
    glUniform1iv(loc, 2, glm::value_ptr(data));
}

void Shader::set3Int(const char* uniformName, glm::ivec3 data)
{
    int loc = glGetUniformLocation(id, uniformName);
    glUniform1iv(loc, 3, glm::value_ptr(data));
}

void Shader::set4Int(const char* uniformName, glm::ivec4 data)
{
    int loc = glGetUniformLocation(id, uniformName);
    glUniform1iv(loc, 4, glm::value_ptr(data));
}

void Shader::setFloat(const char* uniformName, float data)
{
    int loc = glGetUniformLocation(id, uniformName);
    glUniform1fv(loc, 1, &data);
}

void Shader::set2Float(const char* uniformName, glm::vec2 data)
{
    int loc = glGetUniformLocation(id, uniformName);
    glUniform1fv(loc, 2, glm::value_ptr(data));
}

void Shader::set3Float(const char* uniformName, glm::vec3 data)
{
    int loc = glGetUniformLocation(id, uniformName);
    glUniform1fv(loc, 3, glm::value_ptr(data));
}

void Shader::set4Float(const char* uniformName, glm::vec4 data)
{
    int loc = glGetUniformLocation(id, uniformName);
    glUniform1fv(loc, 4, glm::value_ptr(data));
}

void Shader::setMat3x3(const char* uniformName, glm::mat3 data)
{
    int loc = glGetUniformLocation(id, uniformName);
    glUniformMatrix3fv(loc, 1, GL_FALSE, glm::value_ptr(data));
}

void Shader::setMat4x4(const char* uniformName, glm::mat4 data)
{
    int loc = glGetUniformLocation(id, uniformName);
    glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(data));
}

void Shader::Bind()
{
    glUseProgram(id);
}

void Shader::UnBind()
{
    glUseProgram(0);
}

Shader::~Shader()
{
    glDeleteProgram(id);
}

unsigned int Shader::makeShader(unsigned int type, std::string data)
{
    unsigned int shaderId = glCreateShader(type);
    const char* src = data.c_str();
    glShaderSource(shaderId, 1, &src, nullptr);
    glCompileShader(shaderId);

    int didCompiled = 0;
    glGetProgramiv(shaderId, GL_COMPILE_STATUS, &didCompiled);
    if (didCompiled == GL_FALSE) {
        int length = 0;
        glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
        char* message = (char*)alloca(sizeof(char) * length);
        glGetShaderInfoLog(shaderId, length, &length, message);
        std::cout << (type == GL_VERTEX_SHADER ? "Vertex Shader" : "Fragment Shader") <<  " Compilation Failed: " << message << std::endl;
        glDeleteShader(shaderId);
        shaderId = 0;
    }
    return shaderId;
}

这是我的顶点着色器代码

#version 330 core

layout (location=0) in vec2 aPos;

void main()
{
     gl_Position = vec4(aPos.x, aPos.y, 1.0f, 1.0f);
}

这是我的片段着色器代码

#version 330 core
out vec4 FragColor;
uniform vec4 uColor;
void main()
{
    FragColor = uColor;
} 

我抽象事物的方式有问题还是着色器代码本身有问题

makeShader函数中:glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);

id 更改为 shaderId

修复它并尝试获取正确的失败消息。