-
Notifications
You must be signed in to change notification settings - Fork 0
/
PanoramaView.m
executable file
·603 lines (585 loc) · 23.6 KB
/
PanoramaView.m
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//
// PanoramaView.m
// Panorama
//
// Created by Robby Kraft on 8/24/13.
// Copyright (c) 2013 Robby Kraft. All rights reserved.
//
#import <CoreMotion/CoreMotion.h>
#import <OpenGLES/ES1/gl.h>
#import <GLKit/GLKit.h>
#import "PanoramaView.h"
#import "TiBase.h"
#define FPS 60
#define FOV_MIN 1
#define FOV_MAX 155
#define Z_NEAR 0.1f
#define Z_FAR 100.0f
// LINEAR for smoothing, NEAREST for pixelized
#define IMAGE_SCALING GL_LINEAR // GL_NEAREST, GL_LINEAR
// this appears to be the best way to grab orientation. if this becomes formalized, just make sure the orientations match
#define SENSOR_ORIENTATION [[UIApplication sharedApplication] statusBarOrientation] //enum 1(NORTH) 2(SOUTH) 3(EAST) 4(WEST)
// this really should be included in GLKit
GLKQuaternion GLKQuaternionFromTwoVectors(GLKVector3 u, GLKVector3 v){
GLKVector3 w = GLKVector3CrossProduct(u, v);
GLKQuaternion q = GLKQuaternionMake(w.x, w.y, w.z, GLKVector3DotProduct(u, v));
q.w += GLKQuaternionLength(q);
return GLKQuaternionNormalize(q);
}
@interface Sphere : NSObject
-(bool) execute;
-(id) init:(GLint)stacks slices:(GLint)slices radius:(GLfloat)radius textureFile:(NSString *)textureFile;
-(void) swapTexture:(NSString*)textureFile;
-(void) swapTextureWithImage:(UIImage*)image;
-(CGSize) getTextureSize;
@end
@interface PanoramaView (){
Sphere *sphere, *meridians;
CMMotionManager *motionManager;
UIPinchGestureRecognizer *pinchGesture;
UIPanGestureRecognizer *panGesture;
GLKMatrix4 _projectionMatrix, _attitudeMatrix, _offsetMatrix;
float _aspectRatio;
GLfloat circlePoints[64*3]; // meridian lines
}
@end
@implementation PanoramaView
-(id) init{
// it appears that iOS already automatically does this switch, stored in UIScreen mainscreen bounds
// CGRect frame = [[UIScreen mainScreen] bounds];
// if(SENSOR_ORIENTATION == 3 || SENSOR_ORIENTATION == 4){
// return [self initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.height, frame.size.width)];
// } else{
// return [self initWithFrame:CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height)];
// }
return [self initWithFrame:[[UIScreen mainScreen] bounds]];
}
- (id)initWithFrame:(CGRect)frame{
EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
[EAGLContext setCurrentContext:context];
self.context = context;
return [self initWithFrame:frame context:context];
}
-(id) initWithFrame:(CGRect)frame context:(EAGLContext *)context{
self = [super initWithFrame:frame];
if (self) {
[self initDevice];
[self initOpenGL:context];
sphere = [[Sphere alloc] init:48 slices:48 radius:10.0 textureFile:nil];
meridians = [[Sphere alloc] init:48 slices:48 radius:8.0 textureFile:@"equirectangular-projection-lines.png"];
}
return self;
}
-(void) didMoveToSuperview{
// this breaks MVC, but useful for setting GLKViewController's frame rate
UIResponder *responder = self;
while (![responder isKindOfClass:[GLKViewController class]]) {
responder = [responder nextResponder];
if (responder == nil){
break;
}
}
if([responder respondsToSelector:@selector(setPreferredFramesPerSecond:)])
[(GLKViewController*)responder setPreferredFramesPerSecond:FPS];
}
-(void) initDevice{
motionManager = [[CMMotionManager alloc] init];
pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchHandler:)];
[pinchGesture setEnabled:NO];
[self addGestureRecognizer:pinchGesture];
panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHandler:)];
[panGesture setMaximumNumberOfTouches:1];
[panGesture setEnabled:NO];
[self addGestureRecognizer:panGesture];
}
-(void)setFieldOfView:(float)fieldOfView{
_fieldOfView = fieldOfView;
[self rebuildProjectionMatrix];
}
-(void) setImageWithName:(NSString*)fileName{
[sphere swapTexture:fileName];
}
-(void) setImage:(UIImage *)image {
[sphere swapTextureWithImage:image];
}
-(void) setTouchToPan:(BOOL)touchToPan{
_touchToPan = touchToPan;
[panGesture setEnabled:_touchToPan];
}
-(void) setPinchToZoom:(BOOL)pinchToZoom{
_pinchToZoom = pinchToZoom;
[pinchGesture setEnabled:_pinchToZoom];
}
-(void) setOrientToDevice:(BOOL)orientToDevice{
_orientToDevice = orientToDevice;
if(motionManager.isDeviceMotionAvailable){
if(_orientToDevice)
[motionManager startDeviceMotionUpdates];
else
[motionManager stopDeviceMotionUpdates];
}
}
-(void) setVRMode:(BOOL)VRMode{
_VRMode = VRMode;
if(_VRMode){
_aspectRatio = self.frame.size.width/(self.frame.size.height*0.5);
[self rebuildProjectionMatrix];
} else{
_aspectRatio = self.frame.size.width/self.frame.size.height;
[self rebuildProjectionMatrix];
}
}
#pragma mark- OPENGL
-(void)initOpenGL:(EAGLContext*)context{
[(CAEAGLLayer*)self.layer setOpaque:NO];
_aspectRatio = self.frame.size.width/self.frame.size.height;
_fieldOfView = 45 + 45 * atanf(_aspectRatio); // hell ya
[self rebuildProjectionMatrix];
_attitudeMatrix = GLKMatrix4Identity;
_offsetMatrix = GLKMatrix4Identity;
[self customGL];
[self makeLatitudeLines];
}
-(void)rebuildProjectionMatrix{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat frustum = Z_NEAR * tanf(_fieldOfView*0.00872664625997); // pi/180/2
_projectionMatrix = GLKMatrix4MakeFrustum(-frustum, frustum, -frustum/_aspectRatio, frustum/_aspectRatio, Z_NEAR, Z_FAR);
glMultMatrixf(_projectionMatrix.m);
if(!_VRMode){
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
} else{
// no matter. glViewport gets called every draw call anyway.
}
glMatrixMode(GL_MODELVIEW);
}
-(void) customGL{
glMatrixMode(GL_MODELVIEW);
// glEnable(GL_CULL_FACE);
// glCullFace(GL_FRONT);
// glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
-(void)draw{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
if(_VRMode) {
float scale = [UIScreen mainScreen].scale;
// one eye
glMatrixMode(GL_PROJECTION);
glViewport(0, 0, self.frame.size.width * scale, self.frame.size.height * scale * 0.5);
glMatrixMode(GL_MODELVIEW);
[self renderScene];
// other eye
glMatrixMode(GL_PROJECTION);
glViewport(0, self.frame.size.height * scale * 0.5, self.frame.size.width * scale, self.frame.size.height * scale* 0.5);
glMatrixMode(GL_MODELVIEW);
[self renderScene];
}else{
[self renderScene];
}
}
-(void) renderScene{
static GLfloat whiteColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
static GLfloat clearColor[] = {0.0f, 0.0f, 0.0f, 0.0f};
glPushMatrix(); // begin device orientation
_attitudeMatrix = GLKMatrix4Multiply([self getDeviceOrientationMatrix], _offsetMatrix);
[self updateLook];
glMultMatrixf(_attitudeMatrix.m);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, whiteColor); // panorama at full color
[sphere execute];
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, clearColor);
// [meridians execute]; // semi-transparent texture overlay (15° meridian lines)
//TODO: add any objects here to make them a part of the virtual reality
// glPushMatrix();
// // object code
// glPopMatrix();
// touch lines
if(_showTouches && _numberOfTouches){
glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
for(int i = 0; i < [[_touches allObjects] count]; i++){
glPushMatrix();
CGPoint touchPoint = CGPointMake([(UITouch*)[[_touches allObjects] objectAtIndex:i] locationInView:self].x, [(UITouch*)[[_touches allObjects] objectAtIndex:i] locationInView:self].y);
if(_VRMode){
touchPoint.y = ( (int)touchPoint.y % (int)(self.frame.size.height * 0.5) ) * 2.0;
}
[self drawHotspotLines:[self vectorFromScreenLocation:touchPoint inAttitude:_attitudeMatrix]];
glPopMatrix();
}
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
glPopMatrix(); // end device orientation
}
#pragma mark- ORIENTATION
-(GLKMatrix4) getDeviceOrientationMatrix{
if(_orientToDevice && [motionManager isDeviceMotionActive]){
CMRotationMatrix a = [[[motionManager deviceMotion] attitude] rotationMatrix];
// arrangements of mappings of sensor axis to virtual axis (columns)
// and combinations of 90 degree rotations (rows)
switch (SENSOR_ORIENTATION) {
case 4:
return GLKMatrix4Make( a.m21,-a.m11, a.m31, 0.0f,
a.m23,-a.m13, a.m33, 0.0f,
-a.m22, a.m12,-a.m32, 0.0f,
0.0f , 0.0f , 0.0f , 1.0f);
case 3:
return GLKMatrix4Make(-a.m21, a.m11, a.m31, 0.0f,
-a.m23, a.m13, a.m33, 0.0f,
a.m22,-a.m12,-a.m32, 0.0f,
0.0f , 0.0f , 0.0f , 1.0f);
case 2:
return GLKMatrix4Make(-a.m11,-a.m21, a.m31, 0.0f,
-a.m13,-a.m23, a.m33, 0.0f,
a.m12, a.m22,-a.m32, 0.0f,
0.0f , 0.0f , 0.0f , 1.0f);
case 1:
default:
return GLKMatrix4Make( a.m11, a.m21, a.m31, 0.0f,
a.m13, a.m23, a.m33, 0.0f,
-a.m12,-a.m22,-a.m32, 0.0f,
0.0f , 0.0f , 0.0f , 1.0f);
}
}
return GLKMatrix4Identity;
}
-(void) orientToVector:(GLKVector3)v{
_attitudeMatrix = GLKMatrix4MakeLookAt(0, 0, 0, v.x, v.y, v.z, 0, 1, 0);
[self updateLook];
}
-(void) orientToAzimuth:(float)azimuth Altitude:(float)altitude{
[self orientToVector:GLKVector3Make(-cosf(azimuth), sinf(altitude), sinf(azimuth))];
}
-(void) updateLook{
_lookVector = GLKVector3Make(-_attitudeMatrix.m02,
-_attitudeMatrix.m12,
-_attitudeMatrix.m22);
_lookAzimuth = atan2f(_lookVector.x, -_lookVector.z);
_lookAltitude = asinf(_lookVector.y);
}
-(CGPoint) imagePixelAtScreenLocation:(CGPoint)point{
return [self imagePixelFromVector:[self vectorFromScreenLocation:point inAttitude:_attitudeMatrix]];
}
-(CGPoint) imagePixelFromVector:(GLKVector3)vector{
CGPoint pxl = CGPointMake((atan2f(-vector.x, vector.z))/(2*M_PI), acosf(vector.y)/M_PI);
if(pxl.x < 0.0) pxl.x += 1.0;
CGSize tex = [sphere getTextureSize];
// if no texture exists, returns between 0.0 - 1.0
if(!(tex.width == 0.0f && tex.height == 0.0f)){
pxl.x *= tex.width;
pxl.y *= tex.height;
}
return pxl;
}
-(GLKVector3) vectorFromScreenLocation:(CGPoint)point{
return [self vectorFromScreenLocation:point inAttitude:_attitudeMatrix];
}
-(GLKVector3) vectorFromScreenLocation:(CGPoint)point inAttitude:(GLKMatrix4)matrix{
GLKMatrix4 inverse = GLKMatrix4Invert(GLKMatrix4Multiply(_projectionMatrix, matrix), nil);
GLKVector4 screen = GLKVector4Make(2.0*(point.x/self.frame.size.width-.5),
2.0*(.5-point.y/self.frame.size.height),
1.0, 1.0);
// if (SENSOR_ORIENTATION == 3 || SENSOR_ORIENTATION == 4)
// screen = GLKVector4Make(2.0*(screenTouch.x/self.frame.size.height-.5),
// 2.0*(.5-screenTouch.y/self.frame.size.width),
// 1.0, 1.0);
GLKVector4 vec = GLKMatrix4MultiplyVector4(inverse, screen);
return GLKVector3Normalize(GLKVector3Make(vec.x, vec.y, vec.z));
}
-(CGPoint) screenLocationFromVector:(GLKVector3)vector{
GLKMatrix4 matrix = GLKMatrix4Multiply(_projectionMatrix, _attitudeMatrix);
GLKVector3 screenVector = GLKMatrix4MultiplyVector3(matrix, vector);
return CGPointMake( (screenVector.x/screenVector.z/2.0 + 0.5) * self.frame.size.width,
(0.5-screenVector.y/screenVector.z/2) * self.frame.size.height );
}
-(BOOL) computeScreenLocation:(CGPoint*)location fromVector:(GLKVector3)vector inAttitude:(GLKMatrix4)matrix{
//This method returns whether the point is before or behind the screen.
GLKVector4 screenVector;
GLKVector4 vector4;
if(location == NULL)
return NO;
matrix = GLKMatrix4Multiply(_projectionMatrix, matrix);
vector4 = GLKVector4Make(vector.x, vector.y, vector.z, 1);
screenVector = GLKMatrix4MultiplyVector4(matrix, vector4);
location->x = (screenVector.x/screenVector.w/2.0 + 0.5) * self.frame.size.width;
location->y = (0.5-screenVector.y/screenVector.w/2) * self.frame.size.height;
return (screenVector.z >= 0);
}
#pragma mark- TOUCHES
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
_touches = event.allTouches;
_numberOfTouches = event.allTouches.count;
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
_touches = event.allTouches;
_numberOfTouches = event.allTouches.count;
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
_touches = event.allTouches;
_numberOfTouches = 0;
}
-(BOOL)touchInRect:(CGRect)rect{
if(_numberOfTouches){
bool found = false;
for(int i = 0; i < [[_touches allObjects] count]; i++){
CGPoint touchPoint = CGPointMake([(UITouch*)[[_touches allObjects] objectAtIndex:i] locationInView:self].x,
[(UITouch*)[[_touches allObjects] objectAtIndex:i] locationInView:self].y);
found |= CGRectContainsPoint(rect, [self imagePixelAtScreenLocation:touchPoint]);
}
return found;
}
return false;
}
-(void)pinchHandler:(UIPinchGestureRecognizer*)sender{
_numberOfTouches = sender.numberOfTouches;
static float zoom;
if([sender state] == 1)
zoom = _fieldOfView;
if([sender state] == 2){
CGFloat newFOV = zoom / [sender scale];
if(newFOV < FOV_MIN) newFOV = FOV_MIN;
else if(newFOV > FOV_MAX) newFOV = FOV_MAX;
[self setFieldOfView:newFOV];
}
if([sender state] == 3){
_numberOfTouches = 0;
}
}
-(void) panHandler:(UIPanGestureRecognizer*)sender{
static GLKVector3 touchVector;
if([sender state] == 1){
CGPoint location = [sender locationInView:sender.view];
if(_VRMode){
location.y = ( (int)location.y % (int)(self.frame.size.height * 0.5) ) * 2.0;
}
touchVector = [self vectorFromScreenLocation:location inAttitude:_offsetMatrix];
}
else if([sender state] == 2){
CGPoint location = [sender locationInView:sender.view];
if(_VRMode){
location.y = ( (int)location.y % (int)(self.frame.size.height * 0.5) ) * 2.0;
}
GLKVector3 nowVector = [self vectorFromScreenLocation:location inAttitude:_offsetMatrix];
GLKQuaternion q = GLKQuaternionFromTwoVectors(touchVector, nowVector);
_offsetMatrix = GLKMatrix4Multiply(_offsetMatrix, GLKMatrix4MakeWithQuaternion(q));
// in progress for preventHeadTilt
// GLKMatrix4 mat = GLKMatrix4Multiply(_offsetMatrix, GLKMatrix4MakeWithQuaternion(q));
// _offsetMatrix = GLKMatrix4MakeLookAt(0, 0, 0, -mat.m02, -mat.m12, -mat.m22, 0, 1, 0);
}
else{
_numberOfTouches = 0;
}
}
#pragma mark- MERIDIANS
-(void) makeLatitudeLines{
for(int i = 0; i < 64; i++){
circlePoints[i*3+0] = -sinf(M_PI*2/64.0f*i);
circlePoints[i*3+1] = 0.0f;
circlePoints[i*3+2] = cosf(M_PI*2/64.0f*i);
}
}
-(void)drawHotspotLines:(GLKVector3)touchLocation{
glLineWidth(2.0f);
float scale = sqrtf(1-powf(touchLocation.y,2));
glPushMatrix();
glScalef(scale, 1.0f, scale);
glTranslatef(0, touchLocation.y, 0);
glDisableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, circlePoints);
glDrawArrays(GL_LINE_LOOP, 0, 64);
glDisableClientState(GL_VERTEX_ARRAY);
glPopMatrix();
glPushMatrix();
glRotatef(-atan2f(-touchLocation.z, -touchLocation.x)*180/M_PI, 0, 1, 0);
glRotatef(90, 1, 0, 0);
glDisableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, circlePoints);
glDrawArrays(GL_LINE_STRIP, 0, 33);
glDisableClientState(GL_VERTEX_ARRAY);
glPopMatrix();
}
-(void) dealloc{
[EAGLContext setCurrentContext:nil];
}
@end
@interface Sphere (){
// from Touch Fighter by Apple
// in Pro OpenGL ES for iOS
// by Mike Smithwick Jan 2011 pg. 78
GLKTextureInfo *m_TextureInfo;
GLfloat *m_TexCoordsData;
GLfloat *m_VertexData;
GLfloat *m_NormalData;
GLint m_Stacks, m_Slices;
GLfloat m_Scale;
}
-(GLKTextureInfo *) loadTextureFromBundle:(NSString *) filename;
-(GLKTextureInfo *) loadTextureFromPath:(NSString *) path;
-(GLKTextureInfo *) loadTextureFromImage:(UIImage *) image;
@end
@implementation Sphere
-(id) init:(GLint)stacks slices:(GLint)slices radius:(GLfloat)radius textureFile:(NSString *)textureFile{
// modifications:
// flipped(inverted) texture coords across the Z
// vertices rotated 90deg
// if(textureFile != nil) m_TextureInfo = [self loadTextureFromBundle:textureFile];
m_Scale = radius;
if((self = [super init])){
m_Stacks = stacks;
m_Slices = slices;
m_VertexData = nil;
m_TexCoordsData = nil;
// Vertices
GLfloat *vPtr = m_VertexData = (GLfloat*)malloc(sizeof(GLfloat) * 3 * ((m_Slices*2+2) * (m_Stacks)));
// Normals
GLfloat *nPtr = m_NormalData = (GLfloat*)malloc(sizeof(GLfloat) * 3 * ((m_Slices*2+2) * (m_Stacks)));
GLfloat *tPtr = nil;
tPtr = m_TexCoordsData = (GLfloat*)malloc(sizeof(GLfloat) * 2 * ((m_Slices*2+2) * (m_Stacks)));
unsigned int phiIdx, thetaIdx;
// Latitude
for(phiIdx = 0; phiIdx < m_Stacks; phiIdx++){
//starts at -pi/2 goes to pi/2
//the first circle
float phi0 = M_PI * ((float)(phiIdx+0) * (1.0/(float)(m_Stacks)) - 0.5);
//second one
float phi1 = M_PI * ((float)(phiIdx+1) * (1.0/(float)(m_Stacks)) - 0.5);
float cosPhi0 = cos(phi0);
float sinPhi0 = sin(phi0);
float cosPhi1 = cos(phi1);
float sinPhi1 = sin(phi1);
float cosTheta, sinTheta;
//longitude
for(thetaIdx = 0; thetaIdx < m_Slices; thetaIdx++){
float theta = -2.0*M_PI * ((float)thetaIdx) * (1.0/(float)(m_Slices - 1));
cosTheta = cos(theta+M_PI*.5);
sinTheta = sin(theta+M_PI*.5);
//get x-y-x of the first vertex of stack
vPtr[0] = m_Scale*cosPhi0 * cosTheta;
vPtr[1] = m_Scale*sinPhi0;
vPtr[2] = m_Scale*(cosPhi0 * sinTheta);
//the same but for the vertex immediately above the previous one.
vPtr[3] = m_Scale*cosPhi1 * cosTheta;
vPtr[4] = m_Scale*sinPhi1;
vPtr[5] = m_Scale*(cosPhi1 * sinTheta);
nPtr[0] = cosPhi0 * cosTheta;
nPtr[1] = sinPhi0;
nPtr[2] = cosPhi0 * sinTheta;
nPtr[3] = cosPhi1 * cosTheta;
nPtr[4] = sinPhi1;
nPtr[5] = cosPhi1 * sinTheta;
if(tPtr!=nil){
GLfloat texX = (float)thetaIdx * (1.0f/(float)(m_Slices-1));
tPtr[0] = 1.0-texX;
tPtr[1] = (float)(phiIdx + 0) * (1.0f/(float)(m_Stacks));
tPtr[2] = 1.0-texX;
tPtr[3] = (float)(phiIdx + 1) * (1.0f/(float)(m_Stacks));
}
vPtr += 2*3;
nPtr += 2*3;
if(tPtr != nil) tPtr += 2*2;
}
//Degenerate triangle to connect stacks and maintain winding order
vPtr[0] = vPtr[3] = vPtr[-3];
vPtr[1] = vPtr[4] = vPtr[-2];
vPtr[2] = vPtr[5] = vPtr[-1];
nPtr[0] = nPtr[3] = nPtr[-3];
nPtr[1] = nPtr[4] = nPtr[-2];
nPtr[2] = nPtr[5] = nPtr[-1];
if(tPtr != nil){
tPtr[0] = tPtr[2] = tPtr[-2];
tPtr[1] = tPtr[3] = tPtr[-1];
}
}
}
return self;
}
-(void) dealloc{
return;
// GLuint name = m_TextureInfo.name;
// glDeleteTextures(1, &name);
if(m_TexCoordsData != nil){
free(m_TexCoordsData);
}
if(m_NormalData != nil){
free(m_NormalData);
}
if(m_VertexData != nil){
free(m_VertexData);
}
}
-(bool) execute{
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
if(m_TexCoordsData != nil){
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
if(m_TextureInfo != 0)
glBindTexture(GL_TEXTURE_2D, m_TextureInfo.name);
glTexCoordPointer(2, GL_FLOAT, 0, m_TexCoordsData);
}
glVertexPointer(3, GL_FLOAT, 0, m_VertexData);
glNormalPointer(GL_FLOAT, 0, m_NormalData);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (m_Slices +1) * 2 * (m_Stacks-1)+2);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
return true;
}
-(GLKTextureInfo *) loadTextureFromBundle:(NSString *) filename{
if(!filename) return nil;
NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:NULL];
return [self loadTextureFromPath:path];
}
-(GLKTextureInfo *) loadTextureFromPath:(NSString *) path{
if(!path) return nil;
NSError *error;
GLKTextureInfo *info;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], GLKTextureLoaderOriginBottomLeft, nil];
info=[GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
glBindTexture(GL_TEXTURE_2D, info.name);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, IMAGE_SCALING);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, IMAGE_SCALING);
return info;
}
-(GLKTextureInfo *) loadTextureFromImage:(UIImage *) image {
if(!image) return nil;
NSError *error;
GLKTextureInfo *info;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],GLKTextureLoaderOriginBottomLeft, nil];
info = [GLKTextureLoader textureWithCGImage:image.CGImage options:options error:&error];
glBindTexture(GL_TEXTURE_2D, info.name);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, IMAGE_SCALING);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, IMAGE_SCALING);
return info;
}
-(void)swapTexture:(NSString*)textureFile{
//GLuint name = m_TextureInfo.name;
//glDeleteTextures(1, &name);
if ([[NSFileManager defaultManager] fileExistsAtPath:textureFile]) {
m_TextureInfo = [self loadTextureFromPath:textureFile];
}
else {
m_TextureInfo = [self loadTextureFromBundle:textureFile];
}
}
-(void)swapTextureWithImage:(UIImage*)image {
//GLuint name = m_TextureInfo.name;
//glDeleteTextures(1, &name);
m_TextureInfo = [self loadTextureFromImage:image];
}
-(CGSize)getTextureSize{
if(m_TextureInfo){
return CGSizeMake(m_TextureInfo.width, m_TextureInfo.height);
}
else{
return CGSizeZero;
}
}
@end