Voici une version en mode fenetré, mais le curseur n'est plus là ! ( d'où l'utilisation d'un sprite 3D)
Code : Tout sélectionner
; tonton présente sa convertion du raytracing sur PureBasic.
; beauregard a fabriqué une modeste fenêtre.
If InitMouse ()=0 Or InitKeyboard ()=0 Or InitSprite ()=0 Or InitSprite3D ()=0
MessageRequester ( "Error" , "Can't open DirectX 7 or later" , 0)
End
EndIf
UsePNGImageDecoder()
Enumeration
#fleche
#fleche3D
EndEnumeration
nbreplot.l ; connaître le nombre de pixel affiché à l'écran par ce programme, bien complexe.
objnum = 7
x0.f
y0.f
z0.f
mv.f
md.f
ca.f
tn.f
tb.f
rz.f
x1.f
y1.f
z1.f
a2.f
b2.f
c2.f
tb.f
k.f
tmin.f
col.f
coll.f
colll.f
col2.f
cc.f
tb.f
rz.f
a0.f
b0.f
c0.f
nx.f
ny.f
nz.f
p.f
d.f
l.f
l1.f
p.f
Dim OBJ(objnum)
Dim t(92000)
Dim t2(92000)
Dim A(objnum)
Dim B(objnum)
Dim C(objnum)
Dim R(objnum)
Dim coul_r(objnum)
Dim coul_v(objnum)
Dim coul_b(objnum)
D =1530
l =00
OBJ(0) = 0: A(0) = -50 + l: B(0) = 0 + l1: C(0) = -120: R(0) = -10000:coul_r(0)=25:coul_v(0)=25:coul_b(0)=255
OBJ(1) = 0: A(1) = -80 - l: B(1) = -5 + l1: C(1) = -60: R(1) = -1000:coul_r(1)=155:coul_v(1)=55:coul_b(1)=255
OBJ(2) = 0: A(2) = 5: B(2) = -80: C(2) = -580: R(2) = -4000:coul_r(2)=255:coul_v(2)=255:coul_b(2)=255
OBJ(3) = 0: A(3) = -350: B(3) = 10: C(3) = -580: R(3) = -62000:coul_r(3)=255:coul_v(3)=0:coul_b(3)=0
OBJ(4) = 0: A(4) = -50 + l: B(4) = -50 + l1: C(4) = -100: R(4) = -2000:coul_r(4)=255:coul_v(4)=255:coul_b(4)=55
OBJ(5) = 0: A(5) = -80 - l: B(5) = 85 + l1: C(5) = -60: R(5) = -1000:coul_r(5)=55:coul_v(5)=255:coul_b(5)=255
OBJ(6) = 0: A(6) = 16+moux: B(6) = -35+mouy: C(6) = 250
If OpenWindow ( 0 , 0, 0, 320+48, 200+48, "tonton illumine ton PC" , #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar|#PB_Window_ScreenCentered ) ; on ouvre une fenetre
OpenWindowedScreen ( WindowID ( 0 ), 23, 23, 320, 200, 0, 1, 1) ; fabrication d'un ecran graphique à l'intérieur de la fenêtre.
EndIf
; fleche ( remplaçant la mystérieuse disparition du curseur):
CreateSprite ( #fleche ,32,32, #PB_Sprite_Texture ) ; objet dirigé par le joueur.
StartDrawing ( SpriteOutput ( #fleche ))
BackColor ( RGB (0,0,0))
LineXY (0,0, 16,0, RGB (250,250,250))
LineXY (16,0, 0,16, RGB (250,250,250))
LineXY (0,16, 0,0, RGB (250,250,250))
LineXY (12,6, 16,10, RGB (250,250,250))
LineXY (16,10, 10,16, RGB (250,250,250))
LineXY (5,11, 10,16, RGB (250,250,250))
StopDrawing ()
CreateSprite3D ( #fleche3D , #fleche)
; ******************************************************************************************************
Repeat ; Boucle principale
; ******************************************************************************************************
Event= WindowEvent ()
ExamineKeyboard():ExamineMouse()
FlipBuffers()
ClearScreen(RGB(0, 0, 0))
StartDrawing(ScreenOutput())
p = p+0.1
l=(Sin(p)*80)
l1=(Cos(p)*180)
moux=MouseX()
mouy=MouseY()
a(6)=moux*4-800
b(6)=mouy*4-400
A(0) = 50 + l
A(1) = 90 - l
b(0) = 80 + l
b(1) = 50 - l1
A(4) = 40 + l
A(5) = 10 - l1
b(4) = 51 + l1
b(5) = 40 - l
For YQ =0 To 199
For XQ =0 To 319
X0 = (-120 + XQ)
Y0 = (-100 + YQ)
Gosub raytrace
Plot (xq,yq,RGB(col,coll,colll)): nbreplot+1
Next
Next
;Plot (moux, mouy, RGB( 255, 255, 255))
StopDrawing()
Start3D()
DisplaySprite3D ( #fleche3D , MouseX (), MouseY (), 100); remplace le curseur mystérieusement disparu...
Stop3D ()
StartDrawing ( ScreenOutput ())
DrawingMode (1)
FrontColor ( RGB (255,255,255))
moux$= Str (moux)
mouy$= Str (mouy)
nbreplot$= Str (nbreplot)
FrontColor ( RGB (154,154,154)) :DrawText (1+1,80+1, "moux: " +moux$)
FrontColor ( RGB (234,234,234)) :DrawText (1,80, "moux: " +moux$)
FrontColor ( RGB (154,154,154)) :DrawText (1+1,100+1, "mouy: " +mouy$)
FrontColor ( RGB (234,234,234)) :DrawText (1,100, "mouy: " +mouy$)
FrontColor ( RGB (154,154,154)) :DrawText (1+1,120+1, "nbreplot: " +nbreplot$)
FrontColor ( RGB (234,234,234)) :DrawText (1,120, "nbreplot: " +nbreplot$)
StopDrawing ()
nbreplot=0
If KeyboardPushed ( #PB_Key_F ) And nbreis=0:nbreis+1:EndIf
If KeyboardPushed ( #PB_Key_F )=0 And nbreis=1:nbreis+1:EndIf
If KeyboardPushed ( #PB_Key_F ) And nbreis=2:nbreis+1:EndIf
If KeyboardPushed ( #PB_Key_F )=0 And nbreis=3:nbreis=0:EndIf
Gosub fps ; nombre d'image par seconde.
Delay(1)
If Event= #PB_Event_CloseWindow Or KeyboardPushed ( #PB_Key_Escape ):End:EndIf
ForEver
raytrace:
Z0 = 0
MD = 1 / Sqr(X0 * X0 + Y0 * Y0 + d * d)
X1 = X0 * MD
Y1 = Y0 * MD
Z1 = -(D + Z0) * MD
K = 0
COL = 0
COLl = 0
COLll = 0
OBJMIN = objnum
Top:
Gosub test_inter
OBJMIN = OBJMIN2
If TMIN < 327680 And (OBJ(OBJMIN) = 0)
Gosub reflec
Gosub ombre
If s = 0
MV = Sqr(NX * NX + NY * NY + NZ * NZ)
COL2 = ((X1 * A2 + Y1 * B2 + Z1 * C2)*2+ (NX * A2 + NY * B2 + NZ * C2) / MV )
If COL2 < 0
COL2 = 0
EndIf
COL =((COL+COL2*((K+1)*(K+1))*coul_r(OBJMIN))/4)/255
COLl =((coll+COL2*((K+1)*(K+1))*coul_v(OBJMIN))/4)/255
COLll =((colll+cOL2*((K+1)*(K+1))*coul_b(OBJMIN))/4)/255
If COL > 1
COL = 1
EndIf
If COLl > 1
COLl = 1
EndIf
If COLll > 1
COLll = 1
EndIf
EndIf
K = K + 0.1
EndIf
If TMIN < 327680 And K <= 1
Goto Top
EndIf
If K = 0
COL = 55
COLl = 55
COLll = 55
Else
COL = COL * 255
COLl = COLl * 255
COLll = COLll * 255
If col>255
col=255
EndIf
If coll>255
coll=255
EndIf
If colll>255
colll=255
EndIf
EndIf
Return
test_inter:
TMIN = 327680
For n = 0 To 5
A0 = A(n) - X0
B0 = B(n) - Y0
C0 = C(n) - Z0
TB = A0 * X1 + B0 * Y1 + C0 * Z1
RZ = TB * TB - A0 * A0 - B0 * B0 - C0 * C0
If RZ >= R(n)
TN = TB - Sqr(RZ - R(n))
If TN < TMIN And TN > 0
TMIN = TN:OBJMIN2 = n
EndIf
EndIf
Next
Return
reflec:
X0 = X0 + X1 * TMIN
Y0 = Y0 + Y1 * TMIN
Z0 = Z0 + Z1 * TMIN
NX = X0 - A(OBJMIN)
NY = Y0 - B(OBJMIN)
NZ = Z0 - C(OBJMIN)
CA = 2*(NX * X1 + NY * Y1 + NZ * Z1) / ((NX * NX + NY * NY + NZ * NZ +1))
X1 = X1 - NX * CA
Y1 = Y1 - NY * CA
Z1 = Z1 - NZ * CA
A2 = A(6) - X0
B2 = B(6) - Y0
C2 = C(6) - Z0
MV = 1/Sqr(A2 * A2 + B2 * B2 + C2 * C2)
A2 = A2 * MV
B2 = B2 * MV
C2 = C2 * MV
Return
ombre:
s = 0
For n = 0 To 5
If OBJ(n) = 0 And s = 0
A0 = X0 - A(n)
B0 = Y0 - B(n)
C0 = Z0 - C(n)
TB = A2 * A0 + B2 * B0 + C2 * C0
RZ = TB * TB - A0 * A0 - B0 * B0 - C0 * C0
If RZ >= R(n) And TB < 0
s = -1
EndIf
EndIf
Next
Return
fps:
If Val ( FormatDate ( "%ss" , Date ()))=sek ; regardez pas là, c'est trop compliqué. Arrêtez j'vous dis! Bon ben, je
; vous aurez prévenu...
fps+1
Else
FPS$= Str (fps)
fps=0
EndIf
sek= Val ( FormatDate ( "%ss" , Date ()))
StartDrawing ( ScreenOutput ())
DrawingMode (1)
FrontColor ( RGB (255,255,255))
If nbreis=1 Or nbreis=2:DrawText (1,1, "FPS: " +FPS$):EndIf
StopDrawing ()
Return