OpenGL 着色器不绘制

OpenGL Shaders Dont Draw

所以最近我开始看一个 udemy 诅咒,但是当我谈到着色器时,我在制作着色器或定义它们时遇到了来自行的问题:

#include iostream

#define GLEW_STATIC
#include GL/glew.h

#include GLFW/glfw3.h

const GLint WIDTH = 800, HEIGHT = 600;

//here start the shaders def/creation
const GLchar *vertexShaderSource = 
"#version 330 core\n"
"layout ( location = 0 ) in vec3 position;\n"
"void main( )\n"
"{\n"
"gl_Position = vec4( position.x, position.y, position.z, 1.0 )\n"
"}";

const GLchar *fragmentShaderSource = 
"#version 330 core\n"
"out vec4 color;\n"
"void main( )\n"
"}\n"
"color = vec4( 1.0f, 0.5f, 0.2f, 1.0f );\n"
"}";
//here it ends

//more code down here
//....
//....

然后我在控制台中得到的(其他代码在 cmd 上得到那些错误行)是:

错误::着色器::顶点::COMPILATION_FAILED 0(6):错误 C0000:语法错误,意外的“}”,需要“,”或“;”在令牌“}”

错误::着色器::片段::COMPILATION_FAILED 0(4):错误 C0000:语法错误,意外的“}”,需要“,”或“;”在令牌“}”

错误::着色器::程序::LINKING_FAILED

顶点信息

0(6) : error C0000: syntax error, unexpected '}', expecting ',' or ';' at token "}"
(0) : error C2003: incompatible options for link

片段信息

0(4) : error C0000: syntax error, unexpected '}', expecting ',' or ';' at token "}"
(0) : error C2003: incompatible options for link

我知道支持着色器不是问题,因为我实际上可以 运行 3.0 着色器,我可以在代码中定义着色器吗?

编辑: 好的,对不起,如果我的问题打扰了某人,但原因是 在第 16 行 错误是这样的: "gl_Position = vec4( position.x, position.y, position.z, 1.0 )\n"

和右边 "gl_Position = vec4( position.x, position.y, position.z, 1.0 );\n"

是这个符号; XD

并在线 23 错误是 "}\n" 和右边 "{\n" 这只是错过了点击 抱歉打扰

您的顶点着色器缺少分号,您的片段着色器打开 main 函数的大括号错误。

const GLchar *vertexShaderSource = 
"#version 330 core\n"
"layout ( location = 0 ) in vec3 position;\n"
"void main( )\n"
"{\n"
//=============================================Mistake is here↓
"gl_Position = vec4( position.x, position.y, position.z, 1.0 )\n"
//===============================================================
"}";

const GLchar *fragmentShaderSource = 
"#version 330 core\n"
"out vec4 color;\n"
"void main( )\n"
//↓Mistake======================================
"}\n"
//==============================================
"color = vec4( 1.0f, 0.5f, 0.2f, 1.0f );\n"
"}";

修正这两个错误,您的代码应该可以正常工作。

此外,您唯一需要的显式换行符是 #version 指令之后的第一个换行符。它们将在编译阶段被删除,就像在 C 或 C++ 中一样。如果您将着色器代码转储到控制台,它们 可能 使调试更容易,但它们不会影响代码的行为或性能。