RSS

(root)/iphone/common : 73 : source/VertexArray.m

To get this branch, use:
bzr branch /browse/iphone/common

« back to all changes in this revision

Viewing changes to source/VertexArray.m

Dömötör Gulyás
2010-08-10 23:18:22
Revision ID: dognotdog@gmail.com-20100810211822-dr963e78lkbsk96c
now allows variable format for vertex arrays; fixes bug with shared VAs not being properly initialized; adds method to remove normals from VA, to save memory and bandwidth; adds some threading fixes in GLESView, as well as more thorough state management

Show diffs side-by-side

added added

removed removed

40
40
 
41
41
@implementation VertexArray
42
42
 
 
43
static inline size_t _sizeofFormat(int format)
 
44
{
 
45
        switch(format)
 
46
        {
 
47
                case VA_FORMAT_POS3F:
 
48
                        return sizeof(Position3f);
 
49
                case VA_FORMAT_POS3F_TEX2F:
 
50
                        return sizeof(PositionTex3f2f);
 
51
                case VA_FORMAT_POS3F_NORMAL3F_TEX2F:
 
52
                        return sizeof(PositionNormalTex3f3f2f);
 
53
                default:
 
54
                        return -1;
 
55
        }
 
56
}
 
57
 
 
58
- (size_t) sizeofFormat
 
59
{
 
60
        return _sizeofFormat(vertexFormat);
 
61
}
 
62
 
