GLEW 未在 MSYS2 上定位 OpenGL 函数
Posted
技术标签:
【中文标题】GLEW 未在 MSYS2 上定位 OpenGL 函数【英文标题】:GLEW not locating OpenGL functions on MSYS2 【发布时间】:2018-08-21 05:49:51 【问题描述】:我正在尝试在我的笔记本电脑上使用几个 OpenGL。作为最快的方法,我安装了一个 MSYS2。我安装了mingw-w64-x86_64-gcc
、mingw-w64-x86_64-glew
、mingw-w64-x86_64-glfw3
等。我想我安装了所有必需的软件包。一旦我调用 OpenGL 例程,我的程序就会给我分段错误。
为了做一个最小的工作示例,我从LearnOpenGL.com 复制Hello, Triangle
示例,以便保持小。我在这个程序中也遇到了同样的问题。
#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 中打印我的版本。
作为旁注,我将其放入 main.cpp
并编译为
g++ main.cpp -o main -lglfw3 -lglew32 -lglu32 -lopengl32
编译没有任何问题。
【问题讨论】:
你不应该在某个地方打电话给glewInit
吗?
【参考方案1】:
GLEW 库必须在 OpenGL 上下文由glfwMakeContextCurrent
变为当前状态后由glewInit
初始化。见Initializing GLEW。
添加以下代码,解决您的问题:
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
// error handling
【讨论】:
以上是关于GLEW 未在 MSYS2 上定位 OpenGL 函数的主要内容,如果未能解决你的问题,请参考以下文章