Author Topic: RotoSprite 3D  (Read 4392 times)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10728
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
RotoSprite 3D
« on: 2007-Apr-24 »
Here's some code to rotate a sprite around 3 axes. I suggest rotating around X/Z and Y/Z only, though.

Code: (glbasic) [Select]
FILLRECT 0,0,31,16,RGB(255,0,0)
FILLRECT 8,0,24,31,RGB(255,128,0)
GRABSPRITE 0,0,0,32,32
FILLRECT 0,0,31,16,RGB(64,64,64)
FILLRECT 8,0,24,31,RGB(64,64,64)
FILLRECT 12,0,20,31,RGB(99,99,99)
GRABSPRITE 1,0,0,32,32


WHILE TRUE

MOUSESTATE mx, my, b1,b2
phi = GETTIMERALL()/100

PukeSprite(0,1, my*2, mx*2, phi, 100, 100)
PRINT mx + " - " + my + " - "+phi, 0,0
SHOWSCREEN


WEND

// rotate a sprite in a terrible way
// img1 - upper side
// img2 - bottom side (same orientation)
// rotX - rotation angle around x axis (right side)
// rotY - rotation around up-down axis on screen
// rotZ - rotation like ROTOSPRITE
FUNCTION PukeSprite: img1, img2, rotX, rotY, rotZ, x,y
LOCAL w, h, phi, swap, tx, ty
LOCAL s, c, cl, px, py
GETSPRITESIZE img1, w,h
tx=w
ty=h

phi = MOD(ABS(rotY), 360)
IF phi < 90 OR phi>270
swap = img1
img1=img2
img2=swap
ENDIF
w = w * COS(phi)

phi = MOD(ABS(rotX), 360)
IF phi < 90 OR phi>270
swap = img1
img1=img2
img2=swap
ENDIF
h = h * COS(phi)

s=SIN(rotZ)
c=COS(rotZ)
w=w/2
h=h/2

cl=RGB(255,255,255)

STARTPOLY img1
px=-w; py=-h; POLYVECTOR x+px*c+py*s, y-px*s+py*c,  0, 0,cl
px=-w; py= h; POLYVECTOR x+px*c+py*s, y-px*s+py*c,  0,ty,cl
px= w; py= h; POLYVECTOR x+px*c+py*s, y-px*s+py*c, tx,ty,cl
px= w; py=-h; POLYVECTOR x+px*c+py*s, y-px*s+py*c, tx, 0,cl
// and back for back face culling
px= w; py= h; POLYVECTOR x+px*c+py*s, y-px*s+py*c, tx,ty,cl
px=-w; py= h; POLYVECTOR x+px*c+py*s, y-px*s+py*c,  0,ty,cl
px=-w; py=-h; POLYVECTOR x+px*c+py*s, y-px*s+py*c,  0, 0,cl
ENDPOLY
ENDFUNCTION

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1635
  • Particle Hawk
    • View Profile
Re: RotoSprite 3D
« Reply #1 on: 2008-Aug-05 »
2d/3d drawsprite 3d rotation.
This is rewritten for the GLBASIC.EXE V.5+
-Hem

Code: (glbasic) [Select]
// --------------------------------- //
// Project: test
// Start: Tuesday, August 05, 2008
// IDE Version: 5.341
GLOBAL mx,my,b1,b2

DRAWRECT 0,0,31,16,RGB(255,0,0)
DRAWRECT 8,0,24,31,RGB(255,128,0)
GRABSPRITE 0,0,0,32,32
DRAWRECT 0,0,31,16,RGB(64,64,64)
DRAWRECT 8,0,24,31,RGB(64,64,64)
DRAWRECT 12,0,20,31,RGB(99,99,99)
GRABSPRITE 1,0,0,32,32


WHILE TRUE

MOUSESTATE mx, my, b1,b2
phi = GETTIMERALL()/100

PukeSprite(0,1, my*2, mx*2, phi, 100, 100)
PRINT mx + " - " + my + " - "+phi, 0,0
SHOWSCREEN


WEND

// rotate a sprite in a terrible way
// img1 - upper side
// img2 - bottom side (same orientation)
// rotX - rotation angle around x axis (right side)
// rotY - rotation around up-down axis on screen
// rotZ - rotation like ROTOSPRITE
FUNCTION PukeSprite: img1, img2, rotX, rotY, rotZ, x,y
LOCAL w, h, phi, swap, tx, ty
LOCAL s, c, cl, px, py
GETSPRITESIZE img1, w,h
tx=w
ty=h

phi = MOD(ABS(rotY), 360)
IF phi < 90 OR phi>270
swap = img1
img1=img2
img2=swap
ENDIF
w = w * COS(phi)

phi = MOD(ABS(rotX), 360)
IF phi < 90 OR phi>270
swap = img1
img1=img2
img2=swap
ENDIF
h = h * COS(phi)

s=SIN(rotZ)
c=COS(rotZ)
w=w/2
h=h/2

cl=RGB(255,255,255)

STARTPOLY img1
px=-w; py=-h; POLYVECTOR x+px*c+py*s, y-px*s+py*c,  0, 0,cl
px=-w; py= h; POLYVECTOR x+px*c+py*s, y-px*s+py*c,  0,ty,cl
px= w; py= h; POLYVECTOR x+px*c+py*s, y-px*s+py*c, tx,ty,cl
px= w; py=-h; POLYVECTOR x+px*c+py*s, y-px*s+py*c, tx, 0,cl
// and back for back face culling
px= w; py= h; POLYVECTOR x+px*c+py*s, y-px*s+py*c, tx,ty,cl
px=-w; py= h; POLYVECTOR x+px*c+py*s, y-px*s+py*c,  0,ty,cl
px=-w; py=-h; POLYVECTOR x+px*c+py*s, y-px*s+py*c,  0, 0,cl
ENDPOLY
ENDFUNCTION
« Last Edit: 2008-Aug-05 by Hemlos »
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10728
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: RotoSprite 3D
« Reply #2 on: 2008-Aug-05 »
Ah. Thx Hem.