Bonjours, voilà en ce moment, ( et oui les grande vacances, le moment des très gros projets ), je code un moteur de rendu 3D entièrement software, je sais c'est lent, mais je le fait pour l'apprendtissage, et pour appliquer mes connaissances des matrices.
Voilà mon problème, je cherche 2 matrices : Une matrice de correction de la perspective, et une autre de projection d'un point 3D sur un plan 3D, voici mon code :
public Point2D Project(Point3D P,Camera Cam){
Matrix WorldTransform = new Matrix(4,4);
Matrix CameraTransform = new Matrix(4,4);
Matrix PerspectiveTransform = new Matrix(4,4);
WorldTransform = Tr.Translation(P.PointMx.X,P.PointMx.Y,P.PointMx.Z) * Tr.RotationX(P.Alpha) * Tr.RotationY(P.Beta) * Tr.RotationZ(P.Gamma);
CameraTransform = Tr.InverseRotationX(Cam.Alpha) * Tr.InverseRotationY(Cam.Beta) * Tr.InverseRotationZ(Cam.Gamma) * Tr.InverseTranslation(Cam.CamMx.X,Cam.CamMx.Y,Cam.CamMx.Z);
//PerspectiveTransform = Tr.Perspective();
P.PointMx= CameraTransform * WorldTransform;
/*
* u=factor_x*x/z +w/2
* v=factor_y*y/z +h/2
*
* factor_x=w/(2*tan(ax))
* factor_y=h/(2*tan(ay))
* */
return Get2DPoint(P.PointMx);
}
public Matrix Perspective(){
Matrix ResMx = new Matrix(4,4);
float CoefB=100;
float CoefF=2;
ResMx.M[0,0] = (float)(1/Math.Tan(45));
ResMx.M[1,1] = (float)(1/Math.Tan(Math.PI/2));
ResMx.M[2,2] = (CoefB + CoefF) / (CoefB - CoefF);
ResMx.M[3,3] = (-2 * CoefB * CoefF)/(CoefB - CoefF);;
ResMx.M[2,3] = 1;
return ResMx;
}
Je ne sais pas pourquoi mais ma matrice de correction de la perspective, ne marche pas convenablement ( je l'ai trouvé sur Wikipedia pourtant ) et je me demande si ce n'est pas l'ordre de mes produits matricielles dans la matrice camera qui fait tout foirer, pourtant, j'ai essayé toutes le possibilité.
En gros, quelqu'un connait-il une autre matrice de correction de la perspective et une matrice de projection 3D ?
Merci d'avance !
PS : Ma classe matrcielle est totalement fonctionnelle et le problème ne vient pas de là, mais je vous donne quand même mon opérateur :
public static Matrix operator *(Matrix Mx1,Matrix Mx2){
if(Mx1.MWidth == Mx2.MHeight){
Matrix MxRes = new Matrix(Mx1.MHeight,Mx2.MWidth);
MxRes.FillZero();
for(int i=0;i<Mx1.MHeight;i++){
for(int j=0;j<Mx2.MWidth;j++){
for(int k=0;k<Mx1.MWidth;k++){
MxRes.M[i,j]+= Mx1.M[i,k] * Mx2.M[k,j];
}
}
}
return MxRes;
}else{
return null;
}
}
public static Matrix operator *( float n , Matrix A )
{
Matrix MxRes = new Matrix(A);
for ( int i = 0 ; i < A.MHeight ; i++ )
{
for (int j = 0 ; j < A.MWidth ; j++ )
{
MxRes.M[i,j] = n * A.M[i,j];
}
}
return MxRes;
}
public static Matrix operator *( Matrix A , float n )
{
Matrix MxRes;
MxRes = n * A;
return MxRes;
}
private int _MWidth;
private int _MHeight;
public float[,] M;
public Matrix(){
}
public Matrix(int MxWidth,int MxHeight){
this._MWidth = MxWidth;
this._MHeight =MxHeight;
this.M = new float[this._MWidth,this._MHeight];
FillIdentity();
}
public Matrix(float X,float Y,float Z){
this.MWidth = 1;
this.MHeight =4;
M = new float[1,4];
FillIdentity();
M[0,0] = X;
M[0,1] = Y;
M[0,2] = Z;
}
( Les deux constructeurs utilisés, et mon opérateur )
-=Arsenik=-