GLEW不在MSYS2上定位OpenGL函数 [英] GLEW not locating OpenGL functions on MSYS2

查看:99
本文介绍了GLEW不在MSYS2上定位OpenGL函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在笔记本电脑上使用OpenGL.作为最快的方法,我安装了MSYS2.我安装了 mingw-w64-x86_64-gcc mingw-w64-x86_64-glew mingw-w64-x86_64-glfw3 等.我认为我安装了所有必需的软件包.我调用OpenGL例程后,我的程序就给我分段错误.

I am trying to play with couple of OpenGL on my laptop. As the fastest method, I installed an MSYS2. I installed mingw-w64-x86_64-gcc, mingw-w64-x86_64-glew, mingw-w64-x86_64-glfw3 etc. I think I installed all the required packages. My programs are giving me segmentation fault as soon as I call an OpenGL routine.

作为一个最小的工作示例,我从 LearnOpenGL.com <复制了 Hello,Triangle 示例/a>,以使事情变小.我在这个程序中也遇到了同样的问题.

To make a minimal working example, I am copying the Hello, Triangle example from LearnOpenGL.com so that things will be kept small. I get the same problem in this program as well.

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

void processInput(GLFWwindow *window);

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

const char *vertexShaderSource = "#version 330 core\n"
    "layout (location = 0) in vec3 aPos;\n"
    "void main()\n"
    "{\n"
    "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
    "}\0";
const char *fragmentShaderSource = "#version 330 core\n"
    "out vec4 FragColor;\n"
    "void main()\n"
    "{\n"
    "   FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
    "}\n\0";

int main()
{
    // glfw: initialize and configure
    // ------------------------------
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif

    // glfw window creation
    // --------------------
    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    std::cout << "setting opengl...\n" << std::flush;

    int vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);
    // check for shader compile errors
    int success;
    char infoLog[512];
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    // fragment shader
    int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);
    // check for shader compile errors
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    // link shaders
    int shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    // check for linking errors
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
    if (!success) {
        glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
    }
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    float vertices[] = {
        -0.5f, -0.5f, 0.0f, // left  
         0.5f, -0.5f, 0.0f, // right 
         0.0f,  0.5f, 0.0f  // top   
    }; 

    unsigned int VBO, VAO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);

    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

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

    glBindBuffer(GL_ARRAY_BUFFER, 0); 
    glBindVertexArray(0); 

    while (!glfwWindowShouldClose(window))
    {
        processInput(window);

        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // draw our first triangle
        glUseProgram(shaderProgram);
        glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
        glDrawArrays(GL_TRIANGLES, 0, 3);
        // glBindVertexArray(0); // no need to unbind it every time 

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);

    glfwTerminate();
    return 0;
}

void processInput(GLFWwindow *window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
} 

我刚刚添加了打印声明

std::cout << "setting opengl...\n" << std::flush;

现在我可以看到此打印了.但是,如果我尝试在下面的任何一行上打印相同的内容,则看不到该打印内容,因为OpenGL调用给我造成了段错误.我只能进行的OpenGL调用是 glGetString ,并且可以在OpenGL 3.3中打印该版本.

and I can see this print now. But if I try to print the same thing on any of the line below that, I can't see the print since OpenGL calls give me seg fault. Only OpenGL call I can make is glGetString and I can print that my version in OpenGL 3.3.

作为旁注,我将其放在 main.cpp 中并编译为

As a side note, I am putting this in main.cpp and compile as

 g++ main.cpp -o main -lglfw3 -lglew32 -lglu32 -lopengl32

可以正常播放,没有任何问题.

which compies without any problems.

推荐答案

必须使用 GLEW 库在OpenGL上下文已由 glfwMakeContextCurrent 变为当前值之后,由 glewInit 初始化.请参见初始化GLEW .

The GLEW library has to be initialized, by glewInit, after the OpenGL context has become current by glfwMakeContextCurrent. See Initializing GLEW.

添加以下代码,以解决您的问题:

Add the following code, to solve your issue:

glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
    // error handling
}

这篇关于GLEW不在MSYS2上定位OpenGL函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