我写了一个小程序,用顶点缓冲区显示一个简单的三角形。对于使用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;
}
答案 0 :(得分:4)
您正在为渲染选择OpenGL核心配置文件:
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
您的代码缺少许多符合Core Profile标准的内容:
glCreateProgram
,glCreateShader
,glShaderSource
,glCompileShader
,{{1}等来电},glAttachShader
。您应该能够在线和书籍中找到材料。glLinkProgram
和glGenVertexArrays
。