【发布时间】:2014-04-26 02:08:34
【问题描述】:
目前我正在通过《学习现代 3D 图形编程》一书学习 3D 渲染理论,目前正陷入第四章复习的“进一步学习”活动之一,特别是最后一个活动。
this question回答了第三个活动,我理解没有问题。然而,这最后一项活动要求我这次只使用矩阵来完成所有这些工作。
我有一个部分工作的解决方案,但对我来说感觉很糟糕,而且可能不是正确的方法。
我对第三个问题的解决方案涉及在任意范围内振荡 3d 矢量E 的 x、y 和 z 分量,并生成一个缩小的立方体(从左下角开始增长,每个 OpenGL 原点) .我想使用矩阵再次执行此操作,它看起来像这样:
但是我用矩阵得到这个结果(忽略背景颜色的变化):
现在开始代码...
该矩阵是一个名为theMatrix 的浮点数[16],它表示一个 4x4 矩阵,其中数据以列优先顺序写入,除了以下元素之外的所有元素都初始化为零:
float fFrustumScale = 1.0f; float fzNear = 1.0f; float fzFar = 3.0f;
theMatrix[0] = fFrustumScale;
theMatrix[5] = fFrustumScale;
theMatrix[10] = (fzFar + fzNear) / (fzNear - fzFar);
theMatrix[14] = (2 * fzFar * fzNear) / (fzNear - fzFar);
theMatrix[11] = -1.0f;
那么其余代码与matrixPerspective 教程课程相同,直到我们到达void display()函数:
//Hacked-up variables pretending to be a single vector (E)
float x = 0.0f, y = 0.0f, z = -1.0f;
//variables used for the oscilating zoom-in-out
int counter = 0;
float increment = -0.005f;
int steps = 250;
void display()
{
glClearColor(0.15f, 0.15f, 0.2f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(theProgram);
//Oscillating values
while (counter <= steps)
{
x += increment;
y += increment;
z += increment;
counter++;
if (counter >= steps)
{
counter = 0;
increment *= -1.0f;
}
break;
}
//Introduce the new data to the array before sending as a 4x4 matrix to the shader
theMatrix[0] = -x * -z;
theMatrix[5] = -y * -z;
//Update the matrix with the new values after processing with E
glUniformMatrix4fv(perspectiveMatrixUniform, 1, GL_FALSE, theMatrix);
/*
cube rendering code ommited for simplification
*/
glutSwapBuffers();
glutPostRedisplay();
}
这是使用矩阵的顶点着色器代码:
#version 330
layout(location = 0) in vec4 position;
layout(location = 1) in vec4 color;
smooth out vec4 theColor;
uniform vec2 offset;
uniform mat4 perspectiveMatrix;
void main()
{
vec4 cameraPos = position + vec4(offset.x, offset.y, 0.0, 0.0);
gl_Position = perspectiveMatrix * cameraPos;
theColor = color;
}
我做错了什么,或者我感到困惑?感谢您花时间阅读所有这些内容。
【问题讨论】:
-
编辑您的问题并正确添加图片。
标签: c++ opengl matrix shader perspectivecamera