-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommonOpenGL.h
296 lines (241 loc) · 6.42 KB
/
CommonOpenGL.h
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//
// CommonOpenGL.h
// EG-TD
//
// Created by Gurcan Yavuz on 10/6/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <OpenGLES/ES1/gl.h>
#pragma mark -
#pragma mark Debug
//#define DEBUG 0
#pragma mark -
#pragma mark Macros
// Macro which returns a random value between -1 and 1
#define RANDOM_MINUS_1_TO_1() ((random() / (GLfloat)0x3fffffff )-1.0f)
// MAcro which returns a random number between 0 and 1
#define RANDOM_0_TO_1() ((random() / (GLfloat)0x7fffffff ))
// Macro which converts degrees into radians
#define DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) / 180.0 * M_PI)
//Map Change accelerometer
#define kMapAcceloremeter 0.001
#pragma mark -
#pragma mark Enumerations
#define MAX_CUBES 65
#define MAX_Acceleration 0.3
enum {
kControlType_NewGame,
kControlType_Settings,
kControlType_HighScores,
kControlType_QuitGame,
kControlType_PauseGame,
kControl_Idle,
kControl_Scaling,
kControl_Selected,
kGameState_Running,
kGameState_Paused,
kGameState_Loading,
kGameState_towerMenuActive,
kGameState_gameActive,
kSceneState_Idle,
//kSceneState_TransitionIn,
//kSceneState_TransitionOut,
kSceneState_Running,
kSceneState_Paused
};
#pragma mark -
#pragma mark Structures
// Strcuture used to hold 3D color information
typedef struct {
float red;
float green;
float blue;
float alpha;
} EGColor;
// Structure used to hold 3D vertex information
typedef struct {
GLfloat x;
GLfloat y;
GLfloat z;
} EGVertex3D;
typedef struct _Vector2f {
GLfloat x;
GLfloat y;
} EGVector2f;
typedef struct _Quad2f {
GLfloat bl_x, bl_y;
GLfloat br_x, br_y;
GLfloat tl_x, tl_y;
GLfloat tr_x, tr_y;
} EGQuad2f;
#pragma mark -
#pragma mark Color functions
// Returns an EGColor structure from the values passed in
static inline EGColor EGColorMake(GLfloat r, GLfloat g, GLfloat b, GLfloat a)
{
EGColor newColor;
newColor.red = r;
newColor.green = g;
newColor.blue = b;
newColor.alpha = a;
return newColor;
}
#pragma mark -
#pragma mark GLUT functions
#pragma mark -
#pragma mark Inline Functions
static const EGColor Color4fInit = {1.0f, 1.0f, 1.0f, 1.0f};
static const EGVertex3D Vector3fZero = {0.0f, 0.0f, 0.0f};
static inline EGVertex3D Vector3fMake(GLfloat x, GLfloat y, GLfloat z)
{
return (EGVertex3D) {x, y, z};
}
static inline EGColor Color4fMake(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
{
return (EGColor) {red, green, blue, alpha};
}
static inline EGVertex3D Vector3fMultiply(EGVertex3D v, GLfloat s)
{
return (EGVertex3D) {v.x * s, v.y * s, v.z * s};
}
static inline EGVertex3D Vector3fAdd(EGVertex3D v1, EGVertex3D v2)
{
return (EGVertex3D) {v1.x + v2.x , v1.y + v2.y, v1.z + v2.z};
}
static inline EGVertex3D Vector3fSub(EGVertex3D v1, EGVertex3D v2)
{
return (EGVertex3D) {v1.x - v2.x, v1.y - v2.y, v1.z - v2.z};
}
static inline GLfloat Vector3fDot(EGVertex3D v1, EGVertex3D v2)
{
return (GLfloat) v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
static inline GLfloat Vector3fLength(EGVertex3D v)
{
return (GLfloat) sqrtf(Vector3fDot(v, v));
}
static inline EGVertex3D Vector3fNormalize(EGVertex3D v)
{
return Vector3fMultiply(v, 1.0f/Vector3fLength(v));
}
static inline void __gluMakeIdentityf(GLfloat m[16])
{
m[0+4*0] = 1; m[0+4*1] = 0; m[0+4*2] = 0; m[0+4*3] = 0;
m[1+4*0] = 0; m[1+4*1] = 1; m[1+4*2] = 0; m[1+4*3] = 0;
m[2+4*0] = 0; m[2+4*1] = 0; m[2+4*2] = 1; m[2+4*3] = 0;
m[3+4*0] = 0; m[3+4*1] = 0; m[3+4*2] = 0; m[3+4*3] = 1;
}
static inline void gluPerspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar)
{
GLfloat m[4][4];
GLfloat sine, cotangent, deltaZ;
GLfloat radians = fovy / 2 * 3.14 / 180;
deltaZ = zFar - zNear;
sine = sin(radians);
if ((deltaZ == 0) || (sine == 0) || (aspect == 0))
{
return;
}
cotangent = cos(radians) / sine;
__gluMakeIdentityf(&m[0][0]);
m[0][0] = cotangent / aspect;
m[1][1] = cotangent;
m[2][2] = -(zFar + zNear) / deltaZ;
m[2][3] = -1;
m[3][2] = -2 * zNear * zFar / deltaZ;
m[3][3] = 0;
glMultMatrixf(&m[0][0]);
}
// This is a modified version of the function of the same name from
// the Mesa3D project ( http://mesa3d.org/ ), which is licensed
// under the MIT license, which allows use, modification, and
// redistribution
static inline void gluLookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez,
GLfloat centerx, GLfloat centery, GLfloat centerz,
GLfloat upx, GLfloat upy, GLfloat upz)
{
GLfloat m[16];
GLfloat x[3], y[3], z[3];
GLfloat mag;
/* Make rotation matrix */
/* Z vector */
z[0] = eyex - centerx;
z[1] = eyey - centery;
z[2] = eyez - centerz;
mag = sqrtf(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
if (mag) { /* mpichler, 19950515 */
z[0] /= mag;
z[1] /= mag;
z[2] /= mag;
}
/* Y vector */
y[0] = upx;
y[1] = upy;
y[2] = upz;
/* X vector = Y cross Z */
x[0] = y[1] * z[2] - y[2] * z[1];
x[1] = -y[0] * z[2] + y[2] * z[0];
x[2] = y[0] * z[1] - y[1] * z[0];
/* Recompute Y = Z cross X */
y[0] = z[1] * x[2] - z[2] * x[1];
y[1] = -z[0] * x[2] + z[2] * x[0];
y[2] = z[0] * x[1] - z[1] * x[0];
/* mpichler, 19950515 */
/* cross product gives area of parallelogram, which is < 1.0 for
* non-perpendicular unit-length vectors; so normalize x, y here
*/
mag = sqrtf(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
if (mag) {
x[0] /= mag;
x[1] /= mag;
x[2] /= mag;
}
mag = sqrtf(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
if (mag) {
y[0] /= mag;
y[1] /= mag;
y[2] /= mag;
}
#define M(row,col) m[col*4+row]
M(0, 0) = x[0];
M(0, 1) = x[1];
M(0, 2) = x[2];
M(0, 3) = 0.0;
M(1, 0) = y[0];
M(1, 1) = y[1];
M(1, 2) = y[2];
M(1, 3) = 0.0;
M(2, 0) = z[0];
M(2, 1) = z[1];
M(2, 2) = z[2];
M(2, 3) = 0.0;
M(3, 0) = 0.0;
M(3, 1) = 0.0;
M(3, 2) = 0.0;
M(3, 3) = 1.0;
#undef M
glMultMatrixf(m);
/* Translate Eye to Origin */
glTranslatef(-eyex, -eyey, -eyez);
}
//FOR BUILT MENU ORTHOGONAL PROJECTIONS
static inline void switchToOrtho ()
{
CGRect bounds= [[UIScreen mainScreen] bounds];
// NSLog(@"%f",bounds.size.width);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrthof(0, bounds.size.height, 0,bounds.size.width , -5, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//RETURN THE MAIN GAME SCREEN
static inline void switchBackToFrustum()
{
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}