|
|
|
@ -120,10 +120,12 @@ private: |
|
|
|
|
mat4x4 matProj; |
|
|
|
|
|
|
|
|
|
vec3d vCamera={0,0,0}; |
|
|
|
|
vec3d vLookDir; |
|
|
|
|
|
|
|
|
|
float zOffset=2; |
|
|
|
|
|
|
|
|
|
float fTheta=0; |
|
|
|
|
float fYaw=0; |
|
|
|
|
|
|
|
|
|
vec3d Matrix_MultiplyVector(mat4x4 &m, vec3d &i) |
|
|
|
|
{ |
|
|
|
@ -313,16 +315,29 @@ public: |
|
|
|
|
bool OnUserUpdate(float fElapsedTime) override |
|
|
|
|
{ |
|
|
|
|
if (GetKey(olc::DOWN).bHeld) { |
|
|
|
|
zOffset-=1*fElapsedTime; |
|
|
|
|
vCamera.y-=8*fElapsedTime; |
|
|
|
|
} |
|
|
|
|
if (GetKey(olc::UP).bHeld) { |
|
|
|
|
zOffset+=1*fElapsedTime; |
|
|
|
|
vCamera.y+=8*fElapsedTime; |
|
|
|
|
} |
|
|
|
|
if (GetKey(olc::RIGHT).bHeld) { |
|
|
|
|
fTheta-=1*fElapsedTime; |
|
|
|
|
vCamera.x+=8*fElapsedTime; |
|
|
|
|
} |
|
|
|
|
if (GetKey(olc::LEFT).bHeld) { |
|
|
|
|
fTheta+=1*fElapsedTime; |
|
|
|
|
vCamera.x-=8*fElapsedTime; |
|
|
|
|
} |
|
|
|
|
vec3d vForward=Vector_Mul(vLookDir,8*fElapsedTime); |
|
|
|
|
if (GetKey(olc::W).bHeld) { |
|
|
|
|
vCamera=Vector_Add(vCamera,vForward); |
|
|
|
|
} |
|
|
|
|
if (GetKey(olc::S).bHeld) { |
|
|
|
|
vCamera=Vector_Sub(vCamera,vForward); |
|
|
|
|
} |
|
|
|
|
if (GetKey(olc::A).bHeld) { |
|
|
|
|
fYaw+=2*fElapsedTime; |
|
|
|
|
} |
|
|
|
|
if (GetKey(olc::D).bHeld) { |
|
|
|
|
fYaw-=2*fElapsedTime; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Set up rotation matrices
|
|
|
|
@ -336,12 +351,20 @@ public: |
|
|
|
|
matWorld=Matrix_MultiplyMatrix(matRotZ,matRotX); |
|
|
|
|
matWorld=Matrix_MultiplyMatrix(matWorld,matTrans); |
|
|
|
|
|
|
|
|
|
vec3d vUp={0,1,0}; |
|
|
|
|
vec3d vTarget={0,0,1}; |
|
|
|
|
mat4x4 matCameraRot=Matrix_MakeRotationY(fYaw); |
|
|
|
|
vLookDir=Matrix_MultiplyVector(matCameraRot,vTarget); |
|
|
|
|
vTarget=Vector_Add(vCamera,vLookDir); |
|
|
|
|
mat4x4 matCamera = Matrix_PointAt(vCamera,vTarget,vUp); |
|
|
|
|
mat4x4 matView=Matrix_QuickInverse(matCamera); |
|
|
|
|
|
|
|
|
|
std::vector<triangle>vecTrianglesToRaster; |
|
|
|
|
|
|
|
|
|
// Draw Triangles
|
|
|
|
|
for (auto&tri : meshCube.tris) |
|
|
|
|
{ |
|
|
|
|
triangle triProjected, triTransformed; |
|
|
|
|
triangle triProjected, triTransformed,triViewed; |
|
|
|
|
|
|
|
|
|
triTransformed.p[0]=Matrix_MultiplyVector(matWorld,tri.p[0]); |
|
|
|
|
triTransformed.p[1]=Matrix_MultiplyVector(matWorld,tri.p[1]); |
|
|
|
@ -357,18 +380,22 @@ public: |
|
|
|
|
vec3d vCameraRay=Vector_Sub(triTransformed.p[0],vCamera); |
|
|
|
|
|
|
|
|
|
if (Vector_DotProduct(normal,vCameraRay)<0) { |
|
|
|
|
vec3d light_dir={0,1,-1}; |
|
|
|
|
vec3d light_dir=Vector_Mul(vLookDir, -1); |
|
|
|
|
light_dir=Vector_Normalise(light_dir); |
|
|
|
|
|
|
|
|
|
float dp = std::max(0.1f,Vector_DotProduct(light_dir,normal)); |
|
|
|
|
|
|
|
|
|
triTransformed.col=Pixel(255*dp*dp,255*dp*dp,255*dp*dp); |
|
|
|
|
|
|
|
|
|
triViewed.p[0]=Matrix_MultiplyVector(matView,triTransformed.p[0]); |
|
|
|
|
triViewed.p[1]=Matrix_MultiplyVector(matView,triTransformed.p[1]); |
|
|
|
|
triViewed.p[2]=Matrix_MultiplyVector(matView,triTransformed.p[2]); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Project triangles from 3D --> 2D
|
|
|
|
|
triProjected.p[0]=Matrix_MultiplyVector(matProj,triTransformed.p[0]); |
|
|
|
|
triProjected.p[1]=Matrix_MultiplyVector(matProj,triTransformed.p[1]); |
|
|
|
|
triProjected.p[2]=Matrix_MultiplyVector(matProj,triTransformed.p[2]); |
|
|
|
|
triProjected.p[0]=Matrix_MultiplyVector(matProj,triViewed.p[0]); |
|
|
|
|
triProjected.p[1]=Matrix_MultiplyVector(matProj,triViewed.p[1]); |
|
|
|
|
triProjected.p[2]=Matrix_MultiplyVector(matProj,triViewed.p[2]); |
|
|
|
|
triProjected.p[0]=Vector_Div(triProjected.p[0],triProjected.p[0].w); |
|
|
|
|
triProjected.p[1]=Vector_Div(triProjected.p[1],triProjected.p[1].w); |
|
|
|
|
triProjected.p[2]=Vector_Div(triProjected.p[2],triProjected.p[2].w); |
|
|
|
|