C Opengl ES 2.0中的glFrustrum变体-如何修复空白屏幕?
这个问题和我上一个关于glOrtho变体的问题类似C Opengl ES 2.0中的glFrustrum变体-如何修复空白屏幕?,c,opengl-es,opengl-es-2.0,C,Opengl Es,Opengl Es 2.0,这个问题和我上一个关于glOrtho变体的问题类似 下面的三角形是在正交投影下完美绘制的(没有投影,它是挤压三角形,而不是矩形视口中的三条等边三角形) 正交矩阵码: typedef float[16] matrix; void ortho_matrix(float right, float left, float bottom, float top, float near, float far, matrix result) { // First Column result[0]
下面的三角形是在正交投影下完美绘制的(没有投影,它是挤压三角形,而不是矩形视口中的三条等边三角形) 正交矩阵码:
typedef float[16] matrix;
void ortho_matrix(float right, float left, float bottom, float top, float near, float far, matrix result)
{
// First Column
result[0] = 2.0 / (right - left);
result[1] = 0.0;
result[2] = 0.0;
result[3] = 0.0;
// Second Column
result[4] = 0.0;
result[5] = 2.0 / (top - bottom);
result[6] = 0.0;
result[7] = 0.0;
// Third Column
result[8] = 0.0;
result[9] = 0.0;
result[10] = -2.0 / (far - near);
result[11] = 0.0;
// Fourth Column
result[12] = -(right + left) / (right - left);
result[13] = -(top + bottom) / (top - bottom);
result[14] = -(far + near) / (far - near);
result[15] = 1;
}
将我的投影矩阵设置为正交,其中纵横比=屏幕宽度/屏幕高度 ortho_matrix(-aspect_ratio, aspect_ratio, -1.0, 1.0, -1.0, 1.0, PROJECTION_MATRIX);
frustum_matrix(-aspect_ratio, aspect_ratio, -1.0, 1.0, 0.1, 1.0, PROJECTION_MATRIX);
任务是将正交投影更改为透视,所以我为此编写函数UPD:更改为col major
void frustum_matrix(float right, float left, float bottom, float top, float near, float far, matrix result)
{
// First Column
result[0] = 2 * near / (right - left);
result[1] = 0.0;
result[2] = 0.0;
result[3] = 0.0;
// Second Column
result[4] = 0.0;
result[5] = 2 * near / (top - bottom);
result[6] = 0.0;
result[7] = 0.0;
// Third Column
result[8] = (right + left) / (right - left);
result[9] = (top + bottom) / (top - bottom);
result[10] = -(far + near) / (far - near);
result[11] = -1;
// Fourth Column
result[12] = 0.0;
result[13] = 0.0;
result[14] = -(2 * far * near) / (far - near);
result[15] = 0.0;
}
将我的投影设置为平截头体矩阵,其中纵横比=屏幕宽度/屏幕高度 ortho_matrix(-aspect_ratio, aspect_ratio, -1.0, 1.0, -1.0, 1.0, PROJECTION_MATRIX);
frustum_matrix(-aspect_ratio, aspect_ratio, -1.0, 1.0, 0.1, 1.0, PROJECTION_MATRIX);
我在glFrustrum页面上看到了矩阵,但ortho-func的矩阵来自同一个源,工作正常。总之,我在不同的地方看到了类似的截锥矩阵,比如截锥函数
我得到的只是空白屏幕、视口和其他与绘图相关的东西都设置正确。我不得不承认我太懒了,看不懂你的代码,但是。。。假设“清除颜色”设置为白色,则可能是您没有设置视口: 请确保在渲染之前至少调用一次:
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
GLsizei width = viewport[2];
GLsizei height = viewport[3];
glViewport(0, 0, width, height);
至于一些一般性建议:不要试图通过重写矩阵代码来重新发明轮子(除非出于某种学术目的)。如果考虑切换到C++,那么值得检查GLM库:
它完全替代了您试图实现的那些矩阵函数,然后是一些。。。我自己使用它,这是一个非常棒的数学库,因为它专门针对opengl es 2.0和glsl。我不得不承认我太懒了,看不懂你的代码,但是。。。假设“清除颜色”设置为白色,则可能是您没有设置视口: 请确保在渲染之前至少调用一次:
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
GLsizei width = viewport[2];
GLsizei height = viewport[3];
glViewport(0, 0, width, height);
至于一些一般性建议:不要试图通过重写矩阵代码来重新发明轮子(除非出于某种学术目的)。如果考虑切换到C++,那么值得检查GLM库:
它完全替代了您试图实现的那些矩阵函数,然后是一些。。。我自己使用它,它是一个很棒的数学库,可以使用,因为它专门针对opengl es 2.0和glsl。看起来你的矩阵索引是从glFrustum的文档页面转置的。在上传矩阵之前,您是否转换矩阵?OpenGL通常引用列向量矩阵,因此如果从glFrustum复制方程,索引应如下所示:
[0] [4] [ 8] [12]
[1] [5] [ 9] [13]
[2] [6] [10] [14]
[3] [7] [11] [15]
看起来您的矩阵索引是从glFrustum的文档页面转置的。在上传矩阵之前,您是否转换矩阵?OpenGL通常引用列向量矩阵,因此如果从glFrustum复制方程,索引应如下所示:
[0] [4] [ 8] [12]
[1] [5] [ 9] [13]
[2] [6] [10] [14]
[3] [7] [11] [15]
视口已设置,我知道GLM当然,你得到了点-学术目的,无论如何+1视口已设置,我知道GLM当然,你得到了点-学术目的,无论如何+1