简体   繁体   中英

OpenGL gluLookAt

I tried to draw a teapot and view it in 3D but when I ran the program, nothing showed up. There is nothing in the window. I know it has something to do with my gluLookAt() function but I am not sure how to fix it.

// helloteapot.cc

//#include <GLUT/gl.h>
#include <GLUT/glut.h>
#include "GL/glui.h"

void display () {

/* clear window */
glClear(GL_COLOR_BUFFER_BIT);

/* draw scene */
glColor3f(1.0, 0.0, 0.0);
glTranslatef(0.0, 0.5, 0.0);
glutSolidTeapot(0.15);

/* flush drawing routines to the window */
glFlush();

}

int main ( int argc, char * argv[] ) {
glutInit(&argc,argv);

/* setup the size, position, and display mode for new windows */
glutInitWindowSize(800,600);
glutInitWindowPosition(0,0);
glutInitDisplayMode(GLUT_RGB);

/* create and set up a window */
glutCreateWindow("hello, teapot!");

glutDisplayFunc(display);

/*GLfloat width = 800;
GLfloat height = 600;
GLfloat aspect = (GLfloat)width / (GLfloat)height;

// Set the viewport to cover the new window
glViewport(0, 0, width, height);*/

// Set the aspect ratio of the clipping volume to match the 
viewport
glMatrixMode(GL_PROJECTION);  // To operate on the Projection 
matrix
glLoadIdentity();             // Reset
// Enable perspective projection with fovy, aspect, zNear and zFar
//luPerspective(45.0f, aspect, -100.0f, 100.0f);
gluLookAt(0, 0, 0, 0, 0.5, 0, 0, -1, 0);

/* tell GLUT to wait for events */
glutMainLoop();
}

You have asked GL to position the camera at the origin, aim the camera upwards towards the point (0, 0.5, 0), and have also specified (incorrectly) the up vector to be 0, -1, 0. The problem is that the camera's forward direction is (0, 1, 0) [as specified by your eye and aim positions], and this direction conflicts with the up vector or (0, -1, 0). Try using a vector at right angles to the forward direction instead. (eg, [1, 0, 0], [0, 0, 1])

gluLookAt(
  0, 0, 0,   //< camera location
  0, 0.5, 0, //< looking towards point
  1, 0, 0);  //< which direction is up?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM