-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject3D.cpp
More file actions
117 lines (96 loc) · 3.52 KB
/
Copy pathobject3D.cpp
File metadata and controls
117 lines (96 loc) · 3.52 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
// Custom Libraries
#include "object3D.h"
// Standrad Libraries
Object3D::Object3D() : Object()
{
this->emit_mode = 0;
this->draw_mode = 0;
this->attribute_v_coord = 0;
this->attribute_v_colours = 1;
this->attribute_v_normal = 2;
this->attribute_v_uvs = 3;
this->vertexBufferObj = NULL;
this->indexBufferObj = NULL;
this->colourBufferObj = NULL;
this->normalBufferObj = NULL;
this->uvBufferObj = NULL;
}
Object3D::~Object3D()
{
this->vertexBufferObj = NULL;
this->indexBufferObj = NULL;
this->colourBufferObj = NULL;
this->normalBufferObj = NULL;
this->uvBufferObj = NULL;
this->vertex_positions.clear();
this->indices.clear();
this->vertex_colours.clear();
this->vertex_normals.clear();
this->texture_coords.clear();
}
void Object3D::makeObject()
{
/* Create the vertex buffer for the object */
glGenBuffers(1, &vertexBufferObj);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObj);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * this->vertex_positions.size(), &this->vertex_positions[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
/* Create the colours buffer for the object */
glGenBuffers(1, &colourBufferObj);
glBindBuffer(GL_ARRAY_BUFFER, colourBufferObj);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec4) * this->vertex_colours.size(), &this->vertex_colours[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
/* Create the normals buffer for the object */
glGenBuffers(1, &normalBufferObj);
glBindBuffer(GL_ARRAY_BUFFER, normalBufferObj);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * this->vertex_normals.size(), &this->vertex_normals[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
/* Create the texture coordinate buffer for the object */
glGenBuffers(1, &uvBufferObj);
glBindBuffer(GL_ARRAY_BUFFER, uvBufferObj);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec2) * this->texture_coords.size(), &this->texture_coords[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Object3D::drawObject(bool clockwise)
{
/* Bind object vertices. Note that this is in attribute index attribute_v_coord */
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObj);
glEnableVertexAttribArray(attribute_v_coord);
glVertexAttribPointer(attribute_v_coord, 3, GL_FLOAT, GL_FALSE, 0, 0);
/* Bind object colours. Note that this is in attribute index attribute_v_colours */
glBindBuffer(GL_ARRAY_BUFFER, colourBufferObj);
glEnableVertexAttribArray(attribute_v_colours);
glVertexAttribPointer(attribute_v_colours, 4, GL_FLOAT, GL_FALSE, 0, 0);
/* Bind object normals. Note that this is in attribute index attribute_v_normal */
glEnableVertexAttribArray(attribute_v_normal);
glBindBuffer(GL_ARRAY_BUFFER, normalBufferObj);
glVertexAttribPointer(attribute_v_normal, 3, GL_FLOAT, GL_FALSE, 0, 0);
/* Bind object texture coordinates. Note that this is in attribute index attribute_v_uvs */
glEnableVertexAttribArray(attribute_v_uvs);
glBindBuffer(GL_ARRAY_BUFFER, uvBufferObj);
glVertexAttribPointer(attribute_v_uvs, 2, GL_FLOAT, GL_FALSE, 0, 0);
// Clockwise triangle winding
glFrontFace((clockwise * GL_CW) + (!clockwise * GL_CCW));
glPointSize(5.f);
switch (this->draw_mode) {
case 0:
fill:
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
break;
case 1:
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
break;
case 2:
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
break;
default:
std::cerr << "Draw mode only has 4 modes ranging from 0-3 not " << this->draw_mode << std::endl;
goto fill;
}
}
void Object3D::setVertexColour(glm::vec4 rgba)
{
for (auto& element : this->vertex_colours) {
element = rgba;
}
}