OpenGL轴出现不同
OpenGL Axis Appearing Different
本文关键字:OpenGL 更新时间:2023-10-16
当我运行程序时,只是查看xyz轴,偶尔透视图似乎发生了变化。
通常情况下是这样的。但有时它看起来像这样。
在第一张图片中,"前景"中的任何东西都要小得多。而在第二张图中,所有的东西看起来都差不多大。我更喜欢第二张图,但我不知道发生了什么!
下面是我的代码:
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坐标为zoomInOut
。这不是缩放,而是位置。这就像你在电梯里,从外面看,你看着大楼底部广场中央的喷泉,你不放大,你只是看得更近(像这样)。
要做一个真正的变焦,你必须改变视角 (gluPerspective
的第一个参数)。
你在X, Y和Z轴上从-20到20画3条线。
在你的屏幕截图中,我们几乎可以从一端看到另一端。没有奇怪的钳夹(你的Z限制从gluPerspective
是好的[0.1f, 500.0f])。
事实上,你的大脑有时没有看到它应该看到的视角,这不是一个错误,这只是因为你正在看一个只有两种颜色的平面图像(没有阴影,雾…)。
"3D"图像(来自OpenGl &Co .)只是一个光学错觉之王,你写的程序只是画一个糟糕的错觉。
一个可怜的,但正确的!
相关文章:
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 了解 GLM- openGL 中的相机转换
- 如何在全屏模式下(在OpenGL中)使背景透明
- 为什么我不能使用 EGL 创建无头 OpenGl 上下文?
- OpenGL大的3D纹理(>2GB)非常慢
- OpenGl glm rotate
- C++atioglxx.pdb未加载错误glBufferData OpenGL
- OpenGL在启用深度测试时不会丢弃我的碎片
- OpenGL相机和相机空间转型的困惑
- OpenGL将纹理四边形渲染为(0,0)
- OpenGL 和 GLM 矩阵无法正确扩展,总是按比例缩小
- 发布旋转矩阵(openGL/glm)
- 如何在Visual Basic中使用矩形函数OpenGL绘制矩形
- 无法使用VAO和EBO(openGL)绘制多个对象
- 为什么我的点没有在 OpenGL 中绘制鼠标所在的位置?
- OpenGL 16 位模板缓冲区?
- 将QOpenGLWidget子类转换为使用Metal而不是OpenGL的子类是否可行?
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 在 openGL 中多次绑定缓冲区
- opengl glBegin(GL_LINES) 和 glBegin(GL_POINT) 在 2D 中不可视化点矢量