帮助了解gluLookAt() [英] Help understanding gluLookAt()

查看:119
本文介绍了帮助了解gluLookAt()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,您站在地上抬头看着天空中的一个立方体.当您倾斜头部时,立方体将移动.我试图通过在观察围绕原点绘制的简单3D立方体的同时操纵相机的倾斜度,在iPhone上使用OpenGL ES复制此图像.我正在使用Cocos2d中的gluLookAt()函数,该函数应该模拟OpenGL版本,并且似乎当我尝试修改任何值时,我的多维数据集都消失了.

Imagine you're standing on the ground looking up at a cube in the sky. As you tilt your head, the cube moves. I'm trying to replicate this using OpenGL ES on the iPhone by manipulating the tilt of the camera while looking at a simple 3D cube drawn around the origin. I'm using the gluLookAt() function from Cocos2d which is supposed to emulate the OpenGL version and it seems that when I try to tinker with any of the values, my cube disappears.

我的问题是:您能否在此处提供gluLookAt()用法,让我开始操作相机,以便弄清楚它是如何工作的?在学习如何沿Y轴倾斜相机方面,我真的很有趣.

My question is: Can you provide a gluLookAt() usage here that will get me started manipulating the camera so I can figure out how this works? I'm really just interesting in learning how to tilt the camera along the Y axis.

这是我当前的代码:

视口配置

glBindFramebufferOES(GL_FRAMEBUFFER_OES, _viewFramebuffer);
glViewport(0, 0, _backingWidth, _backingHeight);

投影矩阵

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Maybe this should be a perspective projection?? If so,
// can you provide an example using gluPerspective()?
glOrthof(-_backingWidth, _backingWidth,-_backingHeight, _backingHeight, -1, 1);

ModelView矩阵

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt() // What goes here?

绘图代码

static const GLfloat cubeVertices[] = {
    -1.0, -1.0,  1.0,
    1.0, -1.0,  1.0,
    -1.0,  1.0,  1.0,
    1.0,  1.0,  1.0,
    -1.0, -1.0, -1.0,
    1.0, -1.0, -1.0,
    -1.0,  1.0, -1.0,
    1.0,  1.0, -1.0,
};

static const GLushort cubeIndices[] = {
    0, 1, 2, 3, 7, 1, 5, 4, 7, 6, 2, 4, 0, 1
};

static const GLubyte cubeColors[] = {
    255, 255,   0, 255,
    0,   255, 255, 255,
    0,     0,   0,   0,
    255,   0, 255, 255,
    255, 255,   0, 255,
    0,   255, 255, 255,
    0,     0,   0,   0,
    255,   0, 255, 255
};

glVertexPointer(3, GL_FLOAT, 0, cubeVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, cubeColors);
glEnableClientState(GL_COLOR_ARRAY);    
glDrawElements(GL_TRIANGLE_STRIP, 14, GL_UNSIGNED_SHORT, cubeIndices);

推荐答案

我不确定您到底想要什么,但是这里有一些解释:

I'm not completely sure what exactly you want, but here some explanations:

gluLookAt期望3个矢量(每个为3倍):首先是摄像机的位置(视点),然后是您所看的位置(中心点),最后是指定向上方向的向上矢量(这不一定是理想的正交向上方向,因为它仍然是正交的).

gluLookAt expects 3 vectors (each as 3 doubles): first the position of the camera (eye point), then the position to where you look (center point) and finally an up-vector that specifies an up-direction (this need not be the perfect orthogonal upward direction, as it is reorthogonalized anyway).

因此,如果您站在(0,0,5)处并查看立方体(位于中心),并且希望y轴为向上方向,则可以调用gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)来查看您的立方体完全美丽.

So if you stand at (0,0,5) and look at your cube (that is at the center) and want the y-axis to be the up-direction, you would call gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0) to see your cube in full beauty.

如果您想将头向侧面倾斜,则只需更改向上矢量并将其稍微向侧面旋转即可.或者,如果您想向上看,但不将头部向侧面倾斜,则仍然将y轴用作向上矢量,但是您只是看了另一个点,因此将中心点更改为上方和前方的一个点.的人(可能围绕眼睛的位置旋转).但这在您要向上看时不起作用,在这种情况下,您需要将向上向量更改为与y轴正交的值(当然,除了将中心点设置为笔直位于您上方的点之外) ).

If you want to tilt your head to the side, you just need to change the up-vector and rotate it to the side a bit. Or if you want to look up, but without tilting the head to the side, you still use the y-axis as up-vector, but you just look at another point, so you change the center point to a point above and in front of you (maybe rotated about the eye position). But this won't work if you want to look straight up, in this case you need to change the up-vector to something orthogonal to the y-axis (in addition to setting the center point to a point straight above you, of course).

但是我认为您需要透视投影.您当前的正射至少对于您的坐标来说是非常不合适的,因为它指定了一个坐标系统,其中坐标的大小为像素大小,因此[-1,1]多维数据集大约等于屏幕上像素的大小.尝试gluPerspective(60.0, ((double)_backingWidth)/_backingHeight, 0.1, 100.0).如果您确实想要正交投影而没有任何现实的透视变形,则可以使用glOrtho,但是在这种情况下,应保持glOrtho参数的大小比例和模型的坐标大致同步(因此,请不要指定屏幕大小的正交并使用[-1,1]范围内的坐标.

But I think you want a perspective projection. Your current ortho is at least quite inappropriate for your coordinates, as it specifies a coordinate system in which coordinates are in the size of pixel, so your [-1,1]-cube is about the size of a pixel on the screen. Try gluPerspective(60.0, ((double)_backingWidth)/_backingHeight, 0.1, 100.0). If you really want an orthographic projection without any realistic perspective distortion, you can use glOrtho, but in this case you should keep the size proprtions of the glOrtho parameters and your model's coordinates roughly in-sync (therefore not specify a screen-sized ortho and using coordinates in the [-1,1] range).

这篇关于帮助了解gluLookAt()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