Created
September 29, 2016 02:11
-
-
Save jfacoustic/34cc03bae742808a39b587fba2daed06 to your computer and use it in GitHub Desktop.
Creates a vertex array to draw a sphere in OpenGL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <cmath> | |
struct vec3 { | |
float x, y, z; | |
void set(float X, float Y, float Z) | |
{ | |
x = X; | |
y = Y; | |
z = Z; | |
} | |
}; | |
vec3 midpoint(vec3 a, vec3 b) | |
{ | |
vec3 c; | |
c.x = .5f * (a.x + b.x); | |
c.y = .5f * (a.y + b.y); | |
c.z = .5f * (a.z + b.z); | |
return c; | |
} | |
std::vector<float> drawTriangle(vec3 v1, vec3 v2, vec3 v3) | |
{ | |
std::vector<float> triangles; | |
triangles.push_back(v1.x); | |
triangles.push_back(v1.y); | |
triangles.push_back(v1.z); | |
triangles.push_back(v2.x); | |
triangles.push_back(v2.y); | |
triangles.push_back(v2.z); | |
triangles.push_back(v3.x); | |
triangles.push_back(v3.y); | |
triangles.push_back(v3.z); | |
return triangles; | |
} | |
void subdiv(vec3 v1, vec3 v2, vec3 v3, std::vector<float> & sphereQuadrant, int level) | |
{ | |
if (level == 0) | |
//appends the triangle to the sphere quadrant vector: | |
sphereQuadrant.insert(sphereQuadrant.end(), drawTriangle(v1, v2, v3).begin(), drawTriangle(v1, v2, v3).end()); | |
else { | |
vec3 v12, v13, v23; | |
v12 = midpoint(v1, v2); | |
v13 = midpoint(v1, v3); | |
v23 = midpoint(v2, v3); | |
float r = 1.0f / std::sqrt(v12.x * v12.x + v12.y +v12.y + v12.z + v12.z); | |
v12.x *= r; | |
v12.y *= r; | |
v12.z *= r; | |
r = 1.0f / std::sqrt(v13.x * v13.x + v13.y * v13.y + v13.z * v13.z); | |
v13.x *= r; | |
v13.y *= r; | |
v13.z *= r; | |
r = 1.0f / std::sqrt(v23.x * v23.x + v23.y * v23.y + v23.z * v23.z); | |
v23.x *= r; | |
v23.y *= r; | |
v23.z *= r; | |
subdiv(v1, v12, v13, sphereQuadrant, level - 1); | |
subdiv(v12, v2, v23, sphereQuadrant, level - 1); | |
subdiv(v13, v23, v3, sphereQuadrant, level - 1); | |
subdiv(v12, v23, v13, sphereQuadrant, level - 1); | |
} | |
} | |
//this is the function you call. Create a vector of vertices send that in sphere. Prec controls how many triangles: | |
void createSphere(std::vector<float> & sphere, int prec) | |
{ | |
vec3 v1, v2, v3; | |
v1.set(0.0f, 1.0f, 0.0f); | |
v2.set(1.0f, 0.0f, 0.0f); | |
v3.set(0.0f, 0.0f, 1.0f); | |
subdiv(v1, v2, v3, sphere, prec); | |
v1.set(0.0f, 1.0f, 0.0f); | |
v2.set(-1.0f, 0.0f, 0.0f); | |
v3.set(0.0f, 0.0f, 1.0f); | |
subdiv(v1, v2, v3, sphere, prec); | |
v1.set(0.0f, 1.0f, 0.0f); | |
v2.set(1.0f, 0.0f, 0.0f); | |
v3.set(0.0f, 0.0f, -1.0f); | |
subdiv(v1, v2, v3, sphere, prec); | |
v1.set(0.0f, 1.0f, 0.0f); | |
v2.set(-1.0f, 0.0f, 0.0f); | |
v3.set(0.0f, 0.0f, -1.0f); | |
subdiv(v1, v2, v3, sphere, prec); | |
v1.set(0.0f, -1.0f, 0.0f); | |
v2.set(1.0f, 0.0f, 0.0f); | |
v3.set(0.0f, 0.0f, 1.0f); | |
subdiv(v1, v2, v3, sphere, prec); | |
v1.set(0.0f, -1.0f, 0.0f); | |
v2.set(-1.0f, 0.0f, 0.0f); | |
v3.set(0.0f, 0.0f, 1.0f); | |
subdiv(v1, v2, v3, sphere, prec); | |
v1.set(0.0f, -1.0f, 0.0f); | |
v2.set(1.0f, 0.0f, 0.0f); | |
v3.set(0.0f, 0.0f, -1.0f); | |
subdiv(v1, v2, v3, sphere, prec); | |
v1.set(0.0f, -1.0f, 0.0f); | |
v2.set(-1.0f, 0.0f, 0.0f); | |
v3.set(0.0f, 0.0f, -1.0f); | |
subdiv(v1, v2, v3, sphere, prec); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment