我正在使用OpenGL 2.0绘制一个矩形。最初视口是这样的,我从上面看,我可以看到我的预期的矩形。 然后我开始围绕x轴旋转这个矩形。当旋转角度等于-90度(或者如果向另一个方向旋转时为+90度),则矩形消失。 当我旋转超过90度/ -90度但是视图消失时,我希望看到矩形的底部表面。当上表面准备好显示时,它会重新出现,总旋转角度为-270度(或+270度)。
我如何确保我可以一直看到矩形(旋转时上下表面都必须可见)?
这里是相关的代码:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
if ([touches count] == 1) {
CGPoint currLoc = [touch locationInView:self];
CGPoint lastLoc = [touch previousLocationInView:self];
CGPoint diff = CGPointMake(lastLoc.x - currLoc.x, lastLoc.y - currLoc.y);
rotX = -1 * GLKMathDegreesToRadians(diff.y / 2.0);
rotY = -1 * GLKMathDegreesToRadians(diff.x / 2.0);
totalRotationX += ((rotX * 180.0f)/3.141592f);
NSLog(@"rotX: %f, rotY: %f, totalRotationX: %f", rotX, rotY, totalRotationX);
//rotate around x axis
GLKVector3 xAxis = GLKMatrix4MultiplyVector3(GLKMatrix4Invert(_rotMatrix, &isInvertible), GLKVector3Make(1, 0, 0));
_rotMatrix = GLKMatrix4Rotate(_rotMatrix, rotX, xAxis.v[0], 0, 0);
}
}
-(void)update{
GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0, 0, -6.0f);
modelViewMatrix = GLKMatrix4Multiply(modelViewMatrix, _rotMatrix);
self.effect.transform.modelviewMatrix = modelViewMatrix;
float aspect = fabsf(self.bounds.size.width / self.bounds.size.height);
GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0, 10.0f);
self.effect.transform.projectionMatrix = projectionMatrix;
}
- (void)setupGL {
NSLog(@"setupGL");
isInvertible = YES;
totalRotationX = 0;
[EAGLContext setCurrentContext:self.context];
glEnable(GL_CULL_FACE);
self.effect = [[GLKBaseEffect alloc] init];
// New lines
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);
// Old stuff
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);
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
// New lines (were previously in draw)
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color));
_rotMatrix = GLKMatrix4Identity;
// New line
glBindVertexArrayOES(0);
initialized = 1;
}
我是OpenGL的新手,我正在使用GLKit和OpenGL 2.0
感谢。
答案 0 :(得分:2)
导致OpenGL无法呈现的原因有很多。在这种情况下,它是背面剔除(请参阅问题评论)。背面剔除很有用,因为它可以忽略背离相机的三角形并节省一些光栅化/片段处理时间。由于许多网格/物体都是水密的,而且你永远不想看到内部,实际上想要双面着色并不常见。此功能从定义三角形的前/后开始。这是通过给出顶点的顺序来完成的(有时称为绕组方向)。 glFrontFace
选择顺时针/逆时针定义向前的方向,glCullFace
选择剔除前面或后面(我猜有些人可能认为两者都没有多少点)并最终启用/禁用:< / p>
glEnable(GL_CULL_FACE); //discards triangles facing away from the camera
glDisable(GL_CULL_FACE); //default, two-sided rendering
我检查几何体不可见的其他一些事情包括......
0.0f
近平面是常见问题。此外,当切换到投影矩阵时,在Z = 0平面上绘制任何内容都将不再可见。glTranslate
/ glRotate
变换是从前一帧继承的,导致对象射向远处。始终在显示功能的顶部保留glLoadIdentity
。