我正在使用从此处下载的应用程序在我的应用程序中旋转 3d 对象。我成功地停止了旋转对象,如下所示,但我无法从停止的位置再次旋转对象。下面是我的一段代码已经从下载的项目中工作
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
{
if (aRotate) {
[self startOrStopAutorotation:nil];
}
if ([[event touchesForView:self.view] count] > 1) // Pinch gesture, possibly two-finger movement
{
//CGPoint directionOfPanning = CGPointZero;
}
else{
CGPoint currentMovementPosition = [[touches anyObject] locationInView:glView_];
[self renderByRotatingAroundX:(lastMovementPosition.x - currentMovementPosition.x) rotatingAroundY: (lastMovementPosition.y - currentMovementPosition.y) scaling:1.0f translationInX:0.0f translationInY:0.0f];
lastMovementPosition = currentMovementPosition;
}
}
}
- (void)renderByRotatingAroundX:(float)xRotation rotatingAroundY:(float)yRotation scaling:(float)scaleF translationInX:(float)xTranslation translationInY:(float)yTranslation{
glView_ = [[REGLView alloc] initWithFrame:CGRectMake(0, 0, 320, 320) colorFormat:kEAGLColorFormatRGBA8 multisampling:YES];
[self.view addSubview:glView_];
camera_ = [[RECamera alloc] initWithProjection:kRECameraProjectionOrthographic];
camera_.position = CC3VectorMake(0, 0, 320);
camera_.upDirection = CC3VectorMake(0, 1, 0);
camera_.lookDirection = CC3VectorMake(0, 0, -1);
camera_.frustumNear = 10;
camera_.frustumFar = 640;
camera_.frustumLeft = -glView_.frame.size.width / 2.0;
camera_.frustumRight = glView_.frame.size.width / 2.0;
camera_.frustumBottom = -glView_.frame.size.height / 2.0;
camera_.frustumTop = glView_.frame.size.height / 2.0;
scene_ = [[REScene alloc] init];
scene_.camera = camera_;
world_ = [[REWorld alloc] init];
[scene_ addChild:world_];
director_ = [[REDirector alloc] init];
director_.view = glView_;
director_.scene = scene_;
// currentCalculatedMatrix = CATransform3DIdentity;
GLfloat currentModelViewMatrix[16] = {0.402560,0.094840,0.910469,0.000000, 0.913984,-0.096835,-0.394028,0.000000, 0.050796,0.990772,-0.125664,0.000000, 0.000000,0.000000,0.000000,1.000000};
currentCalculatedMatrix = CATransform3DScale(currentCalculatedMatrix, 0.5, 0.5 * (320.0/480.0), 0.5);
//// if ((xRotation != 0.0) || (yRotation != 0.0))
//{
GLfloat totalRotation = sqrt(xRotation*xRotation + yRotation*yRotation);
CATransform3D temporaryMatrix = CATransform3DRotate(currentCalculatedMatrix, totalRotation * M_PI / 180.0,
((xRotation/totalRotation) * currentCalculatedMatrix.m12 + (yRotation/totalRotation) * currentCalculatedMatrix.m11),
((xRotation/totalRotation) * currentCalculatedMatrix.m22 + (yRotation/totalRotation) * currentCalculatedMatrix.m21),
((xRotation/totalRotation) * currentCalculatedMatrix.m32 + (yRotation/totalRotation) * currentCalculatedMatrix.m31));
if ((temporaryMatrix.m11 >= -100.0) && (temporaryMatrix.m11 <= 100.0))
currentCalculatedMatrix = temporaryMatrix;
float currentScaleFactor = sqrt(pow(currentCalculatedMatrix.m11, 2.0f) + pow(currentCalculatedMatrix.m12, 2.0f) + pow(currentCalculatedMatrix.m13, 2.0f));
xTranslation = xTranslation / (currentScaleFactor * currentScaleFactor);
yTranslation = yTranslation / (currentScaleFactor * currentScaleFactor);
// Use the (0,4,8) components to figure the eye's X axis in the model coordinate system, translate along that
temporaryMatrix = CATransform3DTranslate(currentCalculatedMatrix, xTranslation * currentCalculatedMatrix.m11, xTranslation * currentCalculatedMatrix.m21, xTranslation * currentCalculatedMatrix.m31);
// Use the (1,5,9) components to figure the eye's Y axis in the model coordinate system, translate along that
temporaryMatrix = CATransform3DTranslate(temporaryMatrix, yTranslation * currentCalculatedMatrix.m12, yTranslation * currentCalculatedMatrix.m22, yTranslation * currentCalculatedMatrix.m32);
if ((temporaryMatrix.m11 >= -100.0) && (temporaryMatrix.m11 <= 100.0))
currentCalculatedMatrix = temporaryMatrix;
[self convert3DTransform:¤tCalculatedMatrix toMatrix:currentModelViewMatrix];
// Black background, with depth buffer enabled
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
下面是我为停止和停止工作的代码
- (void)startOrStopAutorotation:(id)sender;
{
id dLink=nil;
if (aRotate){
dLink=[[REDisplayLink sharedDisplayLink] observers];
[[REDisplayLink sharedDisplayLink] removeObserver:[dLink objectAtIndex:0]];
}
else{
CADisplayLink *aDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleAutorotationTimer)];
[aDisplayLink setFrameInterval:2];
[aDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
// [[REDisplayLink sharedDisplayLink] addObserver:[[REDirector alloc]init]];
}
aRotate = !aRotate;
}