Pivot's for GLBasic

Previous topic - Next topic

Schranz0r

Here we go!
Copy and test it  :noggin:


Code (glbasic) Select
GLOBAL OrigiMat[]
DIM OrigiMat[16]
DIMDATA OrigiMat[],1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1

SYSTEMPOINTER TRUE

TYPE Pivot
ID = -1
Mat[16]
RotOn
rx; ry; rz
ENDTYPE

GLOBAL Piv[] AS Pivot


TYPE Object
ID = -1
Mesh
PivID = -1
Mat[16]
RotOn
rx; ry; rz
ENDTYPE

GLOBAL Obj[] AS Object


CreateCube(1, 5, RGB(255, 255, 255))

LOCAL Piv1 = CreatePivot()
LOCAL Piv2 = CreatePivot()

LOCAL Obj1 = CreateObjectOnPosition(1,10,0,0,Piv1)
LOCAL Obj2 = CreateObjectOnPosition(1,0,10,0,Piv1)
LOCAL Obj3 = CreateObjectOnPosition(1,-10,0,0,Piv1)
LOCAL Obj4 = CreateObjectOnPosition(1,0,-10,0,Piv1)

LOCAL Obj2_1 = CreateObjectOnPosition(1,20,0,0,Piv2)
LOCAL Obj2_2 = CreateObjectOnPosition(1,0,20,0,Piv2)
LOCAL Obj2_3 = CreateObjectOnPosition(1,-20,0,0,Piv2)
LOCAL Obj2_4 = CreateObjectOnPosition(1,0,-20,0,Piv2)

WHILE TRUE
LOCAL mx,my,b1,b2
MOUSESTATE mx, my, b1, b2

X_MAKE3D 1,1000,45
X_CAMERA 0,0,-80, 0,0,0

X_AMBIENT_LT 1, RGB(255,0,0)

X_SCREEN2WORLD mx, my, 0, wx, wy, wz


SetPivotRotation(Piv1,0,0,2)
SetObjectRotation(Obj1,2,0,0)
SetObjectRotation(Obj2,0,2,0)
SetObjectRotation(Obj3,-2,0,0)
SetObjectRotation(Obj4,0,-2,0)

SetPivotRotation(Piv2,0,0,-2)
SetObjectRotation(Obj2_1,-4,0,0)
SetObjectRotation(Obj2_2,0,-4,0)
SetObjectRotation(Obj2_3,4,0,0)
SetObjectRotation(Obj2_4,0,4,0)

SetPivotPosition(Piv1,wx*80,wy*80,0)
SetPivotPosition(Piv2,wx*80,wy*80,0)

Update()
SHOWSCREEN
WEND
END


FUNCTION CreatePivot:
LOCAL p AS Pivot
STATIC ID

p.ID = ID
p.Mat[] = OrigiMat[]

DIMPUSH Piv[],p

INC ID,1
RETURN p.ID
ENDFUNCTION

FUNCTION CreateObjectOnPosition: ObjectID, x, y, z, PivotID
LOCAL o AS Object
STATIC ID
o.ID = ID
o.Mesh = ObjectID
o.PivID = PivotID

o.Mat[] = OrigiMat[]
o.Mat[12] = x
o.Mat[13] = y
o.Mat[14] = z
INC ID,1
DIMPUSH Obj[],o
RETURN o.ID
ENDFUNCTION

FUNCTION SetObjectPosition: ObjectID, x, y, z

Obj[ObjectID].Mat[12] = x
Obj[ObjectID].Mat[13] = y
Obj[ObjectID].Mat[14] = z

ENDFUNCTION

FUNCTION SetPivotPosition: PivID, x, y, z

Piv[PivID].Mat[12] = x
Piv[PivID].Mat[13] = y
Piv[PivID].Mat[14] = z

ENDFUNCTION

FUNCTION SetObjectRotation: ObjectID, x, y, z
FOREACH i IN Obj[]
IF i.ID = ObjectID
i.RotOn = TRUE

IF i.rx > 359 THEN i.rx = 0
IF i.ry > 359 THEN i.ry = 0
IF i.rz > 359 THEN i.rz = 0
IF i.rx < 0 THEN i.rx = 359
IF i.ry < 0 THEN i.ry = 359
IF i.rz < 0 THEN i.rz = 359

INC i.rx, x
INC i.ry, y
INC i.rz, z

ENDIF
NEXT
ENDFUNCTION

FUNCTION SetPivotRotation: PivID, x, y, z
FOREACH i IN Piv[]
IF i.ID = PivID
i.RotOn = TRUE

IF i.rx > 359 THEN i.rx = 0
IF i.ry > 359 THEN i.ry = 0
IF i.rz > 359 THEN i.rz = 0
IF i.rx < 0 THEN i.rx = 359
IF i.ry < 0 THEN i.ry = 359
IF i.rz < 0 THEN i.rz = 359

INC i.rx, x
INC i.ry, y
INC i.rz, z
ENDIF
NEXT
ENDFUNCTION

