从 YouTube 复制的 OpenGL 着色器中的语法错误
Posted
技术标签:
【中文标题】从 YouTube 复制的 OpenGL 着色器中的语法错误【英文标题】:Syntax error in OpenGl shader that is copied from YouTube 【发布时间】:2020-05-07 21:53:17 【问题描述】:我尝试学习 OpenGL,现在我想在黑色背景上用顶点和着色器制作一个简单的红色三角形。我复制了代码from that YouTube video. 我已经更改了视图。这是我的代码:
#include "prec.h"
struct Vector2
float x, y;
;
struct TrianglePos
Vector2 a, b, c;
;
static unsigned int CompileShader(unsigned int type, const std::string& source)
unsigned int id = glCreateShader(type);
const char* src = source.c_str();
glShaderSource(id, 1, &src, nullptr);
glCompileShader(id);
int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (!result)
int length;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
char* message = (char*)alloca(length * sizeof(char));
glGetShaderInfoLog(id, length, &length, message);
std::cout << "Failed to compile shader, message: " << message << std::endl;
glDeleteShader(id);
return 0;
return id;
static unsigned int createShader(const std::string& vertexShader, const std::string& fragmentShader)
unsigned int program = glCreateProgram();
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glValidateProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
return program;
int main()
if (!glfwInit())
return -1;
GLFWwindow *window = glfwCreateWindow(640, 480, "Sand Box GL", NULL, NULL);
if (!window)
std::cout << "problems with window" << std::endl;
glfwTerminate();
return -1;
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (GLEW_OK != glewInit())
std::cout << "something with glew went wrong" << std::endl;
return -1;
TrianglePos trianglePos =
-0.5f, -0.5f,
0.0f, 0.5f,
0.5f, -0.5f
;
unsigned int buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(trianglePos), &trianglePos, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), 0);
std::string vertexShader =
"#version 400 core\n"
"\n"
"layout(location = 0) in vec4 position;\n"
"\n"
"void main() \n"
"\n"
" gl_Position = position;\n"
"\n";
std::string fragmentShader =
"#version 400 core\n"
"\n"
"layout(location = 0) out vec4 color;\n"
"\n"
"void main()\n"
"\n"
" color = vec4(1.0, 0.0, 0.0, 1.0);\n"
"\n";
const char *versionGL;
versionGL = (char *) (glGetString(GL_VERSION));
std::cout << "openGl version: " << versionGL << std::endl;
if(GL_VERSION_4_0)
std::cout << "opengl 4.0 supported" << std::endl;
unsigned int shader = createShader(vertexShader, fragmentShader);
glUseProgram(shader);
while (!glfwWindowShouldClose(window))
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
glfwTerminate();
return 0;
这是预编译的头文件(prec.h):#pragma once
#ifndef GLEWINIT_PREC_H
#define GLEWINIT_PREC_H
#endif //GLEWINIT_PREC_H
#include "GL/glew.h"
#include <GLFW/glfw3.h>
#include <iostream>
程序将其打印到控制台(“OpenGL 4.0 supported”表示GL_VERSION_4_0 == true
):
openGl version: 2.1 INTEL-14.5.22
opengl 4.0 supported
当我尝试运行它时,我从顶点和片段着色器的着色器编译器中收到以下错误消息:
ERROR: 0:1: '' : version '400' is not supported
ERROR: 0:1: '' : syntax error: #version
ERROR: 0:3: 'layout' : syntax error: syntax error
当我将#version 400 core
更改为#version 120
时,我只收到layout
的语法错误。因此,我认为我用 glew 搞砸了一些事情。我可以尝试什么?
【问题讨论】:
我不使用 GLEW,但我敢打赌GL_VERSION_4_0
是一个编译时定义,它告诉 GLEW 将尝试加载哪个版本。它没有说明硬件实际支持的 OpenGL 版本。所以可能 OpenGL 2.1 是您的硬件支持的最佳版本。
由于语法错误,我该怎么办?
按老方法做,没有layout
限定符。
【参考方案1】:
您的系统不支持 OpenGL 4.0。它只支持 OpenGL 2.1。查看输出
openGl version: 2.1 INTEL-14.5.22
GLSL 1.20 对应 OpenGL 2.1。降级着色器:
顶点着色器
#version 120
attribute vec4 position;
void main()
gl_Position = position;
片段着色器
#version 120
void main()
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
分别使用Raw string literals(C++11):
std::string vertexShader =
R"(#version 120
attribute vec4 position;
void main()
gl_Position = position;
)";
std::string fragmentShader =
R"(#version 120
void main()
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
)";
【讨论】:
对我来说,原始字符串不起作用。但是使用正常的字符串它可以正常工作,非常感谢 Rabbid76! @GianLaager 谢谢。别客气。自 C++11 起支持原始字符串文字以上是关于从 YouTube 复制的 OpenGL 着色器中的语法错误的主要内容,如果未能解决你的问题,请参考以下文章