
J'ai toujours voulu faire un cube

j'ai repris un code en C que vous pouvez trouvez ici :http://membres.lycos.fr/heulin/3D/Plan3D.html
Code : Tout sélectionner
Structure World3D
x.f
y.f
z.f
EndStructure
Dim Point3D.World3D(8)
Dim Point2D.World3D(8)
Dim Sommet.World3D(8)
#Xoff = 512
#Yoff = 384
#Zoff = 250
Dim Matrice.f(3,3)
; /****************************************************************************/
; /* Rotation() : effectue la rotation des points Sommets -> Point3D */
; /****************************************************************************/
Procedure Rotation(Xa.f,Ya.f,Za.f)
matrice(0,0) = Cos(Za)*Cos(Ya)
matrice(1,0) = Sin(Za)*Cos(Ya)
matrice(2,0) = -Sin(Ya)
matrice(0,1) = Cos(Za)*Sin(Ya)*Sin(Xa) - Sin(Za)*Cos(Xa)
matrice(1,1) = Sin(Za)*Sin(Ya)*Sin(Xa) + Cos(Xa)*Cos(Za)
matrice(2,1) = Sin(Xa)*Cos(Ya)
matrice(0,2) = Cos(Za)*Sin(Ya)*Cos(Xa) + Sin(Za)*Sin(Xa)
matrice(1,2) = Sin(Za)*Sin(Ya)*Cos(Xa) - Cos(Za)*Sin(Xa)
matrice(2,2) = Cos(Xa)*Cos(Ya)
For i = 0 To 7
Point3D(i)\x = matrice(0,0)*Sommet(i)\x + matrice(1,0)*Sommet(i)\y + matrice(2,0)*Sommet(i)\z
Point3D(i)\y = matrice(0,1)*Sommet(i)\x + matrice(1,1)*Sommet(i)\y + matrice(2,1)*Sommet(i)\z
Point3D(i)\z = matrice(0,2)*Sommet(i)\x + matrice(1,2)*Sommet(i)\y + matrice(2,2)*Sommet(i)\z
Next i
EndProcedure
; /****************************************************************************/
; /* Projection() : projette en perspective les points après rotation. */
; /****************************************************************************/
Procedure Projection()
For i = 0 To 7
Point2D(i)\x=(Point3D(i)\x*256)/(Point3D(i)\z+#Zoff)+#Xoff;
Point2D(i)\y=(Point3D(i)\y*256)/(Point3D(i)\z+#Zoff)+#Yoff;
Next i
EndProcedure
; /****************************************************************************/
; /* Initialiser() : initialise les coordonnees des sommets du cube */
; /****************************************************************************/
Procedure Initialiser()
Sommet(0)\x = -100 : Sommet(0)\y = -100 : Sommet(0)\z = -100
Sommet(1)\x = 100 : Sommet(1)\y = -100 : Sommet(1)\z = -100
Sommet(2)\x = 100 : Sommet(2)\y = 100 : Sommet(2)\z = -100
Sommet(3)\x = -100 : Sommet(3)\y = 100 : Sommet(3)\z = -100
Sommet(4)\x = 100 : Sommet(4)\y = -100 : Sommet(4)\z = 100
Sommet(5)\x = -100 : Sommet(5)\y = -100 : Sommet(5)\z = 100
Sommet(6)\x = -100 : Sommet(6)\y = 100 : Sommet(6)\z = 100
Sommet(7)\x = 100 : Sommet(7)\y = 100 : Sommet(7)\z = 100
EndProcedure
Procedure ligne(a.l,b.l,couleur.l)
LineXY(Point2D(a)\x,Point2D(a)\y,Point2D(b)\x,Point2D(b)\y,couleur);
EndProcedure
Procedure Update()
Static Couleur.l
Couleur = RGB(100,100,255)
StartDrawing(ScreenOutput())
For i = 0 To 7
; Circle(Point2D(i)\x,Point2D(i)\y,3,Couleur)
;
; Locate(Point2D(i)\x,Point2D(i)\y)
; DrawText(Str(i))
;
;
; ligne(0,1,couleur)
; ligne(1,2,couleur)
; ligne(2,3,couleur)
; ligne(3,0,couleur)
; ligne(4,5,couleur)
; ligne(5,6,couleur)
; ligne(6,7,couleur)
; ligne(7,4,couleur)
; ligne(0,5,couleur)
; ligne(1,4,couleur)
; ligne(2,7,couleur)
; ligne(3,6,couleur)
Next i
StopDrawing()
EndProcedure
InitSprite () :InitSprite3D() : InitKeyboard () : InitMouse ()
OpenScreen (1024,768,32, "" )
;Création de faces (sprite3D())
CreateSprite(0,64,64,#PB_Sprite_Texture)
StartDrawing(SpriteOutput(0))
Box(0,0,64,64,$82E67D)
StopDrawing()
CreateSprite3D(0,0)
CreateSprite(1,64,64,#PB_Sprite_Texture)
StartDrawing(SpriteOutput(1))
Box(0,0,64,64,$0552FA)
StopDrawing()
CreateSprite3D(1,1)
CreateSprite(2,64,64,#PB_Sprite_Texture)
StartDrawing(SpriteOutput(2))
Box(0,0,64,64,$FD0202)
StopDrawing()
CreateSprite3D(2,2)
CreateSprite(3,64,64,#PB_Sprite_Texture)
StartDrawing(SpriteOutput(3))
Box(0,0,64,64,$00FFFF)
StopDrawing()
CreateSprite3D(3,3)
CreateSprite(4,64,64,#PB_Sprite_Texture)
StartDrawing(SpriteOutput(4))
Box(0,0,64,64,$FDFF02)
StopDrawing()
CreateSprite3D(4,4)
CreateSprite(5,64,64,#PB_Sprite_Texture)
StartDrawing(SpriteOutput(5))
Box(0,0,64,64,$B3FF4C)
StopDrawing()
CreateSprite3D(5,5)
Initialiser()
Repeat
ExamineKeyboard () : ExamineMouse() : ClearScreen (0,0,0)
Start3D()
Xangle.f + 0.01
Yangle.f + 0.03
ZAngle.f + 0.01
Sprite3DQuality(1)
DisplaySprite3D(0,0,0)
DisplaySprite3D(1,0,0)
DisplaySprite3D(2,0,0)
DisplaySprite3D(3,0,0)
DisplaySprite3D(4,0,0)
DisplaySprite3D(5,0,0)
TransformSprite3D(0,Point2D(0)\x,Point2D(0)\Y,Point2D(1)\x,Point2D(1)\Y,Point2D(2)\x,Point2D(2)\Y,Point2D(3)\x,Point2D(3)\Y)
TransformSprite3D(1,Point2D(1)\x,Point2D(1)\Y,Point2D(4)\x,Point2D(4)\Y,Point2D(7)\x,Point2D(7)\Y,Point2D(2)\x,Point2D(2)\Y)
TransformSprite3D(2,Point2D(4)\x,Point2D(4)\Y,Point2D(5)\x,Point2D(5)\Y,Point2D(6)\x,Point2D(6)\Y,Point2D(7)\x,Point2D(7)\Y)
TransformSprite3D(3,Point2D(5)\x,Point2D(5)\Y,Point2D(0)\x,Point2D(0)\Y,Point2D(3)\x,Point2D(3)\Y,Point2D(6)\x,Point2D(6)\Y)
TransformSprite3D(4,Point2D(5)\x,Point2D(5)\Y,Point2D(4)\x,Point2D(4)\Y,Point2D(1)\x,Point2D(1)\Y,Point2D(0)\x,Point2D(0)\Y)
TransformSprite3D(5,Point2D(3)\x,Point2D(3)\Y,Point2D(2)\x,Point2D(2)\Y,Point2D(7)\x,Point2D(7)\Y,Point2D(6)\x,Point2D(6)\Y)
Rotation(Xangle,Yangle,Zangle)
Projection()
Update()
Stop3D()
FlipBuffers ()
Until KeyboardPushed ( #PB_Key_Escape )
@++
