-
Notifications
You must be signed in to change notification settings - Fork 2
/
Plane.cpp
90 lines (80 loc) · 2.4 KB
/
Plane.cpp
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
#include "Plane.h"
#include <cstdio>
#include <cmath>
Plane::Plane(QOpenGLShaderProgram* prog, QVector3D center, float xsize, float ysize, int xdivs, int ydivs, float zlevel, float smax, float tmax) :
TriangleMesh(prog, "Plane"),
_center(center),
_xSize(xsize),
_ySize(ysize),
_xDivs(xdivs),
_yDivs(ydivs),
_zLevel(zlevel)
{
_sMax = smax;
_tMax = tmax;
buildMesh(_center, _xSize, _ySize, _xDivs, _yDivs, _zLevel, _sMax, _tMax);
}
void Plane::setPlane(QOpenGLShaderProgram* prog, QVector3D center, float xsize, float ysize, int xdivs, int ydivs, float zlevel, float smax, float tmax)
{
_center = center;
_xSize = xsize;
_ySize = ysize;
_xDivs = xdivs;
_yDivs = ydivs;
_zLevel = zlevel;
_sMax = smax;
_tMax = tmax;
setProg(prog);
buildMesh(_center, _xSize, _ySize, _xDivs, _yDivs, _zLevel, _sMax, _tMax);
}
TriangleMesh* Plane::clone()
{
return new Plane(_prog, _center, _xSize, _ySize, _xDivs, _yDivs, _zLevel, _sMax, _tMax);
}
void Plane::buildMesh(QVector3D center, float xsize, float ysize, int xdivs, int ydivs, float zlevel, float smax, float tmax)
{
std::vector<float> p(3 * (xdivs + 1) * (ydivs + 1));
std::vector<float> n(3 * (xdivs + 1) * (ydivs + 1));
std::vector<float> tex(2 * (xdivs + 1) * (ydivs + 1));
std::vector<unsigned int> el(6 * xdivs * ydivs);
float x2 = xsize / 2.0f;
float y2 = ysize / 2.0f;
float iFactor = (float)ysize / ydivs;
float jFactor = (float)xsize / xdivs;
float texi = smax / ydivs;
float texj = tmax / xdivs;
float x, y;
int vidx = 0, tidx = 0;
for (int i = 0; i <= ydivs; i++) {
y = iFactor * i - y2;
for (int j = 0; j <= xdivs; j++) {
x = jFactor * j - x2;
p[vidx] = center.x() + x;
p[vidx + 1] = center.y() + y;
p[vidx + 2] = zlevel;
n[vidx] = 0.0f;
n[vidx + 1] = 0.0f;
n[vidx + 2] = -1.0f;
tex[tidx] = j * texi;
tex[tidx + 1] = i * texj;
vidx += 3;
tidx += 2;
}
}
unsigned int rowStart, nextRowStart;
int idx = 0;
for (int i = 0; i < ydivs; i++) {
rowStart = (unsigned int)(i * (xdivs + 1));
nextRowStart = (unsigned int)((i + 1) * (xdivs + 1));
for (int j = 0; j < xdivs; j++) {
el[idx] = rowStart + j;
el[idx + 1] = nextRowStart + j;
el[idx + 2] = nextRowStart + j + 1;
el[idx + 3] = rowStart + j;
el[idx + 4] = nextRowStart + j + 1;
el[idx + 5] = rowStart + j + 1;
idx += 6;
}
}
initBuffers(&el, &p, &n, &tex);
}