inchallah ça passe

This commit is contained in:
Milang 2019-08-17 17:28:27 +02:00
parent d45020336e
commit 9900f70201
2 changed files with 50 additions and 35 deletions

View File

@ -1,25 +1,22 @@
#include "face.h"
#include "texture.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)
#define abs(x) (x>0?x:-x)
#include <stdbool.h>
static bool sens_horaire(FE_face const * face)
{
int area = 0;
area+=face->s1->translated.x*face->s2->translated.y;
area-=face->s2->translated.x*face->s1->translated.y;
area += face->s1->translated.x * face->s2->translated.y;
area -= face->s2->translated.x * face->s1->translated.y;
area+=face->s2->translated.x*face->s3->translated.y;
area-=face->s3->translated.x*face->s2->translated.y;
area += face->s2->translated.x * face->s3->translated.y;
area -= face->s3->translated.x * face->s2->translated.y;
area+=face->s3->translated.x*face->s1->translated.y;
area-=face->s1->translated.x*face->s3->translated.y;
area += face->s3->translated.x * face->s1->translated.y;
area -= face->s1->translated.x * face->s3->translated.y;
return (area < 0);
}
@ -63,10 +60,16 @@ static bool get_x(int y, line const * segment, int * x)
return true;
}
#include "texture.h"
#include "zbuffer.h"
#include <gint/display.h>
void FE_draw_face(FE_face const * face)
{
// élimination des faces cachées
if (sens_horaire(face)!=face->visible)
return;
// cas particuliers où la face n'est pas dans l'écran
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)
@ -78,32 +81,38 @@ void FE_draw_face(FE_face const * face)
if (face->s1->translated.z<=0 && face->s2->translated.z<=0 && face->s3->translated.z<=0)
return;
// cas où un côté est nul
line cotes[3];
set_line(&cotes[0], face->s1,face->s2);
set_line(&cotes[1], face->s1,face->s3);
set_line(&cotes[2], face->s2,face->s3);
if (cotes[0].no_line || cotes[1].no_line || cotes[2].no_line)
return;
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 int32_t y1 = max(min(face->s1->translated.y, min(face->s2->translated.y, face->s3->translated.y)), 0);
const int32_t y2 = 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, zAB=face->s2->translated.z-face->s1->translated.z;
const int xAC=face->s3->translated.x-face->s1->translated.x, yAC=face->s3->translated.y-face->s1->translated.y, zAC=face->s3->translated.z-face->s1->translated.z;
const int diviseur_commun=(xAB*yAC-yAB*xAC); //(multiplier par 10000)
const int fact_1=(10000*yAC)/diviseur_commun, fact_2=(10000*xAC)/diviseur_commun;
const int fact_3=(10000*xAB)/diviseur_commun, fact_4=(10000*yAB)/diviseur_commun;
// ressources pour déformer les textures
const int32_t xAB = face->s2->translated.x-face->s1->translated.x,
yAB = face->s2->translated.y-face->s1->translated.y,
zAB=face->s2->translated.z-face->s1->translated.z;
const int32_t xAC = face->s3->translated.x-face->s1->translated.x,
yAC = face->s3->translated.y-face->s1->translated.y,
zAC=face->s3->translated.z-face->s1->translated.z;
const int32_t diviseur_commun=(xAB*yAC-yAB*xAC);
const int fact_1=(1024*yAC)/diviseur_commun, fact_2=(1024*xAC)/diviseur_commun;
const int fact_3=(1024*xAB)/diviseur_commun, fact_4=(1024*yAB)/diviseur_commun;
for (int y=ymin; y<=ymax; y++)
for (int y=y1; y<=y2; y++)
{
// détermination du xmin et du xmax de la ligne
int tx1,tx2;
int tx1, tx2;
for (int t=0;t<3;t++)
{if (get_x(y,&cotes[t],&tx1)) break;}
if (get_x(y,&cotes[t],&tx1))
break;
for (int t=0;t<3;t++)
{if (get_x(y,&cotes[t],&tx2)&&tx1!=tx2) break;}
if (get_x(y,&cotes[t],&tx2)&&tx1!=tx2)
break;
const int txmin=max(min(tx1,tx2),0), txmax=min(max(tx1,tx2),127);
for (int x=txmin; x<=txmax; x++)
@ -116,14 +125,21 @@ void FE_draw_face(FE_face const * face)
vx=(xcalc*fact_1-ycalc*fact_2); // 0 <vx< 10_000
vy=(ycalc*fact_3-xcalc*fact_4); // idem
z=face->s1->translated.z + (vx*zAB+vy*zAC)/10000; // calcul de z non corrigé >> fonctionnel
z=face->s1->translated.z + (vx*zAB+vy*zAC)/1024; // calcul de z non corrigé >> fonctionnel
if (z>0)
{
vx = (8 * vx * face->s1->translated.z)
/
(1024 * face->s1->translated.z + (1024 - vx) * face->s2->translated.z);
vx = (8*vx) / (face->s2->translated.z * ((1-vx) / face->s1->translated.z + (vx) / face->s2->translated.z));
vy = (8*vy) / (face->s3->translated.z * ((1-vy) / face->s1->translated.z + (vy) / face->s3->translated.z));
// Affichage du point
if (FE_zbuffer_set_dist(x,y,z))
dpixel(x, y, 3*FE_get_pixel(face->texturenum, vx, vy)); // 3* means cast to black and white, and vx,and vy casted between 0 and 7
vy = (8 * vy * face->s1->translated.z)
/
(1024 * face->s1->translated.z + (1024 - vy) * face->s3->translated.z);
// Affichage du point
if (FE_zbuffer_set_dist(x,y,z))
dpixel(x, y, 3*FE_get_pixel(face->texturenum, vx, vy)); // 3* means cast to black and white, and vx,and vy casted between 0 and 7
}
}
}

View File

@ -2,14 +2,13 @@
#define FE_FACE_H
#include "coord.h"
#include "FxEngine.h"
#include <stdbool.h>
typedef struct FE_face FE_face;
struct FE_face
{
FE_point *s1,*s2,*s3;
bool visible; // true => clockwised
int texturenum;
FE_point *s1,*s2,*s3;
bool visible; // true => clockwised
int texturenum;
};
void FE_draw_face(FE_face const * face);