我使用OpenGL 2.0繪製矩形。最初,視口是這樣的,我從上面看,我可以看到我的矩形,如我所料。 然後我開始旋轉這個矩形圍繞X軸。當旋轉角度等於-90度(或者在另一方向旋轉時爲+ 90度)時,矩形消失。 當我旋轉90deg/-90deg而不是視圖消失時,我期望看到矩形的底部表面。當上表面即將準備顯示時,它會重新出現,總旋轉角度爲-270deg(或+270度)。OpenGL視圖在旋轉時消失
我如何確保我可以一直看到矩形(上下兩面都必須在旋轉時可見)?
這裏」有關的代碼:
- (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
由於沿。
可能的原因是背面剔除(通常默認情況下禁用)或者照明可能導致它爲完全黑色並且與背景無法區分。 – jozxyqk
近裁剪平面不應爲零。嘗試將其更改爲'GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f),aspect,0.01f,10.0f);'。同樣只是'xAxis.v [0]'在旋轉中不正確。 'rotX'是旋轉,接下來的三個值是旋轉的軸。試試'GLKMatrix4Rotate(_rotMatrix,rotX,1,0,0);'。 – jozxyqk
@jozxyqk背面剔除在我的代碼中啓用。我編輯了我的問題並添加了啓用背面剔除的'setupGL()'方法。 –