-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSphere.cpp
113 lines (84 loc) · 2.47 KB
/
Sphere.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Sphere.cpp
*Basado en el código creado por
* Created on: 06/09/2016
* Author: reynaldo Martell
*/
#include "Sphere.h"
Sphere::Sphere(float ratio, int slices, int stacks) :
ratio(ratio), slices(slices), stacks(stacks), VAO(0), VBO(0), EBO(0) {
}
Sphere::~Sphere() {
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glDeleteBuffers(1, &EBO);
glBindVertexArray(0);
glDeleteVertexArrays(1, &VAO);
}
void Sphere::init() {
vertexC.resize(((slices + 1) * (stacks + 1)));
index.resize((slices * stacks + slices) * 6);
for (int i = 0; i <= stacks; ++i) {
float V = i / (float)stacks;
float phi = V * M_PI;
for (int j = 0; j <= slices; ++j) {
float U = j / (float)slices;
float theta = U * M_PI * 2.0;
float X = cos(theta) * sin(phi);
float Y = cos(phi);
float Z = sin(theta) * sin(phi);
vertexC[i * (slices + 1) + j].position = ratio
* glm::vec3(X, Y, Z);
vertexC[i * (slices + 1) + j].color = glm::sphericalRand(1.0);
}
}
for (int i = 0; i < slices * stacks + slices; ++i) {
index[i * 6] = i;
index[i * 6 + 1] = i + slices + 1;
index[i * 6 + 2] = i + slices;
index[i * 6 + 3] = i + slices + 1;
index[i * 6 + 4] = i;
index[i * 6 + 5] = i + 1;
}
}
void Sphere::load() {
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
size_t stride;
size_t offset1 = 0;
size_t offset2 = 0;
size_t offset3 = 0;
glBufferData(GL_ARRAY_BUFFER, vertexC.size() * sizeof(glm::vec3) * 2,
vertexC.data(),
GL_STATIC_DRAW);
stride = sizeof(vertexC[0]);
offset1 = 0;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, index.size() * sizeof(GLuint),
index.data(),
GL_STATIC_DRAW);
// First attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)offset1);
glEnableVertexAttribArray(0);
// Second attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride,
(GLvoid*)offset2);
glEnableVertexAttribArray(1);
// Thrid attribute
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid*)offset3);
glEnableVertexAttribArray(2);
glBindVertexArray(0); // Unbind VAO
}
void Sphere::render() {
glBindVertexArray(VAO);
glDrawElements( GL_TRIANGLES, index.size(), GL_UNSIGNED_INT,
(GLvoid*) (sizeof(GLuint) * 0));
glBindVertexArray(0);
}