This code displays a cube and allows the user to rotate it in lots of manners. The cube can me be moved too.
All is okay in the keyboard. I just don't know translate it. If anybody could help me, it would be useful.
I hope I will discover a better SPRITE3D library on the next update of PureBASIC (because it doesn't works fine on Linux, shame...) because this library is like the idea of PureBASIC I make for myself: small, simple and cool!
Without hint, you can even test it: all is in the keyboard. absolute rotations, relative rotations, absolute moving, relative moving, and relative shifting moving. All and just what is necessary to continue to a bigger project without bigger problems!
(Cf >> http://www.purebasic.fr/english/viewtop ... 8&start=30)
++
Code: Select all
Structure OIJK
Ox.D: Oy.D: Oz.D
Ix.D: Iy.D: Iz.D
Jx.D: Jy.D: Jz.D
Kx.D: Ky.D: Kz.D
EndStructure
Define Rep.OIJK: Rep\Ix = 1. : Rep\Jy = 1. : Rep\Kz = 1.
Global.I DskW, DskH, DskD, DskF, AffW, AffH
Global.D Zoom = 1000.
Macro Rect(mx, my, mw, mh, mc)
Line(mx, my, mw, 1, mc)
Line(mx, my, 1, mh, mc)
Line(mx, my + mh - 1, mw, 1, mc)
Line(mx + mw - 1, my, 1, mh, mc)
EndMacro
Macro Rotation(a, b, Cos, Sin)
x = a
y = b
a = x * Cos - y * Sin
b = x * Sin + y * Cos
EndMacro
Macro RotaAbso(axe, a, b, c) ; Rotation selon le repère du décor
Cos = Cos(c)
Sin = Sin(c)
Rotation(axe\I#a, axe\I#b, Cos, Sin)
Rotation(axe\J#a, axe\J#b, Cos, Sin)
Rotation(axe\K#a, axe\K#b, Cos, Sin)
EndMacro
Macro RotaRela(axe, a, b, c) ; Rotation selon le repère de l'objet
Cos = Cos(c)
Sin = Sin(c)
Rotation(axe\a#x, axe\b#x, Cos, Sin)
Rotation(axe\a#y, axe\b#y, Cos, Sin)
Rotation(axe\a#z, axe\b#z, Cos, Sin)
EndMacro
Macro Transla(mObject, mInitial, mFinal, mAxe, mStep)
For MI = mInitial To mFinal
mObject(MI)\mAxe + mStep
Next
EndMacro
Define.D x, y, z, Cos, Sin, v = 0.05, Perspective, dx, dy, dz, OnX, OnY
Define.D x1, y1, x2, y2
InitSprite()
InitSprite3D()
InitKeyboard()
ExamineDesktops()
DskW = DesktopWidth(0)
DskH = DesktopHeight(0)
DskD = DesktopDepth(0)
DskF = DesktopFrequency(0)
AffW = DskW
AffH = DskH
OpenScreen(DskW, DskH, DskD, "PureScreen")
KeyboardMode(#PB_Keyboard_International)
Global _Sol = 1, _Sol2 = 2
CreateSprite(_Sol, 256, 256, #PB_Sprite_Texture)
StartDrawing(SpriteOutput(_Sol) )
Box(0, 0, 256, 256, RGB(1, 1, 128) )
Box(1, 1, 254, 254, RGB(1, 1, 254) )
For I = 0 To 15
Rect(16 + I, 16 + I, 224 - I * 2, 224 - I * 2, RGB(1, 1, 127 + I * 8) )
Next
StopDrawing()
CreateSprite3D(_Sol0, _Sol)
CreateSprite3D(_Sol1, _Sol)
CreateSprite3D(_Sol2, _Sol)
CreateSprite3D(_Sol3, _Sol)
CreateSprite3D(_Sol4, _Sol)
CreateSprite3D(_Sol5, _Sol)
Structure V3
x.D: y.D : z.D
EndStructure
Macro SetV3(var, xv, yv, zv)
var\x = xv
var\y = yv
var\z = zv
EndMacro
Global Dim Sol.V3(7)
SetV3(Sol(0), 1., 1., 1.)
SetV3(Sol(1), -1., 1., 1.)
SetV3(Sol(2), -1., 1., -1.)
SetV3(Sol(3), 1., 1., -1.)
SetV3(Sol(4), 1., -1., 1.)
SetV3(Sol(5), -1., -1., 1.)
SetV3(Sol(6), -1., -1., -1.)
SetV3(Sol(7), 1., -1., -1.)
Global.I Dim Vertex0(5)
Global.I Dim Vertex1(5)
Global.I Dim Vertex2(5)
Global.I Dim Vertex3(5)
Macro SetVertices(Mn, Ma, Mb, Mc, Md)
Vertex0(Mn) = Ma
Vertex1(Mn) = Mb
Vertex2(Mn) = Mc
Vertex3(Mn) = Md
EndMacro
SetVertices(0, 0, 1, 2, 3)
SetVertices(1, 1, 0, 4, 5)
SetVertices(2, 4, 7, 6, 5)
SetVertices(3, 2, 6, 7, 3)
SetVertices(4, 0, 3, 7, 4)
SetVertices(5, 1, 5, 6, 2)
Structure VertexDisplay
x.D
y.D
z.D
EndStructure
Global Dim VD.VertexDisplay(7)
Macro SetVertex(Mn, Mx, My, Mz)
x1 = AffW / 2
y1 = AffH / 2
dx = Mx
dy = My
dz = Mz - 10.
Perspective = 1. / Sqr(dx * dx + dy * dy + dz * dz)
OnX = Mx * Perspective * Zoom
OnY = My * Perspective * Zoom
x2 = x1 + OnX
y2 = y1 - OnY
VD(Mn)\x = x2
VD(Mn)\y = y2
VD(Mn)\z = Chiffre + 10.59 - Mz
EndMacro
Macro TransformSprite(mn, ma, mb, mc, md)
TransformSprite3D(mn, VD(ma)\x, VD(ma)\y, VD(ma)\z, VD(mb)\x, VD(mb)\y, VD(mb)\z, VD(mc)\x, VD(mc)\y, VD(mc)\z, VD(md)\x, VD(md)\y, VD(md)\z)
DisplaySprite3D(mn, 0, 0)
EndMacro
Macro Applica(axe, mx, my, mz, mI, mJ, mK)
mx = axe\Ox + mI * axe\Ix + mJ * axe\Jx + mK * axe\Kx
my = axe\Oy + mI * axe\Iy + mJ * axe\Jy + mK * axe\Ky
mz = axe\Oz + mI * axe\Iz + mJ * axe\Jz + mK * axe\Kz
EndMacro
Macro Dessin(axe)
For I = 0 To 7
Applica(axe, x, y, z, Sol(I)\x, Sol(I)\y, Sol(I)\z)
SetVertex(I, x, y, z)
Next
Sprite3DQuality(#PB_Sprite3D_BilinearFiltering)
For I = 0 To 5
TransformSprite(_Sol0, Vertex0(I), Vertex1(I), Vertex2(I), Vertex3(I) )
Next
EndMacro
Macro Deplace(axe, mx, my, mz)
Applica(axe, axe\Ox, axe\Oy, axe\Oz, mx, my, mz)
EndMacro
Repeat
Delay(16)
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Up) : RotaAbso(Rep, y, z, v) : EndIf
If KeyboardPushed(#PB_Key_Down) : RotaAbso(Rep, y, z, -v): EndIf
If KeyboardPushed(#PB_Key_Tab) : RotaAbso(Rep, x, z, v) : EndIf
If KeyboardPushed(#PB_Key_A) : RotaAbso(Rep, x, z, -v): EndIf
If KeyboardPushed(#PB_Key_Left) : RotaAbso(Rep, x, y, v) : EndIf
If KeyboardPushed(#PB_Key_Right): RotaAbso(Rep, x, y, -v): EndIf
If KeyboardPushed(#PB_Key_Pad8) : RotaRela(Rep, J, K, v) : EndIf
If KeyboardPushed(#PB_Key_Pad2) : RotaRela(Rep, J, K, -v): EndIf
If KeyboardPushed(#PB_Key_Pad4) : RotaRela(Rep, I, J, v) : EndIf
If KeyboardPushed(#PB_Key_Pad6) : RotaRela(Rep, I, J, -v): EndIf
If KeyboardPushed(#PB_Key_Pad9) : RotaRela(Rep, K, I, v) : EndIf
If KeyboardPushed(#PB_Key_Pad3) : RotaRela(Rep, K, I, -v): EndIf
If KeyboardPushed(#PB_Key_T) : Zoom * 1.01 : EndIf
If KeyboardPushed(#PB_Key_Y) : Zoom * 0.99 : EndIf
If KeyboardPushed(#PB_Key_Q) : Rep\Ox - 0.1 : EndIf
If KeyboardPushed(#PB_Key_S) : Rep\Ox + 0.1 : EndIf
If KeyboardPushed(#PB_Key_E) : Rep\Oy + 0.1 : EndIf
If KeyboardPushed(#PB_Key_D) : Rep\Oy - 0.1 : EndIf
If KeyboardPushed(#PB_Key_R) : Rep\Oz - 0.1 : EndIf
If KeyboardPushed(#PB_Key_F) : Rep\Oz + 0.1 : EndIf
If KeyboardPushed(#PB_Key_C): Transla(Sol, 0, 7, x, 0.1) : EndIf
If KeyboardPushed(#PB_Key_V): Transla(Sol, 0, 7, x, (-0.1) ): EndIf
If KeyboardPushed(#PB_Key_B): Transla(Sol, 0, 7, y, 0.1) : EndIf
If KeyboardPushed(#PB_Key_N): Transla(Sol, 0, 7, y, (-0.1) ): EndIf
If KeyboardPushed(#PB_Key_G): Transla(Sol, 0, 7, z, 0.1) : EndIf
If KeyboardPushed(#PB_Key_H): Transla(Sol, 0, 7, z, (-0.1) ): EndIf
If KeyboardPushed(#PB_Key_1): Deplace(Rep, (-0.1), 0., 0.) : EndIf
If KeyboardPushed(#PB_Key_2): Deplace(Rep, 0.1 , 0., 0.) : EndIf
If KeyboardPushed(#PB_Key_3): Deplace(Rep, 0., (-0.1), 0.) : EndIf
If KeyboardPushed(#PB_Key_4): Deplace(Rep, 0., 0.1 , 0.) : EndIf
If KeyboardPushed(#PB_Key_5): Deplace(Rep, 0., 0., (-0.1)) : EndIf
If KeyboardPushed(#PB_Key_6): Deplace(Rep, 0., 0., 0.1 ) : EndIf
ClearScreen(RGB(254, 254, 254) )
StartDrawing(ScreenOutput() )
DrawingMode(#PB_2DDrawing_Transparent)
DrawText(0, 0, "Haut/Bas Gauche/Droite Tab/A T/Y Q/S E/D R/F C/V B/N G/H 1/2 3/4 5/6 Pad4/Pad6 Pad2/Pad8 Pad3/Pad9", RGB(1, 1, 1) )
StopDrawing()
Start3D()
Sprite3DQuality(#PB_Sprite3D_BilinearFiltering)
Dessin(Rep)
Stop3D()
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)