forked from Zache51/DV1541_ChrOskZac
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderObject.cpp
More file actions
154 lines (118 loc) · 3.17 KB
/
RenderObject.cpp
File metadata and controls
154 lines (118 loc) · 3.17 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "RenderObject.hpp"
#define GLM_FORCE_RADIANS
#include <glm\glm.hpp>
#include <glm\gtc\matrix_transform.hpp>
#define BUFFER_OFFSET(i) ((char *)nullptr + (i))
renderObject::renderObject()
{
modelMatrix = glm::mat4(1.0);
generated = false;
}
void renderObject::genBuffer(GLuint shader)
{
struct TriangleVertex
{
float x, y, z;
float r, g, b;
};
TriangleVertex tri[1];
glGenBuffers(1, &vBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(tri), tri, GL_DYNAMIC_DRAW);
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(tri), tri, GL_DYNAMIC_DRAW);
glGenVertexArrays(1, &vArray);
glBindVertexArray(vArray);
glEnableVertexAttribArray(0); //the vertex attribute object will remember its enabled attributes
glEnableVertexAttribArray(1);
/// this should be moved out from this class
/// as it is bound to shader, and can be used across multiple objects
GLuint vertexPos = glGetAttribLocation(shader, "vertex_position");
glVertexAttribPointer(vertexPos, 3, GL_FLOAT, GL_FALSE, sizeof(TriangleVertex), BUFFER_OFFSET(0));
GLuint vertexColor = glGetAttribLocation(shader, "vertex_color");
glVertexAttribPointer(vertexColor, 3, GL_FLOAT, GL_FALSE, sizeof(TriangleVertex), BUFFER_OFFSET(sizeof(float) * 3));
///
generated = true;
fillBuffer();
}
void renderObject::update()
{
glm::mat4 rotMatrix = glm::mat4(
cos((glm::pi<float>() / 180)*ry), 0.0f, -sin((glm::pi<float>() / 180)*ry), 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
sin((glm::pi<float>() / 180)*ry), 0.0f, cos((glm::pi<float>() / 180)*ry), 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
modelMatrix = glm::translate(glm::mat4(1.0), glm::vec3(0, 0, -2.0));
modelMatrix = modelMatrix * rotMatrix;
ry += 1.0f;
}
const GLfloat * renderObject::getModelMatrix() const
{
return &modelMatrix[0][0];
}
void renderObject::fillBuffer()
{
if (generated)
{
struct TriangleVertex
{
float x, y, z;
float r, g, b;
};
TriangleVertex tri[8] =
{
-0.500000, -0.500000, 0.500000,
1, 1, 1,
-0.500000, -0.500000, -0.500000,
1, 0, 1,
0.500000, -0.500000, -0.500000,
1, 1, 0,
0.500000, -0.500000, 0.500000,
0, 1, 1,
-0.500000, 0.500000, 0.500000,
0, 0, 1,
-0.500000, 0.500000, -0.500000,
0, 1, 0,
0.500000, 0.500000, -0.500000,
1, 0, 0,
0.500000, 0.500000, 0.500000,
0, 0, 0,
};
struct Ind
{
GLushort v1, v2, v3;
};
Ind index[12] =
{
5, 1, 0,
6, 2, 1,
7, 3, 2,
4, 0, 3,
1, 2, 3,
6, 5, 4,
4, 5, 0,
5, 6, 1,
6, 7, 2,
7, 4, 3,
0, 1, 3,
7, 6, 4
};
glBindBuffer(GL_ARRAY_BUFFER, vBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(tri), tri, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(index), index, GL_DYNAMIC_DRAW);
glFlush();
}
}
renderObject::~renderObject()
{
}
void renderObject::render()
{
glBindBuffer(GL_ARRAY_BUFFER, vBuffer);
glBindVertexArray(vArray);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
// draw points 0-3 from the currently bound VAO with current in-use shader
glDrawElements(GL_TRIANGLES, 3*12, GL_UNSIGNED_SHORT, (void*)0);
}