Help understand gluLookAt ()

Imagine you are standing on the ground looking at a cube in the sky. When you tilt your head, the cube moves. I am trying to reproduce this using OpenGL ES on an iPhone, manipulating the tilt of the camera, looking at a simple 3D cube drawn around the source. I use the gluLookAt() function from Cocos2d, which should emulate the OpenGL version, and it seems that when I try to redo any values, my cube disappears.

My question is: Can you provide the use of gluLookAt() here to get me to start manipulating the camera so that I can understand how this works? I am really interested to know how to tilt the camera along the Y axis.

Here is my current code:

Viewport Configuration

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

Design matrix

 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 Matrix

 glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt() // What goes here? 

Drawing code

 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); 
+4
source share
1 answer

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

gluLookAt expects 3 vectors (each 3 doubles): first, the position of the camera (eye point), then the position where you are looking (center point), and finally, the top vector, which indicates the upward direction (this should not be perfect orthogonal ascending direction, as it is in any case reorthogonal).

So, if you are standing at (0,0,5) and looking at your cube (that is, in the center) and want the y axis pointing up, you would 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.

If you want to tilt your head to the side, you just need to change the up-vector and turn it slightly to the side. Or, if you want to look up, but not tilting your head to the side, you still use the y axis as a vector up, but you just look at another point, so you change the center point to a point above and in front (maybe rotated around position eye). But this will not work if you want to look straight up, in which case you need to change the vector up to something orthogonal to the y axis (in addition to setting the center point to a point directly above you, of course).

But I think you need a perspective. Your current ortho is at least completely inappropriate for your coordinates, because it defines a coordinate system in which the coordinates are in pixel size, so your [-1,1] -cube has a pixel size on the screen. Try gluPerspective(60.0, ((double)_backingWidth)/_backingHeight, 0.1, 100.0) . If you really need a spelling projection without any realistic distortion of perspective, you can use glOrtho , but in this case you should support the size parameters of the glOrtho parameters and the coordinates of your model approximately synchronously (therefore, do not specify a screen-described ortho and use coordinates in the range [ -1.1]).

+5
source

All Articles