-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCube.cpp
74 lines (65 loc) · 3.18 KB
/
Cube.cpp
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
#include "Cube.h"
GLuint create_cube_vao()
{
GLuint vao, vbo;
const float cube_verts[] = { -1.0f,-1.0f,-1.0f,
-1.0f,-1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f,-1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f, 1.0f,-1.0f,
1.0f,-1.0f, 1.0f,
-1.0f,-1.0f,-1.0f,
1.0f,-1.0f,-1.0f,
1.0f, 1.0f,-1.0f,
1.0f,-1.0f,-1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f,-1.0f,
1.0f,-1.0f, 1.0f,
-1.0f,-1.0f, 1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f,-1.0f, 1.0f,
1.0f,-1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f,-1.0f,-1.0f,
1.0f, 1.0f,-1.0f,
1.0f,-1.0f,-1.0f,
1.0f, 1.0f, 1.0f,
1.0f,-1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f,-1.0f,
-1.0f, 1.0f,-1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f,-1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f,-1.0f, 1.0f };
//generate vao id to hold the mapping from attrib variables in shader to memory locations in vbo
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo); // Generate vbo to hold vertex attributes for triangle
//binding vao means that bindbuffer, enablevertexattribarray and vertexattribpointer
// state will be remembered by vao
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo); //specify the buffer where vertex attribute data is stored
//upload from main memory to gpu memory
glBufferData(GL_ARRAY_BUFFER, sizeof(cube_verts), &cube_verts[0], GL_STATIC_DRAW);
//get the current shader program
GLint shader_program = -1;
glGetIntegerv(GL_CURRENT_PROGRAM, &shader_program);
//get a reference to an attrib variable name in a shader
GLint pos_loc = glGetAttribLocation(shader_program, "pos_attrib");
glEnableVertexAttribArray(pos_loc); //enable this attribute
//tell opengl how to get the attribute values out of the vbo (stride and offset)
glVertexAttribPointer(pos_loc, 3, GL_FLOAT, false, 0, 0);
glBindVertexArray(0); //unbind the vao
return vao;
}
void draw_cube_vao(GLuint vao)
{
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 36);
}