-
Notifications
You must be signed in to change notification settings - Fork 160
/
glfw_triangle_vao.c
109 lines (86 loc) · 3.15 KB
/
glfw_triangle_vao.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Minimal triangle example with VAO.
Uses methods:
- glGenVertexArrays
- glBindVertexArray
Expected outcome: a red triangle is drawn.
TODO: what is the advantage of using VAO compared to just VBO?
- http://stackoverflow.com/questions/11821336/what-are-vertex-array-objects
- http://gamedev.stackexchange.com/questions/99236/what-state-is-stored-in-an-opengl-vertex-array-object-vao-and-how-do-i-use-the/99238
Looks like a way to bind a bunch of things at once:
> stores all of the state needed to supply vertex data (with one minor exception noted below).
It stores the format of the vertex data as well as the Buffer Objects
What is stores:
- glBind calls
- glVertexAttribPointer and glEnableVertexAttribArray calls TODO: why not store shaders as well then, since those are bound to shaders?
VAO is a container object: it contains other objects.
https://www.opengl.org/wiki/Vertex_Specification#Vertex_Array_Object
Adapted from
https://github.com/JoeyDeVries/LearnOpenGL/blob/d5c3be70ab2b884cf2b2c94cbf73a31f632fbf47/src/1.getting_started/2.hello_triangle/hellotriangle2.cpp
Tood explanations:
- http://www.learnopengl.com/#!Getting-Started/Hello-Triangle
*/
#include "common.h"
static const GLuint WIDTH = 512;
static const GLuint HEIGHT = 512;
static const GLchar *vertex_shader_source =
"#version 330 core\n"
"in vec3 position;\n"
"void main() {\n"
" gl_Position = vec4(position, 1.0);\n"
"}\n";
static const GLchar *fragment_shader_source =
"#version 330 core\n"
"out vec4 color;\n"
"void main() {\n"
" color = vec4(1.0f, 0.0f, 0.0f, 1.0f);\n"
"}\n";
/* Passed as input to the vertex shader. */
static const GLfloat vertices[] = {
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
};
int main(void) {
GLFWwindow *window;
GLint attribute_position;
GLuint program, vbo, vao;
/* Window system. */
glfwInit();
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
window = glfwCreateWindow(WIDTH, HEIGHT, __FILE__, NULL, NULL);
glfwMakeContextCurrent(window);
glewInit();
/* Shader. */
program = common_get_shader_program(vertex_shader_source, fragment_shader_source);
attribute_position = glGetAttribLocation(program, "position");
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
/* Buffer setup. */
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(attribute_position, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
/* Draw. */
glViewport(0, 0, WIDTH, HEIGHT);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
glfwSwapBuffers(window);
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
}
/* Cleanup. */
glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao);
glDeleteProgram(program);
glfwTerminate();
return EXIT_SUCCESS;
}