|
|
|
@ -368,7 +368,7 @@ private: |
|
|
|
|
// the plane, the triangle simply becomes a smaller triangle
|
|
|
|
|
|
|
|
|
|
// Copy appearance info to new triangle
|
|
|
|
|
out_tri1.col = in_tri.col; |
|
|
|
|
out_tri1.col = {in_tri.col.r,in_tri.col.g,255}; //in_tri.col;
|
|
|
|
|
|
|
|
|
|
// The inside point is valid, so keep that...
|
|
|
|
|
out_tri1.p[0] = *inside_points[0]; |
|
|
|
@ -388,8 +388,8 @@ private: |
|
|
|
|
// represent a quad with two new triangles
|
|
|
|
|
|
|
|
|
|
// Copy appearance info to new triangles
|
|
|
|
|
out_tri1.col = in_tri.col; |
|
|
|
|
out_tri2.col = in_tri.col; |
|
|
|
|
out_tri1.col = {in_tri.col.r,255,in_tri.col.b}; //in_tri.col;
|
|
|
|
|
out_tri2.col = {255,in_tri.col.g,in_tri.col.b};//in_tri.col;
|
|
|
|
|
|
|
|
|
|
// The first triangle consists of the two inside points and a new
|
|
|
|
|
// point determined by the location where one side of the triangle
|
|
|
|
@ -434,7 +434,7 @@ public: |
|
|
|
|
if (GetKey(olc::LEFT).bHeld) { |
|
|
|
|
vCamera.x-=8*fElapsedTime; |
|
|
|
|
} |
|
|
|
|
vec3d vForward=Vector_Mul(vLookDir,8*fElapsedTime); |
|
|
|
|
vec3d vForward=Vector_Mul(vLookDir,1*fElapsedTime); |
|
|
|
|
if (GetKey(olc::W).bHeld) { |
|
|
|
|
vCamera=Vector_Add(vCamera,vForward); |
|
|
|
|
} |
|
|
|
@ -493,12 +493,12 @@ public: |
|
|
|
|
|
|
|
|
|
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]); |
|
|
|
|
|
|
|
|
|
triViewed.col=Pixel(255*dp*dp,255*dp*dp,255*dp*dp); |
|
|
|
|
|
|
|
|
|
int nClippedTriangles = 0; |
|
|
|
|
triangle clipped[2]; |
|
|
|
|
nClippedTriangles = Triangle_ClipAgainstPlane({ 0.0f, 0.0f, 0.1f }, { 0.0f, 0.0f, 1.0f }, triViewed, clipped[0], clipped[1]); |
|
|
|
@ -508,7 +508,7 @@ public: |
|
|
|
|
triProjected.p[0]=Matrix_MultiplyVector(matProj,clipped[n].p[0]); |
|
|
|
|
triProjected.p[1]=Matrix_MultiplyVector(matProj,clipped[n].p[1]); |
|
|
|
|
triProjected.p[2]=Matrix_MultiplyVector(matProj,clipped[n].p[2]); |
|
|
|
|
triProjected.col=triTransformed.col; |
|
|
|
|
triProjected.col=clipped[n].col; |
|
|
|
|
|
|
|
|
|
triProjected.p[0]=Vector_Div(triProjected.p[0],triProjected.p[0].w); |
|
|
|
|
triProjected.p[1]=Vector_Div(triProjected.p[1],triProjected.p[1].w); |
|
|
|
@ -540,35 +540,76 @@ public: |
|
|
|
|
|
|
|
|
|
std::sort(vecTrianglesToRaster.begin(),vecTrianglesToRaster.end(),[](triangle&t1,triangle&t2){return (t1.p[0].z+t1.p[1].z+t1.p[2].z)/3.0f>(t2.p[0].z+t2.p[1].z+t2.p[2].z)/3.0f;}); |
|
|
|
|
|
|
|
|
|
for (auto&triProjected:vecTrianglesToRaster) { |
|
|
|
|
int triRenderCount=0; |
|
|
|
|
for (auto&triToRaster:vecTrianglesToRaster) { |
|
|
|
|
|
|
|
|
|
triangle clipped[2]; |
|
|
|
|
std::list<triangle>listTriangles; |
|
|
|
|
listTriangles.push_back(triToRaster); |
|
|
|
|
int nNewTriangles=1; |
|
|
|
|
|
|
|
|
|
for (int p = 0; p < 4; p++) |
|
|
|
|
{ |
|
|
|
|
int nTrisToAdd = 0; |
|
|
|
|
while (nNewTriangles > 0) |
|
|
|
|
{ |
|
|
|
|
// Take triangle from front of queue
|
|
|
|
|
triangle test = listTriangles.front(); |
|
|
|
|
listTriangles.pop_front(); |
|
|
|
|
nNewTriangles--; |
|
|
|
|
|
|
|
|
|
// Clip it against a plane. We only need to test each
|
|
|
|
|
// subsequent plane, against subsequent new triangles
|
|
|
|
|
// as all triangles after a plane clip are guaranteed
|
|
|
|
|
// to lie on the inside of the plane. I like how this
|
|
|
|
|
// comment is almost completely and utterly justified
|
|
|
|
|
switch (p) |
|
|
|
|
{ |
|
|
|
|
case 0: nTrisToAdd = Triangle_ClipAgainstPlane({ 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, test, clipped[0], clipped[1]); break; |
|
|
|
|
case 1: nTrisToAdd = Triangle_ClipAgainstPlane({ 0.0f, (float)ScreenHeight() - 1, 0.0f }, { 0.0f, -1.0f, 0.0f }, test, clipped[0], clipped[1]); break; |
|
|
|
|
case 2: nTrisToAdd = Triangle_ClipAgainstPlane({ 0.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 0.0f }, test, clipped[0], clipped[1]); break; |
|
|
|
|
case 3: nTrisToAdd = Triangle_ClipAgainstPlane({ (float)ScreenWidth() - 1, 0.0f, 0.0f }, { -1.0f, 0.0f, 0.0f }, test, clipped[0], clipped[1]); break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Clipping may yield a variable number of triangles, so
|
|
|
|
|
// add these new ones to the back of the queue for subsequent
|
|
|
|
|
// clipping against next planes
|
|
|
|
|
for (int w = 0; w < nTrisToAdd; w++) |
|
|
|
|
listTriangles.push_back(clipped[w]); |
|
|
|
|
} |
|
|
|
|
nNewTriangles = listTriangles.size(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for (auto&t:listTriangles) { |
|
|
|
|
// Rasterize triangle
|
|
|
|
|
SetDecalStructure(DecalStructure::LIST); |
|
|
|
|
SetDecalMode(DecalMode::NORMAL); |
|
|
|
|
DrawPolygonDecal(nullptr,{ |
|
|
|
|
{triProjected.p[0].x, triProjected.p[0].y}, |
|
|
|
|
{triProjected.p[1].x, triProjected.p[1].y}, |
|
|
|
|
{triProjected.p[2].x, triProjected.p[2].y} |
|
|
|
|
{t.p[0].x, t.p[0].y}, |
|
|
|
|
{t.p[1].x, t.p[1].y}, |
|
|
|
|
{t.p[2].x, t.p[2].y} |
|
|
|
|
},{ |
|
|
|
|
{triProjected.uv[0].u,triProjected.uv[0].v}, |
|
|
|
|
{triProjected.uv[1].u,triProjected.uv[1].v}, |
|
|
|
|
{triProjected.uv[2].u,triProjected.uv[2].v}, |
|
|
|
|
},triProjected.col); |
|
|
|
|
SetDecalMode(DecalMode::WIREFRAME); |
|
|
|
|
{t.uv[0].u,t.uv[0].v}, |
|
|
|
|
{t.uv[1].u,t.uv[1].v}, |
|
|
|
|
{t.uv[2].u,t.uv[2].v}, |
|
|
|
|
},t.col); |
|
|
|
|
/*SetDecalMode(DecalMode::WIREFRAME);
|
|
|
|
|
DrawPolygonDecal(nullptr,{ |
|
|
|
|
{triProjected.p[0].x, triProjected.p[0].y}, |
|
|
|
|
{triProjected.p[1].x, triProjected.p[1].y}, |
|
|
|
|
{triProjected.p[2].x, triProjected.p[2].y} |
|
|
|
|
{t.p[0].x, t.p[0].y}, |
|
|
|
|
{t.p[1].x, t.p[1].y}, |
|
|
|
|
{t.p[2].x, t.p[2].y} |
|
|
|
|
},{ |
|
|
|
|
{0,0}, |
|
|
|
|
{0,0}, |
|
|
|
|
{0,0}, |
|
|
|
|
},BLACK); |
|
|
|
|
},BLACK);*/ |
|
|
|
|
SetDecalStructure(DecalStructure::FAN); |
|
|
|
|
triRenderCount++; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
SetDecalMode(DecalMode::NORMAL); |
|
|
|
|
DrawStringDecal({0,0},"Triangles: "+std::to_string(vecTrianglesToRaster.size())); |
|
|
|
|
DrawStringDecal({0,0},"Triangles: "+std::to_string(triRenderCount)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|