43
63
+ (VertexArray*) coneWithFraction: (float) fraction
44
64
{
45
65
        VertexArray* va = [[[VertexArray alloc] init] autorelease];
46
66
 
47
67
        size_t divisions = 72;
48
68
        
 
69
        
 
70
        
49
71
        va->numVertices = (divisions+1)*2;
50
 
        va->vertices = malloc(va->numVertices*sizeof(*va->vertices));
 
72
        PositionNormalTex3f3f2f* vertices = calloc(va->numVertices,sizeof(*vertices));
 
73
        va->_vertices = vertices;
 
74
        va->vertexFormat = VA_FORMAT_POS3F_NORMAL3F_TEX2F;
 
75
 
51
76
        for (size_t i = 0; i <= divisions; ++i)
52
77
        {
53
78
                float t = (float)i/(float)divisions;
55
80
                float sina = sinf(alpha);
56
81
                float cosa = cosf(alpha);
57
82
                
58
 
                va->vertices[2*i].pos[0] = cosa;
59
 
                va->vertices[2*i].pos[1] = sina;
60
 
                va->vertices[2*i].pos[2] = 0.0;
61
 
                va->vertices[2*i+1].pos[0] = (1.0-fraction)*cosa;
62
 
                va->vertices[2*i+1].pos[1] = (1.0-fraction)*sina;
63
 
                va->vertices[2*i+1].pos[2] = fraction;
64
 
 
65
 
                va->vertices[2*i].normal[0] = cosa*M_SQRT1_2;
66
 
                va->vertices[2*i].normal[1] = sina*M_SQRT1_2;
67
 
                va->vertices[2*i].normal[2] = M_SQRT1_2;
68
 
                va->vertices[2*i+1].normal[0] = cosa*M_SQRT1_2;
69
 
                va->vertices[2*i+1].normal[1] = sina*M_SQRT1_2;
70
 
                va->vertices[2*i+1].normal[2] = M_SQRT1_2;
71
 
 
72
 
                va->vertices[2*i].texcoord[0] = 1.0-t;
73
 
                va->vertices[2*i].texcoord[1] = 0.0;
74
 
                va->vertices[2*i+1].texcoord[0] = 1.0-t;
75
 
                va->vertices[2*i+1].texcoord[1] = 1.0;
 
83
                vertices[2*i].pos[0] = cosa;
 
84
                vertices[2*i].pos[1] = sina;
 
85
                vertices[2*i].pos[2] = 0.0;
 
86
                vertices[2*i+1].pos[0] = (1.0-fraction)*cosa;
 
87
                vertices[2*i+1].pos[1] = (1.0-fraction)*sina;
 
88
                vertices[2*i+1].pos[2] = fraction;
 
89
 
 
90
                vertices[2*i].normal[0] = cosa*M_SQRT1_2;
 
91
                vertices[2*i].normal[1] = sina*M_SQRT1_2;
 
92
                vertices[2*i].normal[2] = M_SQRT1_2;
 
93
                vertices[2*i+1].normal[0] = cosa*M_SQRT1_2;
 
94
                vertices[2*i+1].normal[1] = sina*M_SQRT1_2;
 
95
                vertices[2*i+1].normal[2] = M_SQRT1_2;
 
96
 
 
97
                vertices[2*i].texcoord[0] = 1.0-t;
 
98
                vertices[2*i].texcoord[1] = 0.0;
 
99
                vertices[2*i+1].texcoord[0] = 1.0-t;
 
100
                vertices[2*i+1].texcoord[1] = 1.0;
76
101
        }
77
102
        
78
103
        va->mode = GL_TRIANGLE_STRIP;
92
117
                size_t divisions = 72;
93
118
                
94
119
                va->numVertices = (divisions+1)*2;
95
 
                va->vertices = malloc(va->numVertices*sizeof(*va->vertices));
 
120
                PositionNormalTex3f3f2f* vertices = calloc(va->numVertices,sizeof(*vertices));
 
121
                va->_vertices = vertices;
 
122
                va->vertexFormat = VA_FORMAT_POS3F_NORMAL3F_TEX2F;
 
123
                
96
124
                for (size_t i = 0; i <= divisions; ++i)
97
125
                {
98
126
                        float t = (float)i/(float)divisions;
100
128
                        float sina = sinf(alpha);
101
129
                        float cosa = cosf(alpha);
102
130
                        
103
 
                        va->vertices[2*i].pos[0] = cosa;
104
 
                        va->vertices[2*i].pos[1] = sina;
105
 
                        va->vertices[2*i].pos[2] = 1.0;
106
 
                        va->vertices[2*i+1].pos[0] = cosa;
107
 
                        va->vertices[2*i+1].pos[1] = sina;
108
 
                        va->vertices[2*i+1].pos[2] = -1.0;
109
 
 
110
 
                        va->vertices[2*i].normal[0] = cosa;
111
 
                        va->vertices[2*i].normal[1] = sina;
112
 
                        va->vertices[2*i].normal[2] = 0.0;
113
 
                        va->vertices[2*i+1].normal[0] = cosa;
114
 
                        va->vertices[2*i+1].normal[1] = sina;
115
 
                        va->vertices[2*i+1].normal[2] = 0.0;
116
 
 
117
 
                        va->vertices[2*i].texcoord[0] = t;
118
 
                        va->vertices[2*i].texcoord[1] = 0.0;
119
 
                        va->vertices[2*i+1].texcoord[0] = t;
120
 
                        va->vertices[2*i+1].texcoord[1] = 1.0;
 
131
                        vertices[2*i].pos[0] = cosa;
 
132
                        vertices[2*i].pos[1] = sina;
 
133
                        vertices[2*i].pos[2] = 1.0;
 
134
                        vertices[2*i+1].pos[0] = cosa;
 
135
                        vertices[2*i+1].pos[1] = sina;
 
136
                        vertices[2*i+1].pos[2] = -1.0;
 
137
 
 
138
                        vertices[2*i].normal[0] = cosa;
 
139
                        vertices[2*i].normal[1] = sina;
 
140
                        vertices[2*i].normal[2] = 0.0;
 
141
                        vertices[2*i+1].normal[0] = cosa;
 
142
                        vertices[2*i+1].normal[1] = sina;
 
143
                        vertices[2*i+1].normal[2] = 0.0;
 
144
 
 
145
                        vertices[2*i].texcoord[0] = t;
 
146
                        vertices[2*i].texcoord[1] = 0.0;
 
147
                        vertices[2*i+1].texcoord[0] = t;
 
148
                        vertices[2*i+1].texcoord[1] = 1.0;
121
149
                }
122
150
        }
123
151
        
138
166
                unsigned divisions = 72;
139
167
                
140
168
                va->numVertices = (divisions+1) + 1;
141
 
                va->vertices = malloc(va->numVertices*sizeof(*va->vertices));
 
