-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlane.cpp
More file actions
58 lines (51 loc) · 1.68 KB
/
Plane.cpp
File metadata and controls
58 lines (51 loc) · 1.68 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
#include "Plane.h"
#include <cstdio>
#include <cmath>
Plane::Plane(QOpenGLShaderProgram* prog, float xsize, float zsize, int xdivs, int zdivs, float smax, float tmax):
TriangleMesh(prog, "Plane")
{
std::vector<GLfloat> p(3 * (xdivs + 1) * (zdivs + 1));
std::vector<GLfloat> n(3 * (xdivs + 1) * (zdivs + 1));
std::vector<GLfloat> tex(2 * (xdivs + 1) * (zdivs + 1));
std::vector<GLuint> el(6 * xdivs * zdivs);
float x2 = xsize / 2.0f;
float z2 = zsize / 2.0f;
float iFactor = (float)zsize / zdivs;
float jFactor = (float)xsize / xdivs;
float texi = smax / zdivs;
float texj = tmax / xdivs;
float x, z;
int vidx = 0, tidx = 0;
for( int i = 0; i <= zdivs; i++ ) {
z = iFactor * i - z2;
for( int j = 0; j <= xdivs; j++ ) {
x = jFactor * j - x2;
p[vidx] = x;
p[vidx+1] = 0.0f;
p[vidx+2] = z;
n[vidx] = 0.0f;
n[vidx+1] = 1.0f;
n[vidx+2] = 0.0f;
tex[tidx] = j * texi;
tex[tidx+1] = i * texj;
vidx += 3;
tidx += 2;
}
}
GLuint rowStart, nextRowStart;
int idx = 0;
for( int i = 0; i < zdivs; i++ ) {
rowStart = (GLuint)( i * (xdivs+1) );
nextRowStart = (GLuint)( (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);
}