OpenGL ES学习2----彩立方制作遇到两个问题

13 篇文章 0 订阅
3 篇文章 0 订阅
第一个问题:视锥还未被定义时,为什么写好了此函数,运行时却什么都看不到?代码:

#pragma mark - GLKView and GLKViewController delegate methods

- (void) glkView:(GLKView *)view drawInRect:(CGRect)rect

{

    

    static const GLfloat cubeVertices[] =

    {

        -0.5, 0.5, 0.5,

        0.5, 0.5, 0.5,

        0.5,-0.5, 0.5,

        -0.5,-0.5, 0.5,

        

        -0.5, 0.5,-0.5,

        0.5, 0.5,-0.5,

        0.5,-0.5,-0.5,

        -0.5,-0.5,-0.5,

    };

    

    

    static const GLubyte cubeColors[] =

    {

        255, 255,   0, 255,

        0,   255, 255, 255,

        0,     0,   0,   0,

        255,   0, 255, 255,

        

        255, 255,   0, 255,

        0,   255, 255, 255,

        0,     0,   0,   0,

        255,   0, 255, 255,

    };

    

    static const GLubyte tfan1[ 6 * 3 ] =

    {

        1,0,3,

        1,3,2,

        1,2,6,

        1,6,5,

        1,5,4,

        1,4,0

    };

    

    static const GLubyte tfan2[6 * 3] =

    {

        7,4,5,

        7,5,6,

        7,6,2,

        7,2,3,

        7,3,0,

        7,0,4

    };

    

    static GLfloat transY = 0.0;

    static GLfloat z = - 2.0;

    static GLfloat spinX = 0;

    static GLfloat spinY = 0;

    

    glClearColor( 0.5 , 0.5 , 0.5 , 1.0 );

    glClear( GL_COLOR_BUFFER_BIT );

    

    glEnable( GL_CULL_FACE );

    glCullFace( GL_BACK );

    

//    glMatrixMode( GL_PROJECTION );

//    glLoadIdentity();

    

    glMatrixMode( GL_MODELVIEW );

    glLoadIdentity();

    

    

//    glTranslatef( 0.0 , ( GLfloat ) ( sinf( transY ) / 2.0 ) , 0.0 );

    glTranslatef( 0.0 , ( GLfloat ) ( sinf( transY ) / 2.0 ) , z );

    glRotatef( spinY , 0.0 , 1.0 , 0.0 );

    glRotatef( spinX , 1.0 , 0.0 , 0.0 );

    

    

    transY += 0.075f;

    

    spinX += 0.25;

    spinY += 0.25;

//    z += 0.01f;

    

    glVertexPointer( 3 , GL_FLOAT , 0 , cubeVertices );

    glEnableClientState( GL_VERTEX_ARRAY );

    glColorPointer( 4 , GL_UNSIGNED_BYTE , 0 , cubeColors );

    glEnableClientState( GL_COLOR_ARRAY );

    

    glDrawElements( GL_TRIANGLE_FAN , 6 * 3 , GL_UNSIGNED_BYTE , tfan1 );

    glDrawElements( GL_TRIANGLE_FAN , 6 * 3 , GL_UNSIGNED_BYTE , tfan2 );

//    if ( !( counter % 100 ) )

//    {

//        NSLog(@"FPS: %d \n" , self.framesPerSecond );

//    }

//    

//    counter ++;

}原因是视锥未定义,默认的远裁剪平面为-1.0,这意味着任何远于它的事物都被剔除掉,此时你可以改改那个z = -2.0;改为-0.02;再运行一下,能够出现一下结果。

另外,还要使用 glFrustumf()函数,将坐标投影到屏幕上,视锥有6部分组成,左右,上下,远近,定以后就像照相机的镜头一样,去看事物。关于定义视锥的代码:

- (void) setClipping

{

    float aspectRatio;

    const float zNear = 0.1;

    const float zFar = 1000;

    const float fieldOfView = 60.0;

    GLfloat size;

    

    CGRect frame = [ [ UIScreen mainScreen ] bounds ];

    

    // height and width values clamp the fov to the height; flipping it would make relative to the width.

    // so if we want the field-of-view to be 60 degrees , similar to that of a wide angle lens , it will be based on the height of our window and not the width.

    // this is important to know when rendering to a non-square screen.

    

    aspectRatio = ( float ) frame.size.width / ( float ) frame.size.height;

    

    // set the openGL projection matrix .

    

    glMatrixMode( GL_PROJECTION );

    

    glLoadIdentity();

    size = zNear * tanf( GLKMathDegreesToRadians( fieldOfView ) / 2.0 );

    

    glFrustumf( - size , size , -size / aspectRatio  , size / aspectRatio , zNear , zFar );

    glViewport( 0 , 0 , frame.size.width , frame.size.height );

    

    // make the OpenGL ModelView matrix the default.

    

    glMatrixMode( GL_MODELVIEW );

}

 

问题二: 先旋转后平移 ,先平移后旋转 这两种对物体移动操作顺序不一样导致效果不一样,究竟为什么呢?这两句就竟放在glTranslatef()前还是后呢?

这个是放在glTranslatef()前:

这个是放在glTranslatef()后:

3D转换是不可交换的,交换后会出现两种不同的结果,前者是物体围绕着视口旋转,后者是围绕自己旋转。 这与矩阵密切相关,先选择就变成以视口为中心轴旋转,然后物体再平移,所以永远看到立方体都是平面;然而先是平移,对应物体的旋转轴也同样的平移,然后旋转,旋转就以旋转轴转动,即自身转动。所以导致两种不同的效果。 以上都是本人学习的理解,有什么错误,请多指点。3D转换是不可交换的,交换后会出现两种不同的结果,前者是物体围绕着视口旋转,后者是围绕自己旋转。

 这与矩阵密切相关,先选择就变成以视口为中心轴旋转,然后物体再平移,所以永远看到立方体都是平面;然而先是平移,对应物体的旋转轴也同样的平

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值