Android catcake ~ 3D World

screen_shot_device-2012-10-04-090818

This sample shows how to build a simple 3d world with Catcake, and make the camera walk around.

The 3D world built from the NeHe OpenGL Lesson10. To make all stuffs become much easier, I tried to binarize the world data into a binary file instead of text file. This could avoid the text read and parsing work.

 

Move a camera with Catcake

There is no camera naming object in the catcake library. But if you debug and trace the code, you will find somewhere that used to place the viewer, set the clip distance, build a projection matrix, and so on. Yeach, that is! The class “ckScr”just work like that. A ckScr object id with “DEFAULT_3D_SCREEN_ID”will work as camera for 3d objects. So what we need to do is to get this object, set the some parameters properly.

void Controller::SetupCamera() 
{ 
    mView = ckDrawMgr::getScreen(ckDrawMgr::DEFAULT_3D_SCREEN_ID); 
    if (NULL != mView) 
    { 
        mView->view() = ckMat::UNIT.translate(0.0f, 0.25f, 0.0f); 
        mView->setClipDist(0.1f, 100.0f); 
    } 
}

To let the camera walk around in the scene, what we need to need is modify it’s view matrix. The following code will make the viewer move forward or back forward:

if ( mInMove ) 
{ 
    ckMat mat = mView->view(); 
    mat.trans += mat.z_axis * mStep; 
    mView->view() = mat; 

    mInMove = false; 
}

The following code will make the viewer turn some angle along some axis:

 

if ( mInTurn ) 
{ 
    ckMat mat = mView->view(); 

    ckVec trans = mat.trans; 
    mat.trans = ckVec(0, 0, 0); 

    r32 localR32[16]; 
    mat.toR32x16(localR32); 

    ckMat rot = mQuat.toMat(ckVec(0,0,0)); 
    r32 rotR32[16]; 
    rot.toR32x16(rotR32); 

    r32 result[16]; 
    ckMat::mulR32x16(result, rotR32, localR32); 

    mat = ckMat::fromR32x16(result); 
    mat.trans = trans; 

    mView->view() = mat; 

    mInTurn = false; 
}

Integrate the Android interactive

At first, I planed to use the Android orientation sensor to make the camera moving.

mobile

I would like to use the “Pitch”angle (from back to front) to move the camera back or forward, use the “Roll”angle (from left to right) to turn the camera right or left. {Pitch value could be got from event.values[1]; Roll value could be got from event.values[2]; event.values[0] keep the direction, from 0 degree (North) to 359 degree} After I implemented all the things, I find that such interactive was far from what I expected before. I even could not turn back, or move further. My activity was very limited. To give myself more freedom, I switch back the code to use the previous interactive method – use touch event.  So at last, I decided to use scroll horizontally to turn left or right, use scroll vertically to move back or front forward. This works quit well.

The following are the code that make the camera moving in the Java source code:

public void onDrag(float dx, float dy) 
{ 
    // If dx, dy too small, this will cause some problem 
    if ( Math.abs(dx) >= 0.5f || Math.abs(dy) >= 0.5f ) 
    { 
        if ( Math.abs(dx) > Math.abs(dy) ) 
            CatcakeUtil.nativeTurnLeft( dx * 0.2f ); 
        else 
            CatcakeUtil.nativeMoveForward( dy * 0.04f ); 
        invalidate();    
    } 
}

 

Something more

Here are something that need to take care:

1) Android file path name is case sensitive. I hard coded a file path like this, “/data/data/catcake_application.C3DWorld/data/World.bin”, but the real file path is as following, “/data/data/catcake_application.C3DWorld/data/world.bin”. This will cause the application could not read the file correctly on Android, but no problem on windows. And the strangle thing is that catcake will not  report any readable errors or warnings. Android OS based on the Linux, case sensitive is make scene. Maybe developing the c++ on Linux OS for android should be more appreciated.

2) If I want to see the log, I need to run “Debug”->”Debug As Android Application”first. Why? The mobile device debug process is not so native. I will check more material later.

 

The full source code could be found from here.

posted @ 2012-10-28 08:14  opencoder  阅读(393)  评论(0编辑  收藏  举报