169
                PositionNormalTex3f3f2f* vertices = calloc(va->numVertices,sizeof(*vertices));
 
170
                va->_vertices = vertices;
 
171
                va->vertexFormat = VA_FORMAT_POS3F_NORMAL3F_TEX2F;
142
172
 
143
 
                va->vertices->pos[0] = 0.0;
144
 
                va->vertices->pos[1] = 0.0;
145
 
                va->vertices->pos[2] = 0.0;
146
 
                va->vertices->normal[0] = 0.0;
147
 
                va->vertices->normal[1] = 0.0;
148
 
                va->vertices->normal[2] = 1.0;
149
 
                va->vertices->texcoord[0] = 0.5;
150
 
                va->vertices->texcoord[1] = 0.5;
 
173
                vertices->pos[0] = 0.0;
 
174
                vertices->pos[1] = 0.0;
 
175
                vertices->pos[2] = 0.0;
 
176
                vertices->normal[0] = 0.0;
 
177
                vertices->normal[1] = 0.0;
 
178
                vertices->normal[2] = 1.0;
 
179
                vertices->texcoord[0] = 0.5;
 
180
                vertices->texcoord[1] = 0.5;
151
181
                for (size_t i = 0; i <= divisions; ++i)
152
182
                {
153
183
                        float t = (float)i/(float)divisions;
155
185
                        float sina = sinf(alpha);
156
186
                        float cosa = cosf(alpha);
157
187
                        
158
 
                        va->vertices[i+1].pos[0] = cosa;
159
 
                        va->vertices[i+1].pos[1] = sina;
160
 
                        va->vertices[i+1].pos[2] = 0.0;
161
 
 
162
 
                        va->vertices[i+1].normal[0] = 0.0;
163
 
                        va->vertices[i+1].normal[1] = 0.0;
164
 
                        va->vertices[i+1].normal[2] = 1.0;
165
 
 
166
 
                        va->vertices[i+1].texcoord[0] = 0.5 + 0.5*cosa;
167
 
                        va->vertices[i+1].texcoord[1] = 0.5 - 0.5*sina;
 
188
                        vertices[i+1].pos[0] = cosa;
 
189
                        vertices[i+1].pos[1] = sina;
 
190
                        vertices[i+1].pos[2] = 0.0;
 
191
 
 
192
                        vertices[i+1].normal[0] = 0.0;
 
193
                        vertices[i+1].normal[1] = 0.0;
 
194
                        vertices[i+1].normal[2] = 1.0;
 
195
 
 
196
                        vertices[i+1].texcoord[0] = 0.5 + 0.5*cosa;
 
197
                        vertices[i+1].texcoord[1] = 0.5 - 0.5*sina;
168
198
                }
169
199
        }
170
200
        
183
213
                quadva = va;
184
214
                
185
215
                va->numVertices = 4;
186
 
                va->vertices = calloc(va->numVertices, sizeof(*va->vertices));
 
216
                PositionNormalTex3f3f2f* vertices = calloc(va->numVertices, sizeof(*vertices));
 
217
                va->_vertices = vertices;
 
218
                va->vertexFormat = VA_FORMAT_POS3F_NORMAL3F_TEX2F;
 
219
                /*
 
220
                va->numIndices = 4;
 
221
                va->indices = calloc(sizeof(*va->indices), va->numIndices);
 
222
                for (size_t i = 0; i < 4; ++i)
 
223
                        va->indices[i] = i;
 
224
                */
187
225
 
188
 
                va->vertices[0].pos[0] = 1.0;
189
 
                va->vertices[0].pos[1] = -1.0;
190
 
                va->vertices[0].normal[2] = 1.0;
191
 
                va->vertices[0].texcoord[0] = 1.0;
192
 
                va->vertices[0].texcoord[1] = 0.0;
193
 
                va->vertices[1].pos[0] = 1.0;
194
 
                va->vertices[1].pos[1] = 1.0;
195
 
                va->vertices[1].normal[2] = 1.0;
196
 
                va->vertices[1].texcoord[0] = 1.0;
197
 
                va->vertices[1].texcoord[1] = 1.0;
198
 
                va->vertices[3].pos[0] = -1.0;
199
 
                va->vertices[3].pos[1] = 1.0;
