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.

转载于:https://www.cnblogs.com/open-coder/archive/2012/10/28/2743126.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值