Problème Zbuffer Software
Publié : lun. 11/déc./2006 15:52
Voila, j'ai un p'tit soucis qui me prend la tête
Je recode un peu mon moteur 3D software , et j'utilise un Zbuffer pour éliminer les faces cachées, je rempli mes faces avec une fonction de remplissage de triangles ( code plus bas )
cette fonction n'est pas de moi, je l'ai adapté d'un code en C ou pris a quelqu'un ici, je sais plus , bref, c'est pas important, mais j'ai du mal à calculer la profondeur Z du pixel en X,Y, je passe en paramètres les coordonnées 2D des vertices du triangle à afficher, j'ai essayer de rajouté 3 paramètres pour chaque Z des vertices qui compose le triangle , mais je m'en sors pas, j'arrive pas à calculer correctement le pas incrémental pour le Z. Si quelqu'un de moins fatigué que moi peu m'aider c'est sympa

Logiquement le beret arrive presque jusqu'au yeux, on ne devrait pas voir mon crâne...
Je recode un peu mon moteur 3D software , et j'utilise un Zbuffer pour éliminer les faces cachées, je rempli mes faces avec une fonction de remplissage de triangles ( code plus bas )
cette fonction n'est pas de moi, je l'ai adapté d'un code en C ou pris a quelqu'un ici, je sais plus , bref, c'est pas important, mais j'ai du mal à calculer la profondeur Z du pixel en X,Y, je passe en paramètres les coordonnées 2D des vertices du triangle à afficher, j'ai essayer de rajouté 3 paramètres pour chaque Z des vertices qui compose le triangle , mais je m'en sors pas, j'arrive pas à calculer correctement le pas incrémental pour le Z. Si quelqu'un de moins fatigué que moi peu m'aider c'est sympa


Logiquement le beret arrive presque jusqu'au yeux, on ne devrait pas voir mon crâne...
Code : Tout sélectionner
Procedure Triangle(ax1.f,ay1.f,ax2.f,ay2.f,ax3.f,ay3.f,Color)
If ax1 =< ax2 And ax1 =< ax3
x1 = ax1
y1 = ay1
If ax2 =< ax3
x2 = ax2 : y2 = ay2
x3 = ax3 : y3 = ay3
Else
x2 = ax3 : y2 = ay3
x3 = ax2 : y3 = ay2
EndIf
EndIf
If ax2 =< ax1 And ax2 =< ax3
x1 = ax2
y1 = ay2
If ax1 =< ax3
x2 = ax1 : y2 = ay1
x3 = ax3 : y3 = ay3
Else
x2 = ax3 : y2 = ay3
x3 = ax1 : y3 = ay1
EndIf
EndIf
If ax3 =< ax1 And ax3 =< ax2
x1 = ax3
y1 = ay3
If ax2 =< ax1
x2 = ax2 : y2 = ay2
x3 = ax1 : y3 = ay1
Else
x2 = ax1 : y2 = ay1
x3 = ax2 : y3 = ay2
EndIf
EndIf
xdepart.f = x1
ydepart.f = y1
yfin.f = y1
Coeff_A.f = ( y2-y1 ) / (x2-x1 )
Coeff_B.f = ( y3-y1 ) / (x3-x1 )
If x2 < x3
Pt_Ct = x2 : fin = x3 : fin2 = y3
Coeff_C.f = -1*( y3-y2 ) / (x3-x2 )
Coeff_D.f = -1*Coeff_B.f
Else
Pt_Ct = x3 : fin = x2 : fin2 = y2
Coeff_C.f = -1*( y2-y3 ) / (x2-x3 )
Coeff_D.f = -1*Coeff_A.f
EndIf
If Coeff_A.f < Coeff_B.f
Coeff_Down.f = Coeff_A.f
Coeff_Up.f = Coeff_B.f
Else
Coeff_Down.f = Coeff_B.f
Coeff_Up.f = Coeff_A.f
EndIf
For x = xdepart.f To Pt_Ct Step 1
y = ydepart.f
Repeat
y = y+1
If x>0 And x<1024-1 And y>0 And y<768-1
Plot( x,y,Color )
EndIf
Until y >= yfin.f
ydepart.f = ydepart.f+Coeff_Down.f
yfin.f = yfin.f+Coeff_Up.f
Next x
;*********************************************************************
If Coeff_C.f < Coeff_D.f
Coeff_Down.f = Coeff_C.f
Coeff_Up.f = Coeff_D.f
Else
Coeff_Down.f = Coeff_D.f
Coeff_Up.f = Coeff_C.f
EndIf
ydepart.f = fin2
yfin.f = fin2
For x = fin To Pt_Ct Step -1
y = ydepart.f
Repeat
y = y+1
If x>0 And x<1024-1 And y>0 And y<768-1
Plot( x,y,Color)
EndIf
Until y >= yfin.f
ydepart.f = ydepart.f+Coeff_Down.f
yfin.f = yfin.f+Coeff_Up.f
Next x
EndProcedure