-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube.cpp
127 lines (101 loc) · 3.93 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "cube.hpp"
#include "vertex_format.hpp"
#include <cstddef>
using namespace plush;
const glm::vec3 LEFT_BOTTOM_FRONT (-.5f, -.5f, .5f);
const glm::vec3 RIGHT_BOTTOM_FRONT( .5f, -.5f, .5f);
const glm::vec3 RIGHT_TOP_FRONT ( .5f, .5f, .5f);
const glm::vec3 LEFT_TOP_FRONT (-.5f, .5f, .5f);
const glm::vec3 LEFT_BOTTOM_BACK (-.5f, -.5f, -.5f);
const glm::vec3 RIGHT_BOTTOM_BACK ( .5f, -.5f, -.5f);
const glm::vec3 RIGHT_TOP_BACK ( .5f, .5f, -.5f);
const glm::vec3 LEFT_TOP_BACK (-.5f, .5f, -.5f);
const glm::vec3 NORMAL_LEFT (-1.0f, 0.0f, 0.0f);
const glm::vec3 NORMAL_RIGHT ( 1.0f, 0.0f, 0.0f);
const glm::vec3 NORMAL_UP ( 0.0f, 1.0f, 0.0f);
const glm::vec3 NORMAL_DOWN ( 0.0f, -1.0f, 0.0f);
const glm::vec3 NORMAL_FRONT ( 0.0f, 0.0f, 1.0f);
const glm::vec3 NORMAL_BACK ( 0.0f, 0.0f, -1.0f);
static const TexturedVertex cubeVertices[] = {
// front side
{ LEFT_BOTTOM_FRONT, NORMAL_FRONT, glm::vec2(0.0f, 0.0f), },
{ RIGHT_BOTTOM_FRONT, NORMAL_FRONT, glm::vec2(0.0f, 1.0f), },
{ RIGHT_TOP_FRONT, NORMAL_FRONT, glm::vec2(1.0f, 1.0f), },
{ LEFT_TOP_FRONT, NORMAL_FRONT, glm::vec2(1.0f, 1.0f), },
// back side
{ LEFT_BOTTOM_BACK, NORMAL_BACK, glm::vec2(0.0f, 0.0f), },
{ RIGHT_BOTTOM_BACK, NORMAL_BACK, glm::vec2(0.0f, 0.0f), },
{ RIGHT_TOP_BACK, NORMAL_BACK, glm::vec2(0.0f, 0.0f), },
{ LEFT_TOP_BACK, NORMAL_BACK, glm::vec2(0.0f, 0.0f), },
// left side
{ LEFT_BOTTOM_FRONT, NORMAL_LEFT, },
{ LEFT_BOTTOM_BACK, NORMAL_LEFT, },
{ LEFT_TOP_BACK, NORMAL_LEFT, },
{ LEFT_TOP_FRONT, NORMAL_LEFT, },
// right side
{ RIGHT_BOTTOM_FRONT, NORMAL_RIGHT, },
{ RIGHT_BOTTOM_BACK, NORMAL_RIGHT, },
{ RIGHT_TOP_BACK, NORMAL_RIGHT, },
{ RIGHT_TOP_FRONT, NORMAL_RIGHT, },
// bottom side
{ LEFT_BOTTOM_FRONT, NORMAL_DOWN, },
{ RIGHT_BOTTOM_FRONT, NORMAL_DOWN, },
{ RIGHT_BOTTOM_BACK, NORMAL_DOWN, },
{ LEFT_BOTTOM_BACK, NORMAL_DOWN, },
// top side
{ LEFT_TOP_FRONT, NORMAL_UP, },
{ RIGHT_TOP_FRONT, NORMAL_UP, },
{ RIGHT_TOP_BACK, NORMAL_UP, },
{ LEFT_TOP_BACK, NORMAL_UP, },
};
static const short cubeIndices[] = {
0, 1, 2, 0, 2, 3, // front side
4, 5, 6, 4, 6, 7, // back side
8, 9, 10, 8, 10, 11, // left side
12, 13, 14, 12, 14, 15, // right side
16, 17, 18, 16, 18, 19, // bottom side
20, 21, 22, 20, 22, 23, // top side
};
/////////////////////////////////////////////////////////////////////////////
// CubeModel
/////////////////////////////////////////////////////////////////////////////
CubeModel *CubeModel::s_instance = nullptr;
CubeModel::CubeModel()
: coordsVBO(GL_STATIC_DRAW), indicesVBO(GL_STATIC_DRAW)
{
GLBinder<VAO> vaoBinder(vao);
coordsVBO.data(sizeof(cubeVertices), cubeVertices);
VAO::vertexAttribPointer("vertexCoord", coordsVBO, 3, GL_FLOAT, false, sizeof(TexturedVertex), offsetof(TexturedVertex, coord));
VAO::enableVertexAttribArray("vertexCoord");
VAO::vertexAttribPointer("vertexNormal", coordsVBO, 3, GL_FLOAT, false, sizeof(TexturedVertex), offsetof(TexturedVertex, normal));
VAO::enableVertexAttribArray("vertexNormal");
indicesVBO.data(sizeof(cubeIndices), cubeIndices);
}
CubeModel::~CubeModel()
{
}
CubeModel *CubeModel::instance()
{
if (!s_instance)
s_instance = new CubeModel();
return s_instance;
}
void CubeModel::render()
{
vao.bind();
glDrawElements(GL_TRIANGLES, sizeof(cubeIndices)/3*sizeof(float), GL_UNSIGNED_SHORT, (GLvoid*) 0);
}
/////////////////////////////////////////////////////////////////////////////
// Cube
/////////////////////////////////////////////////////////////////////////////
Cube::Cube()
{
}
void Cube::prepare()
{
(void) CubeModel::instance();
}
void Cube::render()
{
CubeModel::instance()->render();
}