voila un exemple de transformSprite3D obtenu à cette adresse du forum allemand.
Code : Tout sélectionner
;use transform3d on a sprite for an rotating cube
;lot of code (most) done by DarkDragon, thx for this
#UsePNG =0 ;use for loading from disk
#Usewebpictures=1; use for load from web
#UseOwnerDrawpictures=0; use for drawing spites itself
#UseWindowMode=0 ;windowmode or Fullscreen ?
#Screen_Width=1024 ;size of screen
#Screen_Height=768
Global SP_W.l,SP_H.l
sp_w=256;128 ;breite und höhe
sp_h=256;128
;{ init stuff
InitSprite()
InitSprite3D()
InitKeyboard()
InitMouse()
InitNetwork()
#TIMEOUT = 1500
Enumeration
#PLANE_TOP
#PLANE_BOTTOM
#PLANE_LEFT
#PLANE_RIGHT
#PLANE_FRONT
#PLANE_BACK
EndEnumeration
Structure SVector
x.f
y.f
z.f
EndStructure
Structure SCube
distance.f
*v.SVector[4]
sprite.l
EndStructure
;}
Procedure.l RandomColor()
ProcedureReturn RGB( Random(255), Random(255), Random(255) )
EndProcedure
Procedure.f _cos(angle)
ProcedureReturn Cos(angle * #PI / 180.0)
EndProcedure
Procedure.f _sin(angle)
ProcedureReturn Sin(angle * #PI / 180.0)
EndProcedure
Procedure Rotate(*v.SVector, angleX, angleY)
If angleX <> 0
KA.f = *v\y : KB.f = *v\z
*v\y = _cos(angleX) * KA + _sin(angleX) * KB
*v\z = _cos(angleX) * KB - _sin(angleX) * KA
EndIf
If angleY <> 0
KA.f = *v\z : KB.f = *v\x
*v\z = _cos(angleY) * KA + _sin(angleY) * KB
*v\x = _cos(angleY) * KB - _sin(angleY) * KA
EndIf
EndProcedure
Procedure DisplayCube(SpriteTop, SpriteBottom, SpriteLeft, SpriteRight, SpriteFront, SpriteBack, RotationX, RotationY, Scale, MoveX, MoveY)
Protected Dim v.SVector(7)
Protected Dim planes.SCube(5)
Protected k.l
v(0)\x =-1.0 : v(0)\y = 1.0 : v(0)\z = 1.0
v(1)\x = 1.0 : v(1)\y = 1.0 : v(1)\z = 1.0
v(2)\x = 1.0 : v(2)\y = 1.0 : v(2)\z =-1.0
v(3)\x =-1.0 : v(3)\y = 1.0 : v(3)\z =-1.0
v(4)\x =-1.0 : v(4)\y =-1.0 : v(4)\z = 1.0
v(5)\x = 1.0 : v(5)\y =-1.0 : v(5)\z = 1.0
v(6)\x = 1.0 : v(6)\y =-1.0 : v(6)\z =-1.0
v(7)\x =-1.0 : v(7)\y =-1.0 : v(7)\z =-1.0
; v0
; v3 v1
; v2
;
;
; v4
; v7 v5
; v6
For k=0 To 7
Rotate(@v(k), RotationX, RotationY)
v(k)\x * Scale
v(k)\y * Scale
v(k)\z * Scale + 1000.0
Next k
; Top
planes(0)\v[0] = @v(0)
planes(0)\v[1] = @v(1)
planes(0)\v[2] = @v(2)
planes(0)\v[3] = @v(3)
planes(0)\sprite = SpriteTop
; Back
planes(1)\v[0] = @v(3)
planes(1)\v[1] = @v(2)
planes(1)\v[2] = @v(6)
planes(1)\v[3] = @v(7)
planes(1)\sprite = SpriteBack
; Right
planes(2)\v[0] = @v(2)
planes(2)\v[1] = @v(1)
planes(2)\v[2] = @v(5)
planes(2)\v[3] = @v(6)
planes(2)\sprite = SpriteRight
; Front
planes(3)\v[0] = @v(1)
planes(3)\v[1] = @v(0)
planes(3)\v[2] = @v(4)
planes(3)\v[3] = @v(5)
planes(3)\sprite = SpriteFront
; Left
planes(4)\v[0] = @v(0)
planes(4)\v[1] = @v(3)
planes(4)\v[2] = @v(7)
planes(4)\v[3] = @v(4)
planes(4)\sprite = SpriteLeft
; Bottom
planes(5)\v[0] = @v(4)
planes(5)\v[1] = @v(7)
planes(5)\v[2] = @v(6)
planes(5)\v[3] = @v(5)
planes(5)\sprite = SpriteBottom
For k=0 To 5
planes(k)\distance = (planes(k)\v[0]\z + planes(k)\v[1]\z + planes(k)\v[2]\z + planes(k)\v[3]\z) * 0.25
Next k
SortStructuredArray(planes(), 1, OffsetOf(SCube\distance), #PB_Sort_Float)
For k=0 To 5
TransformSprite3D(planes(k)\sprite, planes(k)\v[0]\x, planes(k)\v[0]\y, planes(k)\v[0]\z, planes(k)\v[1]\x, planes(k)\v[1]\y, planes(k)\v[1]\z, planes(k)\v[2]\x, planes(k)\v[2]\y, planes(k)\v[2]\z, planes(k)\v[3]\x, planes(k)\v[3]\y, planes(k)\v[3]\z)
DisplaySprite3D(planes(k)\sprite, MoveX, MoveY)
Next k
EndProcedure
Procedure.s ReceiveLine(ConnectionID)
Text.s = ""
While char.b <> #LF
char = 0
ReceiveNetworkData(ConnectionID, @char, 1)
If char <> 0
Text.s + Chr(char)
EndIf
Wend
ProcedureReturn RemoveString(RemoveString(Text, #CR$), #LF$)
EndProcedure
Procedure HTTPRequest(URL.s, *EndSize.LONG, PacketSize, *Callback)
Protected Size.l, Data_.s, s.l, Method.s, File.s, Content.s, Text.s, Length.l, Line.s
Protected CurSize.l, oSize.l, t.l, ConnectionID.l
If Left(URL, 7) = "http://" : URL = Right(URL, Len(URL)-7) : EndIf
s = FindString(URL, "/", 1)
Host.s = "" : File.s = ""
If s <> 0 : Host.s = Left(URL, s-1) : File.s = Right(URL, Len(URL)-s) : Else : Host = URL : EndIf
s = FindString(File, "?", 1)
If s <> 0
Method.s = "POST"
Content.s = Right(File, Len(File)-s)
File = Left(File, s-1)
Else
Method.s = "GET"
Content.s = ""
EndIf
ConnectionID = OpenNetworkConnection(Host, 80)
If ConnectionID
Data_.s = Method+" /"+File+" HTTP/1.0"+#CRLF$
Data_.s + "Host: "+Host+#CRLF$
Data_.s + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7"+#CRLF$
If Content <> ""
Data_.s + "Connection: close"+#CRLF$
Data_.s + "Content-Type: application/x-www-form-urlencoded"+#CRLF$
Data_.s + "Content-Length: "+Str(Len(Content))+#CRLF$
Data_.s + #CRLF$ + Content.s + #CRLF$+#CRLF$
Else
Data_.s + "Connection: close"+#CRLF$+#CRLF$
EndIf
SendNetworkString(ConnectionID, Data_)
While NetworkClientEvent(ConnectionID) <> 2 : Delay(10) : Wend
Length = 0
Line.s = ""
Repeat
Line = ReceiveLine(ConnectionID)
Select LCase(StringField(Line, 1, ":"))
Case "content-length"
Length = Val(Trim(StringField(Line, 2, ":")))
EndSelect
Until Len(Trim(Line)) <= 4
Size = 0
If Length <> 0
*Result = AllocateMemory(Length)
While Size < Length
If CurSize > 0
Size + CurSize
If *Callback
CallFunctionFast(*Callback, Size, Length)
EndIf
EndIf
If Size > (Length-PacketSize)
PacketSize = Length-Size
EndIf
If PacketSize > 0
CurSize = ReceiveNetworkData(ConnectionID, *Result+Size, PacketSize)
EndIf
Wend
Else
*Buffer = AllocateMemory(PacketSize)
*Result = AllocateMemory(1)
t = ElapsedMilliseconds()
While ElapsedMilliseconds()-t <= #TIMEOUT
If NetworkClientEvent(ConnectionID) = 2
CurSize = ReceiveNetworkData(ConnectionID, *Buffer, PacketSize)
If CurSize > 0
oSize = Size
Size + CurSize
*Result = ReAllocateMemory(*Result, Size)
CopyMemory(*Buffer, *Result+oSize, CurSize)
If *Callback
CallFunctionFast(*Callback, Size, 0)
EndIf
EndIf
t = ElapsedMilliseconds()
EndIf
Wend
Length = Size
EndIf
*EndSize\l = Length
If *Callback
CallFunctionFast(*Callback, Size, Length)
EndIf
CloseNetworkConnection(ConnectionID)
ProcedureReturn *Result
EndIf
EndProcedure
UsePNGImageDecoder()
CompilerIf #UseWindowMode
OpenWindow(0, 0, 0, #Screen_Width, #Screen_height, "Rotating Cube", #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, #Screen_Width, #Screen_Height, 1, 0, 0)
CompilerElse
If OpenScreen(#Screen_Width,#Screen_Height,16,"WeatherCube")=0:EndIf
CompilerEndIf
;{ using PNG from Harddisk
CompilerIf #usePNG
path.s="media\"
UsePNGImageDecoder()
LoadSprite(0, path + "imgradio.png", #PB_Sprite_Texture)
LoadSprite(1, path + "imgWeather.png", #PB_Sprite_Texture)
LoadSprite(2, path + "imgtv.png", #PB_Sprite_Texture)
CreateSprite3D(0,0)
CreateSprite3D(1,1)
CreateSprite3D(2,2)
CompilerEndIf
;}
;{ using Pictures from the web
CompilerIf #Usewebpictures
;search for weather information
UseJPEGImageDecoder()
adress.s="http://image.de.weather.com/web/maps/satellite/regions/europesat_720x486.jpg"
*Buffer = HTTPRequest(adress, @Size, 16, 0)
CatchSprite(0, *Buffer, #PB_Sprite_Texture)
FreeMemory(*Buffer)
CreateSprite3D(0,0)
adress.s="http://image.de.weather.com/images/maps/de_DE/satellite/regions/germany_sat_600_nl.jpg"
*Buffer = HTTPRequest(adress, @Size, 16, 0)
CatchSprite(1, *Buffer, #PB_Sprite_Texture)
FreeMemory(*Buffer)
CreateSprite3D(1,1)
adress.s="http://image.de.weather.com/web/maps/de_DE/weather/forecast/germany_outlook_day1_720_de.jpg"
*Buffer = HTTPRequest(adress, @Size, 16, 0)
CatchSprite(2, *Buffer, #PB_Sprite_Texture)
FreeMemory(*Buffer)
CreateSprite3D(2,2)
CompilerEndIf
;}
;{ creating sprites with 2d-commands
CompilerIf #UseOwnerDrawpictures
; ;3d sprites als seitenflächen
For n=0 To 2;seiten
CreateSprite(n,sp_w,sp_h,#PB_Sprite_Texture)
StartDrawing(SpriteOutput(n))
FrontColor(RandomColor() )
Box(0, 0, sp_w, sp_h)
;DrawingFont(FontID(0))
DrawingMode(3)
FrontColor( RandomColor() )
Circle(sp_w/2, sp_h/2, sp_w/3 ,RandomColor())
DrawText(sp_w/2,sp_h/2,Str(n),RandomColor(),RandomColor())
StopDrawing()
CreateSprite3D(n,n)
Next
CompilerEndIf
;}
Define mdx.d
Define mdy.d
Define mdx2.d
Define mdy2.d
Define lastTime.l
Define currentTime.l
currentTime = ElapsedMilliseconds()
Repeat
CompilerIf #UseWindowMode
WindowEvent()
CompilerEndIf
ExamineKeyboard()
ExamineMouse()
lastTime = currentTime
currentTime = ElapsedMilliseconds()
mdx + (lastTime - currentTime) * MouseDeltaX()/20
mdy + (lastTime - currentTime) * MouseDeltaY()/20
;mdx + 0.1
ClearScreen(0)
Start3D()
DisplayCube(0, 0, 1, 1, 2, 2, mdy, mdx, 200.0, #Screen_Width/2, #Screen_height/2)
mdx2 + 0.5
mdy2 + 0.5
DisplayCube(0, 0, 1, 1, 2, 2, mdx2,mdy2, 50.0, 100, #Screen_height-100)
Stop3D()
StartDrawing(ScreenOutput())
DrawText(10,10,"Move your mouse to rotate cube " +StrD(mdx,2) + " " + StrD(mdy,2) )
StopDrawing()
FlipBuffers(2)
CompilerIf #UseWindowMode
Until WindowEvent() = #PB_Event_CloseWindow Or KeyboardPushed(#PB_Key_Escape)
CompilerElse
Until KeyboardPushed(#PB_Key_Escape)
CompilerEndIf
End