-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCMesh.cpp
More file actions
133 lines (114 loc) · 3.57 KB
/
CMesh.cpp
File metadata and controls
133 lines (114 loc) · 3.57 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
#include "CMesh.h"
#include <iostream>
#include <assert.h>
CMesh::CMesh()
{
}
CMesh::CMesh(const CMesh& other)
{
Shapes = other.Shapes;
generateVBO();
}
CMesh::CMesh(const char* fname)
{
char* base = basename(fname);
std::string err = LoadObj(Shapes, fname, base);
free(base);
if (!err.empty())
std::cout << err << "\n";
else
printf("Loaded %s\n", fname);
generateVBO();
}
CMesh::CMesh(CSpline* spline,
const PointVector& stencil,
int numberOfDivisions,
float texscale,
const vec3& stencilscale)
{
shape_t shape;
shape.name = "Road Object";
material_t& mat = shape.material;
mat.emission[0] = mat.emission[1] = mat.emission[2] = 0;
mat.ambient[0] = mat.ambient[1] = mat.ambient[2] = 0.3f;
mat.diffuse[0] = mat.diffuse[1] = mat.diffuse[2] = 1;
mat.specular[0] = mat.specular[1] = mat.specular[2] = 1;
mat.shininess = 100;
UintVector& Indices = shape.mesh.indices;
FloatVector& Vertices = shape.mesh.positions;
FloatVector& Normals = shape.mesh.normals;
FloatVector& TexCoord = shape.mesh.texcoords;
float t = 0;
float texcoord = 0;
float end = (float) spline->getNumberOfControlPoints();
float dt = end / numberOfDivisions;
uint w = (uint) stencil.size();
PointVector Stencil(stencil);
for (uint i=0; i<Stencil.size(); i++)
{
Stencil[i].X *= stencilscale.X;
Stencil[i].Y *= stencilscale.Y;
}
mat4 basis(MT_NULL);
int idx = 0;
bool firstIteration = true; // first iter create only verts, tris in later iters
for (int subdiv=0; subdiv<=numberOfDivisions; subdiv++)
{
spline->getFrameBasis(t, basis);
for (uint i=0; i<w; i++)
{
pushVector(Vertices, basis * vec3(Stencil[i].X, Stencil[i].Y, 0));
TexCoord.push_back(Stencil[i].Z);
TexCoord.push_back(texcoord * texscale);
if (i == 0) // starting stencil point
pushVector(Normals, basis.mulnorm((Stencil[i+1] - Stencil[i]).cross(vec3(0,0,-1)).norm()));
else if (i == w-1) // ending stencil point
pushVector(Normals, basis.mulnorm((Stencil[i] - Stencil[i-1]).cross(vec3(0,0,-1)).norm()));
else // middle stencil point
pushVector(Normals, basis.mulnorm((Stencil[i+1] - Stencil[i-1]).cross(vec3(0,0,-1)).norm()));
}
if (!firstIteration) // first iter create only verts, tris in later iters
{
for (uint i=0; i<w-1; i++)
{
Indices.push_back(idx+i);
Indices.push_back(idx+i+1);
Indices.push_back(idx+i+w);
Indices.push_back(idx+i+1);
Indices.push_back(idx+i+w+1);
Indices.push_back(idx+i+w);
}
idx += w;
}
firstIteration = false;
t += dt;
texcoord += spline->getDerivative(t).len() * dt;
}
Shapes.push_back(shape);
generateVBO();
}
void CMesh::generateVBO()
{
for (size_t i=0; i<Shapes.size(); i++)
{
vbo_t vbo;
glGenBuffers(4, vbo.buffer);
const mesh_t& m = Shapes[i].mesh;
glBindBuffer(GL_ARRAY_BUFFER, vbo.buffer[VBO_VERTEX_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, m.positions.size() * sizeof(float), m.positions.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo.buffer[VBO_NORMAL_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, m.normals.size() * sizeof(float), m.normals.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vbo.buffer[VBO_TEXCOORD_BUFFER]);
glBufferData(GL_ARRAY_BUFFER, m.texcoords.size() * sizeof(float), m.texcoords.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.buffer[VBO_INDEX_BUFFER]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m.indices.size() * sizeof(uint), m.indices.data(), GL_STATIC_DRAW);
VBOs.push_back(vbo);
}
//Shapes.clear(); // free main memory
}
void CMesh::pushVector(FloatVector& where, const vec3& v)
{
where.push_back(v.X);
where.push_back(v.Y);
where.push_back(v.Z);
}