-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySphere.h
More file actions
128 lines (102 loc) · 3.16 KB
/
MySphere.h
File metadata and controls
128 lines (102 loc) · 3.16 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#pragma once
#include <vgl.h>
#include <vec.h>
struct MySphereVertex
{
vec4 position;
vec4 color;
vec3 normal;
};
class MySphere
{
public:
MySphere(void);
~MySphere(void);
int NumVertices;
MySphereVertex * Vertices;
GLuint Init(int la_slice, int lo_slice, vec4 color=vec4(0.5,0.5,0.5,1));
void SetPositionAndOtherAttributes(GLuint program);
GLuint vao;
GLuint buffer;
bool bInitialized;
void Draw(GLuint program);
};
MySphere::MySphere(void)
{
bInitialized = false;
NumVertices = 0;
Vertices = NULL;
}
MySphere::~MySphere(void)
{
if(Vertices != NULL)
delete [] Vertices;
}
GLuint MySphere::Init(int la_slice, int lo_slice, vec4 color)
{
// The Cube should be initialized only once;
if(bInitialized == true) return vao;
NumVertices = (la_slice-2)*lo_slice*2*3 + 2*lo_slice*3;
Vertices = new MySphereVertex [NumVertices];
float da = 3.141592/la_slice;
float db = 3.141592*2.0/lo_slice;
int cur = 0;
for(int j=0; j<lo_slice; j++)
{
float y2 = cos(da);
float r2 = sqrt(1-y2*y2);
}
for(int i=0; i<la_slice; i++)
{
float y1 = cos(da*i);
float r1 = sqrt(1-y1*y1);
float y2 = cos(da*(i+1));
float r2 = sqrt(1-y2*y2);
for(int j=0; j<lo_slice; j++)
{
vec3 a(r1*cos(db*j), y1, r1*sin(db*j));
vec3 b(r2*cos(db*j), y2, r2*sin(db*j));
vec3 c(r2*cos(db*(j+1)), y2, r2*sin(db*(j+1)));
vec3 d(r1*cos(db*(j+1)), y1, r1*sin(db*(j+1)));
if(i!=lo_slice-1)
{
Vertices[cur].position = a; Vertices[cur].color = color; Vertices[cur].normal = a; cur ++;
Vertices[cur].position = b; Vertices[cur].color = color; Vertices[cur].normal = b; cur ++;
Vertices[cur].position = c; Vertices[cur].color = color; Vertices[cur].normal = c; cur ++;
}
if(i!=0)
{
Vertices[cur].position = c; Vertices[cur].color = color; Vertices[cur].normal = c; cur ++;
Vertices[cur].position = d; Vertices[cur].color = color; Vertices[cur].normal = d; cur ++;
Vertices[cur].position = a; Vertices[cur].color = color; Vertices[cur].normal = a; cur ++;
}
}
}
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(MySphereVertex)*NumVertices, Vertices, GL_STATIC_DRAW);
bInitialized = true;
return vao;
}
void MySphere::SetPositionAndOtherAttributes(GLuint program)
{
GLuint vPosition = glGetAttribLocation(program, "vPosition");
glEnableVertexAttribArray(vPosition);
glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, sizeof(MySphereVertex), BUFFER_OFFSET(0));
GLuint vColor = glGetAttribLocation(program, "vColor");
glEnableVertexAttribArray(vColor);
glVertexAttribPointer(vColor, 4, GL_FLOAT, GL_FALSE, sizeof(MySphereVertex), BUFFER_OFFSET(sizeof(vec4)));
GLuint vNormal = glGetAttribLocation(program, "vNormal");
glEnableVertexAttribArray(vNormal);
glVertexAttribPointer(vNormal, 4, GL_FLOAT, GL_FALSE, sizeof(MySphereVertex), BUFFER_OFFSET(sizeof(vec4)+sizeof(vec4)));
}
void MySphere::Draw(GLuint program)
{
if(!bInitialized) return; // check whether it is initiazed or not.
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
SetPositionAndOtherAttributes(program);
glDrawArrays(GL_TRIANGLES, 0, NumVertices);
}