我正在学习 OpenGL,由于某种原因,我绘制三角形的程序无法正常工作。这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glfw.h>
#include <glm/glm.hpp>
using namespace glm;
int main()
{
if(!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
if(!glfwOpenWindow(1080, 720, 0, 0, 0, 0, 32, 0, GLFW_WINDOW))
{
fprintf(stderr, "Failed to open GLFW window\n");
glfwTerminate();
return -1;
}
if(glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
glfwSetWindowTitle("Tutorial 01");
glfwEnable(GLFW_STICKY_KEYS);
do
{
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(
0,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glfwSwapBuffers();
}
while(glfwGetKey(GLFW_KEY_ESC) != GLFW_PRESS &&
glfwGetWindowParam(GLFW_OPENED));
}
调试日志:
Debugger name and version: GNU gdb (GDB) 7.5
At /home/mike/Programming/OpenGL Tutorials/Opening a window/main.cpp:11
At /home/mike/Programming/OpenGL Tutorials/Opening a window/main.cpp:17
At /home/mike/Programming/OpenGL Tutorials/Opening a window/main.cpp:18
At /home/mike/Programming/OpenGL Tutorials/Opening a window/main.cpp:19
At /home/mike/Programming/OpenGL Tutorials/Opening a window/main.cpp:20
At /home/mike/Programming/OpenGL Tutorials/Opening a window/main.cpp:22
At /home/mike/Programming/OpenGL Tutorials/Opening a window/main.cpp:29
At /home/mike/Programming/OpenGL Tutorials/Opening a window/main.cpp:36
Program received signal SIGSEGV, Segmentation fault.
In ?? () ()
At /home/mike/Programming/OpenGL Tutorials/Opening a window/main.cpp:36
Debugger finished with status 0