generated from sigonasr2/CPlusPlusProjectTemplate
Normals to hide faces with shading
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
90093ef258
commit
b9bb57d67a
Binary file not shown.
20
main.cpp
20
main.cpp
@ -37,6 +37,8 @@ private:
|
||||
mesh meshCube;
|
||||
mat4x4 matProj;
|
||||
|
||||
vec3d vCamera={0,0,0};
|
||||
|
||||
float fTheta=0;
|
||||
|
||||
void MultiplyMatrixVector(vec3d &i, vec3d &o, mat4x4 &m)
|
||||
@ -158,7 +160,19 @@ public:
|
||||
float l = sqrtf(normal.x*normal.x+normal.y*normal.y+normal.z*normal.z);
|
||||
normal.x/=l;normal.y/=l;normal.z/=l;
|
||||
|
||||
if (normal.z<0) {
|
||||
if (normal.x*(triTranslated.p[0].x-vCamera.x)+
|
||||
normal.y*(triTranslated.p[0].y-vCamera.y)+
|
||||
normal.z*(triTranslated.p[0].z-vCamera.z)<0) {
|
||||
|
||||
vec3d light_dir = {0,0,-1};
|
||||
float l = sqrtf(light_dir.x*light_dir.x+light_dir.y*light_dir.y+light_dir.z*light_dir.z);
|
||||
light_dir.x/=l;light_dir.y/=l;light_dir.z/=l;
|
||||
|
||||
float dp = normal.x*(light_dir.x-vCamera.x)+
|
||||
normal.y*(light_dir.y-vCamera.y)+
|
||||
normal.z*(light_dir.z-vCamera.z);
|
||||
|
||||
|
||||
// Project triangles from 3D --> 2D
|
||||
MultiplyMatrixVector(triTranslated.p[0], triProjected.p[0], matProj);
|
||||
MultiplyMatrixVector(triTranslated.p[1], triProjected.p[1], matProj);
|
||||
@ -176,7 +190,7 @@ public:
|
||||
triProjected.p[2].y *= 0.5f * (float)ScreenHeight();
|
||||
|
||||
// Rasterize triangle
|
||||
SetDecalMode(DecalMode::WIREFRAME);
|
||||
//SetDecalMode(DecalMode::WIREFRAME);
|
||||
DrawPolygonDecal(nullptr,{
|
||||
{triProjected.p[0].x, triProjected.p[0].y},
|
||||
{triProjected.p[1].x, triProjected.p[1].y},
|
||||
@ -185,7 +199,7 @@ public:
|
||||
{0,0},
|
||||
{0,0},
|
||||
{0,0},
|
||||
});
|
||||
},Pixel(255*dp*dp,255*dp*dp,255*dp*dp));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user