gpt4 book ai didi

c++ - 在 Mac 上使用 OpenGL 编译着色器时出现 "version not supported"错误

转载 作者:太空宇宙 更新时间:2023-11-04 12:33:15 24 4
gpt4 key购买 nike

我最近学习了一个关于 OpenGL (TheChernoProject) 的在线教程,在我真正开始编写着色器之前,一切都非常顺利。当我编译我的代码时,它运行了,但屏幕上没有任何显示,尽管应该有一个红色三角形。

我正在终端中的 MacOS 10.14.6 上编写这段代码。我已经下载了 glfw 和 glew,并且正在使用以下命令编译代码:

clang++  -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo -lglfw -lglew main.cpp -o main.o

我已经尝试用#version 440 核心替换#version 330 核心,因为我的 Mac 支持它的最新版本,但无济于事。

这是我的代码:

#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

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 "<<(type==GL_VERTEX_SHADER?"vertex":"fragment")<<" shader"<<std::endl;
std::cout<<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(void){
GLFWwindow* window;

if(!glfwInit())return -1;
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE );
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );

window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window){
glfwTerminate();
return -1;
}

glfwMakeContextCurrent(window);
if(glewInit() != GLEW_OK)return -1;

float positions[6] = {
-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, 6 * sizeof(float), positions, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0);

std::string fragmentShader =
"#version 330 core\n"
"layout(location = 0) out vec4 color;\n"
"void main(){\n"
" color = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";

std::string vertexShader =
"#version 330 core\n"
"layout(location = 0) in vec4 position;\n"
"void main(){\n"
" gl_Position = position;\n"
"}\n";

unsigned int shader = CreateShader(vertexShader, fragmentShader);
glUseProgram(shader);

glBindBuffer(GL_ARRAY_BUFFER, 0);

while (!glfwWindowShouldClose(window)){

glClear(GL_COLOR_BUFFER_BIT);

glDrawArrays(GL_TRIANGLES, 0, 3);

glfwSwapBuffers(window);

glfwPollEvents();
}
glfwTerminate();
return 0;
}

最佳答案

Ripi2 有,你需要vertex array object (VAO)绑定(bind)到核心上下文中呈现任何:

# Mesa on Linux is about as picky as Apple's OpenGL implementation :)
$ g++ `pkg-config --cflags --libs glfw3 glew` main.cpp
$ MESA_DEBUG=1 ./a.out
Mesa: User error: GL_INVALID_OPERATION in glVertexAttribPointer(no array object bound)
Mesa: User error: GL_INVALID_OPERATION in glDrawArrays(no VAO bound)

例如:

GLuint vao = 0;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
// glVertexAttribPointer()/glEnableVertexAttribArray()/glDraw*() calls here

一起:

#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

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 "<<(type==GL_VERTEX_SHADER?"vertex":"fragment")<<" shader"<<std::endl;
std::cout<<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(void){
GLFWwindow* window;

if(!glfwInit())return -1;
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE );
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );

window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window){
glfwTerminate();
return -1;
}

glfwMakeContextCurrent(window);
if(glewInit() != GLEW_OK)return -1;

float positions[6] = {
-0.5f,-0.5f,
0.0f, 0.5f,
0.5f,-0.5f
};

GLuint vao = 0;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );

unsigned int buffer;
glGenBuffers(1,&buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0);

std::string fragmentShader =
"#version 330 core\n"
"layout(location = 0) out vec4 color;\n"
"void main(){\n"
" color = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";

std::string vertexShader =
"#version 330 core\n"
"layout(location = 0) in vec4 position;\n"
"void main(){\n"
" gl_Position = position;\n"
"}\n";

unsigned int shader = CreateShader(vertexShader, fragmentShader);
glUseProgram(shader);

glBindBuffer(GL_ARRAY_BUFFER, 0);

while (!glfwWindowShouldClose(window)){

glClear(GL_COLOR_BUFFER_BIT);

glDrawArrays(GL_TRIANGLES, 0, 3);

glfwSwapBuffers(window);

glfwPollEvents();
}
glfwTerminate();
return 0;
}

关于c++ - 在 Mac 上使用 OpenGL 编译着色器时出现 "version not supported"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57778498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com