forked from SirDifferential/instrument_controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject3d.cpp
More file actions
141 lines (122 loc) · 3.54 KB
/
object3d.cpp
File metadata and controls
141 lines (122 loc) · 3.54 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
#include "object3d.hpp"
#include "objLoader.hpp"
#include "renderer.hpp"
#include "toolbox.hpp"
// Create a new 3D Object by loading the data from a .obj file
Object3D::Object3D(std::string path)
{
name = path;
loadModel(path);
location.x = 1.0;
location.y = 1.0;
location.z = 0.0;
rotation = 0;
scale = 40.0f;
printStats();
}
// Generate a flat plane with n subdivisions
Object3D::Object3D(int n)
{
name = "Procedurally generated plane with subdivisions: ";
name = tbox.combineStringAndInt(name, n);
generatePlaneWithVerticesPerRow(n);
location.x = 1.0;
location.y = 1.0f;
location.z = 0.0f;
rotation = 0.0f;
scale = 10.0f;
printStats();
}
Object3D::~Object3D()
{
}
void Object3D::printStats()
{
fprintf(stderr, "----------------\n");
fprintf(stderr, "Object: %s\n", name.c_str());
fprintf(stderr, "Vertices: %d\n", verticeCount);
fprintf(stderr, "Normals: %d\n", normals.size());
fprintf(stderr, "Indices: %d\n", indiceCount);
fprintf(stderr, "----------------\n");
}
// Call the Object Loader to load the .obj, then fill all the required
// fields. Naturally this consists of vertices, normals and indices.
// In future, include materials and animations, stuff like that
void Object3D::loadModel(std::string path)
{
std::vector<std::vector<double> > data;
data = objectLoader.readObjectFile(path);
vertices = data.at(0);
normals = data.at(1);
indices = tbox.doubleVectorToIntVector(data.at(2));
verticeCount = vertices.size();
indiceCount = indices.size();
}
int getVerticesCount(int width, int height)
{
return width * height * 3;
}
int getIndicesCount(int width, int height)
{
return (width*height) + (width-1)*(height-2);
}
// Influenced by http://stackoverflow.com/questions/5915753/generate-a-plane-with-triangle-strips
void Object3D::generatePlaneWithVerticesPerRow(int n)
{
int width = n;
int height = n;
float spacing = 1.0f;
// Generate the vertices of the grid
for (int row = 0; row < width; row++)
{
for (int column = 0; column < height; column++)
{
vertices.push_back(float(column)*spacing);
vertices.push_back(0.0f);
vertices.push_back(float(row)*spacing);
normals.push_back(float(column)*spacing);
normals.push_back(0.0f);
normals.push_back(float(column)*spacing);
}
}
// Figure out the indices
for (int row = 0; row < height-1; row++ )
{
if ((row & 1) == 0)
{ // even rows
for (int col = 0; col < width; col++)
{
indices.push_back(col + row * width);
indices.push_back(col + (row+1) * width);
}
} else
{ // odd rows
for (int col = width-1; col > 0; col--)
{
indices.push_back(col + (row+1) * width);
indices.push_back(col - 1 + + row * width);
}
}
}
verticeCount = vertices.size();
indiceCount = indices.size();
}
// Set the location to which the object is rendered
void Object3D::setLocation(Vector3 in)
{
location = in;
}
// Set the rotation at which the object is rendered
void Object3D::setRotation(float in)
{
rotation = in;
}
// Call the renderer with all our data
void Object3D::render()
{
rotation += 1.0f;
renderer.renderObject(GL_TRIANGLES, indiceCount, GL_UNSIGNED_INT, indices, vertices, normals, location, rotation, scale);
}
void Object3D::applyHeightMap(std::shared_ptr<Sprite> spritePtr)
{
}