200
 
                va->vertices[3].normal[2] = 1.0;
201
 
                va->vertices[3].texcoord[0] = 0.0;
202
 
                va->vertices[3].texcoord[1] = 1.0;
203
 
                va->vertices[2].pos[0] = -1.0;
204
 
                va->vertices[2].pos[1] = -1.0;
205
 
                va->vertices[2].normal[2] = 1.0;
206
 
                va->vertices[2].texcoord[0] = 0.0;
207
 
                va->vertices[2].texcoord[1] = 0.0;
 
226
                vertices[0].pos[0] = 1.0;
 
227
                vertices[0].pos[1] = -1.0;
 
228
                vertices[0].normal[2] = 1.0;
 
229
                vertices[0].texcoord[0] = 1.0;
 
230
                vertices[0].texcoord[1] = 0.0;
 
231
                vertices[1].pos[0] = 1.0;
 
232
                vertices[1].pos[1] = 1.0;
 
233
                vertices[1].normal[2] = 1.0;
 
234
                vertices[1].texcoord[0] = 1.0;
 
235
                vertices[1].texcoord[1] = 1.0;
 
236
                vertices[3].pos[0] = -1.0;
 
237
                vertices[3].pos[1] = 1.0;
 
238
                vertices[3].normal[2] = 1.0;
 
239
                vertices[3].texcoord[0] = 0.0;
 
240
                vertices[3].texcoord[1] = 1.0;
 
241
                vertices[2].pos[0] = -1.0;
 
242
                vertices[2].pos[1] = -1.0;
 
243
                vertices[2].normal[2] = 1.0;
 
244
                vertices[2].texcoord[0] = 0.0;
 
245
                vertices[2].texcoord[1] = 0.0;
208
246
        }
209
247
        
210
248
        va->mode = GL_TRIANGLE_STRIP;
225
263
                unsigned latdivisions = 36;
226
264
                
227
265
                va->numVertices = (londivisions+1)*(latdivisions+1);
228
 
                va->vertices = malloc(va->numVertices*sizeof(*va->vertices));
 
266
                PositionNormalTex3f3f2f* vertices = calloc(va->numVertices,sizeof(*vertices));
 
267
                va->_vertices = vertices;
 
268
                va->vertexFormat = VA_FORMAT_POS3F_NORMAL3F_TEX2F;
229
269
                
230
270
                int soff = (latdivisions+1);
231
271
 
242
282
                                float sinj = sinf(aj);
243
283
                                float cosj = cosf(aj);
244
284
                                
245
 
                                va->vertices[i*soff + j].pos[0] = sinj*cosi;
246
 
                                va->vertices[i*soff + j].pos[1] = sinj*sini;
247
 
                                va->vertices[i*soff + j].pos[2] = cosj;
248
 
 
249
 
                                va->vertices[i*soff + j].normal[0] = sinj*cosi;
250
 
                                va->vertices[i*soff + j].normal[1] = sinj*sini;
251
 
                                va->vertices[i*soff + j].normal[2] = cosj;
252
 
 
253
 
                                va->vertices[i*soff + j].texcoord[0] = ti;
254
 
                                va->vertices[i*soff + j].texcoord[1] = tj;
 
285
                                vertices[i*soff + j].pos[0] = sinj*cosi;
 
286
                                vertices[i*soff + j].pos[1] = sinj*sini;
 
287
                                vertices[i*soff + j].pos[2] = cosj;
 
288
 
 
289
                                vertices[i*soff + j].normal[0] = sinj*cosi;
 
290
                                vertices[i*soff + j].normal[1] = sinj*sini;
 
291
                                vertices[i*soff + j].normal[2] = cosj;
 
292
 
 
293
                                vertices[i*soff + j].texcoord[0] = ti;
 
294
                                vertices[i*soff + j].texcoord[1] = tj;
255
295
                        }
256
296
                }
257
297
 
258
298
                va->numIndices = (londivisions)*(latdivisions+1)*2;
259
 
                va->indices = malloc(2*va->numIndices);
 
299
                va->indices = calloc(2*va->numIndices,sizeof(*va->indices));
260
300
                
261
301
                uint16_t* indices = va->indices;
262
302
                
