Fred changed the Structure PB_Sprite3D.
Now the structure is bigger.
Code: Select all
;
; PB Sprite3D Special Effects
;
; by Danilo, 02.01.2004
;
; greetings to Ypser for the basic version
; that showed the wrong tranformation
;
!extrn _PB_3DViewPort
!extrn _PB_D3DBase
;!extrn _PB_Direct3D_Device
!extrn _PB_Direct3D_Start3DState
!extrn _PB_Sprite3D_ListFirstElement
!extrn _PB_Sprite3D_Quality
!extrn _PB_Sprite3D_ObjectsNumber
;!extrn _PB_Sprite3D_ObjectsArea
!extrn _PB_Sprite3D_CurrentDC
If InitSprite()=0 Or InitSprite3D()=0 Or InitKeyboard()=0
MessageRequester("ERROR","Cant init DirectX !",#MB_ICONERROR)
EndIf
DisableDebugger
Procedure GetD3Ddevice_interface()
Shared D3Ddevice_interface.IDirect3DDevice7
; Fill Interface variable
!extrn _PB_Direct3D_Device
!MOV dword EAX, [_PB_Direct3D_Device]
!MOV dword [v_D3Ddevice_interface],EAX
ProcedureReturn
EndProcedure
#D3DRENDERSTATE_CULLMODE = 22
#D3DCULL_NONE = $1
#D3DCULL_CW = $2
#D3DCULL_CCW = $3
#D3DCULL_FORCE_DWORD = $7FFFFFFF
Procedure Setrenderstate(a,b)
Shared D3Ddevice_interface.IDirect3DDevice7
If D3Ddevice_interface.IDirect3DDevice7=0
GetD3Ddevice_interface()
EndIf
D3Ddevice_interface\Setrenderstate(a,b)
EndProcedure
Structure D3DTLVERTEX
StructureUnion ; Screen coordinates
sx.f
dvSX.f
EndStructureUnion
StructureUnion
sy.f
dvSY.f
EndStructureUnion
StructureUnion
sz.f
dvSZ.f
EndStructureUnion
StructureUnion ; Reciprocal of homogeneous w
rhw.f
dvRHW.f
EndStructureUnion
StructureUnion ; Vertex color
color.l
dcColor.l
EndStructureUnion
StructureUnion ; Specular component of vertex
specular.l
dcSpecular.l
EndStructureUnion
StructureUnion ; Texture coordinates
tu.f
dvTU.f
EndStructureUnion
StructureUnion
tv.f
dvTV.f
EndStructureUnion
EndStructure
Structure PB_Sprite3D
Texture.l ; DirectX7 surface
Vertice.D3DTLVERTEX[4] ; The 4 vertices for the rectangle sprite
Width.w
Height.w
unknown.l
EndStructure
Procedure Display3DObject(sprite)
*p.PB_Sprite3D
!extrn _PB_Sprite3D_ObjectsArea
!MOV dword EBX,[_PB_Sprite3D_ObjectsArea]
!MOV dword EAX,140 ; sizeof(PB_Sprite3D)
!MUL dword [ESP]
!ADD dword EBX,EAX
!MOV dword [ESP+4],EBX
StartDrawing(ScreenOutput())
DrawingMode(1):FrontColor($FF,$FF,$00)
DrawingFont(UseFont(1))
Locate(100,320):DrawText("Texture: "+StrU(*p\Texture,#Long))
Locate(300,320):DrawText("Width: "+StrU(*p\Width&$FFFF,#Word))
Locate(450,320):DrawText("Height: "+StrU(*p\Height&$FFFF,#Word))
Locate(100,350):DrawText("X: "+StrF(*p\Vertice[0]\sx,2))
Locate(100,365):DrawText("Y: "+StrF(*p\Vertice[0]\sy,2))
Locate(100,380):DrawText("Z: "+StrF(*p\Vertice[0]\sz,2))
Locate(100,395):DrawText("rhw: "+StrF(*p\Vertice[0]\rhw,2))
Locate(100,410):DrawText("color: "+Hex( *p\Vertice[0]\color))
Locate(100,425):DrawText("specular: "+Hex( *p\Vertice[0]\specular))
Locate(100,440):DrawText("tu : "+StrF(*p\Vertice[0]\tu,2))
Locate(100,455):DrawText("tv : "+StrF(*p\Vertice[0]\tv,2))
Locate(500,350):DrawText("X: "+StrF(*p\Vertice[1]\sx,2))
Locate(500,365):DrawText("Y: "+StrF(*p\Vertice[1]\sy,2))
Locate(500,380):DrawText("Z: "+StrF(*p\Vertice[1]\sz,2))
Locate(500,395):DrawText("rhw: "+StrF(*p\Vertice[1]\rhw,2))
Locate(500,410):DrawText("color: "+Hex( *p\Vertice[1]\color))
Locate(500,425):DrawText("specular: "+Hex( *p\Vertice[1]\specular))
Locate(500,440):DrawText("tu : "+StrF(*p\Vertice[1]\tu,2))
Locate(500,455):DrawText("tv : "+StrF(*p\Vertice[1]\tv,2))
Locate(100,485):DrawText("X: "+StrF(*p\Vertice[2]\sx,2))
Locate(100,500):DrawText("Y: "+StrF(*p\Vertice[2]\sy,2))
Locate(100,515):DrawText("Z: "+StrF(*p\Vertice[2]\sz,2))
Locate(100,530):DrawText("rhw: "+StrF(*p\Vertice[2]\rhw,2))
Locate(100,545):DrawText("color: "+Hex( *p\Vertice[2]\color))
Locate(100,560):DrawText("specular: "+Hex( *p\Vertice[2]\specular))
Locate(100,575):DrawText("tu : "+StrF(*p\Vertice[2]\tu,2))
Locate(100,590):DrawText("tv : "+StrF(*p\Vertice[2]\tv,2))
Locate(500,485):DrawText("X: "+StrF(*p\Vertice[3]\sx,2))
Locate(500,500):DrawText("Y: "+StrF(*p\Vertice[3]\sy,2))
Locate(500,515):DrawText("Z: "+StrF(*p\Vertice[3]\sz,2))
Locate(500,530):DrawText("rhw: "+StrF(*p\Vertice[3]\rhw,2))
Locate(500,545):DrawText("color: "+Hex( *p\Vertice[3]\color))
Locate(500,560):DrawText("specular: "+Hex( *p\Vertice[3]\specular))
Locate(500,575):DrawText("tu : "+StrF(*p\Vertice[3]\tu,2))
Locate(500,590):DrawText("tv : "+StrF(*p\Vertice[3]\tv,2))
;Debug StrU(*p\unknown,#Long)
StopDrawing()
EndProcedure
Procedure CorrectSprite3D(sprite)
*p.PB_Sprite3D
;!extrn _PB_Sprite3D_ObjectsArea
!MOV dword EBX,[_PB_Sprite3D_ObjectsArea]
!MOV dword EAX,140
!MUL dword [ESP]
!ADD dword EBX,EAX
!MOV dword [ESP+4],EBX
; PB:
;
; 0,0
; 0 1
;
;
; 2 3
;
;
; dvRHW
; Value of the D3DVALUE type that is the reciprocal
; of homogeneous w from homogeneous coordinates (x,y,z,w).
; This value is often 1 divided by the distance from the
; origin To the object along the z-axis.
; X = x / w
; Y = y / w
; Z = z / w
; RHW = 1 / w ; Distanz des Punktes zum Bildschirm
; ; Distance of the Point to the Screen
; test
; a.f = (0 - *p\vertice[0]\sx) / (*p\Width/2)
; *p\vertice[0]\rhw = 1.0 + a.f
; ok, works
;*p\vertice[0]\rhw = 1.0 - ((*p\vertice[0]\sx + *p\vertice[0]\sy +
;*p\vertice[0]\sz)/(*p\Width/2))
;*p\vertice[1]\rhw = 1.0 + ((*p\vertice[1]\sx-*p\Width-1 + *p\vertice[1]\sy +
*p\vertice[1]\sz)/(*p\Width/2))
;*p\vertice[2]\rhw = 1.0 - ((*p\vertice[2]\sx + *p\vertice[2]\sy-*p\Height-1 +
;*p\vertice[2]\sz)/(*p\Width/2))
;*p\vertice[3]\rhw = 1.0 + ((*p\vertice[3]\sx-*p\Width-1 + *p\vertice[3]\sy-*p\Height-1 +
;*p\vertice[3]\sz)/(*p\Width/2))
; z is always 0, can be removed
*p\Vertice[0]\rhw = 1.0 - ((*p\Vertice[0]\sx + *p\Vertice[0]\sy )/(*p\Width/2))
*p\Vertice[1]\rhw = 1.0 + ((*p\Vertice[1]\sx-*p\Width-1 + *p\Vertice[1]\sy )/(*p\Width/2))
*p\Vertice[2]\rhw = 1.0 - ((*p\Vertice[2]\sx + *p\Vertice[2]\sy-*p\Height-1 )/(*p\Width/2))
*p\Vertice[3]\rhw = 1.0 + ((*p\Vertice[3]\sx-*p\Width-1 + *p\Vertice[3]\sy-*p\Height-1 )/(*p\Width/2))
EndProcedure
Procedure SetSprite3DColor(sprite,a,b,c,d)
;
; set a color for the 4 Sprite3D corners
;
*p.PB_Sprite3D
;!extrn _PB_Sprite3D_ObjectsArea
!MOV dword EBX,[_PB_Sprite3D_ObjectsArea]
!MOV dword EAX,140
!MUL dword [ESP]
!ADD dword EBX,EAX
!MOV dword [ESP+20],EBX
*p\Vertice[0]\color = $FF000000+a
*p\Vertice[1]\color = $FF000000+b
*p\Vertice[2]\color = $FF000000+c
*p\Vertice[3]\color = $FF000000+d
EndProcedure
Procedure SetSprite3DSpecular(sprite,a,b,c,d)
;
; set specular light for the 4 Sprite3D corners
;
*p.PB_Sprite3D
;!extrn _PB_Sprite3D_ObjectsArea
!MOV dword EBX,[_PB_Sprite3D_ObjectsArea]
!MOV dword EAX,140
!MUL dword [ESP]
!ADD dword EBX,EAX
!MOV dword [ESP+20],EBX
*p\Vertice[0]\specular = a
*p\Vertice[1]\specular = b
*p\Vertice[2]\specular = c
*p\Vertice[3]\specular = d
EndProcedure
Procedure SetSprite3DTextureCoordinates(sprite,corner,a.f,b.f)
;
; set new texture coordinates for a Sprite3D corner
;
*p.PB_Sprite3D
;!extrn _PB_Sprite3D_ObjectsArea
!MOV dword EBX,[_PB_Sprite3D_ObjectsArea]
!MOV dword EAX,140
!MUL dword [ESP]
!ADD dword EBX,EAX
!MOV dword [ESP+16],EBX
*p\Vertice[corner]\tu = a
*p\Vertice[corner]\tv = b
EndProcedure
Global Size, texture_transform.f
texture_transform = 1.0
If OpenWindow(0,0,0,800,650,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"")
If OpenWindowedScreen(WindowID(),0,0,800,650,0,0,0)
LoadFont(1,"Lucida Console",10,#PB_Font_Bold)
#UseSprite = 1
CompilerIf #UseSprite
LoadSprite(0,"Danilo.bmp",#PB_Sprite_Texture) ; 256x256!
CompilerElse
CreateSprite (0, 256, 256, #PB_Sprite_Texture)
StartDrawing (SpriteOutput (0))
Box (0, 0, 256, 256, RGB (0, 0, 100))
For I = 0 To 250 Step 10
Box (I, 0, 5, 256, RGB (60, 80, 255))
Next
StopDrawing ()
CompilerEndIf
CreateSprite3D(10, 0)
Repeat
Select WindowEvent()
Case #PB_Event_CloseWindow
Quit = 1
Case 0
Delay(10)
EndSelect
ExamineKeyboard()
ClearScreen (0, 0, 0)
Start3D()
; disable culling
;SetRenderState(#D3DRENDERSTATE_CULLMODE,#D3DCULL_NONE)
TransformSprite3D (10, Size, 0, 256 - Size, 0, 256+Size, 256, 0-Size, 256)
; correct texture mapping
CorrectSprite3D(10)
; set corner colors
SetSprite3DColor(10,$FFFF00,$FF0000,$0000FF,$00FFFF)
; display infos
DisplaySprite3D (10, 250,250, 255)
; display sprite
Display3DObject(10)
Stop3D()
StartDrawing(ScreenOutput())
FrontColor(255, 255, 255)
DrawingMode(1):DrawingFont(UseFont(1))
Locate(10,10):DrawText("CURSOR: left+right to adjust the sprite.")
Locate(10,25):DrawText(" (Value: "+Str(Size)+")")
Locate(10,45):DrawText("CURSOR: up+down to change texture mapping.")
Locate(10,60):DrawText(" (Value: "+Str(texture_transform)+")")
StopDrawing()
FlipBuffers()
If KeyboardPushed(#PB_Key_Right)
Size + 1
If Size > 100 : Size = 100 : EndIf
ElseIf KeyboardPushed(#PB_Key_Left)
Size - 1
If Size < -100 : Size = -100 : EndIf
ElseIf KeyboardPushed(#PB_Key_Up)
texture_transform + 1.0
If texture_transform > 10.0
texture_transform = 10.0
EndIf
; set new texture mapping coordinates
SetSprite3DTextureCoordinates(10,1,texture_transform,0.0)
SetSprite3DTextureCoordinates(10,2,0.0 ,texture_transform)
SetSprite3DTextureCoordinates(10,3,texture_transform,texture_transform)
ElseIf KeyboardPushed(#PB_Key_Down)
texture_transform - 1.0
If texture_transform < 1.0
texture_transform = 1.0
EndIf
; set new texture mapping coordinates
SetSprite3DTextureCoordinates(10,1,texture_transform,0.0)
SetSprite3DTextureCoordinates(10,2,0.0 ,texture_transform)
SetSprite3DTextureCoordinates(10,3,texture_transform,texture_transform)
ElseIf KeyboardPushed(#PB_Key_Escape)
Quit = #True
EndIf
Until Quit
Else
MessageRequester("ERROR","Cant open screen !",#MB_ICONERROR)
EndIf
Else
MessageRequester("ERROR","Cant open window !",#MB_ICONERROR)
EndIf
I hope it is usefull for you.
Sorry for my bad English.