-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShapeDrawer.cpp
More file actions
167 lines (133 loc) · 4.02 KB
/
ShapeDrawer.cpp
File metadata and controls
167 lines (133 loc) · 4.02 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <GLFW/glfw3.h>
#include <cmath>
#include "ShapeDrawer.h"
void ShapeDrawer::square(int texture){
GLfloat vertices[] =
{
-1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1
};
/* GLfloat verticesBottom[] =
{
-1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1
};*/
GLfloat colors[] =
{
1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0
};
GLfloat textureVertices[] =
{
0, 0, 1, 0, 1, 1, 0, 1
};
GLfloat normals[] =
{
0,1,0, 0,1,0, 0,1,0, 0,1,0 //Top
};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
if(texture != -1){
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}else{
glEnableClientState(GL_COLOR_ARRAY);
}
if(texture != -1){
glBindTexture(GL_TEXTURE_2D, texture);
}
glVertexPointer(3, GL_FLOAT, 0, vertices);
if(texture != -1){
glTexCoordPointer(2, GL_FLOAT, 0, textureVertices);
}else{
glColorPointer(3, GL_FLOAT, 0, colors);
}
glNormalPointer(GL_FLOAT, 0, normals);
/* Send data : 24 vertices */
glDrawArrays(GL_QUADS, 0, 4);
/* Cleanup states */
if(texture != -1){
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
}else{
glDisableClientState(GL_COLOR_ARRAY);
}
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
void ShapeDrawer::circle(float cx, float cy, float r, int num_segments) {
glBegin(GL_TRIANGLE_FAN);
for (int ii = 0; ii < num_segments; ii++) {
float theta = 2.0f * 3.1415926f * float(ii) / float(num_segments);//get the current angle
float x = r * cosf(theta);//calculate the x component
float y = r * sinf(theta);//calculate the y component
glVertex3f(x + cx, 0, y + cy);//output vertex
}
glEnd();
}
void ShapeDrawer::cube(int texture){
GLfloat vertices[] =
{
-1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1,
1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1,
-1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1,
-1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1,
-1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1,
-1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1
};
GLfloat colors[] =
{
0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0,
1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0,
0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0,
0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1
};
GLfloat textureVertices[] =
{
0, 0, 1, 0, 1, 1, 0, 1,
0, 0, 1, 0, 1, 1, 0, 1,
0, 0, 1, 0, 1, 1, 0, 1,
0, 0, 1, 0, 1, 1, 0, 1,
0, 0, 1, 0, 1, 1, 0, 1,
0, 0, 1, 0, 1, 1, 0, 1
};
GLfloat normals[] =
{
-1,0,0, -1,0,0, -1,0,0, -1,0,0, //Left
1,0,0, 1,0,0, 1,0,0, 1,0,0, //Right
0,-1,0, 0,-1,0, 0,-1,0, 0,-1,0, //Bottom
0,1,0, 0,1,0, 0,1,0, 0,1,0, //Top
0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1, //front
0,0,1, 0,0,1, 0,0,1, 0,0,1 //back
};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
// glEnableClientState (GL_TEXTURE_COORD_ARRAY_EXT);
if(texture != -1){
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}else{
glEnableClientState(GL_COLOR_ARRAY);
}
if(texture != -1){
glBindTexture(GL_TEXTURE_2D, texture);
}
glVertexPointer(3, GL_FLOAT, 0, vertices);
if(texture != -1){
glTexCoordPointer(2, GL_FLOAT, 0, textureVertices);
}else{
glColorPointer(3, GL_FLOAT, 0, colors);
}
glNormalPointer(GL_FLOAT, 0, normals);
/* Send data : 24 vertices */
glDrawArrays(GL_QUADS, 0, 24);
/* Cleanup states */
// glDisableClientState (GL_TEXTURE_COORD_ARRAY_EXT);
if(texture != -1){
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
}else{
glDisableClientState(GL_COLOR_ARRAY);
}
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}