gpt4 book ai didi

c++ - OpenGL 轴出现不同

转载 作者:行者123 更新时间:2023-11-28 06:05:19 25 4
gpt4 key购买 nike

当我运行我的程序时,只查看 xyz 轴,视角偶尔会发生变化。
通常它看起来像 this .但有时它看起来像 this .
在第一张图片中,“前景”中的任何东西都小得多。而在第二张图片中,一切看起来都差不多大小。我更喜欢第二张图片,但我不知道发生了什么!
这是我的代码:

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)     // Resize And Initialize The GL Window
{
if (height == 0) // Prevent A Divide By Zero By
{
height = 1; // Making Height Equal One
}

glViewport(0, 0, width, height); // Reset The Current Viewport

aspectRatio = (GLfloat)width / (GLfloat)height;
screenHeight = (GLfloat)height;
screenWidth = (GLfloat)width;
}

int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(1.0f, 1.0f, 1.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
return TRUE; // Initialization Went OK
}

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
//3D setup
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

gluPerspective(45.0f, aspectRatio, 0.1f, 500.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer

//3D stuff
glTranslatef(-8.0f*aspectRatio, 0.0f, -zoomInOut); // Move and zoom axes
glRotatef(rotateLeftRight, 0.0f, 1.0f, 0.0f); // Rotations
glRotatef(rotateUpDown, 1.0f, 0.0f, 0.0f);
//axes
glLineWidth(1.0);
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINES);//x axis
glVertex3f(-20.0, 0.0, 0.0);
glVertex3f(20.0, 0, 0);
glEnd();
glBegin(GL_LINES);//y axis
glVertex3f(0.0, 20.0, 0.0);
glVertex3f(0.0, -20.0, 0);
glEnd();
glBegin(GL_LINES);//z axis
glVertex3f(0.0, 0.0, 20.0);
glVertex3f(0.0, 0.0, -20.0);
glEnd();

//2D setup
glDepthMask(GL_FALSE);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluOrtho2D(0, screenWidth, 0, screenHeight); //left,right,bottom,top
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//2D overlay goes here

return TRUE; // Keep Going
}

我做了什么来改变轴的外观?

最佳答案

我看不出你的代码有什么问题。

你的翻译代码有点怪glTranslatef(-8.0f*aspectRatio, 0.0f, -zoomInOut);

您在 X=-8,Y=0 线上,您将 Z 坐标命名为 zoomInOutWhich is not a zoom but a position .这就像你在电梯里看到外面的景色,你在大楼底部的广场中间看喷泉,你没有放大,你只是靠近看(like that)。

要进行真正的缩放,您必须更改视角(gluPerspective 的第一个参数)。

您在 X、Y 和 Z 轴上绘制从 -20 到 20 的 3 条线。

在您的屏幕截图中,我们几乎可以从一端看到另一端。没有奇怪的钳位(gluPerspective 的 Z 限制很好 [0.1f, 500.0f])。

事实上,您的大脑有时看不到应有的透视图,这不是错误,这只是因为您正在查看只有两种颜色(没有阴影、雾...)的平面图像。

“3D”图像(来自 OpenGl & co.)只是一种视错觉之王,而您编写的程序只是绘制了一种糟糕的幻觉。

一个糟糕的,但是一个正确的!这是一个好的开始。

关于c++ - OpenGL 轴出现不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32500759/

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