-
Notifications
You must be signed in to change notification settings - Fork 1
/
RCCamera.m
183 lines (137 loc) · 4.6 KB
/
RCCamera.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
//
// RCCamera.m
// Texture3
//
// Created by rcrebs on 11/1/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "RCCamera.h"
#import "Texture3AppDelegate.h"
#import <math.h>
#define DefalutZoom 50.0f
@implementation RCCamera
@synthesize firstTouch;
@synthesize firstTouchPoint, secondTouchPoint;
@synthesize view;
-(id)init {
[super init];
self.view = [Texture3AppDelegate sharedAppDelegate].glView;
zoom = 100;
rotate = 0;
return self;
}
- (void)checkRun {
}
- (void)checkRise {
}
#pragma mark -
#pragma mark Zooming Camera
- (void)zoom:(NSArray*)arrayOfTouches {
CGPoint newFirstTouchPoint, newSecondTouchPoint;
// Because touch events are order randomly the index of the first touch event must be Identified
if ([arrayOfTouches indexOfObject:firstTouch] == 0) {
newFirstTouchPoint = [[arrayOfTouches objectAtIndex:0] locationInView:self.view];
newSecondTouchPoint = [[arrayOfTouches objectAtIndex:1] locationInView:self.view];
if (!secondTouchPointSet) {
secondTouchPoint = [[arrayOfTouches objectAtIndex:1] locationInView:self.view];
secondTouchPointSet = YES;
}
}
else {
newFirstTouchPoint = [[arrayOfTouches objectAtIndex:1] locationInView:self.view];
newSecondTouchPoint = [[arrayOfTouches objectAtIndex:0] locationInView:self.view];
if (!secondTouchPointSet) {
secondTouchPoint = [[arrayOfTouches objectAtIndex:0] locationInView:self.view];
secondTouchPointSet = YES;
}
}
// Calculate distances and the difference of distances
[self zoomDifferenceOfPoint:firstTouchPoint andPoint:secondTouchPoint andNewPoint:newFirstTouchPoint andNewPoint:newSecondTouchPoint];
// After camera zoom has taken place assign old touch points to the new for the next calculations
firstTouchPoint = newFirstTouchPoint;
secondTouchPoint = newSecondTouchPoint;
}
-(void)zoomDifferenceOfPoint:(CGPoint)firstPoint
andPoint:(CGPoint)secondPoint
andNewPoint:(CGPoint)newFirstPoint
andNewPoint:(CGPoint)newSecondPoint
{
// Calculate the distances of the two previous points and the two new touch points. Then caluclate the change in zoom.
GLfloat distanceBetweenPreviousPoints = sqrt([self value:secondPoint.x - firstPoint.x toThePositivePower:2.0f] + [self value:secondPoint.y - firstPoint.y toThePositivePower:2.0f]);
GLfloat distanceBetweenNewPoints = sqrt([self value:newSecondPoint.x - newFirstPoint.x toThePositivePower:2.0f] + [self value:newSecondPoint.y - newFirstPoint.y toThePositivePower:2.0f]);
// Change in zoom
GLfloat changeInZoom = (distanceBetweenNewPoints - distanceBetweenPreviousPoints);
// Make sure the new zoom position is not less than 0.0. If it is, it is set to minimum zoom of 0.0.
if (zoom - (changeInZoom / [self absoluteValue:changeInZoom]) > 0.0f) {
GLfloat newZoom = zoom - (changeInZoom / [self absoluteValue:changeInZoom]);
if (isnan(newZoom) ) {
NSLog(@"nan found");
}
else {
zoom = newZoom;
}
}
}
-(void)defaultZoom
{
// Set zoom to default
zoom = DefalutZoom;
}
#pragma mark -
#pragma mark Scrolling Camera
- (void)scroll:(NSArray*)arrayOfTouches {
CGPoint moveToPoint = [[arrayOfTouches objectAtIndex:0] locationInView:self.view];
if ([self absoluteValue: moveToPoint.x - firstTouchPoint.x] > [self absoluteValue:moveToPoint.y - firstTouchPoint.y]) {
xRotation += (moveToPoint.x - firstTouchPoint.x) / 3.5;
//[camera scrollCamerasPositionXByDifferenceOf:moveToPoint.x - firstTouchPoint.x];
}
else if ([self absoluteValue: moveToPoint.x - firstTouchPoint.x] < [self absoluteValue:moveToPoint.y - firstTouchPoint.y]) {
yRotation += (moveToPoint.y - firstTouchPoint.y) / 3.5;
}
else {
NSLog(@"Change not hadled");
}
firstTouchPoint.x = moveToPoint.x;
firstTouchPoint.y = moveToPoint.y;
}
#pragma mark -
#pragma mark Transform
- (void)transformForCameraPosition {
glLoadIdentity();
glTranslatef(0,0, -zoom);
// TODO: This is need to implement Panning
//glTranslatef(-10, 10, 0);
/*********************************/
glRotatef(xRotation, 0, 1, 0);
glRotatef(yRotation, 1, 0, 0);
}
#pragma mark -
#pragma mark Math For Calculating Camera's Position
#pragma mark -
-(GLfloat)aPositionOnOneAxisAtPositionOfAnotherAxis:(GLfloat)position
{
return sqrt(zoom - (position * position));
}
-(GLfloat)value:(GLfloat)value toThePositivePower:(GLint)exponent
{
GLfloat newValue = value;
if (exponent == 0.0f) {
return 1.0f;
}
else {
GLint count = 1;
while (count < exponent) {
newValue *= value;
++count;
}
}
return newValue;
}
-(GLfloat)absoluteValue:(GLfloat)value
{
if (value < 0.0f) {
return -value;
}
return value;
}
@end