salut j'ai un code c++ et j'aimerias le transformer en xilinx
quelqu'un peut m'aider????????
je sais pas comment transformer le new :'(
voici le code
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// ---------------------- CONSTANTES & MACROS --------------------------//
#define MX 160 // Millieu de l'abscisse
#define MY 100 // Millieu de l'ordonnee
#define DISTANCE 250 // Distances de l'obervateur
#define AMBIANT 20 // Lumiere ambiante
#define DIFFUSE 220 // Lumiere diffuse
#define MAX_POLY 150 // Nb max de polygones
#define MAX_SOMM 75 // Nb max de sommets
#define SIN(x) SinTable[x] // Macro SIN()
#define COS(x) CosTable[x] // Macro COS()
#define SWAP(a,b) {a ^= b; b ^=a; a ^= b;} // Macro SWAP()
// ------------------- STRUCTURES DE DONNEES --------------------------//
// type matrice reelle de 4x4
typedef float _mat4x4[4][4];
// Structure pour representer un point dans un espace 2D
typedef struct
{
int x,y;
unsigned char couleur;
}_2D;
// Structure pour representer un point dans un espace 3D
typedef struct
{
float x,y,z;
} _3D;
// Structure qui definie un sommet avec ses differentes coordonnees
typedef struct
{
_2D ecran; // coordonnees d'ecran
_3D local; // coordonnees locales
_3D monde; // coordonnees dans le monde
_3D normale; // normale au sommet
} _sommet;
// Structure pour representer une face d'un polygone
typedef struct
{
short a,b,c; // trois points d'un triangle
unsigned char col; // couleur de la face
float z; // profondeur z moyenne (pour tri)
_3D normale; // normale de la face
}_tri;
// Structure pour contenir un objet
typedef struct
{
int nbsommet, nbpolygone; // nombre de sommets et de polygones
_sommet sommet[MAX_SOMM]; // coordonnees des sommets
_tri poly[MAX_POLY]; // polygones (triangles)
}_objet;
typedef struct // Structure pour le remplissage de polygones
{
long gauche,droite;
long cmin, cmax;
}_TScan;
//------------------------- VARIABLES GLOBALES ------------------------//
char *ecran = (char *) (0xA0000000L); // Memoire video
char *virtuel = new char[64000L]; // Ecran virtuel
float SinTable[360], CosTable[360]; // Table des sinus et cosinus
int ordre[MAX_POLY]; // Tableau pour trier les polys
TScan scanline[200]; // Largeur des lignes des polys
int miny,maxy; // hauteur min et max des polys
_mat4x4 matrice; // mat de transformation homogene
_mat4x4 mat1, mat2; // matrices temporaires
_3D lumiere = {0,0,1}; // vecteur de lumiere
_objet unObjet; // un objet 3D
// ------------------------- FONCTIONS --------------------------------//
/////////////////////////////////////////////////////////////////////////
// hline - Dessine une ligne horizontale //
/////////////////////////////////////////////////////////////////////////
void hlineG(int x1, int x2, int coul1, int coul2, int y)
{
long x;
float difx = x2-x1+1;
float nbcoul = coul2-coul1+1;
float couleur = coul1;
float pas = nbcoul/difx;
unsigned int offset = (y<<8)+(y<<6)+x1;
if(x2<x1)
{
SWAP(x1,x2);
SWAP(coul1,coul2);
}
for (x=x1;x<=x2;x++)
{
virtuel[offset++] = couleur;
couleur += pas;
}
}
/////////////////////////////////////////////////////////////////////////
// precalc - Calcule le tableau de sinus/cosinus //
/////////////////////////////////////////////////////////////////////////
void precalc()
{
int angle;
for(angle=0; angle<360; angle++)
{
SinTable[angle]=sin(angle*M_PI/180.0);
CosTable[angle]=cos(angle*M_PI/180.0);
}
}
/////////////////////////////////////////////////////////////////////////
// scalaire - Produit scalaire (retourne l'angle entre v1 et v2) //
/////////////////////////////////////////////////////////////////////////
double scalaire(_3D v1, _3D v2)
{
return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
}
/////////////////////////////////////////////////////////////////////////
// vectoriel - Produit vectoriel (retourne l'orthogonal de v1 et v2) //
/////////////////////////////////////////////////////////////////////////
void vectoriel(_3D *v, _3D v1, _3D v2)
{
v -> x = (v1.y * v2.z) - (v2.y * v1.z);
v -> y = (v1.z * v2.x) - (v2.z * v1.x);
v -> z = (v1.x * v2.y) - (v2.x * v1.y);
}
/////////////////////////////////////////////////////////////////////////
// normalise - retourne un vecteur unitaire (longueur de 1) //
/////////////////////////////////////////////////////////////////////////
void normalise(_3D *n)
{
double longueur = sqrt(n->x*n->x + n->y*n->y + n->z*n->z);
if(longueur==0) return;
n -> x /= longueur;
n -> y /= longueur;
n -> z /= longueur;
}
/////////////////////////////////////////////////////////////////////////
// swap - Effectue l'echange entre 2 variables float //
/////////////////////////////////////////////////////////////////////////
void swap(float &x,float &y)
{
float temp = x;
x = y;
y = temp;
}
/////////////////////////////////////////////////////////////////////////
// Scan - Trouve le minX et maxX d'un cote d'un polygone //
/////////////////////////////////////////////////////////////////////////
void scanG(_2D *p1, _2D *p2)
{
float x1 = p1->x;
float x2 = p2->x;
float y1 = p1->y;
float y2 = p2->y;
float coul1 = p1->couleur;
float coul2 = p2->couleur;
if (y1==y2) return;
if (y2<y1) {swap (y1,y2); swap (x1,x2); swap(coul1,coul2); }
if (y1<miny) miny=y1;
if (y2>maxy) maxy=y2;
float Xinc = (x2-x1) / (y2-y1);
float x = x1 += Xinc;
float coulInc = (coul2-coul1) / (y2-y1);
float coul = coul1 += coulInc;
int y;
for ( y=y1;y<y2;y++)
{
if (x < scanline[y].gauche)
{
scanline[y].gauche = x;
scanline[y].cmin = coul;
}
if (x > scanline[y].droite)
{
scanline[y].droite = x;
scanline[y].cmax = coul;
}
x += Xinc;
coul += coulInc;
}
}
/////////////////////////////////////////////////////////////////////////
// dessinepoly - Dessine un polygone avec liste de points(listesommet) //
/////////////////////////////////////////////////////////////////////////
void dessine_poly(_2D *listesommet)
{
_2D *ptrcour = listesommet;
_2D *ptrsuiv = listesommet+1;
miny=200; maxy=0;
int i;
for ( i=0;i<200;i++)
{
scanline[i].gauche = 32000;
scanline[i].droite = -32000;
}
for (i=1; i<3; i++)
{
scanG(ptrcour, ptrsuiv);
ptrcour++;
ptrsuiv++;
}
ptrsuiv = listesommet;
scanG(ptrcour, ptrsuiv);
int y;
for (y=miny;y<maxy;y++)
hlineG(scanline[y].gauche,scanline[y].droite,scanline[y].cmin,scanline[y].cmax,y);
}
/////////////////////////////////////////////////////////////////////////
// copie_matrice - copie une matrice source vers matrice destination //
/////////////////////////////////////////////////////////////////////////
void copie_matrice(_mat4x4 source, _mat4x4 dest)
{
memcpy(dest,source,sizeof(_mat4x4));
}
/////////////////////////////////////////////////////////////////////////
// mult_matrice - multiplie 2 matrices et mets le resultat dans dest //
/////////////////////////////////////////////////////////////////////////
void mult_matrice(_mat4x4 m1, _mat4x4 m2, _mat4x4 dest)
{
short i,j;
for( i=0;i<4;i++)
for( j=0;j<4;j++)
dest[i][j] = m1[i][0]*m2[0][j]+
m1[i][1]*m2[1][j]+
m1[i][2]*m2[2][j]+
m1[i][3]*m2[3][j];
}
/////////////////////////////////////////////////////////////////////////
// ident_matrice - construit une matrice identite //
/////////////////////////////////////////////////////////////////////////
void ident_matrice(_mat4x4 m)
{
memset(m,NULL,sizeof(_mat4x4)); // 1 0 0 0
m[0][0] = 1.0; // 0 1 0 0
m[1][1] = 1.0; // 0 0 1 0
m[2][2] = 1.0; // 0 0 0 1
m[3][3] = 1.0; // matrice identite
}
/////////////////////////////////////////////////////////////////////////
// echelle - matrice de changement d'echelle //
/////////////////////////////////////////////////////////////////////////
void echelle(_mat4x4 m,float ex,float ey, float ez)
{
_mat4x4 emat; // matrice echelle
ident_matrice(emat); // initialise matrice identite
emat[0][0]=ex; // ex 0 0 0
emat[1][1]=ey; // 0 ey 0 0
emat[2][2]=ez; // 0 0 ez 0
// 0 0 0 1
mult_matrice(m,emat,mat1); // (emat X m) -> mat1
copie_matrice(mat1,m); // copie le resultat dans matrice
} // globale de transformation homogene
/////////////////////////////////////////////////////////////////////////
// translation - matrice de translation //
/////////////////////////////////////////////////////////////////////////
void translation(_mat4x4 m,float tx,float ty,float tz)
{
_mat4x4 tmat; // matrice translation
ident_matrice(tmat); // initialise matrice identite
tmat[3][0]=tx; // 1 0 0 0
tmat[3][1]=ty; // 0 1 0 0
tmat[3][2]=tz; // 0 0 1 0
// tx ty tz 1
mult_matrice(m,tmat,mat1); // (tmat X m) -> mat1
copie_matrice(mat1,m); // copie le resultat dans matrice
} // globale de transformation homogene
/////////////////////////////////////////////////////////////////////////
// rotation - matrices de rotations //
/////////////////////////////////////////////////////////////////////////
void rotation(_mat4x4 m,int ax,int ay,int az)
{
_mat4x4 xmat, ymat, zmat;
ident_matrice(xmat);
ident_matrice(ymat);
ident_matrice(zmat);
xmat[1][1] = COS(ax); xmat[1][2] = SIN(ax);
xmat[2][1] = -SIN(ax); xmat[2][2] = COS(ax);
ymat[0][0] = COS(ay); ymat[0][2] = -SIN(ay);
ymat[2][0] = SIN(ay); ymat[2][2] = COS(ay);
zmat[0][0] = COS(az); zmat[0][1] = SIN(az);
zmat[1][0] = -SIN(az); zmat[1][1] = COS(az);
mult_matrice(m,ymat,mat1);
mult_matrice(mat1,xmat,mat2);
mult_matrice(mat2,zmat,m);
}
/////////////////////////////////////////////////////////////////////////
// projection - transformation 3D -> 2D //
/////////////////////////////////////////////////////////////////////////
void projection(_sommet *sommet)
{
sommet->ecran.x = sommet->monde.x * DISTANCE / sommet->monde.z + MX;
sommet->ecran.y = sommet->monde.y * DISTANCE / sommet->monde.z + MY;
}
/////////////////////////////////////////////////////////////////////////
// transformation - multiplication de chaque sommet par la matrice //
/////////////////////////////////////////////////////////////////////////
void transformation(_mat4x4 m)
{
_sommet *sommet;
int v;
for(v=0; v<unObjet.nbsommet; v++)
{
sommet = &unObjet.sommet[v];
sommet->monde.x = sommet->local.x*m[0][0]+
sommet->local.y*m[1][0]+
sommet->local.z*m[2][0]+
m[3][0];
sommet->monde.y = sommet->local.x*m[0][1]+
sommet->local.y*m[1][1]+
sommet->local.z*m[2][1]+
m[3][1];
sommet->monde.z = sommet->local.x*m[0][2]+
sommet->local.y*m[1][2]+
sommet->local.z*m[2][2]+
m[3][2];
projection(sommet);
}
}
/////////////////////////////////////////////////////////////////////////
// calcnormal - calcule les normales de face pour chaque polygones //
/////////////////////////////////////////////////////////////////////////
void calcnormal()
{
_3D v1,v2;
int face,sommet;
for( face=0; face<unObjet.nbpolygone; face++)
{
// Normales de faces
v1.x = unObjet.sommet[unObjet.poly[face].a].monde.x - unObjet.sommet[unObjet.poly[face].b].monde.x;
v1.y = unObjet.sommet[unObjet.poly[face].a].monde.y - unObjet.sommet[unObjet.poly[face].b].monde.y;
v1.z = unObjet.sommet[unObjet.poly[face].a].monde.z - unObjet.sommet[unObjet.poly[face].b].monde.z;
v2.x = unObjet.sommet[unObjet.poly[face].c].monde.x - unObjet.sommet[unObjet.poly[face].b].monde.x;
v2.y = unObjet.sommet[unObjet.poly[face].c].monde.y - unObjet.sommet[unObjet.poly[face].b].monde.y;
v2.z = unObjet.sommet[unObjet.poly[face].c].monde.z - unObjet.sommet[unObjet.poly[face].b].monde.z;
vectoriel(&unObjet.poly[face].normale,v2,v1);
normalise(&unObjet.poly[face].normale);
}
for(sommet=0;sommet<unObjet.nbsommet;sommet++)
{
for(face=0;face<unObjet.nbpolygone;face++)
{
if (unObjet.poly[face].a==sommet || unObjet.poly[face].b==sommet || unObjet.poly[face].c==sommet)
{
unObjet.sommet[sommet].normale.x += unObjet.poly[face].normale.x;
unObjet.sommet[sommet].normale.y += unObjet.poly[face].normale.y;
unObjet.sommet[sommet].normale.z += unObjet.poly[face].normale.z;
}
}
unObjet.sommet[sommet].normale.x /= 3;
unObjet.sommet[sommet].normale.y /= 3;
unObjet.sommet[sommet].normale.z /= 3;
normalise(&unObjet.sommet[sommet].normale);
}
}
/////////////////////////////////////////////////////////////////////////
// trier_faces - trie les faces selon leurs Z moyen (Bubble Sort) //
/////////////////////////////////////////////////////////////////////////
void trier_faces(int nb)
{
int position = 0;
int tempval;
while (position < nb-1)
{
if (unObjet.poly[ordre[position]].z > unObjet.poly[ordre[position+1]].z)
{
tempval = ordre[position+1];
ordre[position+1] = ordre[position];
ordre[position] = tempval;
position = -1;
}
position++;
}
}
/////////////////////////////////////////////////////////////////////////
// dessine_objet - dessine les sommets de l'objet //
/////////////////////////////////////////////////////////////////////////
void dessine_objet()
{
int nb=0;
double angle;
float Znormale;
unsigned char col;
_2D poly2D[3];
// Boucle principale du rendeur
int face;
for( face=0; face<unObjet.nbpolygone; face++)
{
poly2D[0] = unObjet.sommet[unObjet.poly[face].a].ecran;
poly2D[1] = unObjet.sommet[unObjet.poly[face].b].ecran;
poly2D[2] = unObjet.sommet[unObjet.poly[face].c].ecran;
Znormale = (poly2D[0].y - poly2D[2].y) *
(poly2D[1].x - poly2D[0].x) -
(poly2D[0].x - poly2D[2].x) *
(poly2D[1].y - poly2D[0].y);
if (Znormale < 0)
{
unObjet.poly[face].z = unObjet.sommet[unObjet.poly[face].a].monde.z+
unObjet.sommet[unObjet.poly[face].b].monde.z+
unObjet.sommet[unObjet.poly[face].c].monde.z;
ordre[nb++] = face;
angle = scalaire(unObjet.sommet[unObjet.poly[face].a].normale,lumiere);
if (angle<0) col = AMBIANT; else col = AMBIANT + DIFFUSE * angle;
unObjet.sommet[unObjet.poly[face].a].ecran.couleur = col;
angle = scalaire(unObjet.sommet[unObjet.poly[face].b].normale,lumiere);
if (angle<0) col = AMBIANT; else col = AMBIANT + DIFFUSE * angle;
unObjet.sommet[unObjet.poly[face].b].ecran.couleur = col;
angle = scalaire(unObjet.sommet[unObjet.poly[face].c].normale,lumiere);
if (angle<0) col = AMBIANT; else col = AMBIANT + DIFFUSE * angle;
unObjet.sommet[unObjet.poly[face].c].ecran.couleur = col;
}
}
trier_faces(nb);
for (face=0;face<nb;face++)
{
poly2D[0] = unObjet.sommet[unObjet.poly[ordre[face]].a].ecran;
poly2D[1] = unObjet.sommet[unObjet.poly[ordre[face]].b].ecran;
poly2D[2] = unObjet.sommet[unObjet.poly[ordre[face]].c].ecran;
dessine_poly(poly2D);
}
}
/////////////////////////////////////////////////////////////////////////
// loadASC - charge les vertices et les polygones d'un objet 3D Studio //
/////////////////////////////////////////////////////////////////////////
void loadASC(char *nom)
{
FILE *fichier;
char chaine[200];
char *fin;
int i,j;
char temp[50];
float x,y,z;
int a,b,c;
int Nb_points=0;
int Nb_faces=0;
int decalage=0;
if ((fichier = fopen(nom,"rt"))==NULL)
{
perror("Impossible d'ouvrir le fichier en lecture");
exit(-2);
}
do
{
// On lit le fichier contenant les informations sur l'objet
fin=fgets(chaine,100,fichier);
if (!strncmp(chaine,"Vertex",6))
{
if (strncmp(chaine,"Vertex list",11))
{
// Lecture des coordonn‚es d'un point
i=0;
while(chaine[i]!='X') i++;
i+=2;
sscanf(chaine+i,"%f",&x);
while(chaine[i]!='Y') i++;
i+=2;
sscanf(chaine+i,"%f",&y);
while(chaine[i]!='Z') i++;
i+=2;
sscanf(chaine+i,"%f",&z);
unObjet.sommet[Nb_points].local.x=x;
unObjet.sommet[Nb_points].local.y=y;
unObjet.sommet[Nb_points].local.z=z;
Nb_points++;
}
}
else
{
if (!strncmp(chaine,"Face",4))
{
if (strncmp(chaine,"Face list",9))
{
// Lecture d'une facette
i=j=0;
while(chaine[i]!='A') i++;
i+=2;
j=i;
while(chaine[j]!=' ') j++;
strncpy(temp,chaine+i,j-i);
temp[j-i]=0;
unObjet.poly[Nb_faces].a=atoi(temp)+decalage;
while(chaine[i]!='B') i++;
i+=2;
j=i;
while(chaine[j]!=' ') j++;
strncpy(temp,chaine+i,j-i);
temp[j-i]=0;
unObjet.poly[Nb_faces].b=atoi(temp)+decalage;
while(chaine[i]!='C') i++;
i+=2;
j=i;
while(chaine[j]!=' ') j++;
strncpy(temp,chaine+i,j-i);
temp[j-i]=0;
unObjet.poly[Nb_faces].c=atoi(temp)+decalage;
Nb_faces++;
}
}
else
if (!strncmp(chaine,"Named object",12))
decalage=Nb_points;
}
} while(fin!=NULL);
fclose(fichier);
unObjet.nbpolygone=Nb_faces;
unObjet.nbsommet=Nb_points;
}
//------------------------ FONCTION PRINCIPALE --------------------------//
int main()
{
char *nom;
int angle=0;
int choix=0;
// choix d'un objet
nom = "cube.asc";
// charge et affiche les informations de l'objet (sommet et polygones)
loadASC(nom);
printf("\nNom de l'objet : %s\n",nom);
printf("Nombre de sommet : %i\n",unObjet.nbsommet);
printf("Nombre de polygone : %i\n",unObjet.nbpolygone);
printf("\n\nAppuyez sur <retour>\n");
//getch();
// asm {MOV AX,0x13; INT 0x10} // à modifier durant le traitement selon les ports de la carte
precalc(); // Calcul de la table de Sinus
normalise(&lumiere);// normalisation du vecteur de la lumière
// while(!kbhit()) // clic sur une touche pour sortir de la boucle
{
memset(virtuel,0,64000L); // memoire virtuelle pour afficher l'objet sur un écran noir (0)
ident_matrice(matrice); // matrice Id
echelle(matrice,1.0,1.0,1.0); // echelle de l'objet, 1 = taille réelle
rotation(matrice,angle,angle,angle);
translation(matrice,0,0,-100);
transformation(matrice);
calcnormal();
dessine_objet();
memcpy(ecran,virtuel,64000L);
if (angle++ == 359) angle = 0;
}
delete []virtuel;
//asm {MOV AX,0x03; INT 0x10}
return 0;
}