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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
| #define VERTEX_SHADER @"vertex"
#define FRAGMENT_SHADER @"fragment"
typedef struct
{
char* Name;
GLint Location;
}Uniform;
GLfloat Cube2VertexData[72] =
{
// right
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 1.5f,
0.5f, -0.5f, 1.5f,
// top
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 1.5f,
0.5f, 0.5f, 1.5f,
// left
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 1.5f,
-0.5f, 0.5f, 1.5f,
// bottom
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 1.5f,
-0.5f, -0.5f, 1.5f,
// front
0.5f, 0.5f, 1.5f,
-0.5f, 0.5f, 1.5f,
-0.5f, -0.5f, 1.5f,
0.5f, -0.5f, 1.5f,
// back
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
};
GLuint Cube2IndicesData[36] =
{
// right
0, 1, 2, 2, 3, 0,
// top
4, 5, 6, 6, 7, 4,
// left
8, 9, 10, 10, 11, 8,
// bottom
12, 13, 14, 14, 15, 12,
// front
16, 17, 18, 18, 19, 16,
// back
20, 21, 22, 22, 23, 20
};
GLfloat CubeVertexData[72] =
{
// right
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
// top
0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
// left
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
// bottom
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
// front
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
// back
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
};
GLuint CubeIndicesData[36] =
{
// right
0, 1, 2, 2, 3, 0,
// top
4, 5, 6, 6, 7, 4,
// left
8, 9, 10, 10, 11, 8,
// bottom
12, 13, 14, 14, 15, 12,
// front
16, 17, 18, 18, 19, 16,
// back
20, 21, 22, 22, 23, 20
};
@interface ViewController () {
GLuint _program;
GLint _uniformArraySize;
Uniform *_uniformArray;
GLuint _verticesVBO;
GLuint _indicesVBO;
GLuint _VAO;
GLuint _verticesVBO2;
GLuint _indicesVBO2;
GLKMatrix4 _projectionMatrix;
GLKMatrix4 _modelViewMatrix;
}
@end
@implementation ViewController
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
// Clear the screen
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
// Bind the VAO and the program
glBindVertexArrayOES( _VAO );
glUseProgram( _program );
for (int i = 0; i < _uniformArraySize; i++) {
if (!strcmp(_uniformArray[i].Name, "ModelViewProjectionMatrix"))
{
// Multiply the transformation matrices together
GLKMatrix4 modelViewProjectionMatrix = GLKMatrix4Multiply(_projectionMatrix, _modelViewMatrix);
glUniformMatrix4fv(_uniformArray[i].Location, 1, GL_FALSE, modelViewProjectionMatrix.m);
}
}
// Draw!
glDrawElements( GL_TRIANGLES, sizeof(CubeIndicesData)/sizeof(GLuint), GL_UNSIGNED_INT, NULL );
glDrawElements( GL_TRIANGLES, sizeof(Cube2IndicesData)/sizeof(GLuint), GL_UNSIGNED_INT, NULL );
}
- (void)update
{
_modelViewMatrix = GLKMatrix4RotateY(_modelViewMatrix, 0.2);
}
- (GLuint)compileShader:(NSString*)shaderName withType:(GLenum)shaderType
{
// Load the shader in memory
NSString *shaderPath = [[NSBundle mainBundle] pathForResource:shaderName ofType:@"glsl"];
NSError *error;
NSString *shaderString = [NSString stringWithContentsOfFile:shaderPath encoding:NSUTF8StringEncoding error:&error];
if(!shaderString)
{
NSLog(@"Error loading shader: %@", error.localizedDescription);
exit(1);
}
// Create the shader inside openGL
GLuint shaderHandle = glCreateShader(shaderType);
// Give that shader the source code loaded in memory
const char *shaderStringUTF8 = [shaderString UTF8String];
int shaderStringLength = [shaderString length];
glShaderSource(shaderHandle, 1, &shaderStringUTF8, &shaderStringLength);
// Compile the source code
glCompileShader(shaderHandle);
// Get the error messages in case the compiling has failed
GLint compileSuccess;
glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &compileSuccess);
if (compileSuccess == GL_FALSE) {
GLint logLength;
glGetShaderiv(shaderHandle, GL_INFO_LOG_LENGTH, &logLength);
if(logLength > 0)
{
GLchar *log = (GLchar *)malloc(logLength);
glGetShaderInfoLog(shaderHandle, logLength, &logLength, log);
NSLog(@"Shader compile log:\n%s", log);
free(log);
}
exit(1);
}
return shaderHandle;
}
-(void)createProgram
{
// Compile both shaders
GLuint vertexShader = [self compileShader:VERTEX_SHADER withType:GL_VERTEX_SHADER];
GLuint fragmentShader = [self compileShader:FRAGMENT_SHADER withType:GL_FRAGMENT_SHADER];
// Create the program in openGL, attach the shaders and link them
GLuint programHandle = glCreateProgram();
glAttachShader(programHandle, vertexShader);
glAttachShader(programHandle, fragmentShader);
glLinkProgram(programHandle);
// Get the error message in case the linking has failed
GLint linkSuccess;
glGetProgramiv(programHandle, GL_LINK_STATUS, &linkSuccess);
if (linkSuccess == GL_FALSE)
{
GLint logLength;
glGetProgramiv(programHandle, GL_INFO_LOG_LENGTH, &logLength);
if(logLength > 0)
{
GLchar *log = (GLchar *)malloc(logLength);
glGetProgramInfoLog(programHandle, logLength, &logLength, log);
NSLog(@"Program link log:\n%s", log);
free(log);
}
exit(1);
}
_program = programHandle;
}
-(void)getUniforms
{
GLint maxUniformLength;
GLint numberOfUniforms;
char *uniformName;
// Get the number of uniforms and the max length of their names
glGetProgramiv(_program, GL_ACTIVE_UNIFORMS, &numberOfUniforms);
glGetProgramiv(_program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniformLength);
_uniformArray = malloc(numberOfUniforms * sizeof(Uniform));
_uniformArraySize = numberOfUniforms;
for(int i = 0; i < numberOfUniforms; i++)
{
GLint size;
GLenum type;
GLint location;
// Get the Uniform Info
uniformName = malloc(sizeof(char) * maxUniformLength);
glGetActiveUniform(_program, i, maxUniformLength, NULL, &size, &type, uniformName);
_uniformArray[i].Name = uniformName;
// Get the uniform location
location = glGetUniformLocation(_program, uniformName);
_uniformArray[i].Location = location;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Create context
self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
if (!self.context) {
NSLog(@"Failed to create ES context");
exit(1);
}
if (![EAGLContext setCurrentContext:self.context])
{
NSLog(@"Failed to set current OpenGL context");
exit(1);
}
// Initialize view
GLKView *view = (GLKView *)self.view;
view.context = self.context;
// Change the format of the depth renderbuffer
// This value is None by default
view.drawableDepthFormat = GLKViewDrawableDepthFormat16;
// Enable face culling and depth test
glEnable( GL_DEPTH_TEST );
glEnable( GL_CULL_FACE );
// Set up the viewport
int width = view.bounds.size.width;
int height = view.bounds.size.height;
glViewport(0, 0, width, height);
[self createProgram];
[self getUniforms];
// Make the vertex buffer
glGenBuffers( 1, &_verticesVBO );
glBindBuffer( GL_ARRAY_BUFFER, _verticesVBO );
glBufferData( GL_ARRAY_BUFFER, sizeof(CubeVertexData), CubeVertexData, GL_STATIC_DRAW );
glBindBuffer( GL_ARRAY_BUFFER, 0 );
// Make the indices buffer
glGenBuffers( 1, &_indicesVBO );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, _indicesVBO );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof(CubeIndicesData), CubeIndicesData, GL_STATIC_DRAW );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
// Make the indices buffer2
glGenBuffers( 1, &_indicesVBO2 );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, _indicesVBO2 );
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof(Cube2IndicesData), Cube2IndicesData, GL_STATIC_DRAW );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
// Make the vertex buffer2
glGenBuffers( 1, &_verticesVBO2 );
glBindBuffer( GL_ARRAY_BUFFER, _verticesVBO2 );
glBufferData( GL_ARRAY_BUFFER, sizeof(Cube2VertexData), Cube2VertexData, GL_STATIC_DRAW );
glBindBuffer( GL_ARRAY_BUFFER, 0 );
// Bind the attribute pointers to the VAO
GLint attribute;
GLsizei stride = sizeof(GLfloat) * 3;
glGenVertexArraysOES( 1, &_VAO );
glBindVertexArrayOES( _VAO );
glBindBuffer( GL_ARRAY_BUFFER, _verticesVBO );
glBindBuffer( GL_ARRAY_BUFFER, _verticesVBO2 );
attribute = glGetAttribLocation(_program, "Position");
glEnableVertexAttribArray( attribute );
glVertexAttribPointer( attribute, 3, GL_FLOAT, GL_FALSE, stride, NULL );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, _indicesVBO );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, _indicesVBO2 );
glBindVertexArrayOES( 0 );
_modelViewMatrix = GLKMatrix4MakeLookAt(2.0f, 2.0f, 4.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
_projectionMatrix = GLKMatrix4MakePerspective(45.0f, (float)width/(float)height, 0.01f, 100.0f);
}
- (void)viewDidUnload
{
[super viewDidUnload];
[EAGLContext setCurrentContext:self.context];
glDeleteBuffers(1, &_verticesVBO);
glDeleteBuffers(1, &_indicesVBO);
glDeleteVertexArraysOES(1, &_VAO);
if (_program) {
glDeleteProgram(_program);
_program = 0;
}
for (int i = 0; i < _uniformArraySize; i++) {
free(_uniformArray[i].Name);
}
free(_uniformArray);
} |