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

时间:2016-01-25 02:25:03

标签: c++ macos opengl

我有一些代码,我直接按照教程生成三角形。作者在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()返回falsey值。还有什么可能是错的。

2 个答案:

答案 0 :(得分:5)

您必须决定是否要使用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标头,但未初始化GLEW。 GLEW头将包含OpenGL入口点的声明,但它们是函数指针,在GLEW初始化之前它将为null。因此,如果您之后调用glCreateShader(),它将是一个空函数指针。

要解决此问题,您需要包含本机OpenGL标头,其中包含实际OpenGL入口点的声明,而不仅仅是函数指针:

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

答案 1 :(得分:1)

在初始化GLEW之前,必须将

glfwMakeContextCurrent(window);称为。如果您在glewInit()中交换initialize_window()main()的顺序,那么一切都应该有效