83
static VertexArray* cylinderva = nil;
83
85
+ (VertexArray*) sharedCylinder
85
static VertexArray* va = nil;
87
VertexArray* va = cylinderva;
88
90
va = [[VertexArray alloc] init];
89
92
size_t divisions = 72;
91
94
va->numVertices = (divisions+1)*2;
128
static VertexArray* diskva = nil;
125
130
+ (VertexArray*) sharedDisk
127
static VertexArray* va = nil;
132
VertexArray* va = diskva;
130
135
va = [[VertexArray alloc] init];
132
138
unsigned divisions = 72;
175
static VertexArray* quadva = nil;
169
177
+ (VertexArray*) sharedQuad
171
static VertexArray* va = nil;
179
VertexArray* va = quadva;
174
182
va = [[VertexArray alloc] init];
177
185
va->numVertices = 4;
178
186
va->vertices = calloc(va->numVertices, sizeof(*va->vertices));
181
189
va->vertices[0].pos[1] = -1.0;
182
190
va->vertices[0].normal[2] = 1.0;
183
191
va->vertices[0].texcoord[0] = 1.0;
184
va->vertices[0].texcoord[1] = 1.0;
192
va->vertices[0].texcoord[1] = 0.0;
185
193
va->vertices[1].pos[0] = 1.0;
186
194
va->vertices[1].pos[1] = 1.0;
187
195
va->vertices[1].normal[2] = 1.0;
188
196
va->vertices[1].texcoord[0] = 1.0;
189
va->vertices[1].texcoord[1] = 0.0;
197
va->vertices[1].texcoord[1] = 1.0;
190
198
va->vertices[3].pos[0] = -1.0;
191
199
va->vertices[3].pos[1] = 1.0;
192
200
va->vertices[3].normal[2] = 1.0;
193
201
va->vertices[3].texcoord[0] = 0.0;
194
va->vertices[3].texcoord[1] = 0.0;
202
va->vertices[3].texcoord[1] = 1.0;
195
203
va->vertices[2].pos[0] = -1.0;
196
204
va->vertices[2].pos[1] = -1.0;
197
205
va->vertices[2].normal[2] = 1.0;
198
206
va->vertices[2].texcoord[0] = 0.0;
199
va->vertices[2].texcoord[1] = 1.0;
207
va->vertices[2].texcoord[1] = 0.0;
202
210
va->mode = GL_TRIANGLE_STRIP;
214
static VertexArray* sphereva = nil;
207
216
+ (VertexArray*) sharedSphere
209
static VertexArray* va = nil;
218
VertexArray* va = sphereva;
212
221
va = [[VertexArray alloc] init];
214
224
unsigned londivisions = 72;
215
225
unsigned latdivisions = 36;
283
static VertexArray* xsemisphereva = nil;
273
285
+ (VertexArray*) sharedXSemiSphere
275
static VertexArray* va = nil;
287
VertexArray* va = xsemisphereva;
278
290
va = [[VertexArray alloc] init];
280
294
unsigned londivisions = 36;
281
295
unsigned latdivisions = 36;
346
374
usageHint = GL_STATIC_DRAW;
376
float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
378
if (osVersion >= 4.0f)
353
387
if (vertexBufferName)
354
388
glDeleteBuffers(1, &vertexBufferName);
355
389
if (indexBufferName)
356
390
glDeleteBuffers(1, &indexBufferName);
391
if (vertexArrayObject)
392
glDeleteVertexArraysOES(1, &vertexArrayObject);
380
416
memcpy(vertices, [[coder decodeObjectForKey: @"vertices"] bytes], sizeof(*vertices)*numVertices);
381
417
memcpy(indices, [[coder decodeObjectForKey: @"indices"] bytes], sizeof(*indices)*numIndices);
419
float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
421
if (osVersion >= 4.0f)
394
434
[coder encodeObject: [NSData dataWithBytes: indices length: sizeof(*indices)*numIndices] forKey: @"indices"];
437
- (void) setupArrayState
439
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferName);
442
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferName);
444
EAGLRenderingAPI api = [[EAGLContext currentContext] API];
448
case kEAGLRenderingAPIOpenGLES2:
449
glVertexAttribPointer(POSITION_ATTRIB_INDEX, 3, GL_FLOAT, GL_FALSE, sizeof(PositionNormalTex3f3f2f), (void*)offsetof(PositionNormalTex3f3f2f,pos));
450
glVertexAttribPointer(NORMAL_ATTRIB_INDEX, 3, GL_FLOAT, GL_FALSE, sizeof(PositionNormalTex3f3f2f), (void*)offsetof(PositionNormalTex3f3f2f,normal));
451
glVertexAttribPointer(TEXCOORD_ATTRIB_INDEX, 2, GL_FLOAT, GL_FALSE, sizeof(PositionNormalTex3f3f2f), (void*)offsetof(PositionNormalTex3f3f2f,texcoord));
453
glEnableVertexAttribArray(POSITION_ATTRIB_INDEX);
454
glEnableVertexAttribArray(NORMAL_ATTRIB_INDEX);
455
glEnableVertexAttribArray(TEXCOORD_ATTRIB_INDEX);
459
case kEAGLRenderingAPIOpenGLES1:
460
glNormalPointer(GL_FLOAT, sizeof(PositionNormalTex3f3f2f), (void*)offsetof(PositionNormalTex3f3f2f,normal));
461
glTexCoordPointer(3, GL_FLOAT, sizeof(PositionNormalTex3f3f2f), (void*)offsetof(PositionNormalTex3f3f2f,texcoord));
462
glVertexPointer(3, GL_FLOAT, sizeof(PositionNormalTex3f3f2f), (void*)offsetof(PositionNormalTex3f3f2f,pos));
464
glEnableClientState(GL_VERTEX_ARRAY);
465
glEnableClientState(GL_NORMAL_ARRAY);
466
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
471
[[NSException exceptionWithName: @"com.elmonkey.invalidShader" reason: @" OpenGL ES API selected not supported by VertexArray." userInfo: nil] raise];
397
477
- (void) generateVBO
399
479
if (!vertexBufferName)
409
489
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 2*numIndices, indices, usageHint);
492
if (!vertexArrayObject && useVAO)
494
glGenVertexArraysOES(1, &vertexArrayObject);
495
glBindVertexArrayOES(vertexArrayObject);
497
[self setupArrayState];
498
glBindVertexArrayOES(0);
414
502
- (void) convertTriangleIndicesToLines
452
540
if (!vertexBufferName)
453
541
[self generateVBO];
455
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferName);
545
glBindVertexArrayOES(vertexArrayObject);
549
[self setupArrayState];
458
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferName);
460
EAGLRenderingAPI api = [[EAGLContext currentContext] API];
464
case kEAGLRenderingAPIOpenGLES2:
465
glVertexAttribPointer(POSITION_ATTRIB_INDEX, 3, GL_FLOAT, GL_FALSE, sizeof(PositionNormalTex3f3f2f), (void*)offsetof(PositionNormalTex3f3f2f,pos));
466
glVertexAttribPointer(NORMAL_ATTRIB_INDEX, 3, GL_FLOAT, GL_FALSE, sizeof(PositionNormalTex3f3f2f), (void*)offsetof(PositionNormalTex3f3f2f,normal));
467
glVertexAttribPointer(TEXCOORD_ATTRIB_INDEX, 2, GL_FLOAT, GL_FALSE, sizeof(PositionNormalTex3f3f2f), (void*)offsetof(PositionNormalTex3f3f2f,texcoord));
469
glEnableVertexAttribArray(POSITION_ATTRIB_INDEX);
470
glEnableVertexAttribArray(NORMAL_ATTRIB_INDEX);
471
glEnableVertexAttribArray(TEXCOORD_ATTRIB_INDEX);
475
case kEAGLRenderingAPIOpenGLES1:
476
glNormalPointer(GL_FLOAT, sizeof(PositionNormalTex3f3f2f), (void*)offsetof(PositionNormalTex3f3f2f,normal));
477
glTexCoordPointer(3, GL_FLOAT, sizeof(PositionNormalTex3f3f2f), (void*)offsetof(PositionNormalTex3f3f2f,texcoord));
478
glVertexPointer(3, GL_FLOAT, sizeof(PositionNormalTex3f3f2f), (void*)offsetof(PositionNormalTex3f3f2f,pos));
480
glEnableClientState(GL_VERTEX_ARRAY);
481
glEnableClientState(GL_NORMAL_ARRAY);
482
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
487
[[NSException exceptionWithName: @"com.elmonkey.invalidShader" reason: @" OpenGL ES API selected not supported by VertexArray." userInfo: nil] raise];
491
552
if (!indexBufferName)
492
553
glDrawArrays(mode, 0, numVertices);
494
555
glDrawElements(mode, numIndices, GL_UNSIGNED_SHORT, (void*)0);
559
glBindVertexArrayOES(0);
496
561
GLenum err = glGetError();