moi j'ai 15 , même si je n'affiche rien :/
Code : Tout sélectionner
Enumeration $FF
#PB_3DSoftware_AllocateBufferError
#PB_3DSoftware_AllocatePixelError
EndEnumeration
#PB_3DSoftware_DepthBufferHorizon = $FFFFFFFF
Structure depthBufferPixel
mDepth.f ; Profondeur du pixel
mColor.i ; Couleur du pixel
EndStructure
Structure depthBuffer
mWidth.i ; Taille du buffer
mHeight.i ; Taille du buffer
*mPixel.depthBufferPixel ; Liste des pixels
mCountOperation.i ; Nombre d'opération sur le zBuffer ( ajout de pixel )
*mPostRenderCallBack.i ; Pointeur sur une fonction de rendu de pixel
mTimer.i
EndStructure
; -----------------------------------------------------------------------------
; Fonction "virtuelle" qui permet de traité un pixel
; selon un besoin particulier,celle ci ne fait strictement rien
; elle est à redefinir pour un traitement sur le pixel
; si on veut ajouter un brouillard par exemple
; -----------------------------------------------------------------------------
Procedure.i drawDepthBufferPixelCallBack(x.i, y.i, *pixel.depthBufferPixel)
ProcedureReturn *pixel\mColor
EndProcedure
; -----------------------------------------------------------------------------
; Création d'un buffer de profondeur
; -----------------------------------------------------------------------------
Procedure.i createDepthBuffer(width.i, height.i)
;Allocation de l'espace mémoire pour notre buffer
*buffer.depthBuffer = AllocateMemory(SizeOf(depthBuffer))
If *buffer
;Allocation des pixels du buffer
*buffer\mPixel = AllocateMemory(width * height * SizeOf(depthBufferPixel))
If *buffer\mPixel
*buffer\mWidth = width
*buffer\mHeight = height
*buffer\mCountOperation = 0
*buffer\mPostRenderCallBack = @drawDepthBufferPixelCallBack()
ProcedureReturn *buffer
Else
FreeMemory(*buffer)
ProcedureReturn #PB_3DSoftware_AllocatePixelError
EndIf
Else
ProcedureReturn #PB_3DSoftware_AllocateBufferError
EndIf
EndProcedure
; -----------------------------------------------------------------------------
; Suppression d'un buffer
; -----------------------------------------------------------------------------
Procedure freeDepthBuffer(*buffer.depthBuffer)
If MemorySize(*buffer)
If MemorySize(*buffer\mPixel)
FreeMemory(*buffer\mPixel)
EndIf
FreeMemory(*buffer)
EndIf
EndProcedure
; -----------------------------------------------------------------------------
; Efface le depth buffer avec la couleur d'arrière plan
; -----------------------------------------------------------------------------
Procedure clearDepthBuffer(*buffer.depthBuffer, color.i)
If MemorySize(*buffer)
If MemorySize(*buffer\mPixel)
structSize.i = SizeOf(depthBufferPixel)
*buffer\mCountOperation = 0
For dby = 0 To *buffer\mHeight - 1
For dbx = 0 To *buffer\mWidth - 1
emptyPixel.depthBufferPixel
emptyPixel\mColor = color
emptyPixel\mDepth = #PB_3DSoftware_DepthBufferHorizon
memory_offset.i = (dbx*structSize) + *buffer\mWidth * (dby*structSize)
CopyMemory(@emptyPixel, *buffer\mPixel + memory_offset, StructSize)
Next
Next
EndIf
EndIf
EndProcedure
; -----------------------------------------------------------------------------
; Ajoute un pixel au depth buffer
; si la profondeur du pixel est plus grande que la profondeur
; du pixel déjà présent dans le buffer , le pixel ne sera pas dessiné.
; -----------------------------------------------------------------------------
Procedure putDepthBufferPixel(*buffer.depthBuffer, x.i, y.i, depth.f, color.i)
structSize.i = SizeOf(depthBufferPixel)
memory_offset.i = (x*structSize) + *buffer\mWidth * (y*structSize)
*cPixel.depthBufferPixel = AllocateMemory(structSize)
CopyMemory(*buffer\mPixel + memory_offset, *cPixel, structSize)
If (depth > 0) And (depth < *cPixel\mDepth)
*cPixel\mColor = color
*cPixel\mDepth = depth
*buffer\mCountOperation + 1
CopyMemory(*cPixel,*buffer\mPixel + memory_offset, structSize)
EndIf
FreeMemory(*cPixel)
EndProcedure
; -----------------------------------------------------------------------------
; Renvois un pointeur sur une structure ( depthBufferPixel )
; ne pas oublier de faire un freememory sous peine de fuite de mémoire.
; -----------------------------------------------------------------------------
Procedure.i getDepthBufferPixel(*buffer.depthBuffer, x.i, y.i)
structSize.i = SizeOf(depthBufferPixel)
memory_offset.i = (x*structSize) + *buffer\mWidth * (y*structSize)
*cPixel.depthBufferPixel = AllocateMemory(structSize)
CopyMemory(*buffer\mPixel + memory_offset, *cPixel, structSize)
ProcedureReturn *cPixel
EndProcedure
; -----------------------------------------------------------------------------
;
; -----------------------------------------------------------------------------
Procedure renderDepthBuffer(*buffer.depthBuffer, pb_output.i)
If (ElapsedMilliseconds() > *buffer\mTimer + 1000 / 24)
*buffer\mTimer = ElapsedMilliseconds()
StartDrawing(pb_output)
If MemorySize(*buffer)
If MemorySize(*buffer\mPixel)
For dby = 0 To *buffer\mHeight - 1
For dbx = 0 To *buffer\mWidth - 1
; On récupère le pixel courant
*cPixel.depthBufferPixel = getDepthBufferPixel(*buffer, dbx, dby)
; On appel le callback pour un eventuel traitement sur le pixel
newColor.i = CallCFunctionFast( *buffer\mPostRenderCallBack, dbx, dby, *cPixel )
; On l'affiche
Plot(dbx, dby,newColor)
FreeMemory(*cPixel)
Next
Next
EndIf
EndIf
StopDrawing()
EndIf
EndProcedure
; -----------------------------------------------------------------------------
;
; -----------------------------------------------------------------------------
Procedure setDepthBufferPostRenderCallBack(*buffer.depthBuffer, *function.i)
*buffer\mPostRenderCallBack = *function
EndProcedure
; -----------------------------------------------------------------------------
;
; -----------------------------------------------------------------------------
;-TEST
Procedure.i DEPTH_PIXEL(x.i, y.i, *pixel.depthBufferPixel)
If *pixel\mDepth <> #PB_3DSoftware_DepthBufferHorizon
cColor.i = *pixel\mColor
cDepth.f = *pixel\mDepth
DEPTH_VALUE.f = ((cDepth * 255) / #PB_3DSoftware_DepthBufferHorizon)/255
DEPTH_COLOR.i = $CACACA
cRed.f = ((Red(cColor)/255) * (1-DEPTH_VALUE)) + ((Red(DEPTH_COLOR)/255) * (DEPTH_VALUE))
cGreen.f = ((Green(cColor)/255) * (1-DEPTH_VALUE)) + ((Green(DEPTH_COLOR)/255) * (DEPTH_VALUE))
cBlue.f = ((Blue(cColor)/255) * (1-DEPTH_VALUE)) + ((Blue(DEPTH_COLOR)/255) * (DEPTH_VALUE))
cRed * 255
cGreen * 255
cBlue * 255
If cRed > 255 : cRed = 255 : EndIf
If cGreen > 255 : cGreen = 255 : EndIf
If cBlue > 255 : cBlue = 255 : EndIf
If cRed < 0 : cRed = 0 : EndIf
If cGreen < 0 : cGreen = 0 : EndIf
If cBlue < 0 : cBlue = 0 : EndIf
ProcedureReturn RGB(cRed,cGreen,cBlue)
Else
ProcedureReturn $CACACA
EndIf
EndProcedure
Procedure DEPTH_NEGATIF(x.i, y.i, *pixel.depthBufferPixel)
ProcedureReturn RGB(255-Red(*pixel\mColor), 255-Green(*pixel\mColor) , 255-Blue(*pixel\mColor))
EndProcedure
Procedure.i DEPTH_PIXEL_BAW(x.i, y.i, *pixel.depthBufferPixel)
If *pixel\mDepth <> #PB_3DSoftware_DepthBufferHorizon
cColor.i = *pixel\mColor
cDepth.f = *pixel\mDepth
DEPTH_VALUE.f = ((cDepth * 255) / #PB_3DSoftware_DepthBufferHorizon)/255
DEPTH_COLOR.i = $CACACA
cRed.f = ((Red(cColor)/255) * (1-DEPTH_VALUE)) + ((Red(DEPTH_COLOR)/255) * (DEPTH_VALUE))
cGreen.f = ((Green(cColor)/255) * (1-DEPTH_VALUE)) + ((Green(DEPTH_COLOR)/255) * (DEPTH_VALUE))
cBlue.f = ((Blue(cColor)/255) * (1-DEPTH_VALUE)) + ((Blue(DEPTH_COLOR)/255) * (DEPTH_VALUE))
cRed * 255
cGreen * 255
cBlue * 255
If cRed > 255 : cRed = 255 : EndIf
If cGreen > 255 : cGreen = 255 : EndIf
If cBlue > 255 : cBlue = 255 : EndIf
If cRed < 0 : cRed = 0 : EndIf
If cGreen < 0 : cGreen = 0 : EndIf
If cBlue < 0 : cBlue = 0 : EndIf
Gray = (cRed+cGreen+cBlue) / 3
ProcedureReturn RGB(255-Gray,255-Gray,255-Gray)
Else
ProcedureReturn RGB(255-$CA,255-$CA,255-$CA)
EndIf
EndProcedure
InitSprite() : InitKeyboard() : InitSprite3D()
ExamineDesktops()
OpenScreen(DesktopWidth(0),DesktopHeight(0),DesktopDepth(0),"",#PB_Screen_SmartSynchronization)
ratio.f = DesktopWidth(0) / DesktopHeight(0)
If ratio = 1.33
w = 320
h = 240
Else ; 1.66
w = 320
h = 200
EndIf
CreateSprite(0,w,h,#PB_Sprite_Texture)
CreateSprite3D(0,0)
zoomSprite3D(0,DesktopWidth(0),DesktopHeight(0))
buffer = createDepthBuffer(w,h)
If buffer = #PB_3DSoftware_AllocateBufferError Or buffer = #PB_3DSoftware_AllocatePixelError
;erreur
End
EndIf
setDepthBufferPostRenderCallBack(buffer,@DEPTH_PIXEL())
mode$ = "[F2] mode couleur & profondeur"
Repeat
ExamineKeyboard()
ClearScreen(0)
If (ElapsedMilliseconds() > Timer + 1000 / 24)
Timer = ElapsedMilliseconds()
clearDepthBuffer(buffer, RGB(64,128,255))
; plan mouvant
Depth.f = (#PB_3DSoftware_DepthBufferHorizon/2) + (#PB_3DSoftware_DepthBufferHorizon/3) * Cos(ElapsedMilliseconds()/250)
Size.i = 50 - 25 * Cos(ElapsedMilliseconds()/250)
For Y = 0 To Size
For X = 0 To Size
putDepthBufferPixel(buffer,(w/2)-(Size/2)+X+D,60-(Size/2)+Y+(D/10),Depth,RGB(0,255,0))
Next
Next
;plan fixe
For Y = 0 To 25
For X = 0 To 25
putDepthBufferPixel(buffer,200-12+X,60-12+Y,#PB_3DSoftware_DepthBufferHorizon/4,RGB(0,0,255))
Next
Next
For Y = 0 To 120
For X = 0 To 320-1
Depth.f = Y * #PB_3DSoftware_DepthBufferHorizon / 120
putDepthBufferPixel(buffer,X, h-Y,Depth,RGB(255,0,0))
Next
Next
renderDepthBuffer(buffer,SpriteOutput(0))
EndIf
FPS_C + 1
If FPS_T < ElapsedMilliseconds()
FPS_T = ElapsedMilliseconds() + 1000
FPS = FPS_C
FPS_C = 0
EndIf
start3D()
DisplaySprite3D(0,0,0)
stop3D()
StartDrawing(ScreenOutput())
DrawText(10,10,"F1 TO F4 -- "+mode$)
DrawText(10,30,"FRAME RATE : "+Str(FPS))
DrawText(10,50,"BUFFER OPERATIONS = "+Str( PeekI(buffer+OffsetOf(depthBuffer\mCountOperation))))
StopDrawing()
If KeyboardPushed(#PB_Key_F1)
setDepthBufferPostRenderCallBack(buffer,@drawDepthBufferPixelCallBack())
mode$ = "[F1] mode couleur"
EndIf
If KeyboardPushed(#PB_Key_F2)
setDepthBufferPostRenderCallBack(buffer,@DEPTH_PIXEL())
mode$ = "[F2] mode couleur & profondeur"
EndIf
If KeyboardPushed(#PB_Key_F3)
setDepthBufferPostRenderCallBack(buffer,@DEPTH_NEGATIF())
mode$ = "[F3] mode négatif"
EndIf
If KeyboardPushed(#PB_Key_F4)
setDepthBufferPostRenderCallBack(buffer,@DEPTH_PIXEL_BAW())
mode$ = "[F4] mode profondeur"
EndIf
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)