295
335
                unsigned latdivisions = 36;
296
336
                
297
337
                va->numVertices = (londivisions+1)*(latdivisions+1);
298
 
                va->vertices = malloc(va->numVertices*sizeof(*va->vertices));
 
338
                PositionNormalTex3f3f2f* vertices = calloc(va->numVertices,sizeof(*vertices));
 
339
                va->_vertices = vertices;
 
340
                va->vertexFormat = VA_FORMAT_POS3F_NORMAL3F_TEX2F;
299
341
                
300
342
                int soff = (latdivisions+1);
301
343
 
312
354
                                float sinj = sinf(aj);
313
355
                                float cosj = cosf(aj);
314
356
                                
315
 
                                va->vertices[i*soff + j].pos[0] = sinj*cosi;
316
 
                                va->vertices[i*soff + j].pos[1] = sinj*sini;
317
 
                                va->vertices[i*soff + j].pos[2] = cosj;
318
 
 
319
 
                                va->vertices[i*soff + j].normal[0] = sinj*cosi;
320
 
                                va->vertices[i*soff + j].normal[1] = sinj*sini;
321
 
                                va->vertices[i*soff + j].normal[2] = cosj;
322
 
 
323
 
                                va->vertices[i*soff + j].texcoord[0] = ti;
324
 
                                va->vertices[i*soff + j].texcoord[1] = tj;
 
357
                                vertices[i*soff + j].pos[0] = sinj*cosi;
 
358
                                vertices[i*soff + j].pos[1] = sinj*sini;
 
359
                                vertices[i*soff + j].pos[2] = cosj;
 
360
 
 
361
                                vertices[i*soff + j].normal[0] = sinj*cosi;
 
362
                                vertices[i*soff + j].normal[1] = sinj*sini;
 
363
                                vertices[i*soff + j].normal[2] = cosj;
 
364
 
 
365
                                vertices[i*soff + j].texcoord[0] = ti;
 
366
                                vertices[i*soff + j].texcoord[1] = tj;
325
367
                        }
326
368
                }
327
369
 
328
370
                va->numIndices = (londivisions)*(latdivisions+1)*2;
329
 
                va->indices = malloc(2*va->numIndices);
 
371
                va->indices = calloc(2*va->numIndices,sizeof(*va->indices));
330
372
                
331
373
                uint16_t* indices = va->indices;
332
374
                
372
414
 
373
415
        
374
416
        usageHint = GL_STATIC_DRAW;
 
417
        vertexFormat = VA_FORMAT_POS3F_NORMAL3F_TEX2F;
375
418
        
376
419
        float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
377
420
        
391
434
        if (vertexArrayObject)
392
435
                glDeleteVertexArraysOES(1, &vertexArrayObject);
393
436
        
394
 
        if (vertices)
395
 
                free(vertices);
 
437
        if (_vertices)
 
438
                free(_vertices);
396
439
 
397
440
        if (indices)
398
441
                free(indices);
407
450
 
408
451
        usageHint = [coder decodeIntegerForKey: @"usageHint"];
409
452
        mode = [coder decodeIntegerForKey: @"mode"];
 
453
        vertexFormat = [coder decodeIntegerForKey: @"vertexFormat"];
410
454
        numVertices = [coder decodeIntegerForKey: @"numVertices"];
411
455
        numIndices = [coder decodeIntegerForKey: @"numIndices"];
412
456
        
413
 
        vertices = malloc(sizeof(*vertices)*numVertices);
414
 
        indices = malloc(sizeof(*indices)*numIndices);
 
457
        _vertices = calloc(_sizeofFormat(vertexFormat),numVertices);
 
458
        indices = calloc(sizeof(*indices),numIndices);
415
459
        
416
 
        memcpy(vertices, [[coder decodeObjectForKey: @"vertices"] bytes], sizeof(*vertices)*numVertices);
 
460
        memcpy(_vertices, [[coder decodeObjectForKey: @"vertices"] bytes], _sizeofFormat(vertexFormat)*numVertices);
417
461
        memcpy(indices, [[coder decodeObjectForKey: @"indices"] bytes], sizeof(*indices)*numIndices);
418
462
 
419
463
        float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
