# GL how to look at the center of the viewport

I'm using GLFW3 and OpenGL to visualize measurements from an IMU that I have. I've gotten the simple cube example working and I can rotate it successfully with quaternions that I've input.

However since I'm visualizing an IMU, it is important that I know which way I am looking at it. I would prefer to look at it from a top down view.

Hence I want to keep the cube at the center of the world (0, 0, 0) and look down at it from a corresponding height (0, 0, z) which is where I want my camera to be at. I've the following code, can someone please guide me on how to set up my glLookAt function? All the values that I input make my cube disappear.

void display( GLFWwindow* window )
{
while(!glfwWindowShouldClose(window))
{
// Scale to window size
GLint windowWidth, windowHeight;
glfwGetWindowSize(window, &windowWidth, &windowHeight);
glViewport(0, 0, windowWidth, windowHeight);

// Draw stuff
glClearColor(0.2, 0.2, 0.2, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION_MATRIX);
gluPerspective( 60, (double)windowWidth / (double)windowHeight, 0.1, 100 );

glMatrixMode(GL_MODELVIEW_MATRIX);
glTranslatef(0,0,-5);

drawCube();

// Update Screen
glfwSwapBuffers(window);

// Check for any input, or window movement
glfwPollEvents();
}

• Please stop using deprecated OpenGL functions such as glLoadIdentity, glMatrixMode e.t.c. Move to modern OpenGL it will be better for you, trust me. Nice tutorial with GLFW3 which you are already familiar with and modern OpenGL(3.3) opengl-tutorial.org/beginners-tutorials/… Nov 11, 2016 at 19:03

Let's look at the declaration of gluLookAt:

void gluLookAt(GLdouble eyeX,
GLdouble eyeY,
GLdouble eyeZ,
GLdouble centerX,
GLdouble centerY,
GLdouble centerZ,
GLdouble upX,
GLdouble upY,
GLdouble upZ);


eyeX, eyeY and eyeZ are (obviously) the camera position (which you should have already).

center X, Y and Z are the positions of the object you want to focus on.

upX, upY and upZ are the up vector's coordinates (I seriously have no idea why OpenGL needs this, but I won't argue). This should be 0, 1 and 0 respectively.

Although, you mentioned that you wanted to visualize some data without messing with vbos or even vaos, but this is one of the reasons engines are implemented.

• I've tried both combinations of this: gluLookAt(0, 0, -5, 0, 0, 0, 0, 1, 0) and from +5, but I still can't see my object. Nov 12, 2016 at 4:23
• @Nope Try loading the identity matrix to the modelview matrix befire you do anything to it. Nov 12, 2016 at 10:09
• The Up vector is needed because there's no guarantee that the camera's "up" is the (0,1,0). Imagine doing a barrel roll, or a spider walking on the ceiling. Everything in the view would be upside down. Feb 6, 2017 at 14:29
• In fact, to quote OpenGL docs: "The UP vector must not be parallel to the line of sight from the eye point to the reference point" (emphasis mine). If it does, it produces a degenerate transform (specifically, a zero side-vector which maps all vertices onto a single vertical line) which is a Very Bad Thing™. In OP's situation, looking straight down, this happens with an up vector that points straight "up", i.e. (0,1,0). Point that thing somewhere else, like toward whichever direction you actually want at the top of your window. Oct 10, 2017 at 20:28