OpenGL ES學習2----彩立方製作遇到兩個問題

第一個問題:視錐還未被定義時,爲什麼寫好了此函數,運行時卻什麼都看不到?代碼:

#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轉換是不可交換的,交換後會出現兩種不同的結果,前者是物體圍繞着視口旋轉,後者是圍繞自己旋轉。

 這與矩陣密切相關,先選擇就變成以視口爲中心軸旋轉,然後物體再平移,所以永遠看到立方體都是平面;然而先是平移,對應物體的旋轉軸也同樣的平

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章