FUNCTION Update:

FOREACH i IN Obj[]



X_MOVEMENT Piv[i.PivID].Mat[12], Piv[i.PivID].Mat[13], Piv[i.PivID].Mat[14]

          IF Piv[i.PivID].RotOn = TRUE
          IF Piv[i.PivID].rx > 0
          X_ROTATION Piv[i.PivID].rx,1, 0, 0
          ENDIF
          IF Piv[i.PivID].ry > 0
          X_ROTATION Piv[i.PivID].ry,0, 1, 0
          ENDIF
          IF Piv[i.PivID].rz > 0
          X_ROTATION Piv[i.PivID].rz, 0, 0, 1
          ENDIF
      ENDIF

X_PUSHMATRIX
          X_MOVEMENT i.Mat[12], i.Mat[13], i.Mat[14]
          IF i.RotOn = TRUE
          IF i.rx > 0
          X_ROTATION i.rx,1, 0, 0
          ENDIF
          IF i.ry > 0
          X_ROTATION i.ry,0, 1, 0
          ENDIF
          IF i.rz > 0
          X_ROTATION i.rz, 0, 0, 1
          ENDIF
      ENDIF
          X_DRAWOBJ i.Mesh, 0
    X_POPMATRIX

NEXT
ENDFUNCTION

FUNCTION CreateCube: num, sz, col
// Diese Variablen sind als LOCAL definiert:
// num, sz,
X_AUTONORMALS 1 // For a cube, hard edges
sz=sz/2
X_OBJSTART num
// Front Face
X_OBJADDVERTEX  sz, -sz,  sz, 1, 0, col
X_OBJADDVERTEX -sz, -sz,  sz, 0, 0, col
X_OBJADDVERTEX  sz,  sz,  sz, 1, 1, col
X_OBJADDVERTEX -sz,  sz,  sz, 0, 1, col
X_OBJNEWGROUP
// Back Face
X_OBJADDVERTEX -sz,  sz, -sz, 1, 1, col
X_OBJADDVERTEX -sz, -sz, -sz, 1, 0, col
X_OBJADDVERTEX  sz,  sz, -sz, 0, 1, col
X_OBJADDVERTEX  sz, -sz, -sz, 0, 0, col
X_OBJNEWGROUP
// Top Face
X_OBJADDVERTEX -sz,  sz,  sz, 0, 0, col
X_OBJADDVERTEX -sz,  sz, -sz, 0, 1, col
X_OBJADDVERTEX  sz,  sz,  sz, 1, 0, col
X_OBJADDVERTEX  sz,  sz, -sz, 1, 1, col
X_OBJNEWGROUP
// Bottom Face
X_OBJADDVERTEX  sz, -sz, -sz, 0, 1, col
X_OBJADDVERTEX -sz, -sz, -sz, 1, 1, col
X_OBJADDVERTEX  sz, -sz,  sz, 0, 0, col
X_OBJADDVERTEX -sz, -sz,  sz, 1, 0, col
X_OBJNEWGROUP
// Right face
X_OBJADDVERTEX  sz,  sz, -sz, 1, 1, col
X_OBJADDVERTEX  sz, -sz, -sz, 1, 0, col
X_OBJADDVERTEX  sz,  sz,  sz, 0, 1, col
X_OBJADDVERTEX  sz, -sz,  sz, 0, 0, col
X_OBJNEWGROUP
// Left Face
X_OBJADDVERTEX -sz, -sz,  sz, 1, 0, col
X_OBJADDVERTEX -sz, -sz, -sz, 0, 0, col
X_OBJADDVERTEX -sz,  sz,  sz, 1, 1, col
X_OBJADDVERTEX -sz,  sz, -sz, 0, 1, col
X_OBJNEWGROUP
X_OBJEND

ENDFUNCTION // sz
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

D2O

I7 2600K; 8 GB RAM ; Win10 Pro x64 | NVidia GTX 750 TI 2048MB ; Realtec OnBoard Sound;
Lenovo ThinkPad T400: XP Pro
GLB Premium-immer Aktuell

Kitty Hello

KRASS!!!  :noggin:
Wie hast'n das gemacht!?

Schranz0r

Gecheatet :)

Aber die Rotationen kann man besser machen Gernot....
Deshalb hab ich alles schonmal in eine Matrix gepackt ;)

Da müsstes du mir evtl. helfen?!

I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Moru

Please don't make this into an german only programming language, it's so nice compared to others it's sad that much of the information on the official forum already is in german. Don't close out the rest of the world!

Schranz0r

My code is completely english ;)
Everybody can use and understand it .

If somebody ask me in german, i reply in german.
That's the way it is :P
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Kitty Hello

Sorry-my fault.
Anyway, that code would be a great slowdown in comparison to my version. I think I'd better reccomend you to create the models with the pivot points where you need them, rather than "fixing" this in the code.

Schranz0r

Realy?
Cool, Heiko..... thats it :good:
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard