1v13d/src/FxEngine/face.c

89 lines
3.2 KiB
C

#include "face.h"
#include "zbuffer.h"
#include <stdbool.h>
#include <gint/display.h>
#define min(x,y) (x<y?x:y)
#define max(x,y) (x>y?x:y)
static bool compare(FE_face const * face)
{
const int dx_1=face->s2->translated.x-face->s1->translated.x;
const int dx_2=face->s3->translated.x-face->s1->translated.x;
const int dy_1=face->s2->translated.y-face->s1->translated.y;
const int dy_2=face->s3->translated.y-face->s1->translated.y;
double coef1,coef2;
if (dx_1==0) coef1=10000*dy_1;
else coef1=dy_1/dx_1;
if (dx_2==0) coef2=10000*dy_2;
else coef2=dy_2/dx_2;
return (coef1<coef2);
}
void FE_draw_face(FE_face const * face)
{
if (face->s1->translated.x==face->s2->translated.x && face->s1->translated.x==face->s3->translated.x)
return;
if (face->s1->translated.y==face->s2->translated.y && face->s1->translated.y==face->s3->translated.y)
return;
if (face->s1->translated.x<0 && face->s2->translated.x<0 && face->s3->translated.x<0)
return;
if (face->s1->translated.x>127 && face->s2->translated.x>127 && face->s3->translated.x>127)
return;
if (face->s1->translated.y<0 && face->s2->translated.y<0 && face->s3->translated.y<0)
return;
if (face->s1->translated.y>63 && face->s2->translated.y>63 && face->s3->translated.y>63)
return;
/*if (compare(face)!=face->visible)
return;*/
const int xmin=max(min(face->s1->translated.x,min(face->s2->translated.x,face->s3->translated.x)),0);
const int xmax=min(max(face->s1->translated.x,max(face->s2->translated.x,face->s3->translated.x)),127);
const int ymin=max(min(face->s1->translated.y,min(face->s2->translated.y,face->s3->translated.y)),0);
const int ymax=min(max(face->s1->translated.y,max(face->s2->translated.y,face->s3->translated.y)),63);
const int xAB=face->s2->translated.x-face->s1->translated.x, yAB=face->s2->translated.y-face->s1->translated.y;
const int xAC=face->s3->translated.x-face->s1->translated.x, yAC=face->s3->translated.y-face->s1->translated.y;
const int diviseur_commun=xAB*yAC-yAB*xAC;
bool x_previous_success=0;
bool x_success=0;
for (int tx=xmin; tx<=xmax; tx++)
{
bool y_previous_success=0;
bool y_success=0;
for (int ty=ymin; ty<=xmax; ty++)
{
int xAM=tx-face->s1->translated.x, yAM=ty-face->s1->translated.y;
int nx=(yAC*xAM*8+xAC*yAM*8)/diviseur_commun;
int ny=(yAM*xAB*8+xAM*yAB*8)/diviseur_commun;
if (nx>=0&&nx<8&&ny>=0&&ny<8)
{
if(FE_zbuffer_set_dist(tx, ty, 1))
dpixel(tx,ty,C_BLACK);
}
/*if (y_success==y_previous_success|| (!y_success)) // si fin de la ligne
break;
y_previous_success=y_success;*/
}
/*if (x_success==x_previous_success|| (!x_success)) // si fin de la ligne
break;
x_previous_success=x_success;*/
}
// x=Det(AM,AC)/det(AB,AC)
// y=det(AM,AB)/det(AC,AB)
// x=(xAM*yAC-yAM*xAC)/(xAB*yAC-yAB*xAC)
// y=(xAM*yAB-yAM*xAB)/(xAC*yAB-yAC*xAB)
// soit diviseur_commun = (xAB*yAC-yAB*xAC)
// x=xAM*yAC/diviseur_commun-yAM*xAC/diviseur_commun
// y=yAM*xAB/diviseur_commun-xAM*yAB/diciseur_commun
}