gpt4 book ai didi

c++ - 绘制形状后 gluLookAt 无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 16:09:01 24 4
gpt4 key购买 nike

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

我在-5的z轴上画了一个二维三角形和一个二维五边形。

//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

同样,当我将相机定义为正 5 并朝正 z 轴看时,我仍然可以看到物体。为什么?

另外一个问题是为什么我把far 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
}

最佳答案

你是说:

I draw a 2D triangle and a 2D pentagon in the z-axis of -5
...
And Then I define my camera position (0,0,-10) and perspective

如果这确实是您执行操作的顺序,那么这就是罪魁祸首。 OpenGL 是基于命令的 API,而不是场景图。调用 glVertex(x, y, z) 并不意味着“场景中有一个坐标为 x, y, z 的顶点”。它的意思是“现在去在坐标 x, y, z 上画一个顶点。”这意味着在 glVertex() 调用时,顶点由事件的模型 View 和投影矩阵转换

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

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

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

评论似乎很好地说明了问题。


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

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

关于c++ - 绘制形状后 gluLookAt 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30871142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com