在 2D iPhone 游戏中使用 gluLookAt 移动相机?
我正在尝试使用 gluLookAt 在我的 iPhone 游戏中移动相机,但每次我尝试使用 gluLookAt 时,我的屏幕都会变成“空白”(在本例中为灰色),
我正在尝试渲染一个简单的三角形并移动相机,这是我的代码:
要设置我的场景,我这样做:
glViewport(0, 0, backingWidth, backingHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glRotatef(-90.0, 0.0, 0.0, 1.0); //using iPhone in horizontal mode
glOrthof(-240, 240, -160, 160, -1, 1);
glMatrixMode(GL_MODELVIEW);
然后我的“三角形渲染”代码如下所示:
GLfloat triangle[] = {0, 100, 100, 0, -100, 0,};
glClearColor(0.7, 0.7, 0.7, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertexPointer(2, GL_FLOAT, 0, &triangle);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableClientState(GL_VERTEX_ARRAY);
当我尝试应用 gluLookAt 时,这会在屏幕中间绘制一个红色三角形(我得到了实现来自 Cocos2D 的函数,所以我认为它是正确的),我这样做:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,1,0,0,0,0,0,1); // try to move the camera a bit ?
GLfloat triangle[] = {0, 100, 100, 0, -100, 0,};
glClearColor(0.7, 0.7, 0.7, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertexPointer(2, GL_FLOAT, 0, &triangle);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableClientState(GL_VERTEX_ARRAY);
这导致我进入灰屏(glClearColor 是灰色),我尝试了各种方法并阅读了我在网上找到的有关 gluLookAt 的内容,但没有运气:(,如果有人可以向我解释或向我展示如何以自上而下的方式移动相机(塞尔达等),我将非常感激。
谢谢!
I'm trying to use gluLookAt to move the camera in my iPhone game, but every time I've tried to use gluLookAt my screen just goes "blank" ( grey in this case )
I'm trying to render a simple triangle and to move the camera, this is my code:
to setup my scene I do:
glViewport(0, 0, backingWidth, backingHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glRotatef(-90.0, 0.0, 0.0, 1.0); //using iPhone in horizontal mode
glOrthof(-240, 240, -160, 160, -1, 1);
glMatrixMode(GL_MODELVIEW);
then my "triangle rendering" code looks like:
GLfloat triangle[] = {0, 100, 100, 0, -100, 0,};
glClearColor(0.7, 0.7, 0.7, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertexPointer(2, GL_FLOAT, 0, &triangle);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableClientState(GL_VERTEX_ARRAY);
This draws a red triangle in the middle of the screen, when I try to apply gluLookAt ( I got the implementation of the function from Cocos2D so I asume it's correct ), i do:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,1,0,0,0,0,0,1); // try to move the camera a bit ?
GLfloat triangle[] = {0, 100, 100, 0, -100, 0,};
glClearColor(0.7, 0.7, 0.7, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(1.0, 0.0, 0.0, 1.0);
glVertexPointer(2, GL_FLOAT, 0, &triangle);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableClientState(GL_VERTEX_ARRAY);
This leads me to grey screen (glClearColor is grey), I've tried all sort of things and read what I've found about gluLookAt on the net, but no luck :(, if someone could explain me or show me how to move to move the camera in a top-down fashion ( zelda, etc ), I would really appreciate it.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后 3 个参数看起来不正确。您正在 Z 轴正方向上制作“向上”向量,这意味着您的视线方向平行于三角形的平面(因此三角形不再可见)。你可以尝试:
但是,如果你想做的只是将视图向上/下/左/右移动,那么你可以只使用
glTranslatef
来代替,例如:这应该使你的三角形距离 100 像素右侧和顶部 20 像素。
The last 3 arguments don't look right. You're making the "up" vector in the positive Z direction, which means your view direction is parallel to the triangle's plane (so the triangle is no longer visible). You could try:
However, if all you want to do is move the view up/down/left/right, then you can just use
glTranslatef
instead, like:That should make your triangle 100 pixels to the right and 20 pixels to the top.