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 @   opencoder  阅读(398)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示