1
\$\begingroup\$

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);
    glLoadIdentity();
    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();
  }
\$\endgroup\$
1
  • \$\begingroup\$ 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/… \$\endgroup\$
    – mkanakis
    Nov 11 '16 at 19:03
1
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$ Nov 12 '16 at 4:23
  • \$\begingroup\$ @Nope Try loading the identity matrix to the modelview matrix befire you do anything to it. \$\endgroup\$
    – Bálint
    Nov 12 '16 at 10:09
  • 3
    \$\begingroup\$ 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. \$\endgroup\$
    – 3Dave
    Feb 6 '17 at 14:29
  • \$\begingroup\$ 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. \$\endgroup\$ Oct 10 '17 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.