gluPerspective & gluLookAt Implementation in Android NDK

There is no glu libraries for android ndk, so I need re-implement one for it. After some time research, I grab some ideas and re-implement glPerspective and gluLookAt function. Both of them works pretty well on PC and android.

 

gluPerspective Implementation

复制代码
void xgluPerspective( float fovy, float aspect, float near_clip, float far_clip )
{
    const double PI = 3.1415926;
    double TWOPI_OVER_360 = 2.0 * PI / 360.0;
    float half_height = near_clip * (float)tan( fovy * 0.5 * TWOPI_OVER_360 );
    float half_width = half_height * aspect;

#ifdef WIN32
    glFrustum( -half_width, half_width, -half_height, half_height, near_clip, far_clip );
#elif defined(CK_ANDROID)
    glFrustumf( -half_width, half_width, -half_height, half_height, near_clip, far_clip );
#endif
}
复制代码

 

gluLookAt Implementation

复制代码
void xgluLookAt (
                        float eyex, 
                        float eyey, 
                        float eyez, 
                        float centerx, 
                        float centery, 
                        float centerz, 
                        float upx, 
                        float upy, 
                        float upz)
{
    vector3_t forward(centerx - eyex, centery - eyey, centerz - eyez);
    vector3_t up(upx, upy, upz);

    forward.Normalize();

    vector3_t side = forward ^ up;
    side.Normalize();

    up = side ^ forward;

    float m[4][4];

    m[0][0] = side[0];
    m[1][0] = side[1];
    m[2][0] = side[2];
    m[3][0] = 0.0f;

    m[0][1] = up[0];
    m[1][1] = up[1];
    m[2][1] = up[2];
    m[3][1] = 0.0f;

    m[0][2] = -forward[0];
    m[1][2] = -forward[1];
    m[2][2] = -forward[2];
    m[3][2] = 0.0f;

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