如何在OpenGL ES中更改3D立方体的透视图?

时间:2011-11-02 17:44:28

标签: iphone opengl-es

我用OpenGL ES绘制一个立方体,我想对它有一个上面的观点。但我不知道应该修改什么...也许你可以帮忙。

- (void)render:(CADisplayLink*)displayLink {
    //-(void) render{

    glClearColor(255.0, 255.0/255.0, 255.0/255.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);    

    //projection
    CC3GLMatrix *projection = [CC3GLMatrix matrix];
    float h = 4.0f * self.frame.size.height / self.frame.size.width;


    [projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:4 andFar:8];

      glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);

    CC3GLMatrix *modelView = [CC3GLMatrix matrix];

  //  _currentScaling += displayLink.duration * 1.0;


    [modelView populateFromTranslation:CC3VectorMake(sin(30), _currentScaling , -7)];

    [modelView rotateBy:CC3VectorMake(0, -15, 0)];


   // [modelView scaleByY:_currentScaling];


    if(_currentScaling > 2 ){
        [displayLink invalidate];

        _running = 1;
    //    scale = 0;
    }

    glUniformMatrix4fv(_modelViewUniform, 1, 0, modelView.glMatrix);

    // 1
    glViewport(0, 0, self.frame.size.width, self.frame.size.height);

    // 2
    glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, 
                          sizeof(Vertex), 0);
    glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, 
                          sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));

    // 3
    glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), 
                   GL_UNSIGNED_BYTE, 0);

    [_context presentRenderbuffer:GL_RENDERBUFFER];
}

1 个答案:

答案 0 :(得分:1)

我最近开始研究OpenGL,发现this same example。我相信你要找的是改变这条线来改变观点:

[modelView rotateBy:CC3VectorMake(0, -15, 0)];

并指定x,y,z轴所需的旋转度。