gluLook在绘制形状

时间:2015-06-16 14:53:45

标签: c++ opengl

我对gluLookAtglOrthogluPersective如何协同工作感到困惑。这是问题所在。

我绘制了一个二维三角形和一个二维五边形,其z轴为-5。

//pentagon
glVertex3f(0.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);

glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);

glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);
glVertex3f(1.0f, 1.5f, -5.0f);

//Triangle
glVertex3f(-0.5f, 0.5f, -5.0f);
glVertex3f(-1.0f, 1.5f, -5.0f);
glVertex3f(-1.5f, 0.5f, -5.0f);

然后我定义我的相机位置(0,0,-10)和透视

//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, w, h);
gluLookAt(0, 0, -10, 0, 0, -200, 0, 1, 0);
glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective

//Set the camera perspective
glLoadIdentity(); //Reset the camera
gluPerspective(45.0,                  //The camera angle
    (double)w / (double)h, //The width-to-height ratio
    1.0,                   //The near z clipping coordinate
    5.2);                //The far z clipping coordinate

根据我的理解,我在场景中什么都看不到。但是,由于对象是在-5 z轴中定义的,因此摄像机位于-10 z轴,并且它看起来是负z轴。因此,物体应该在相机后面。但为什么我仍然能看到场景中的物体? enter image description here

同样地,当我在正面z轴上定位我的相机时,我仍然可以看到物体。为什么呢?

另一个问题是为什么我将远z剪裁坐标设置为5后可以看到对象?

任何人都可以解释这个吗?

我的完整代码:

#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
#include <iostream>
#include <stdlib.h> //Needed for "exit" function
//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

//Called when a key is pressed
void handleKeypress(unsigned char key, //The key that was pressed
  int x, int y) {    //The current mouse coordinates
  switch (key) {
  case 27: //Escape key
    exit(0); //Exit the program
  }
}

//Initializes 3D rendering
void initRendering() {
  //Makes 3D drawing work when something is in front of something else
  glEnable(GL_DEPTH_TEST);
}

//Called when the window is resized
void handleResize(int w, int h) {
  //Tell OpenGL how to convert from coordinates to pixel values
  glViewport(0, 0, w, h);

  gluLookAt(0, 0, -10, 0, 0, -200, 0, 1, 0);

  glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective

  //Set the camera perspective
  glLoadIdentity(); //Reset the camera
  gluPerspective(45.0,                  //The camera angle
    (double)w / (double)h, //The width-to-height ratio
    1.0,                   //The near z clipping coordinate
    5.2);                //The far z clipping coordinate
}

//Draws the 3D scene
void drawScene() {
  //Clear information from last draw
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


  glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
  glLoadIdentity(); //Reset the drawing perspective

  glBegin(GL_QUADS); //Begin quadrilateral coordinates

  //Trapezoid
  glVertex3f(-0.7f, -1.5f, -5.0f);
  glVertex3f(0.7f, -1.5f, -5.0f);
  glVertex3f(0.4f, -0.5f, -5.0f);
  glVertex3f(-0.4f, -0.5f, -5.0f);

  glEnd(); //End quadrilateral coordinates

  glBegin(GL_TRIANGLES); //Begin triangle coordinates

  //Pentagon
  glVertex3f(0.5f, 0.5f, -5.0f);
  glVertex3f(1.5f, 0.5f, -5.0f);
  glVertex3f(0.5f, 1.0f, -5.0f);

  glVertex3f(0.5f, 1.0f, -5.0f);
  glVertex3f(1.5f, 0.5f, -5.0f);
  glVertex3f(1.5f, 1.0f, -5.0f);

  glVertex3f(0.5f, 1.0f, -5.0f);
  glVertex3f(1.5f, 1.0f, -5.0f);
  glVertex3f(1.0f, 1.5f, -5.0f);

  //Triangle
  glVertex3f(-0.5f, 0.5f, -5.0f);
  glVertex3f(-1.0f, 1.5f, -5.0f);
  glVertex3f(-1.5f, 0.5f, -5.0f);

  glEnd(); //End triangle coordinates

  glutSwapBuffers(); //Send the 3D scene to the screen
}

int main(int argc, char** argv) {
  //Initialize GLUT
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowSize(400, 400); //Set the window size

  //Create the window
  glutCreateWindow("Basic Shapes - videotutorialsrock.com");
  initRendering(); //Initialize rendering

  //Set handler functions for drawing, keypresses, and window resizes
  glutDisplayFunc(drawScene);
  glutKeyboardFunc(handleKeypress);
  glutReshapeFunc(handleResize);

  glutMainLoop(); //Start the main loop.  glutMainLoop doesn't return.
  return 0; //This line is never reached
}

1 个答案:

答案 0 :(得分:5)

你在说:

  

我在-5的z轴上绘制一个2D三角形和一个2D五边形   ...
  然后我定义我的相机位置(0,0,-10)和透视

如果这确实是您正在进行操作的顺序,那么这就是罪魁祸首。 OpenGL是基于命令的API,而不是场景图。调用glVertex(x, y, z)并不意味着&#34;场景中有一个坐标为x, y, z的顶点。&#34;这意味着&#34; 现在去坐标x, y, z上绘制一个verex。&#34;这意味着顶点在glVertex()调用时由模型视图和投影矩阵激活进行变换。

换句话说,您使用默认的模型视图和投影矩阵发出顶点,因此它们会在屏幕上正常绘制。然后你改变模型视图和投影;如果你继续发出更多顶点,它们将被这些新的模型视图和投影值转换。但之前发布的已经在屏幕上并且不受影响。

换句话说,从绘制函数中删除这两行:

glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective

评论似乎很好地表明了这个问题。

最常见的设置是在调整大小钩子中定义投影矩阵(因为它取决于宽高比),以及在绘制函数中的视图矩阵(=摄像机位置和方向),之前发出任何渲染命令。

正如@datenwolf在评论中正确指出的那样,这是单视图渲染的常见设置。如果您有多个视口,则每个视口可能需要不同的投影矩阵,在这种情况下,您需要在发布图元之前在渲染代码中设置投影矩阵和视图矩阵。