-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmesh.cpp
More file actions
203 lines (175 loc) · 5.72 KB
/
mesh.cpp
File metadata and controls
203 lines (175 loc) · 5.72 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* mesh.cpp
*
* Implement Mesh objects
*/
#include <iostream>
#include <stdio.h>
#include <memory>
#include <math.h>
#include "mesh.h"
using namespace std;
Mesh::Mesh(shared_ptr<float> vertices, int number_of_vertices,
list<IndexStrip> index_list, shared_ptr<float> normals) {
_vertices = vertices;
_number_of_vertices = number_of_vertices;
_index_list = index_list;
_normals = normals;
#ifdef MESH_VBO
_vertex_vbo = shared_ptr<VertexVBO>(new VertexVBO(vertices.get(),
sizeof(float) * 3 * number_of_vertices));
_normals_vbo = shared_ptr<VertexVBO>(new VertexVBO(normals.get(),
sizeof(float) * 3 * number_of_vertices ));
#endif
}
RectangleMesh::RectangleMesh(shared_ptr<float> vertices,
int xsize, int ysize) : Mesh(vertices, xsize * ysize,
list<IndexStrip>(), shared_ptr<float>(0)),
_xsize(xsize), _ysize(ysize) {
make_normals();
}
void
normalize(float * v, float * rv) {
float size = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
rv[0] = v[0] / size;
rv[1] = v[1] / size;
rv[2] = v[2] / size;
}
void
sub(float * v1, float * v2, float * rv) {
rv[0] = v1[0] - v2[0];
rv[1] = v1[1] - v2[1];
rv[2] = v1[2] - v2[2];
}
void
cross(float * v1, float * v2, float * rv) {
rv[0] = v1[1] * v2[2] - v1[2] * v2[1];
rv[1] = v1[0] * v2[2] - v1[2] * v2[0];
rv[2] = v1[0] * v2[1] - v1[1] * v2[0];
}
void get_neighbors(float * vertices, int xoff, int yoff,
int xsize, int ysize, float ** v1, float ** v2) {
int n1_x = xoff, n1_y = 0;
if (yoff < ysize - 1)
n1_y = yoff + 1;
else
n1_y = yoff - 1;
int n2_y = yoff, n2_x = 0;
if (xoff < xsize - 1)
n2_x = xoff + 1;
else
n2_x = xoff - 1;
int v1off = n1_x + n1_y * xsize;
*v1 = &vertices[3 * v1off];
int v2off = n2_x + n2_y * xsize;
*v2 = &vertices[3 * v2off];
}
void
RectangleMesh::make_normals() {
_normals = shared_ptr<float>(new float[_number_of_vertices * 3]);
for(int yoff = 0; yoff < _ysize; yoff++) {
for(int xoff = 0; xoff < _xsize; xoff++) {
float * coord = &_vertices.get()[3 * (xoff + yoff * _xsize)];
float * v1, * v2;
get_neighbors(_vertices.get(), xoff, yoff, _xsize, _ysize,
&v1, &v2);
float s1[3], s2[3];
sub(v1, coord, s1);
sub(v2, coord, s2);
float cp[3];
cross(s1, s2, cp);
float norm[3];
normalize(cp, norm);
int offset = 3 * (xoff + yoff * _xsize);
if (norm[2] >= 0) {
_normals.get()[offset] = norm[0];
_normals.get()[offset+1] = norm[1];
_normals.get()[offset+2] = norm[2];
} else {
_normals.get()[offset] = -norm[0];
_normals.get()[offset+1] = -norm[1];
_normals.get()[offset+2] = -norm[2];
}
}
}
}
WireRectangleMesh::WireRectangleMesh(shared_ptr<float> vertices,
int xsize, int ysize) : RectangleMesh(vertices, xsize, ysize) {
make_mesh_indices();
}
void
WireRectangleMesh::make_mesh_indices() {
int east_west_count = (2 * (_xsize - 1)) * _ysize;
int north_south_count = (2 * (_ysize - 1)) * _xsize;
int number_of_indices = east_west_count + north_south_count;
shared_ptr<int> indices(new int[number_of_indices]);
IndexStrip index_strip(indices, number_of_indices);
// do east west lines
int i = 0;
for(int row = 0; row < _ysize; row++) {
indices.get()[i++] = row * _xsize;
for(int col = 1; col < _xsize - 1; col++) {
indices.get()[i++] = col + row * _xsize;
indices.get()[i++] = col + row * _xsize;
}
indices.get()[i++] = _xsize - 1 + row * _xsize;
}
// do north south lines
for(int col = 0; col < _xsize; col++) {
indices.get()[i++] = col;
for(int row = 1; row < _ysize - 1; row++) {
indices.get()[i++] = col + row * _xsize;
indices.get()[i++] = col + row * _xsize;
}
indices.get()[i++] = col + (_ysize - 1) * _xsize;
}
_index_list.push_back(index_strip);
}
FaceRectangleMesh::FaceRectangleMesh(shared_ptr<float> vertices,
int xsize, int ysize) : RectangleMesh(vertices, xsize, ysize) {
make_face_indices();
}
void
FaceRectangleMesh::make_face_indices() {
int i = 0;
for(int yoff = 0; yoff < _ysize - 1; yoff++) {
list<int> indices;
for(int xoff = 0; xoff < _xsize; xoff++) {
indices.push_back(i);
indices.push_back(i + _xsize);
i += 1;
}
// Made one strip, now copy it
int num_indices = indices.size();
shared_ptr<int> actual(new int[num_indices]);
int y = 0;
for(list<int>::iterator it = indices.begin();
it != indices.end(); it++) {
actual.get()[y++] = *it;
}
IndexStrip strip(actual, num_indices);
_index_list.push_back(strip);
}
}
list<IndexStrip> make_faces(int _xsize, int _ysize) {
list<IndexStrip> _index_list;
int i = 0;
for(int yoff = 0; yoff < _ysize - 1; yoff++) {
list<int> indices;
for(int xoff = 0; xoff < _xsize; xoff++) {
indices.push_back(i);
indices.push_back(i + _xsize);
i += 1;
}
int num_indices = indices.size();
shared_ptr<int> actual(new int[num_indices]);
int y = 0;
for(list<int>::iterator it = indices.begin();
it != indices.end(); it++) {
actual.get()[y++] = *it;
}
IndexStrip strip(actual, num_indices);
_index_list.push_back(strip);
}
return _index_list;
}