Iphone 如何绕x轴旋转GLKView?
我已经对GLKView进行了子类化,我想绕x轴旋转它。我曾尝试在raywenderlich的教程中应用该方法,但没有结果。不同的是,我有一个GLKView,他碰巧用一个GLKViewController(+它的委托)做任何事情。因此,我没有代理的更新方法等。我几乎没有OpenGL的经验(刚刚开始),所以我希望我在这里遇到一些启示 ViewController.mIphone 如何绕x轴旋转GLKView?,iphone,objective-c,rotation,opengl-es-2.0,Iphone,Objective C,Rotation,Opengl Es 2.0,我已经对GLKView进行了子类化,我想绕x轴旋转它。我曾尝试在raywenderlich的教程中应用该方法,但没有结果。不同的是,我有一个GLKView,他碰巧用一个GLKViewController(+它的委托)做任何事情。因此,我没有代理的更新方法等。我几乎没有OpenGL的经验(刚刚开始),所以我希望我在这里遇到一些启示 ViewController.m EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLR
EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
BlogCell *view = [[BlogCell alloc] initWithFrame:CGRectMake(0, 200, 320, 80) context:context];
view.drawableMultisample = GLKViewDrawableMultisample4X;
view.context = context;
view.delegate = view;
[view setupGL];
[self.view addSubview:view];
- (void)setupGL {
[EAGLContext setCurrentContext:self.context];
glEnable(GL_CULL_FACE);
self.effect = [[GLKBaseEffect alloc] init];
NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],
GLKTextureLoaderOriginBottomLeft,
nil];
NSError * error;
NSString *path = [[NSBundle mainBundle] pathForResource:@"myImage" ofType:@"png"];
GLKTextureInfo * info = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
if (info == nil) {
NSLog(@"Error loading file: %@", [error localizedDescription]);
}
self.effect.texture2d0.name = info.name;
self.effect.texture2d0.enabled = true;
glGenVertexArraysOES(1, &vertexArray);
glBindVertexArrayOES(vertexArray);
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color));
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition,
3,
GL_FLOAT,
GL_FALSE,
sizeof(Vertex),
(const GLvoid *) offsetof(Vertex, Position)
);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, TexCoord));
glBindVertexArrayOES(0);
rotMatrix = GLKMatrix4Identity;
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
self.contentScaleFactor = 2.0;
self.opaque = NO;
self.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
[self.effect prepareToDraw];
glBindVertexArrayOES(vertexArray);
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
}
MyGLKViewSubclass.m
EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
BlogCell *view = [[BlogCell alloc] initWithFrame:CGRectMake(0, 200, 320, 80) context:context];
view.drawableMultisample = GLKViewDrawableMultisample4X;
view.context = context;
view.delegate = view;
[view setupGL];
[self.view addSubview:view];
- (void)setupGL {
[EAGLContext setCurrentContext:self.context];
glEnable(GL_CULL_FACE);
self.effect = [[GLKBaseEffect alloc] init];
NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],
GLKTextureLoaderOriginBottomLeft,
nil];
NSError * error;
NSString *path = [[NSBundle mainBundle] pathForResource:@"myImage" ofType:@"png"];
GLKTextureInfo * info = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
if (info == nil) {
NSLog(@"Error loading file: %@", [error localizedDescription]);
}
self.effect.texture2d0.name = info.name;
self.effect.texture2d0.enabled = true;
glGenVertexArraysOES(1, &vertexArray);
glBindVertexArrayOES(vertexArray);
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color));
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition,
3,
GL_FLOAT,
GL_FALSE,
sizeof(Vertex),
(const GLvoid *) offsetof(Vertex, Position)
);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, TexCoord));
glBindVertexArrayOES(0);
rotMatrix = GLKMatrix4Identity;
}
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
self.contentScaleFactor = 2.0;
self.opaque = NO;
self.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
[self.effect prepareToDraw];
glBindVertexArrayOES(vertexArray);
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
}
这是我想旋转对象的地方
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
CGPoint lastLoc = [touch previousLocationInView:[touch view]];
CGPoint diff = CGPointMake(lastLoc.x - location.x, lastLoc.y - location.y);
float rotX = -1 * GLKMathDegreesToRadians(diff.y / 2.0);
float rotY = -1 * GLKMathDegreesToRadians(diff.x / 2.0);
GLKVector3 xAxis = GLKVector3Make(1, 0, 0);
rotMatrix = GLKMatrix4Rotate(rotMatrix, rotX, xAxis.x, xAxis.y, xAxis.z);
GLKVector3 yAxis = GLKVector3Make(0, 1, 0);
rotMatrix = GLKMatrix4Rotate(rotMatrix, rotY, yAxis.x, yAxis.y, yAxis.z);
[self update];
}
你似乎没有任何东西来运行你的绘图循环。将其放入您的viewDidLoad中:
displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(render:)];
displayLink.frameInterval = 2;
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
将displayLink定义为标题中的属性,并将@selector(render:)更改为渲染函数(不必是glkview函数,因为您不使用GLKViewController)。旋转glkview有四个要求: 1.与旋转相关的代码必须放置在视图控制器内 2.横向旋转必须增加90° 3.旋转相关代码必须包含在UIView动画块中 4.度应以弧度表示,并以负数表示 以下是我成功使用的代码: GLKViewController实现文件(.m)中的:
static inline double radians (double degrees) { return degrees * M_PI/180; }
。
.
这些要求听起来可能有点可疑;但是,我向您保证,您不会以任何其他方式旋转它,并在所有方向和所有方面保持一致性。比旋转视图更好的是旋转四边形纹理坐标;尽管苹果的任何示例代码或文档中都没有演示,但他们的文档确实建议旋转输出而不是旋转GLKView 以下是四边形纹理坐标设置的所有排列:
static const GLfloat noRotationTextureCoordinates[] = {
0.0f, 0.0f,
1.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
};
static const GLfloat rotateLeftTextureCoordinates[] = {
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 0.0f,
0.0f, 1.0f,
};
static const GLfloat rotateRightTextureCoordinates[] = {
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 1.0f,
1.0f, 0.0f,
};
static const GLfloat verticalFlipTextureCoordinates[] = {
0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
};
static const GLfloat horizontalFlipTextureCoordinates[] = {
1.0f, 0.0f,
0.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f,
};
static const GLfloat rotateRightVerticalFlipTextureCoordinates[] = {
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f,
};
static const GLfloat rotateRightHorizontalFlipTextureCoordinates[] = {
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f,
};
static const GLfloat rotate180TextureCoordinates[] = {
1.0f, 1.0f,
0.0f, 1.0f,
1.0f, 0.0f,
0.0f, 0.0f,
};
要在其AVBasicVideo中使用的GLKView中使用此选项。。。示例应用程序,例如:
self.bounds.size
而不是self.presentationRect.size
,以便输出填充整个屏幕,而不考虑其旋转:
CGRect vertexSamplingRect = AVMakeRectWithAspectRatioInsideRect(self.bounds.size, self.eaglLayer.bounds);
谢谢你的回答。我试过你的代码,可惜没用。对我来说,每当我需要重新绘制GLKView时,都可以使用
[self display]
。顺便说一下,我将委托设置为GLKView本身,因此在调用其上的display
方法时,将调用其委托方法。这是一种糟糕的方法吗?displayLink运行循环与GLKViewController/GLKView组合提供的运行循环相同。使用CADisplayLink,您可以精确查看自上次刷新以来发生的循环数;但是,这些信息与我认识的每个人都无关。使用GLKViewController,我可以设置给定视频的每秒帧数;以每秒15帧的速度播放的视频不需要以每秒60帧的速度渲染。能够降低fps比没有fps更有利于snappier应用。