沿y轴查看时OpenGL不显示任何内容 [英] OpenGL is not displaying anything when looking along the y axis
问题描述
我目前正在尝试使用SFML和OpenGL显示从顶部观察的红色网格. 网格平面垂直于y轴(y轴是从顶部看的向量).
I am currently trying to display a red grid viewed from the top using SFML and OpenGL. The grid plan is normal to the y axis (y axis is the vector looking to the top).
这在我的渲染循环之前被调用:
This is called before my rendering loop:
glEnable(GL_DEPTH_TEST);
gluPerspective(90.0f, (GLfloat)640/(GLfloat)480, 1.0f, 1000.0f);
在我的渲染循环中:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, -3.0, 0.0,
0.0, -0.8, 0.0,
0.0, 1.0, 0.0);
for (float x = -10.0f, x < 10.0f, x += 1.0f) {
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
glVertex3f(x, -0.5f, -10.0f);
glVertex3f(x, -0.5f, 10.0f);
glVertex3f(-10.0f, -0.5f, x);
glVertex3f(10.0f, -0.5f, x);
glEnd();
}
window.display(); // swap buffers in SFML
我剥去了SFML部分.
I stripped the SFML part.
当我替换gluLookAt()时;一部分(相机略微倾斜):
When I replace the gluLookAt(); part by this one (slight tilt of the camera):
gluLookAt(0.0, -3.0, 0.0,
0.0, -0.8, 0.01,
0.0, 1.0, 0.0);
或这一个: gluLookAt(0.0,-3.0,0.0, 0.0,-0.8、0.0, 0.0、0.0、1.0);
or this one: gluLookAt(0.0, -3.0, 0.0, 0.0, -0.8, 0.0, 0.0, 0.0, 1.0);
一切显示正常.
我得出的结论是,当我精确地沿-y方向(90°)看时,什么也没画.但为什么?我错过了什么吗?
I concluded that when I look exactly in the -y direction (90°), nothing is drawn. But why? Did I miss something?
我的OpenGL支持的版本是2.1 Mesa 10.2.4
My OpenGL supported version is 2.1 Mesa 10.2.4
非常感谢!
推荐答案
问题是,您的上轴也指向y轴.向上矢量不必完全指向上方,但必须与您的观察方向不同.有关更多详细信息,请此答案
The problem is, that you up-axis also points towards the y-axis. It is not necessary that the up-vector points exactly upwards, but it has to be different from your viewing-direction. For more details have a look at this answer
根据要求,以下是数学解释:
Since requested, here is the mathematical explaination:
gluLookAt生成一个3d坐标框架,该框架由三个垂直向量组成. 假设gluLookAt的输入是摄影机位置(C),眼睛位置(E)和上矢量(U),然后使用以下公式构造矩阵:
gluLookAt generates a 3d coordinate frame, that consists of three perpendicular vectors. Assuming the input to gluLookAt is a camera position (C), a eye-position (E), and a up-vector (U), than the matrix is constructed using the following equations:
F = C - E
f = F / |F|
U' = U / |U|
这将导致两个向量跨越一个平面.我们已经知道,f(视图矢量)将是我们最终坐标系的-z轴(opengl始终沿负z轴显示).可以通过找到由f和u跨越的平面的法线向量来计算x轴,因此可以通过以下方式进行计算:
this results in two vectors that span a plane. We already know, that f (the view-vector) will be the -z axis of our final coordinate frame (opengl looks always along the negative z-axis). The x-axis can be calculated by finding the normal vector of the plane spanned by f and u and can thus be calculated via
S = f x U' (x is the cross product)
s = S / |S|
请注意,如果u和f已经垂直,则不需要进行归一化,但是由于gluLookAt不需要实向上向量,因此不必这样做.出于同样的原因,我们不能直接将u用作y轴.我们正在寻找的是一个垂直于f和s的y向量,其给出为:
Note that the normalization would not be required if u and f are already perpendicular, but since gluLookAt does not require the real up vector this is not necessarily given. Out of the same reason we cannot use u directly as y-axis. What we are looking for is a y-vector that is perpendicular to both, f and s, which is given as:
u = s x f
最终矩阵是由
| s 0 |
M = | u 0 |
| -f 0 |
| 0 0 0 1 |
现在考虑您的情况,其中f = U': 计算S时得到
Now consider your case where f = U': When calculating S we get
S = f x U' = U' x U' = [0, 0, 0]
由于这给出了一个零向量,因此u也将是一个零向量.总而言之,这将产生一个矩阵,其中前两行全为零,因此,每个与该矩阵相乘的向量都将以x和y等于0结束.
Since this gives a null-vector, u will also be a null-vector. In total this results in a matrix where the first two rows are all zero, and thus each vector multiplied with this matrix will end up with x and y beeing 0.
希望这对您有帮助. gluLookAt的公式取自此处
Hope this helps you. The formulas for gluLookAt are taken from here
这篇关于沿y轴查看时OpenGL不显示任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!