Iphone OpenGl ES 1.1 : How to modify the camera center from (0, 0,0) 到 (x,y,z)?

标签 iphone ios4 opengl-es

例如,我的对象的中心是(1,2,-1)。我如何修改opengl es“相机”的中心(坐标系的中心)从(0,0,0)到(1,2,-1)

如果我没记错的话,gluLookAt 就是执行此操作的工具,但它不包含在 openGl es 1.1 中。有没有其他函数可以实现相同的功能

最佳答案

gluLookAt 只是将当前矩阵与根据输入创建的明确定义的矩阵相乘。 Its man page包含您需要的公式。

例如:

void mygluLookAt(GLfloat eyeX, GLfloat eyeY, GLfloat eyeZ, GLfloat centreX, GLfloat centreY, GLfloat centreZ, GLfloat upX, GLfloat upY, GLfloat upZ)
{
    GLfloat f[3] =
    {
        centreX - eyeX,
        centreY - eyeY,
        centreZ - eyeZ,
    };

    // make f and the up vector have unit length
    GLfloat lengthOfF = sqrtf(f[0]*f[0] + f[1]*f[1] + f[2]*f[2]);
    f[0] /= lengthOfF;
    f[1] /= lengthOfF;
    f[2] /= lengthOfF;

    GLfloat lengthOfUp = sqrtf(upX*upX + upY*upY + upZ*upZ);
    upX /= lengthOfUp;
    upY /= lengthOfUp;
    upZ /= lengthOfUp;

    // use the cross product of f and Up to get s,
    // and the cross product of s and f to get u
    GLfloat s[3] =
    {
        f[1]*upZ - upY*f[2],
        f[2]*upX - upZ*f[0],
        f[0]*upY - upX*f[1]
    };

    GLfloat u[3] =
    {
        s[1]*f[2] - f[1]*s[2],
        s[2]*f[0] - f[2]*s[0],
        s[0]*f[1] - f[0]*s[1]
    };

    // Fill the matrix as prescribed.
    // Note: OpenGL is "column major"
    GLfloat M[16] =
    {
          s[0], u[0], -f[0], 0.0f,
          s[1], u[1], -f[1], 0.0f,
          s[2], u[2], -f[2], 0.0f,
          0.0f, 0.0f, 0.0f,  1.0f
    };

    glMultMatrixf(M);
    glTranslatef(-eyeX, -eyeY, -eyeZ);
}

这是从手册页直接输入的。因此它尚未经过测试,但如果我犯了任何错误,应该很容易发现。

关于Iphone OpenGl ES 1.1 : How to modify the camera center from (0, 0,0) 到 (x,y,z)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7426823/

相关文章:

iphone - 如何从 Web 浏览器控件中托管的 HTML 在 Safari 中打开链接?

android - 将 iPhone/iPad 应用程序转换为 Android

iphone - iPhone OS4,无法使用SystemSounds调整音量设置

opengl-es - Cocos2d v3 重复纹理

java - glTexImage2D 中的 ByteBuffer.wrap() 导致过多的垃圾收集

iphone - 以编程方式切换自定义 View (推送和弹出)

iphone - 如何使用 AFNetworking 2.0 下载图像?

javascript - 在手持设备上禁用 fancybox

java - 我应该如何在游戏中绘制这个?

ios - xcode 6 升级导致 prepare for segue lag showing screen