OpenGl着色器链接错误,- (0):错误C5145:必须写入gl_Position [closed]

zzwlnbp8  于 2023-05-22  发布在  其他
关注(0)|答案(1)|浏览(337)

**关闭。**此题需要debugging details。目前不接受答复。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
3天前关闭。
Improve this question
我正在编写一个opengl着色引擎,唯一的目的就是用opengl探索着色。在我的程序的其他版本中,我在链接程序着色器时没有任何错误,但现在我得到了这个错误:

Success to compile 35633 shader!
Success to compile 35632 shader!
program linking failed, error: Vertex info
-----------
(0) : error C5145: must write to gl_Position

我知道这个错误告诉我们问题出在顶点着色器第0行,并且必须写入priv变量gl_Position,如你所见。
在这里,您可以看到在其他时刻已经工作的代码。我没有改变任何东西。

struct ShaderProgramSource {
    std::string vertexSrc;
    std::string fragmentSrc;
};

struct Program
{
    VertexShaderLayout VSLayout;
    unsigned int                handle;
    std::string                 filepath;
    std::string                 programName;
    unsigned long long int      lastWriteTimestamp;
};

//Init function:
unsigned int myShaderIDx = LoadAndCreateProgram(app, "Basic.shader", app->shaderProgramsSrc);
Program& TexturedMeshProgram = app->programs[myShaderIDx];

//--------------------------------------------------------------------------------------

//returns the program position in programs array
unsigned int LoadAndCreateProgram(App*app,std::string filePath, ShaderProgramSource shaderProgramsSrc) {
    
    const GLubyte* glslVersion = glGetString(GL_SHADING_LANGUAGE_VERSION);
    if (glslVersion != nullptr) {
        // Print or store the GLSL version
        printf("GLSL version: %s\n", glslVersion);
    }
    else {
        // Failed to retrieve GLSL version
        printf("Failed to retrieve GLSL version\n");
    }
    
    app->shaderProgramsSrc = parseShader(filePath);
    Program program = {};
    program.handle = createShader(app->shaderProgramsSrc.vertexSrc, app->shaderProgramsSrc.fragmentSrc);
    program.filepath = filePath;
    program.programName = filePath;
    program.lastWriteTimestamp = GetFileLastWriteTimestamp(filePath.c_str());
    app->programs.push_back(program);

    return app->programs.size() - 1;
}

//--------------------------------------------------------------------------------------

ShaderProgramSource parseShader(std::string filePath) {
    std::ifstream stream(filePath);//opens the file

    enum class ShaderType{
        NONE = -1, VERTEX = 0, FRAGMENT = 1
    };

    std::string line;
    std::stringstream ss[2];
    ShaderType type = ShaderType::NONE;

    while (getline(stream, line)) {

        if (line.find("#shader") != std::string::npos) {

            if (line.find("vertex") != std::string::npos) {
                type = ShaderType::VERTEX;
            }
            else if (line.find("fragment") != std::string::npos) {
                type = ShaderType::FRAGMENT;
            }
        }
        else {
            ss[(int)type] << line << '\n';
        }
    }

    return { ss[0].str(),ss[1].str() };

}

//--------------------------------------------------------------------------------------

unsigned int createShader(const std::string& vertexShader, const std::string& fragmentShader) {

    unsigned int program = glCreateProgram();
    glCheckError();
    unsigned int vs = compileShader(GL_VERTEX_SHADER, vertexShader);
    unsigned int fs = compileShader(GL_FRAGMENT_SHADER, fragmentShader);

    glAttachShader(program, vs);
    glCheckError();
    glAttachShader(program, fs);
    glCheckError();
    glLinkProgram(program);
    glCheckError();
    //HANDLE LINKING ERRORS
    int linkSuccess = GL_FALSE;
    glGetProgramiv(program, GL_LINK_STATUS, &linkSuccess);

    if (linkSuccess == GL_FALSE) {
        GLint logLength = 0;
        glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);

        // Allocate memory for the log
        char* logMessage = new char[logLength];
        glGetProgramInfoLog(program, logLength, nullptr, logMessage);
        ELOG("program linking failed, error: %s", logMessage);
        delete[] logMessage;
    }

    glValidateProgram(program);
    glCheckError();
    
   

    glDeleteShader(vs);
    glDeleteShader(fs);

    return program;
}

//--------------------------------------------------------------------------------------

unsigned int compileShader( unsigned int type, const std::string& source) {

    unsigned int id = glCreateShader(type);
    glCheckError();
    const char* src = source.c_str();
    glShaderSource(id, 1, &src, nullptr);
    glCheckError();
    glCompileShader(id);
    glCheckError();

    //ERROR HANDLING HERE
    int result;
    glGetShaderiv(id, GL_COMPILE_STATUS, &result);
    glCheckError();
    if (result == GL_FALSE) {
        int length;
        glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
        char* message = (char*)alloca(length*sizeof(char));
        glGetShaderInfoLog(id, length, &length, message);
        ELOG("Failed to compile %i shader!", type);
        glDeleteShader(id);
        return 0;
    }
    else if (result == GL_TRUE) {
        ELOG("Success to compile %i shader!", type);
    }

    return id;
}

//--------------------------------------------------------------------------------------

GLSL代码:

#shader vertex 
#version 430 core

layout(location = 0) in vec3 aPosition;
layout(location = 1) in vec2 aTexCoord;

out vTexCoord;

void main() {
    vTexCoord = aTexCoord;
    gl_Position = vec4(aPosition, 1.0);
}

#shader fragment
#version 430 core

layout(location = 0) out vec4 color;

in vec2 vTexCoord;

uniform sampler2D uTexture;

void main() {
    color = texture(uTexture, vTexCoord);
}
j7dteeu8

j7dteeu81#

问题是文件路径无效。注意函数glCompileShader(id);和glGetShaderiv(id,GL_COMPILE_STATUS,&result);不会给予你任何编译错误,如果你传递一个空的glsl程序。glgeterror()函数也不会给予你任何错误信息

相关问题