我不知道我有什么问题,我的代码或配置。我仔细检查了代码和 VS 设置,为我的 GPU 安装了最新的驱动程序,但仍然看到此错误。
error C0000: ... or #version 410
和 error C7548: 'layout(location)' requires "#extension GL_ARB_separate_shader_objects : enable" before use
。 https://www.youtube.com/watch?v=71BLZwRGUJE&list=PLlrATfBNZ98foTJPJ_Ev03o2oq3-GGOS2&index=12 我编写代码的视频。
代码:
图形.h
class Window
{
private:
static unsigned int CompilerShader(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);
//Error handling
int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE)
{
int lenght;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &lenght);
char* message = (char*)alloca(lenght * sizeof(char));
glGetShaderInfoLog(id, lenght, &lenght, message);
printf(message);
glDeleteShader(id);
return 0;
}
return id;
}
const std::string VertexShader = R"glsl(
#version 330 core
#extension GL_ARB_separate_shader_objects : enable
layout (location = 0) in vec3 position;
void main(void)
{
gl_Position = vec4(position, 1.0);
}
)glsl";
const std::string FragmentShader = R"glsl(
#version 330 core
#extension GL_ARB_separate_shader_objects : enable
out vec4 color;
void main(void)
{
color = vec4(1.0, 0.0, 0.0, 1.0);
}
)glsl";
static unsigned int CreateShader(const std::string& vertex, const std::string& fragment)
{
unsigned int program = glCreateProgram();
unsigned int vs = CompilerShader(GL_VERTEX_SHADER, vertex);
unsigned int fs = CompilerShader(GL_FRAGMENT_SHADER, fragment);
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glValidateProgram(program);
glDeleteShader(vs);
glDeleteShader(fs);
return program;
}
public:
GLFWwindow* window;
const char* name = "Window";
int width, height = 500;
bool open = true;
Window(const char* name, int width, int height)
{
this->height = height;
this->width = width;
this->name = name;
}
int Init()
{
if (!glfwInit())
return -1;
CreateWindow();
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK)
printf("ERROR with glew init");
unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
unsigned int shader = CreateShader(VertexShader, FragmentShader);
glUseProgram(shader);
}
void CreateWindow()
{
window = glfwCreateWindow(width, height, name, NULL, NULL);
}
void Update(Color clearColor)
{
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
open = !glfwWindowShouldClose(window);
glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);
}
void OnWindowClose()
{
glfwTerminate();
}
};
main.cpp
#include "Krapst.h"
int main(void)
{
const char* name = "Window";
Window window(name, 1280, 720);
window.Init();
Triangle t;
Color color(0, 0, 0, 1);
while (window.open)
{
window.Update(color);
t.Draw();
}
window.OnWindowClose();
return 0;
}
PS:在 Krapst.h 中,我已经包含了所有可以在 .cpp 中工作的文件。
我尝试搜索帮助,并使用 ChatGBT,但仍然无法解决该问题。就像我说的,我仔细检查了配置和代码。我正在使用 GLEW 和 GLFW。
编辑:如果我从代码中删除“Triangle t”和“t.Draw()”,一切正常。