gpt4 book ai didi

c++ - glCreateShader 在 OSX 上抛出异常,为什么?

转载 作者:行者123 更新时间:2023-11-30 00:47:24 24 4
gpt4 key购买 nike

我有一些代码,我直接按照教程生成三角形。作者在 Windows 中编写他的代码,但他说这也可以在 OSX 中完成。我能够让程序编译,但它在遇到 glCreateShader 时抛出异常。我到处都看过,但我不知道出了什么问题。这一定是一些新手错误。有谁知道可能出了什么问题?

#include <iostream>
#include <string>
#include <Dunjun/Common.hpp>
#include <GL/glew.h>
#include <OpenGL/glext.h>
#include <OpenGL/gl.h>
#include <GLFW/glfw3.h>

#define GLOBAL static
#define internal static
#define LOCAL_PERSIST static

#define float32 float
#define float64 double

GLOBAL const int g_windowWidth = 854;
GLOBAL const int g_windowHeight = 480;
GLOBAL const char* g_gameTitle = "Dunjun";

GLFWwindow *toggleFullScreenWindow(GLFWwindow *window, int key);

void setColor(float32 red, float32 blue, float32 green, float32 alpha);

bool toggleExit(GLFWwindow *window, bool isRunning);

GLFWwindow* initialize_window(int width, int height, const char* title);

void init_glfw();

void init_glew();

void glfwHints();

int main(int argc, char **argv) {

GLFWwindow *window = initialize_window(g_windowWidth, g_windowHeight, g_gameTitle);

float vertices[] = {
+0.0f, -0.5f, //vertex 1
-0.5f, -0.5f, //vertex 2
+0.5f, -0.5f //vertex 3
};

const char* vertexShaderText = {
"version 120\n"
"\n"
"attribute vec2 position"
"void main()"
"{"
" gl_position = vec4(position, 0.0, 1.0);"
"}"
};

const char* framentShaderText = {
"version 120\n"
"\n"
"void main()"
"{"
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);"
"}"
};

GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderText, nullptr);
glCompileShader(vertexShader);

GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &vertexShaderText, nullptr);
glCompileShader(vertexShader);

GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);

glBindAttribLocation(shaderProgram, 0, "position");

glLinkProgram(shaderProgram);

glUseProgram(shaderProgram);




GLuint vertexBufferObject;
glGenBuffers(1, &vertexBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
/*
* GL_STATIC_DRAW Things that are static
* GL_DYNAMIC_DRAW Things that are changed but not to often
* GL_STREAM_DRAW THings that change all the time.
*/

bool running = true;
/* Loop until the user closes the window */
while (running) {

/* Render here */
setColor(0.5f, 0.69f, 1.0f, 1.0f);

//draw things
{
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
}

/*Swap front and back buffers */
glfwSwapBuffers(window);

/*poll for and process events*/
glfwPollEvents();

running = toggleExit(window, running);

window = toggleFullScreenWindow(window, GLFW_KEY_F11);
}

glfwTerminate();
return EXIT_SUCCESS;
}




void glfwHints(){
glfwWindowHint(GLFW_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_VERSION_MINOR, 1);
}


void setColor(float32 red, float32 blue, float32 green, float32 alpha){
glClearColor(red, green, blue, alpha);
glClear(GL_COLOR_BUFFER_BIT);
}

bool toggleExit(GLFWwindow *window, bool isRunning) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) ||
glfwWindowShouldClose(window)) {
isRunning = false;
}

return isRunning;
}

GLFWwindow* initialize_window(int width, int height, const char* title){

init_glfw();

/* Create a windowed mode window and its OpenGL context */
glfwHints();
GLFWwindow *window = glfwCreateWindow(width, height, title, nullptr, nullptr);
if (!window) {
glfwTerminate();
exit(EXIT_FAILURE);
}

glfwMakeContextCurrent(window);
init_glew();
return window;
}

GLFWwindow *toggleFullScreenWindow(GLFWwindow *window, int key) {
if (glfwGetKey(window, key)) {
LOCAL_PERSIST bool isFullScreen = false;
isFullScreen = !isFullScreen;

GLFWwindow *newWindow;
if (isFullScreen) {
int count;
const GLFWvidmode *modes = glfwGetVideoModes(glfwGetPrimaryMonitor(), &count);
int monitorHeight = modes[count - 1].height;
int monitorWidth = modes[count - 1].width;
newWindow = glfwCreateWindow(monitorWidth, monitorHeight, g_gameTitle, glfwGetPrimaryMonitor(), window);
} else {
newWindow = glfwCreateWindow(g_windowWidth, g_windowHeight, g_gameTitle, nullptr, window);
}
glfwDestroyWindow(window);
glfwMakeContextCurrent(newWindow);
return newWindow;


}
}

void init_glew(){
if(!glewInit()){
std :: cout << "glew failed to init!";
exit(EXIT_FAILURE);
}
}

void init_glfw(){
if (!glfwInit()) {
std :: cout << "glfw failed to init!";
exit(EXIT_FAILURE);
}
}

更新:我已经编辑了代码以包含建议的更改,但 glewInit() 返回一个错误的值。还有什么可能是错的。

最佳答案

您必须决定是否要使用 GLEW,然后始终如一地坚持使用。 Mac OS 不需要它,我建议避免使用它。但是有些人仍然喜欢使用它,所以这是您的选择。

你现在拥有的是:

#include <GL/glew.h>
#ifndef __APPLE__
#include <OpenGL/glext.h>
#include <OpenGL/gl.h>
#endif
...
#ifndef __APPLE__
if(!glewInit()){
exit(EXIT_FAILURE);
}
#endif

您包含了 GLEW header ,但没有初始化 GLEW。 GLEW header 将包含 OpenGL 入口点的声明,但它们是函数指针,在 GLEW 初始化之前它们将为空。因此,如果您之后调用 glCreateShader(),它将是一个空函数指针。

要更正此问题,您需要包含 native OpenGL header ,其中包含实际 OpenGL 入口点的声明,而不仅仅是函数指针:

#ifdef __APPLE__
#include <OpenGL/gl.h>
#else
#include <GL/glew.h>
#endif

关于c++ - glCreateShader 在 OSX 上抛出异常,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34984208/

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