代码之家  ›  专栏  ›  技术社区  ›  VJ Vélan Solutions

OpenGL视图在旋转时消失

  •  4
  • VJ Vélan Solutions  · 技术社区  · 11 年前

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

    谢谢

    1 回复  |  直到 11 年前
        1
  •  3
  •   jozxyqk    11 年前

    OpenGL中无法渲染的原因有很多。在这种情况下 背面剔除 (见对该问题的评论)。背面剔除很有用,因为它可以忽略背对相机的三角形,并节省一些光栅化/碎片处理时间。由于许多网格/对象都是防水的,而且你永远不想看到里面,所以实际上想要双面着色是不常见的。此功能从定义三角形的前/后开始。这是按照顶点的顺序完成的(有时称为 弯曲的 方向)。 glFrontFace 选择定义向前的顺时针/逆时针方向, glCullFace 选择剔除前面或后面(我想有些人可能会认为两者都有没有多大意义),最后启用/禁用:

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

    我检查的其他一些不可见的几何图形包括。。。

    • 几何图形的颜色与背景颜色相同吗。选择非黑/白背景在这里很方便。
    • 几何图形是否实际绘制在查看体积内。加入一个简单的对象(即时模式有帮助),也许可以使用身份投影/模型视图来排除它们。
    • 音量是否正确。近/远飞机相距太远(导致z战)或 0.0f 飞机附近是常见的问题。此外,当切换到投影矩阵时,在Z=0平面上绘制的任何内容都将不再可见。
    • 启用了混合,一切都是透明的。
    • 深度缓冲区是否未被清除并导致后续帧被丢弃。
    • 在固定管道渲染中 glTranslate / glRotate 从前一帧延续下来的变换导致物体射向远处。始终保持 glLoadIdentity 位于显示功能的顶部。
    • 渲染循环的结构是否正确-清除/绘制/交换缓冲区
    • 当然,还有很多——几何着色器不输出任何东西,顶点着色器将顶点转换到同一位置(因此它们都退化了),片段着色器在不应该时调用丢弃,VBO绑定/索引问题等。检查GL错误是必须的,但决不能捕捉所有错误。