我目前正在實現一個基於these lessons的模擬OpenGL作爲學習體驗的軟件渲染器。我的項目代碼可以是found here.3D圖形網格法向量旋轉加倍
我在處理頂點法線方面遇到了一些困難。我想用模型矩陣對它們進行變換,我知道當矩陣不正交時應該使用模型矩陣的逆轉置。光的方向應該在世界空間中指定,因此應該轉換法線,然後用世界空間光方向的點積來計算光強。
雖然這是個問題。它在這裏工作得很好,請注意相機在向上旋轉45度時看着模型。
如果我旋轉模型以任何軸旋轉90度,取向上軸現在,光的方向翻轉到指向的其他方式。正如你在這裏看到的那樣,光線從後面傳來。
如果我轉180度,它的罰款一次。
如果我旋轉到45度的光點在90度在此顯示。注意尖峯,看看光線從哪裏來。
這已經困擾了我好幾個小時。我無法弄清楚什麼是錯的。就好像旋轉在光線上翻了一番。光矢量未被雖然改變了,看看這裏:
vec4 SmoothShader::Vertex(int iFace, int nthVert)
{
vec3 vposition = model->vert(iFace, nthVert);
vec4 projectionSpace = MVP * embed<4>(vposition);
vec3 light = vec3(0, 0, 1);
mat4 normTrans = M.invert_transpose();
vec4 normal = normTrans * embed<4>(model->normal(iFace, nthVert), 0.f);
vec3 norm = proj<3>(normal);
intensity[nthVert] = std::max(0.0f, norm.normalise() * light.normalise());
return projectionSpace;
}
bool SmoothShader::Fragment(vec3 barycentric, vec3 &Colour)
{
float pixelIntensity = intensity * barycentric;
Colour = vec3(255, 122, 122) * pixelIntensity;
return true;
}
的MVP(模型,視圖投影)和M(模式)矩陣計算如下:
// Model Matrix, converts to world space
mat4 scale = MakeScale(o->scale);
mat4 translate = MakeTranslate(o->position);
mat4 rotate = MakeRotate(o->rotation);
// Move objects backward from the camera's position
mat4 cameraTranslate = MakeTranslate(vec3(-cameraPosition.x, -cameraPosition.y, -cameraPosition.z));
// Get the camera's rotated basis vectors to rotate everything to camera space.
vec3 Forward;
vec3 Right;
vec3 Up;
GetAxesFromRotation(cameraRotation, Forward, Right, Up);
mat4 cameraRotate = MakeLookAt(Forward, Up);
// Convert from camera space to perspective projection space
mat4 projection = MakePerspective(surf->w, surf->h, 1, 10, cameraFOV);
// Convert from projection space (-1, 1) to viewport space
mat4 viewport = MakeViewport(surf->w, surf->h);
mat4 M = translate * rotate * scale;
mat4 MVP = viewport * projection * cameraRotate * cameraTranslate * M;
任何想法我做錯了?
恐怕這不起作用,沒有任何改變。模型矩陣在這種情況下是正交的,所以它的逆轉換實際上是同樣的事情。儘管許多在線資源都表示,當進行非均勻縮放(非正交)時,例如在轉換法線時需要逆轉置。 – Constan7ine
@ Constan7ine對不起。我錯誤地認爲你正在乘以逆。逆轉置是有意義的。我會再看一次。 –
@ Constan7ine我看不到任何其他問題。我認爲你的問題可能在於你的代碼庫中的其他地方。 –