-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstatic_geometry.cpp
More file actions
executable file
·269 lines (237 loc) · 9.98 KB
/
static_geometry.cpp
File metadata and controls
executable file
·269 lines (237 loc) · 9.98 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "common_header.h"
#include "static_geometry.h"
// Generates centered torus with specified parameters and stores it in VBO.
// vboDest - VBO where to store torus
// fRadius - outer radius
// fTubeRadius - inner radius
// iSubDivAround - subdivisions around torus
// iSubDivTube - subdivisions of tube
int GenerateTorus(CVertexBufferObject &vboDest, float fRadius, float fTubeRadius, int iSubDivAround, int iSubDivTube)
{
float fAddAngleAround = 360.0f/(float)iSubDivAround;
float fAddAngleTube = 360.0f/(float)iSubDivTube;
float fCurAngleAround = 0.0f;
int iStepsAround = 1;
const float PI = float(atan(1.0)*4.0);
int iFacesAdded = 0;
while(iStepsAround <= iSubDivAround)
{
float fSineAround = sin(fCurAngleAround/180.0f*PI);
float fCosineAround = cos(fCurAngleAround/180.0f*PI);
glm::vec3 vDir1(fSineAround, fCosineAround, 0.0f);
float fNextAngleAround = fCurAngleAround+fAddAngleAround;
float fNextSineAround = sin(fNextAngleAround/180.0f*PI);
float fNextCosineAround = cos(fNextAngleAround/180.0f*PI);
glm::vec3 vDir2(fNextSineAround, fNextCosineAround, 0.0f);
float fCurAngleTube = 0.0f;
int iStepsTube = 1;
while(iStepsTube <= iSubDivTube)
{
float fSineTube = sin(fCurAngleTube/180.0f*PI);
float fCosineTube = cos(fCurAngleTube/180.0f*PI);
float fNextAngleTube = fCurAngleTube+fAddAngleTube;
float fNextSineTube = sin(fNextAngleTube/180.0f*PI);
float fNextCosineTube = cos(fNextAngleTube/180.0f*PI);
glm::vec3 vMid1 = vDir1*(fRadius-fTubeRadius/2), vMid2 = vDir2*(fRadius-fTubeRadius/2);
glm::vec3 vQuadPoints[] =
{
vMid1 + glm::vec3(0.0f, 0.0f, -fNextSineTube*fTubeRadius) + vDir1*fNextCosineTube*fTubeRadius,
vMid1 + glm::vec3(0.0f, 0.0f, -fSineTube*fTubeRadius) + vDir1*fCosineTube*fTubeRadius,
vMid2 + glm::vec3(0.0f, 0.0f, -fSineTube*fTubeRadius) + vDir2*fCosineTube*fTubeRadius,
vMid2 + glm::vec3(0.0f, 0.0f, -fNextSineTube*fTubeRadius) + vDir2*fNextCosineTube*fTubeRadius
};
glm::vec3 vNormals[] =
{
glm::vec3(0.0f, 0.0f, -fNextSineTube) + vDir1*fNextCosineTube,
glm::vec3(0.0f, 0.0f, -fSineTube) + vDir1*fCosineTube,
glm::vec3(0.0f, 0.0f, -fSineTube) + vDir2*fCosineTube,
glm::vec3(0.0f, 0.0f, -fNextSineTube) + vDir2*fNextCosineTube
};
glm::vec2 vTexCoords[] =
{
glm::vec2(fCurAngleAround/360.0f, fNextAngleTube/360.0f),
glm::vec2(fCurAngleAround/360.0f, fCurAngleTube/360.0f),
glm::vec2(fNextAngleAround/360.0f, fCurAngleTube/360.0f),
glm::vec2(fNextAngleAround/360.0f, fNextAngleTube/360.0f)
};
int iIndices[] = {0, 1, 2, 2, 3, 0};
FOR(i, 6)
{
int index = iIndices[i];
vboDest.AddData(&vQuadPoints[index], sizeof(glm::vec3));
vboDest.AddData(&vTexCoords[index], sizeof(glm::vec2));
vboDest.AddData(&vNormals[index], sizeof(glm::vec3));
}
iFacesAdded += 2; // Keep count of added faces
fCurAngleTube += fAddAngleTube;
iStepsTube++;
}
fCurAngleAround += fAddAngleAround;
iStepsAround++;
}
return iFacesAdded;
}
// Generates centered opened cylinder and stores it in VBO.
// vboDest - VBO where to store torus
// fRadius - outer radius
// fHeight - height of cylinder
// iSubDivAround - subdivisions around cylinder
int GenerateCylinder(CVertexBufferObject &vboDest, float fRadius, float fHeight, int iSubDivAround, float fMapU, float fMapV)
{
float fAddAngleAround = 360.0f/(float)(iSubDivAround-1);
float fCurAngleAround = 0.0f;
int iStepsAround = 1;
const float PI = float(atan(1.0)*4.0);
int iFacesAdded = 0;
while(iStepsAround <= iSubDivAround)
{
float fSineAround = sin(fCurAngleAround/180.0f*PI);
float fCosineAround = cos(fCurAngleAround/180.0f*PI);
glm::vec3 vDir1(fCosineAround, 0.0f, fSineAround);
float fNextAngleAround = fCurAngleAround+fAddAngleAround;
float fNextSineAround = sin(fNextAngleAround/180.0f*PI);
float fNextCosineAround = cos(fNextAngleAround/180.0f*PI);
glm::vec3 vDir2(fNextCosineAround, 0.0f, fNextSineAround);
glm::vec3 vQuadPoints[] =
{
glm::vec3(0.0f, fHeight, 0.0f) + vDir1*fRadius,
vDir1*fRadius,
vDir2*fRadius,
glm::vec3(0.0f, fHeight, 0.0f) + vDir2*fRadius
};
glm::vec2 vTexCoords[] =
{
glm::vec2(fMapU*fCurAngleAround/360.0f, fMapV),
glm::vec2(fMapU*fCurAngleAround/360.0f, 0.0f),
glm::vec2(fMapU*fNextAngleAround/360.0f, 0.0f),
glm::vec2(fMapU*fNextAngleAround/360.0f, fMapV)
};
glm::vec3 vNormals[] =
{
vDir1,
vDir1,
vDir2,
vDir2
};
int iIndices[] = {0, 1, 2, 2, 3, 0};
FOR(i, 6)
{
int index = iIndices[i];
vboDest.AddData(&vQuadPoints[index], sizeof(glm::vec3));
vboDest.AddData(&vTexCoords[index], sizeof(glm::vec2));
vboDest.AddData(&vNormals[index], sizeof(glm::vec3));
}
iFacesAdded += 2; // Keep count of added faces
fCurAngleAround += fAddAngleAround;
iStepsAround++;
}
return iFacesAdded;
}
//glm::vec3 vCubeVertices[36] =
//{
// // Front face
// glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec3(-0.5f, 0.5f, 0.5f),
// // Back face
// glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(0.5f, 0.5f, -0.5f),
// // Left face
// glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(-0.5f, 0.5f, -0.5f),
// // Right face
// glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(0.5f, 0.5f, 0.5f),
// // Top face
// glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(-0.5f, 0.5f, -0.5f),
// // Bottom face
// glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(-0.5f, -0.5f, 0.5f)
//};
//glm::vec3 vCubeVertices2[24] =
//{
// // Front face
// glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(-0.5f, -0.5f, 0.5f),
// // Back face
// glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(0.5f, -0.5f, -0.5f),
// // Left face
// glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(-0.5f, -0.5f, -0.5f),
// // Right face
// glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(0.5f, -0.5f, 0.5f),
// // Top face
// glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(0.5f, 0.5f, 0.5f),
// // Bottom face
// glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(-0.5f, -0.5f, 0.5f),
//};
//
//unsigned int iCubeindices[36] =
//{
// 0, 2, 1, 1, 2, 3, // front
// 4, 6, 5, 5, 6, 7, // back
// 8, 10, 9, 9, 10, 11, // left
// 12, 14, 13, 13, 14, 15, // right
// 16, 18, 17, 17, 18, 19, // top
// 20, 22, 21, 21, 22, 23 // bottom
//};
glm::vec3 vCubeVertices2[8] =
{
// Front face
glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(-0.5f, -0.5f, 0.5f),
// Back face
glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(0.5f, -0.5f, -0.5f),
//// Left face
//glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(-0.5f, -0.5f, -0.5f),
//// Right face
//glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(0.5f, -0.5f, 0.5f),
//// Top face
//glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec3(0.5f, 0.5f, 0.5f),
//// Bottom face
//glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(-0.5f, -0.5f, 0.5f),
};
unsigned int iCubeindices[36] =
{
0, 2, 1, 1, 2, 3, // front
4, 6, 5, 5, 6, 7, // back
5, 2, 4, 5, 3, 2, // left
1, 7, 6, 1, 6, 0, // right
0, 6, 4, 0, 4, 2, // top
1, 5, 7, 1, 3, 5 // bottom
};
glm::vec2 vCubeTexCoords[6] = {glm::vec2(0.0f, 1.0f), glm::vec2(1.0f, 1.0f), glm::vec2(1.0f, 0.0f), glm::vec2(1.0f, 0.0f), glm::vec2(0.0f, 0.0f), glm::vec2(0.0f, 1.0f)};
glm::vec2 vCubeTexCoords2[4] = { glm::vec2(0.0f, 1.0f), glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 1.0f), glm::vec2(1.0f, 0.0f) };
glm::vec3 vCubeNormals[6] =
{
glm::vec3(0.0f, 0.0f, 1.0f),
glm::vec3(0.0f, 0.0f, -1.0f),
glm::vec3(-1.0f, 0.0f, 0.0f),
glm::vec3(1.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.0f),
glm::vec3(0.0f, -1.0f, 0.0f)
};
glm::vec3 vGround[6] =
{
glm::vec3(-1000, 0, -1000), glm::vec3(-1000, 0, 1000), glm::vec3(1000, 0, 1000), glm::vec3(1000, 0, 1000), glm::vec3(1000, 0, -1000), glm::vec3(-1000, 0, -1000)
};
int iTorusFaces, iTorusFaces2;
// Adds all static objects to scene.
// vboDest - VBO where to store objects
void AddSceneObjects(CVertexBufferObject& vboDest)
{
// Add ground to VBO
FOR(i, 6)
{
vboDest.AddData(&vGround[i], sizeof(glm::vec3));
glm::vec2 vCoord = vCubeTexCoords[i]*50.0f;
vboDest.AddData(&vCoord, sizeof(glm::vec2));
glm::vec3 vGroundNormal(0.0f, 1.0f, 0.0f);
vboDest.AddData(&vGroundNormal, sizeof(glm::vec3));
}
iTorusFaces = GenerateTorus(vboDest, 7.0f, 2.0f, 20, 20);
iTorusFaces2 = GenerateTorus(vboDest, 3.0f, 1.0f, 20, 20);
}
// Adds all static objects to scene.
// vboDest - VBO where to store objects
void AddCube(CVertexBufferObject& vboDest)
{
// Add cube to VBO
FOR(i, 8)
{
vboDest.AddData(&vCubeVertices2[i], sizeof(glm::vec3));
vboDest.AddData(&vCubeTexCoords2[i % 4], sizeof(glm::vec2));
vboDest.AddData(&vCubeNormals[i / 4], sizeof(glm::vec3));
}
}