-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.cpp
More file actions
122 lines (90 loc) · 2.81 KB
/
object.cpp
File metadata and controls
122 lines (90 loc) · 2.81 KB
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
#include "object.h"
Object::Object()
{
createVertices();
}
void Object::createVertices() {
Vertices = {
{{-1.0f, 1.0f, -1.0f}, {1.0f, 1.0f, 1.0f}},
{{1.0f, 1.0f, -1.0f}, {1.0f, 1.0f, 0.0f}},
{{-1.0f, -1.0f, -1.0f}, {1.0f, 0.0f, 1.0f}},
{{1.0f, -1.0f, -1.0f}, {1.0f, 0.0f, 0.0f}},
{{-1.0f, 1.0f, 1.0f}, {0.0f, 1.0f, 1.0f}},
{{1.0f, 1.0f, 1.0f}, {0.0f, 1.0f, 0.0f}},
{{-1.0f, -1.0f, 1.0f}, {0.0f, 0.0f, 1.0f}},
{{1.0f, -1.0f, 1.0f}, {0.0f, 0.0f, 0.0f}},
};
Indices = {
0, 1, 2,
2,3,1,
1,5,7,
7,3,1,
5,4,6,
6,7,5,
4,0,2,
2,6,4,
5,4,0,
0,1,5,
2,6,7,
2,7,3,
};
}
void Object::Initialize(GLint posAttribLoc, GLint colAttribLoc) {
// Set up your VOA
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// setting the Vertex VBO
glGenBuffers(1, &VB);
glBindBuffer(GL_ARRAY_BUFFER, VB);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * Vertices.size(), &Vertices[0], GL_STATIC_DRAW);
glVertexAttribPointer(posAttribLoc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(colAttribLoc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, color));
// Setting the Index VBO
glGenBuffers(1, &IB);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IB);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * Indices.size(), &Indices[0], GL_STATIC_DRAW);
// Computing the model matrix
// Model orientation
angle = 1 * 3.1415f;
float tvec1 = glm::linearRand(-5.f, 5.f);
float tvec2 = glm::linearRand(-5.f, 5.f);
float tvec3 = glm::linearRand(-2.f, 2.f);
model = glm::translate(glm::mat4(1.0f), glm::vec3(tvec1, tvec2, tvec3));
model *= glm::rotate(glm::mat4(1.0f), angle, glm::vec3(0, 1.0f, .0f));
}
Object::~Object()
{
Vertices.clear();
Indices.clear();
}
void Object::Update(unsigned int dt)
{
model = glm::translate(model, m_speed);
//To Do: Extra credit / Grad Only
// Make the object move in a circle around the origin
}
glm::mat4 Object::GetModel()
{
return model;
}
void Object::Render(GLint posAttribLoc, GLint colAttribLoc)
{
// Bind VAO
glBindVertexArray(vao);
// Bind VBO(s)
glBindBuffer(GL_ARRAY_BUFFER, VB);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IB);
// enable the vertex attribute arrays
// this is the poistion attrib in the vertex shader
glEnableVertexAttribArray(posAttribLoc);
// this is the color attribe in the vertex shader
glEnableVertexAttribArray(colAttribLoc);
// Draw call to OpenGL
glDrawElements(GL_TRIANGLES, Indices.size(), GL_UNSIGNED_INT, 0);
// disable the vertex attributes
glDisableVertexAttribArray(posAttribLoc);
glDisableVertexAttribArray(colAttribLoc);
// unbind VBO(s) and ElementBuffer(s)
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}