編輯
我已經確定這不是我的矩陣的一個問題,而glGetUniformLocation
沒有找到請求的變量投影和視圖矩陣
編輯2
我已經解決了上述的錯誤和單位矩陣現在工作。所以現在我認爲這個錯誤可以追溯到我的矩陣。
我有一些問題讓我的投影和視圖矩陣正確。我已經檢查了幾個例子,並且不知道我哪裏錯了。除非我禁用着色器,否則屏幕上什麼也看不到。
我計算模型矩陣,如下所示:
float aspect = (float)width/std::max(1.0f, (float)height);
float top = tan(Maths::toRadian(FOV * 0.5f)) * near;
float bottom = -top;
float right = top * aspect;
float left = -right;
projMatrix.reset();
projMatrix(0, 0) = (2.0f * near)/(right - left);
projMatrix(1, 1) = (2.0f * near)/(top - bottom);
projMatrix(2, 2) = -(far + near)/(far - near);
projMatrix(2, 3) = -1.0f;
projMatrix(3, 2) = (-2.0f * far * near)/(far - near);
projMatrix(3, 3) = 0.0f;
我計算像這樣視圖矩陣:
Camera::Camera(const Maths::Vector3& pos)
: position(pos), target(0.0f, 0.0f, 0.0f), up(0.0f, 1.0f, 0.0f) {
target.normalize();
up.normalize();
}
Maths::Matrix4 Camera::getMatrix() const {
Maths::Matrix4 mat;
Maths::Vector3 z = position - target;
Maths::Vector3 x = Maths::crossProduct(up, z);
Maths::Vector3 y = Maths::crossProduct(z, x);
z.normalize();
x.normalize();
mat(0, 0) = x.x; mat(0, 1) = y.x; mat(0, 2) = y.z;
mat(1, 0) = x.y; mat(1, 1) = y.y; mat(1, 2) = y.z;
mat(2, 0) = x.z; mat(1, 2) = y.z; mat(2, 2) = y.z;
mat(3, 0) = -Maths::dotProduct(x, position);
mat(3, 1) = -Maths::dotProduct(y, position);
mat(3, 2) = -Maths::dotProduct(z, position);
return mat;
}
然後,我將其傳遞到着色器最終像這樣:
glGetUniformLocation(viewMatrix, "view");
glGetUniformLocation(projMatrix, "proj");
glUniformMatrix4fv(viewMatrix, 1, GL_TRUE, view.asArray());
glUniformMatrix4fv(projMatrix, 1, GL_TRUE, proj.asArray());
最後我的着色器:
頂點:
#version 330
layout (location = 0) in vec3 position;
uniform mat4 view;
uniform mat4 proj;
void main()
{
gl_Position = proj * view * vec4(position, 1.0);
};
片段:
#version 330
out vec4 gl_FragColor;
void main()
{
gl_FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
}
,並覆蓋所有基地這裏是我計算的點積和叉積方法:
float dotProduct(const Vector3& a, const Vector3& b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
Vector3 crossProduct(const Vector3& a, const Vector3& b) {
return Vector3(a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.z);
}
是故意的,你沒有使用GLM庫? –
你實際上是在畫什麼嗎?某種三角形或其他模型?我假設你編輯了這個,但想確定。 –
是的,我繪製了一個三角形,但只有在禁用着色器時纔會顯示。 @ Need4Sleep自從我做了這樣的數學已經有一段時間了,所以我想提醒自己這一切。制定我自己的實施方案迫使我重新學習它。如果我已經瞭解了所有的東西,如果這不是爲了學習,或者我有閱讀文章而沒有在最後獲得「嘿它有用」的小獎勵的紀律,我會使用GLM。 – Lerp