428
472
{
429
473
        [coder encodeInteger: usageHint forKey: @"usageHint"];
430
474
        [coder encodeInteger: mode forKey: @"mode"];
 
475
        [coder encodeInteger: vertexFormat forKey: @"vertexFormat"];
431
476
        [coder encodeInteger: numVertices forKey: @"numVertices"];
432
477
        [coder encodeInteger: numIndices forKey: @"numIndices"];
433
 
        [coder encodeObject: [NSData dataWithBytes: vertices length: sizeof(*vertices)*numVertices] forKey: @"vertices"];
 
478
        [coder encodeObject: [NSData dataWithBytes: _vertices length: _sizeofFormat(vertexFormat)*numVertices] forKey: @"vertices"];
434
479
        [coder encodeObject: [NSData dataWithBytes: indices length: sizeof(*indices)*numIndices] forKey: @"indices"];
435
480
}
436
481
 
440
485
        
441
486
        if (indexBufferName)
442
487
                glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferName);
 
488
        else
 
489
                glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
443
490
 
444
491
        EAGLRenderingAPI api = [[EAGLContext currentContext] API];
445
492
 
446
493
        switch(api)
447
494
        {
448
495
                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));
452
 
 
453
 
                        glEnableVertexAttribArray(POSITION_ATTRIB_INDEX);
454
 
                        glEnableVertexAttribArray(NORMAL_ATTRIB_INDEX);
455
 
                        glEnableVertexAttribArray(TEXCOORD_ATTRIB_INDEX);
456
 
 
 
496
                        switch (vertexFormat)
 
497
                        {
 
498
                                case VA_FORMAT_POS3F_NORMAL3F_TEX2F:
 
499
                                        glVertexAttribPointer(POSITION_ATTRIB_INDEX, 3, GL_FLOAT, GL_FALSE, sizeof(PositionNormalTex3f3f2f), (void*)offsetof(PositionNormalTex3f3f2f,pos));
 
500
                                        glVertexAttribPointer(NORMAL_ATTRIB_INDEX, 3, GL_FLOAT, GL_FALSE, sizeof(PositionNormalTex3f3f2f), (void*)offsetof(PositionNormalTex3f3f2f,normal));
 
501
                                        glVertexAttribPointer(TEXCOORD_ATTRIB_INDEX, 2, GL_FLOAT, GL_FALSE, sizeof(PositionNormalTex3f3f2f), (void*)offsetof(PositionNormalTex3f3f2f,texcoord));
 
502
 
 
503
                                        glEnableVertexAttribArray(POSITION_ATTRIB_INDEX);
 
504
                                        glEnableVertexAttribArray(NORMAL_ATTRIB_INDEX);
 
505
                                        glEnableVertexAttribArray(TEXCOORD_ATTRIB_INDEX);
 
506
 
 
507
                                        break;
 
508
                                case VA_FORMAT_POS3F_TEX2F:
 
509
                                        glVertexAttribPointer(POSITION_ATTRIB_INDEX, 3, GL_FLOAT, GL_FALSE, sizeof(PositionTex3f2f), (void*)offsetof(PositionTex3f2f,pos));
 
510
                                        glVertexAttribPointer(TEXCOORD_ATTRIB_INDEX, 2, GL_FLOAT, GL_FALSE, sizeof(PositionTex3f2f), (void*)offsetof(PositionTex3f2f,texcoord));
 
511
 
 
512
                                        glEnableVertexAttribArray(POSITION_ATTRIB_INDEX);
 
513
                                        glDisableVertexAttribArray(NORMAL_ATTRIB_INDEX);
 
514
                                        glEnableVertexAttribArray(TEXCOORD_ATTRIB_INDEX);
 
515
 
 
516
                                        break;
 
517
                                case VA_FORMAT_POS3F:
 
518
                                        glVertexAttribPointer(POSITION_ATTRIB_INDEX, 3, GL_FLOAT, GL_FALSE, sizeof(Position3f), (void*)offsetof(Position3f,pos));
 
519
 
 
520
                                        glEnableVertexAttribArray(POSITION_ATTRIB_INDEX);
 
521
                                        glDisableVertexAttribArray(NORMAL_ATTRIB_INDEX);
 
522
                                        glDisableVertexAttribArray(TEXCOORD_ATTRIB_INDEX);
 
523
 
 
524
                                        break;
 
525
                                default:
 
526
                                        assert(0);
 
527
                        }
