我用visual studio 2022写openGL程序,使用了GLFW和GLEW库。在运行过程中,程序总是在运行一小会儿以后出现异常退出。
报错信息:Exception thrown at 0x79E1A369 (igxelpicd32.dll) in openGL_VS.exe: 0xC0000005: Access violation writing location 0x04AE0000.
#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include <iostream>
using namespace std;
int main(void)
{
GLFWwindow* window;
/* Initialize the library GLFW */
if (!glfwInit())
{
cout << "Failed to initialize GLFW" << endl;
return -1;
}
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
cout << "Failed to create window using GLFW" << endl;
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Initialize the library GLEW */
if (glewInit() != GLEW_OK) {
cout << "Failed to initialize GLEW" << endl;
glfwTerminate();
return -1;
}
float vertex[] = {
0.0f, 0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f
};
unsigned int VAO,VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float)/*Stride*/, (void*)0/*Pointer*/);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
异常发生的位置是
glfwSwapBuffers(window);
这句代码
退出之前能够正常绘制图形:
请教一下这是怎么回事?