You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

学习OpenGL第七章修改代码后资源加载失败且着色器报错,求解决

Troubleshooting Your OpenGL Issues from Learn OpenGL Chapter 7

Hey there, let's walk through fixing those errors you're hitting. First, let's connect the dots between the issues you're seeing—they're mostly linked to shader file loading problems, which then cascade into other errors.

Let's Break Down the Errors

  • ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: This is the root cause. Your program can't find or read your shader files. Even though removing slashes lets you open the resource in Windows Explorer, your program's working directory might not match where you're expecting it to look for files. Plus, removing slashes probably broke the actual file path (e.g., shaders/vertex.glsl turning into shadersvertex.glsl isn't a valid file name, even if Explorer somehow opened something—maybe you tested a different path?).
  • ERROR::PROGRAM_LINKING_ERROR: Since the shader files weren't read successfully, the OpenGL shader compiler didn't get valid source code to compile. Without compiled shaders, linking the program fails.
  • Vertex info (0) : error C5145: must write to gl_Position: Again, this comes from invalid or missing vertex shader code. If the shader source wasn't loaded, the compiler might be trying to compile an empty string, which obviously won't include the mandatory assignment to gl_Position (a requirement for all vertex shaders).

Fixes to Try

  1. Fix the File Path Issue

    • Don't remove slashes from your file paths! Use either forward slashes (/) or double backslashes (\\) in your code (Windows supports both). For example, if your shader is in a shaders folder, keep the path as "shaders/vertex.glsl".
    • Check your program's working directory: If you're using an IDE like Visual Studio, go to Project Properties → Debug → Working Directory and make sure it's set to the folder where your shaders and resources are stored (often the project root directory).
    • Re-add the filesystem.h include: The original Learn OpenGL code uses this to handle cross-platform path resolution. Removing it might have broken the code's ability to locate files correctly, especially if you're relying on relative paths.
  2. Ensure Your Vertex Shader is Correct
    Once you fix the file loading, double-check your vertex shader code to make sure it explicitly assigns to gl_Position. Every valid vertex shader needs this—something like:

    #version 330 core
    layout (location = 0) in vec3 aPos;
    
    uniform mat4 model;
    uniform mat4 view;
    uniform mat4 projection;
    
    void main()
    {
        gl_Position = projection * view * model * vec4(aPos, 1.0);
    }
    

    If this line is missing or commented out, you'll hit that C5145 error even if the file loads correctly.

Wrap-Up

Start with fixing the shader file path and working directory issues—once your program can read the shader files properly, the linking and gl_Position errors should resolve themselves (assuming your shader code is correct, which it should be if you're following the Learn OpenGL chapter closely).

内容的提问来源于stack exchange,提问作者gm9089zo

火山引擎 最新活动