/
square.cpp
46 lines (40 loc) · 1.3 KB
/
square.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
#include "square.h"
Square::Square() {
vShader = "vshader.glsl";
fShader = "fshader.glsl";
vertexCount = 2*3; //2 tris/face, 3 verticies/tri
bufferCount = 2;
}
void Square::buildModel() {
int verticies = 6;
vec3* points = new vec3[verticies];
vec3* colors = new vec3[verticies];
//front
buildFace(points, 0,
vec3(0.3,0.3,0.3), vec3(-0.3,0.3,0.3),
vec3(-0.3,-0.3,0.3), vec3(0.3,-0.3,0.3));
for(int i=0;i<verticies;i++)
colors[i] = vec3(1,0,0); //red
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
glBufferData(GL_ARRAY_BUFFER, verticies*sizeof(vec3), points, GL_STATIC_DRAW);
vPosition = glGetAttribLocation(program, "vPosition");
glEnableVertexAttribArray(vPosition);
glVertexAttribPointer(vPosition, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glBindBuffer(GL_ARRAY_BUFFER, buffers[1]);
glBufferData(GL_ARRAY_BUFFER, verticies*sizeof(vec3), colors, GL_STATIC_DRAW);
vColor = glGetAttribLocation(program, "vColor");
glEnableVertexAttribArray(vColor);
glVertexAttribPointer(vColor, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
delete[] points;
delete[] colors;
}
/**
* p2 - p1
* | / |
* | / |
* p3 - p4
*/
void Square::buildFace(vec3* points, int i, vec3 p1, vec3 p2, vec3 p3, vec3 p4) {
buildTri(points, i, p1, p2, p3);
buildTri(points, i+3, p1, p4, p3);
}