Page 1 of 1

TransformSprite3D() anyone got an example?

Posted: Sun Sep 05, 2010 1:53 am
by Rook Zimbabwe
I am hoping someone has an example of how to use TransformSprite3D() (with the X,Y, and Z) so I could get a batter hanndle on it... Anyone? :D

Re: TransformSprite3D() anyone got an example?

Posted: Sun Sep 05, 2010 2:40 am
by STARGÅTE
You can use it for a real 3-D rotation without 3D-Lib.
z is important for the correct pulling the triangles of the texture

Code: Select all

InitSprite()
InitSprite3D()

OpenWindow(0, 0, 0, 800, 600, "Screen", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0), 0, 0, 0)

  CreateSprite(1, 64, 64, #PB_Sprite_Texture)
  StartDrawing(SpriteOutput(1))
    Box(0, 0, 64, 64, $808080)
    For x = 0 To 7
    For y = 0 To 7
      If (x+y)%2 : Box(x*8, y*8, 8, 8, $FFFFFF) : EndIf
    Next
    Next
  StopDrawing()
  CreateSprite3D(1, 1)
    
Repeat

  ClearScreen(0)

  #HalfSize = 150
  #Distance = 500

  Start3D()
 
    a.f = ElapsedMilliseconds()/1000
 
    z1.f = #Distance-Sin(a)*#HalfSize : z4.f=z1
    z2.f = #Distance+Sin(a)*#HalfSize : z3.f=z2
    x1.f = -Cos(a)*#HalfSize*#Distance/z1 : x4.f=x1
    x2.f =  Cos(a)*#HalfSize*#Distance/z2 : x3.f=x2
    j.f = #HalfSize*#HalfSize
    y1.f = -#HalfSize-Sin(a)*j/z1 : y2.f=-#HalfSize+Sin(a)*j/z2
    y3.f =  #HalfSize-Sin(a)*j/z3 : y4.f= #HalfSize+Sin(a)*j/z4
    TransformSprite3D(1, x1,y1, x2,y2, x3,y3, x4,y4)
    DisplaySprite3D(1, 200, 300)
    TransformSprite3D(1, x2,y2, x1,y1, x4,y4, x3,y3)
    DisplaySprite3D(1, 200, 300)

    TransformSprite3D(1, x1,y1,z1, x2,y2,z2, x3,y3,z3, x4,y4,z4)
    DisplaySprite3D(1, 600, 300)
    TransformSprite3D(1, x2,y2,z2, x1,y1,z1, x4,y4,z4, x3,y3,z3)
    DisplaySprite3D(1, 600, 300)

  Stop3D()
  
  StartDrawing(ScreenOutput())
    DrawText(100,50,"3D-rotation without parameter z")
    DrawText(500,50,"3D-rotation with parameter z")
  StopDrawing()
  
  FlipBuffers()

Until WindowEvent() = #PB_Event_CloseWindow
Image

EDIT: Bug-Fix

Re: TransformSprite3D() anyone got an example?

Posted: Sun Sep 05, 2010 4:11 am
by Innesoft
Here's a peice of code I use to flip a Sprite3D using transforms, it ignores the z-axis since this is parallel to the screen, and not needed for flipping...

Code: Select all

;0=No Flip, 1=Horiz, 2=Vert, 3=Both
Procedure TformSprite3D(SpriteID.l,scale.f=512,flipmode.i=0)
  Select flipmode.i
    Case 0
      TransformSprite3D(SpriteID,0,0,0,scale.f,0,0,scale.f,scale.f,0,0,scale.f,0)  
    Case 1
      TransformSprite3D(SpriteID,scale.f,0,0,0,0,0,0,scale.f,0,scale.f,scale.f,0)  
    Case 2
      TransformSprite3D(SpriteID,0,scale.f,0,scale.f,scale.f,0,scale.f,0,0,0,0,0)
    Case 3
      TransformSprite3D(SpriteID,scale.f,scale.f,0,0,scale.f,0,0,0,0,scale.f,0,0)  
  EndSelect
EndProcedure
..of course you need to disable backface culling in dx9 with the following (taken from some other thread)....

Code: Select all

  ;Disable Backface Culling
  Define pd3d.IDirect3DDevice9
  Start3D()
  EnableASM
  !extrn _PB_Screen_Direct3DDevice
  !MOV dword EAX, [_PB_Screen_Direct3DDevice]
  !MOV dword [v_pd3d],EAX
  DisableASM
  pd3d\SetRenderState(22,1)
  Stop3D()
All you're really doing with TransformSprite3D is changing the vertex positions (in screen-space, relative to the sprite origin)

It's also worth noting that you can't combine the above flipping method with RotateSprite3D or ZoomSprite3D, since they will override the new vertex positions.

Re: TransformSprite3D() anyone got an example?

Posted: Sun Sep 05, 2010 5:35 am
by Rook Zimbabwe
Interesstig work each of you! :D

I was hoping that the Z order would let me overcome a roadblock Jared and I are having in our Isometric engine... namely layering and (hopefully) collisions...

Re: TransformSprite3D() anyone got an example?

Posted: Sun Sep 05, 2010 8:09 am
by Innesoft
I was hoping that the Z order would let me overcome a roadblock Jared and I are having in our Isometric engine... namely layering and (hopefully) collisions...
Edit: On second thoughts, this wouldn't work for z-order because in PB the z-order is determined by the order you use DisplaySprite3D in, which is always on top of the current drawing buffer.
---

It should work for z-order (not tested), if you use the vertex z value...

Code: Select all

scale.f=256

SpriteID=background : z.f=-1
TransformSprite3D(SpriteID,0,0,z.f,scale.f,0,z.f,scale.f,scale.f,z.f,0,scale.f,z.f)

SpriteID=foreground : z.f=0
TransformSprite3D(SpriteID,0,0,z.f,scale.f,0,z.f,scale.f,scale.f,z.f,0,scale.f,z.f)
..but for isometric drawing, as long as you draw in top-->bottom order you shouldn't need it.

Re: TransformSprite3D() anyone got an example?

Posted: Thu Sep 09, 2010 3:00 am
by Huitbit
@STARGÅTE
z is important for the correct pulling the triangles of the texture
Very good job :P , it's the first time I read that z is usefull !!!

Thanks

Hasta la vista !

Re: TransformSprite3D() anyone got an example?

Posted: Thu Sep 09, 2010 9:48 pm
by Rook Zimbabwe
Yep that would usually wrk... BUT

We are drawing the animated dude inn center of screen and then attempting to disply the world around him. That way he will go behind some things and in front of others... Layering is necessary but difficult!

Re: TransformSprite3D() anyone got an example?

Posted: Fri Sep 17, 2010 8:47 pm
by Olliv
Rook wrote:Yep that would usually wrk... BUT

We are drawing the animated dude inn center of screen and then attempting to disply the world around him. That way he will go behind some things and in front of others... Layering is necessary but difficult!
Hello Rook,

First, I thank you for this interesting subject. This function is a base for lots of representations.

I thank STARGÅTE too for his response, and the code he gave here. It's very insteresting. Thank to him, I look an other problem with the function TransformSprite3D().

I think OPENGL doesn't support z parameter. I work on Windows but I worry about compatibiliy on Linux.

Could somebody check if OPENGL is okay or not okay on Linux with the code of STARGÅTE ?

Ollivier

Re: TransformSprite3D() anyone got an example?

Posted: Sun Nov 14, 2010 3:23 am
by jamirokwai
Olliv wrote:Could somebody check if OPENGL is okay or not okay on Linux with the code of STARGÅTE ?
I tested on Mac OX X 10.6.5. At least, it works, so should work on Linux, too...
STARGÅTE wrote:Image
On Max OS X, your example displays the same image with or without z-parameter. Maybe a bug??

Image