2013-10-27 126 views
3

我使用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

由於沿。

+0

可能的原因是背面剔除(通常默認情況下禁用)或者照明可能導致它爲完全黑色並且與背景無法區分。 – jozxyqk

+0

近裁剪平面不應爲零。嘗試將其更改爲'GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f),aspect,0.01f,10.0f);'。同樣只是'xAxis.v [0]'在旋轉中不正確。 'rotX'是旋轉,接下來的三個值是旋轉的軸。試試'GLKMatrix4Rotate(_rotMatrix,rotX,1,0,0);'。 – jozxyqk

+0

@jozxyqk背面剔除在我的代碼中啓用。我編輯了我的問題並添加了啓用背面剔除的'setupGL()'方法。 –

回答

2

在OpenGL中沒有渲染的東西有很多原因。在這種情況下,它是背面剔除(請參閱問題的評論)。背面剔除很有用,因爲它可以忽略背離相機的三角形並節省一些光柵化/碎片處理時間。由於許多網格/對象是水密的,你永遠不想看到裏面,所以實際上需要雙面着色是不常見的。該功能從定義三角形的前/後開始。這是通過給定頂點的順序完成的(有時稱爲繞組方向)。 glFrontFace順時針方向/逆時針方向定義轉發,選擇glCullFace選剔除正面或背面(我想一些可以認爲在具有兩者沒有太大的點),最後,以啓用/禁用:

glEnable(GL_CULL_FACE); //discards triangles facing away from the camera 
glDisable(GL_CULL_FACE); //default, two-sided rendering 

其他一些我檢查幾何不可見的東西包括...

  • 幾何顏色是否與背景相同。在這裏選擇非黑色/白色背景可能非常方便。
  • 幾何實際是在觀察體積內繪製的。拋出一個簡單的對象(即時模式幫助),也許使用標識投影/模型視圖來排除它們。
  • 查看量是否正確。太近的飛機(造成z戰鬥)或靠近飛機的飛機是常見問題。另外,當切換到投影矩陣時,在Z = 0平面上繪製任何東西將不再可見。
  • 啓用混合並且一切都是透明的。
  • 是否清除深度緩衝區並導致後續幀被丟棄。
  • 在固定的流水線渲染中,有從前一幀繼承的變換,導致物體射入距離。始終將glLoadIdentity保留在顯示功能的頂部。
  • 是渲染循環結構正確 - 清晰/戰平/ swapbuffers
  • 當然還有堆更多 - 幾何着色器不能輸出任何東西,頂點着色器轉換頂點到同一個位置(所以他們都退化),片段着色呼叫當他們不應該拋棄時,VBO綁定/索引問題等。檢查GL錯誤是必須的,但從不會發現所有錯誤。