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

最佳答案

您必须决定是否要使用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之前,该指针将为null。因此,如果您稍后再调用glCreateShader(),它将是一个空函数指针。

要更正此问题,您需要包括本机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/

10-11 20:56