457
528
                        break;
458
529
 
459
530
                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));
463
 
                        
464
 
                        glEnableClientState(GL_VERTEX_ARRAY);
465
 
                        glEnableClientState(GL_NORMAL_ARRAY);
466
 
                        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
467
 
                        
 
531
                        switch (vertexFormat)
 
532
                        {
 
533
                                case VA_FORMAT_POS3F_NORMAL3F_TEX2F:
 
534
                                        glNormalPointer(GL_FLOAT, sizeof(PositionNormalTex3f3f2f), (void*)offsetof(PositionNormalTex3f3f2f,normal));
 
535
                                        glTexCoordPointer(2, GL_FLOAT, sizeof(PositionNormalTex3f3f2f), (void*)offsetof(PositionNormalTex3f3f2f,texcoord));
 
536
                                        glVertexPointer(3, GL_FLOAT, sizeof(PositionNormalTex3f3f2f), (void*)offsetof(PositionNormalTex3f3f2f,pos));
 
537
                                        
 
538
                                        glEnableClientState(GL_VERTEX_ARRAY);
 
539
                                        glEnableClientState(GL_NORMAL_ARRAY);
 
540
                                        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 
541
                                        break;
 
542
                                        
 
543
                                case VA_FORMAT_POS3F_TEX2F:
 
544
                                        glTexCoordPointer(2, GL_FLOAT, sizeof(PositionTex3f2f), (void*)offsetof(PositionTex3f2f,texcoord));
 
545
                                        glVertexPointer(3, GL_FLOAT, sizeof(PositionTex3f2f), (void*)offsetof(PositionTex3f2f,pos));
 
546
                                        
 
547
                                        glEnableClientState(GL_VERTEX_ARRAY);
 
548
                                        glDisableClientState(GL_NORMAL_ARRAY);
 
549
                                        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 
550
                                        break;
 
551
                                        
 
552
                                case VA_FORMAT_POS3F:
 
553
                                        glVertexPointer(3, GL_FLOAT, sizeof(Position3f), (void*)offsetof(Position3f,pos));
 
554
                                        
 
555
                                        glEnableClientState(GL_VERTEX_ARRAY);
 
556
                                        glDisableClientState(GL_NORMAL_ARRAY);
 
557
                                        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 
558
                                        break;
 
559
                                default:
 
560
                                        assert(0);
 
561
                        }
468
562
                        break;
469
563
                        
470
564
                default:
482
576
        {
483
577
                glGenBuffers(1, &vertexBufferName);
484
578
                glBindBuffer(GL_ARRAY_BUFFER, vertexBufferName);
485
 
                glBufferData(GL_ARRAY_BUFFER, sizeof(*vertices)*numVertices, vertices, usageHint);
 
579
                glBufferData(GL_ARRAY_BUFFER, _sizeofFormat(vertexFormat)*numVertices, _vertices, usageHint);
 
580
                glBindBuffer(GL_ARRAY_BUFFER, 0);
486
581
                
487
582
                if (numIndices)
488
583
                {
489
584
                        glGenBuffers(1, &indexBufferName);
490
585
                        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferName);
491
586
                        glBufferData(GL_ELEMENT_ARRAY_BUFFER, 2*numIndices, indices, usageHint);
 
587
                        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
492
588
                }
493
589
        }
494
590
        if (!vertexArrayObject && useVAO)
573
669
 
