gpt4 book ai didi

c++ - 以下代码中 glTransalatef 和 glScalef 的输入是什么?怎么运行的?

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

我无法理解 Write 函数中发生了什么。请解释一下如何计算Write 函数的参数值。我了解到 gl 坐标系值介于 -1+1 之间。但是这个程序运行良好。怎么可能?

void Write(double x,double y,double z,double scale,char *s) 
{
int i,l=strlen(s);
glPushMatrix();
glTranslatef(x,y,z);
glScalef(scale,scale,scale);
for (i=0;i <l;i++)
glutStrokeCharacter(GLUT_STROKE_ROMAN,s[i]);
glPopMatrix();
}

以上函数在窗口中的某个位置打印传递的字符串。

void redisplay(void) 
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
Write(-4.0,2.5,0.0,0.003,"Hello world");
glFlush();
}

void myreshape2 (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef (0.0, 0.0, -5.0);
}

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

glutInitWindowSize( 1024, 700 );
glutCreateWindow("introduction");
glutReshapeFunc(myreshape2);
glutDisplayFunc(redisplay);
//glutKeyboardFunc(keyboard1);
glutMainLoop( );
return(0); // This line is never reached.
}

最佳答案

I learnt that gl coordinate system value lies between -1 to +1. But this program is working nicely. How it is possible?

当然,规范化的设备空间在 [-1, 1] 范围内,规范化的设备空间映射到视口(viewport)。

但是您已经定义了一个透视投影矩阵(参见 gluPerspective):

glMatrixMode(GL_PROJECTION); 
glLoadIdentity();
gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);

投影矩阵描述了从场景的 3D 点到视口(viewport)的 2D 点的映射。它从眼睛空间转换到剪辑空间,剪辑空间中的坐标通过除以剪辑坐标的 w 分量转换为归一化设备坐标 (NDC)。 NDC 的范围是 (-1,-1,-1) 到 (1,1,1)。
在透视投影中,投影矩阵描述了从针孔相机看到的世界中的 3D 点到视口(viewport)的 2D 点的映射。
相机平截头体(截棱锥)中的眼空间坐标映射到立方体(归一化设备坐标)。

perspective projection


此外,字母按 glScalef 缩放。 :

glScalef(scale, scale, scale);

glScalef 缩放几何体的大小。这些参数指定沿 xyz 轴的比例因子。在您的情况下,所有 3 个参数都是 0.003,因此字母缩小到大小的 0.3%,就像没有缩放一样。

glTranslatef将字母移动到它的位置。参数指定翻译的 xy、z 坐标。 vector 。

设置投影矩阵时,到近平面和远平面的距离设置为 1.0 和 20.0。由于字母必须位于近平面和远平面之间,因此它们通过沿 z 轴的平移移动到该范围。

glTranslatef(0.0, 0.0, -5.0); 

视口(viewport)在 xy 平面中的位置由第二个平移定义:

glTranslatef(-4.0, 2.5, 0.0);

另见 OpenGL translation before and after a rotation有关 OpenGL 固定功能管线矩阵堆栈的更多解释。



关于评论的答案的扩展

I am beginner in OpenGL. I am sorry if my question is appropriate. glTranslatef(-4.0, 2.5, 0.0); In this how the parameters range is decided ? ( What is max and min value of x,y and z) . It moves the text to left or right. How can I calculate all these things?

你的透视投影定义为:

gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0); 

在透视投影中,投影矩阵描述了从针孔相机看到的世界中的 3D 点到视口(viewport)的 2D 点的映射。

z 坐标的最小值和最大值由近平面和远平面定义。在您的情况下,近平面为 1.0,远平面为 20.0。由于 z 轴指向视口(viewport)外,这意味着:

1.0 <= -z <= 20.0

View 空间中的投影区域与 View 空间的 Z 坐标之间的关系是线性的。这取决于视场角和纵横比。

field of view

这意味着视口(viewport)上投影区域的大小取决于到 View 位置的 Z 距离,可以这样计算:

a = w / h
t = tan(fov_y / 2) * 2

x_min = -z * a * -t/2
y_min = -z * -t/2
x_max = -z * a * t/2
y_max = -z * t/2

在你的情况下:

w     = 1024
h = 700
fov_y = 65°
z = -5.0

x_min = -4.66
y_min = -3.19
x_max = 4.66
y_max = 3.19

如果 View 空间的坐标系是Right-handed系统,则 X 轴指向左侧,Y 轴指向上方,Z 轴指向 View 外(请注意,在右手系统中,Z 轴是 X 轴和 Y 轴的叉积轴)。

view space

关于c++ - 以下代码中 glTransalatef 和 glScalef 的输入是什么?怎么运行的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49461076/

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