我编写了一个小程序来使用顶点缓冲区显示一个简单的三角形。对于我使用 glfw 的窗口,我的环境是 Mac 10.9,XCode 5。
窗口看起来是黑色的,但三角形不是油漆。
这里是代码:
#include <GLFW/glfw3.h>
#include <OpenGL/gl.h>
#include <iostream>
int main(int argc, const char * argv[])
{
GLFWwindow* window;
if (!glfwInit())
{
return -1;
}
glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(640, 480, "Hello Triangle", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
GLfloat verts[] =
{
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f
};
//Generate a buffer id
GLuint vboID;
//Create a buffer on GPU memory
glGenBuffers(1, &vboID);
//Bind an arraybuffer to the ID
glBindBuffer(GL_ARRAY_BUFFER, vboID);
// Fill that buffer with the client vertex
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);
//Enable attributes
glEnableVertexAttribArray(0);
// Setup a pointer to the attributes
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
while (!glfwWindowShouldClose(window))
{
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwPollEvents();
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}