574
670
- (void) applyVertexTransform: (m16) m
575
671
{
576
 
        for (size_t i = 0; i < numVertices; ++i)
577
 
        {
578
 
                v3 v = {{vertices[i].pos[0], vertices[i].pos[1], vertices[i].pos[2]}};
579
 
                v = mtransformpos3(m, v);
580
 
                vertices[i].pos[0] = v.a[0];
581
 
                vertices[i].pos[1] = v.a[1];
582
 
                vertices[i].pos[2] = v.a[2];
583
 
        }
584
 
        
 
672
        switch (vertexFormat)
 
673
        {
 
674
                case VA_FORMAT_POS3F_NORMAL3F_TEX2F:
 
675
                {
 
676
                        PositionNormalTex3f3f2f* vertices = _vertices;
 
677
                        for (size_t i = 0; i < numVertices; ++i)
 
678
                        {
 
679
                                v3 v = {{vertices[i].pos[0], vertices[i].pos[1], vertices[i].pos[2]}};
 
680
                                v = mtransformpos3(m, v);
 
681
                                vertices[i].pos[0] = v.a[0];
 
682
                                vertices[i].pos[1] = v.a[1];
 
683
                                vertices[i].pos[2] = v.a[2];
 
684
                        }
 
685
                        
 
686
                        break;
 
687
                }
 
688
                case VA_FORMAT_POS3F_TEX2F:
 
689
                {
 
690
                        PositionTex3f2f* vertices = _vertices;
 
691
                        for (size_t i = 0; i < numVertices; ++i)
 
692
                        {
 
693
                                v3 v = {{vertices[i].pos[0], vertices[i].pos[1], vertices[i].pos[2]}};
 
694
                                v = mtransformpos3(m, v);
 
695
                                vertices[i].pos[0] = v.a[0];
 
696
                                vertices[i].pos[1] = v.a[1];
 
697
                                vertices[i].pos[2] = v.a[2];
 
698
                        }
 
699
                        
 
700
                        break;
 
701
                }
 
702
                case VA_FORMAT_POS3F:
 
703
                {
 
704
                        Position3f* vertices = _vertices;
 
705
                        for (size_t i = 0; i < numVertices; ++i)
 
706
                        {
 
707
                                v3 v = {{vertices[i].pos[0], vertices[i].pos[1], vertices[i].pos[2]}};
 
708
                                v = mtransformpos3(m, v);
 
709
                                vertices[i].pos[0] = v.a[0];
 
710
                                vertices[i].pos[1] = v.a[1];
 
711
                                vertices[i].pos[2] = v.a[2];
 
712
                        }
 
713
                        
 
714
                        break;
 
715
                }
 
716
                default:
 
717
                        assert(0);
 
718
        }
 
719
        if (vertexBufferName)
 
720
                glDeleteBuffers(1, &vertexBufferName);
 
721
        vertexBufferName = 0;
 
722
        if (indexBufferName)
 
723
                glDeleteBuffers(1, &indexBufferName);
 
724
        indexBufferName = 0;
 
725
}
 
726
 
 
727
- (void) disposeOfNormals
 
728
{
 
729
        switch (vertexFormat)
 
730
        {
 
731
                case VA_FORMAT_POS3F_NORMAL3F_TEX2F:
 
732
                {
 
733
                        PositionNormalTex3f3f2f* vertices = _vertices;
 
734
                        PositionTex3f2f* nvertices = calloc(_sizeofFormat(VA_FORMAT_POS3F_TEX2F),numVertices);
 
735
                        for (size_t i = 0; i < numVertices; ++i)
 
736
                        {
 
737
                                nvertices[i].pos[0] = vertices[i].pos[0];
 
738
                                nvertices[i].pos[1] = vertices[i].pos[1];
 
739
                                nvertices[i].pos[2] = vertices[i].pos[2];
 
740
                                nvertices[i].texcoord[0] = vertices[i].texcoord[0];
 
741
                                nvertices[i].texcoord[1] = vertices[i].texcoord[1];
 
742
                        }
 
743
                        free(vertices);
 
744
                        _vertices = nvertices;
 
745
                        vertexFormat = VA_FORMAT_POS3F_TEX2F;
 
746
                        
 
747
                        break;
 
748
                }
 
749
                case VA_FORMAT_POS3F_TEX2F:
 
750
                {
 
751
                        break;
 
752
                }
 
753
                case VA_FORMAT_POS3F:
 
754
                {                       
 
755
                        break;
 
756
                }
 
757
                default:
 
758
                        assert(0);
 
759
        }
585
760
        if (vertexBufferName)
586
761
                glDeleteBuffers(1, &vertexBufferName);
587
762
        vertexBufferName = 0;

Loggerhead 1.17 is a web-based interface for Bazaar branches