上篇帖子写了怎么创建窗口,最近复习了一下,然户学了新一章节,关于buffer vao vbo的一些知识,现在只在代码中添加绑定了vao 和vbo 用的是GL_ARRAY_BUFFER 的方法
因为还没有添加shader所以三角形不会绘制出来,然后使用的是GL_STATIC_DRAW这个方式,(三角形的位置数据不会改变,经常使用,并且每次渲染调用都保持不变,因此其使用类型最好为GL_STATIC_DRAW。例如,如果有一个带有数据可能经常更改的缓冲区,则GL_DYNAMIC_DRAW的使用类型可确保显卡将数据放置在内存中,以便更快地写入。)
[C++] 纯文本查看 复制代码 #include <iostream>
#define GLEW_STATIC//告诉编译器使用static版本的glew
#include <GL/glew.h>
#include <GLFW/glfw3.h>
float vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f,0.0f,
0.0f, 0.0f, 0.0f
};
void processInput(GLFWwindow* window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
}//前置声明(c++特性)
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//Open GLFW Window
GLFWwindow* window = glfwCreateWindow(800, 600, "MY OPGLGL GAME", NULL, NULL);
if (window == NULL)
{
printf("opengl window failed.");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
//Init GLEW
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
printf("Init GLEW failed.");
glfwTerminate();
return -1;
}
glViewport(0, 0, 800, 600);
unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
while (!glfwWindowShouldClose(window))
{
processInput(window);
glClearColor(0, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
} |