3d point and line graphs in PB

Everything related to 3D programming
hgcoon
New User
New User
Posts: 9
Joined: Wed Feb 19, 2014 11:09 pm

3d point and line graphs in PB

Post by hgcoon »

Hi all,
I have already posted to "programming" but it was suggested that I post it here (a more appropriate audience).

I have not used the 3D pkg in PureBasic but now I find that I'd like to make 3D graphs (point and line). Is PB 3D the way to go? or must I break out the matrix books and see if I can work out how to pitch and yaw such graphs after they are plotted? I searched without result for simple rotating cube code and the like. I can do these graphs easily in Mathematica but the interface with PB seems difficult -- maybe impossible.
Are there code examples buried in the PB archives? If not, can you suggest a place for me to start without using OpenGL etc.? Is there an introduction or tutorial that might prepare me for the 3D programming in PB?
Thanks for your thoughts as always, hgc
T4r4ntul4
Enthusiast
Enthusiast
Posts: 118
Joined: Tue Mar 04, 2014 4:15 pm
Location: Netherlands

Re: 3d point and line graphs in PB

Post by T4r4ntul4 »

just a repeat what i said in the other topic, because youre asking the same questions ;)
T4r4ntul4 wrote:hi, its not impossible.
3d points here: http://www.purebasic.fr/english/viewtop ... oint+cloud
and 3d lines example can you find in the examples folder named: CreateLine3D.pb

(edit: btw post 3D questions in the 3D forum. the more like you get answers there instead of here.)
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: 3d point and line graphs in PB

Post by applePi »

the reference by T4r4ntul4 contains many things about 3d points.
we can plot data easily with purebasic 3d engine .
but i think there is a need for a 2 pages introduction in the help docs to describe how to plot a 3d point because this is what 96% of the users wants from the Graphics, the other 4% is the games / software developers , and how in the world the user will know that he should first create a mesh before plotting a 3d point !!.
here is my viewpoint:
1- to be able to draw a 3d point or line in PB we need to prepare a 3d environment for plotting , or say a place in the memory, imagine this env. a 3d room, or even a body, undefined object yet, waiting for us to run our color brush over it, we call it here "mesh" , we should create it with CreateMesh(...) before we plot points/lines , CreateMesh(...) has many parameters to suit different graphics options.
such as CreateMesh(#mesh, #PB_Mesh_PointList, #PB_Mesh_Static) :
#PB_Mesh_PointList is the most usable option since it means we want to plot any collection of unrelated points. we can create several instances from the same mesh by using CreateEntity(...) and every entity (or say instance) have different material, or positions ...etc. in this case we must finalize the mesh with FinishMesh(#True). else we use FinishMesh(#False) as used in the the official example MeshManual2.pb in PureBasic\Examples\3D folder . MeshManual2.pb contains everything needed, but to study it i suggest to isolate the several codes it contains and to experiment with it alone. as an example here is i isolate plotting the points only and i have finalized the mesh and created an entity. compare with the original example
note that we set the mesh material to white, imagine this like a white blackboard we want to draw color over it with the following brush :
MeshVertexPosition(x, y, z) ; position of the point
MeshVertexColor(RGB(?,?,?)) ; color of the point

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - MeshManual 
;
;    (c) Fantaisie Software
;
; ------------------------------------------------------------
;

#CameraSpeed = 1
#scale = 3


Define.f KeyX, KeyY, MouseX, MouseY

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/fonts", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  ExamineDesktops()
  DesktopW = DesktopWidth(0)
  DesktopH = DesktopHeight(0)
  
  OpenWindow(0, 0, 0, DesktopW, DesktopH, "Just Points ")
    OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)
      
    ;- Material
    CreateMaterial(0, LoadTexture(0, "White.jpg"))
    DisableMaterialLighting(0, #True)
     
    ;- Mesh Stars
    CreateMesh(1, #PB_Mesh_PointList, #PB_Mesh_Static)
    For i = 0 To 100000
      MeshVertexPosition(Random(200)-100, Random(200)-100, Random(200)-100)
      MeshVertexColor(RGB(Random(255,1),Random(255,1),Random(255,1)))
      
    Next i 
    FinishMesh(#True)
    
    SetMeshMaterial(1, MaterialID(0))
    
    CreateEntity(1, MeshID(1), MaterialID(0))
    
    ;-Camera
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 40, 150, #PB_Absolute)
    CameraFOV(0, 40)
    ;CameraLookAt(0, NodeX(Grid),  NodeY(Grid),  NodeZ(Grid))
    CameraBackColor(0, RGB(0, 0, 40))
    
    ;-Light
    CreateLight(0, RGB(255,255,255), -10, 60, 10)
    AmbientColor(RGB(90, 90, 90))
    
    Repeat
      Event = WindowEvent()
      ExamineKeyboard()
      
      RotateEntity(1, 0.1, 0.1, 0.1, #PB_Relative)
      RenderWorld()
      
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  

End



2- suppose you want to plot the point as a tiny rose
save the following lines to rose.material file together with the following code and the rose.png (it has Alpha channel and it is referred to in the "rose.material".
http://s28.postimg.org/6k98je5n1/rose.png
Image
what we will see is the mandelbrot set made from roses:
Image
i know little about material scripts, and in fact the first time i know about it is from Samuel codes about materials. before that i was scaring from the word "script" and still.
rose.material :

Code: Select all

material PointsSprites
{
   technique
   {
      pass
      {
         cull_hardware none
         cull_software none
         scene_blend alpha_blend
         lighting on
         
         diffuse 1 1 1 1
         ambient 1 1 1 1
         specular 1 1 1 1
         emissive 1 1 1 1
         
         point_sprites on
         point_size 20
         //point_size_min 2.0
         //point_size_max 256.0
         
         depth_write off
         depth_check on
         
         depth_func less_equal
         
         //point_size_attenuation on 1.0 0.0 3.5
         
         texture_unit
         {
            filtering anisotropic
            max_anisotropy 16
            texture rose.png
         }
      }
   }
}
mandelbrot set from roses:

Code: Select all

Enumeration
   #MESH
   #LIGHT
   #CAMERA_ONE
  
 EndEnumeration
   
Global.f dx, dy, x, y, z
Global wd, ht, i, count, n, iter
Global.f w, leng, tx, ty, tz, tem
Global.f cr, ci, cj, ck, wk, inc, distance
Global mand, zval
Global.f angle
Define.f red, green, blue

;mand = 0 is Julia Set
;mand = 1 is Mandelbrot 3D
mand = 1
;zval = 1 shows entire set
;zval = 0 cuts set in half
;zval = 0 is an interesting effect
zval = 1
iter = 5
inc = 5 
;#quat = 1
zval = 1
iter = 5
inc = 5 
Procedure.f RandF(Min.f, Max.f, Resolution.i = 10000)
  ProcedureReturn (Min + (Max - Min) * Random(Resolution) / Resolution)
EndProcedure

Quit.b = #False

ExamineDesktops()
If OpenWindow(0, 0, 0, DesktopWidth(0), DesktopHeight(0), "mandelbrot set from roses", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
;Initialize environment
InitEngine3D()
InitSprite()
OpenWindowedScreen(WindowID(0), 0, 0, DesktopWidth(0), DesktopHeight(0), 0, 0, 0)

InitKeyboard()
SetFrameRate(60)

Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Scripts", #PB_3DArchive_FileSystem)
Add3DArchive(".", #PB_3DArchive_FileSystem)
Parse3DScripts()

CreateLight(0,RGB(255,255,255),-100,40,30)
AmbientColor(RGB(100,100,100))

CreateCamera(#CAMERA_ONE, 0, 0, 100, 100)
MoveCamera(#CAMERA_ONE, 0, 2, 7)
CameraLookAt(#CAMERA_ONE, 0, 1, 0)

RotateCamera(#CAMERA_ONE, -15, 0, 0)
EndIf


CreateMesh(1, #PB_Mesh_PointList, #PB_Mesh_Dynamic )

GetScriptMaterial(0, "PointsSprites")
SetMeshMaterial(1, MaterialID(0))
DisableMaterialLighting(0, #True)

Global Stars = CreateNode(#PB_Any)
AttachNodeObject(Stars, MeshID(1))


Procedure.f calcleng( x.f,  y.f,  z.f)
    w.f: kr.f: ki.f: kj.f: kk.f
    w = wk
    n = 0
    If mand = 1  ;full Mandelbrot set
        kr = x
        ki = y
        kj = z
        kk = 0
    Else                ;else draw Julia Set
        kr = cr
        ki = ci
        kj = cj
        kk = ck
    EndIf
    
    While n < iter
        tem = x+x
        x = x*x-y*y-z*z-w*w+kr
        y = tem*y + ki
        z = tem*z + kj
        w = tem*w + kk
        
        n+1
        distance = x*x+y*y+z*z+w*w
        
        If distance > 4 
          n = iter
        EndIf
        
    Wend
        
    ;Return distance
    ProcedureReturn distance
  
EndProcedure

  

Procedure calcit()
    zz.f
    foo.l
    iterations = 10000
    count = 0
    
    If zval = 0
        zz = 2.0
    Else
        zz = 4.0
    EndIf
      
      For foo = 0 To iterations
          ;x.f = RandF(0, 1)
          ;y.f = RandF(0, 1)
          x.f = RandF(-2, 2)
          y.f = RandF(-2, 2)
          
          z.f = zz*RandF(0, 1) -2.0
          
          ;calls the quaternion calculation
          leng.f = calcleng(x,y,z)
          
          If leng < 4 
                                          
              MeshVertexPosition(x, y, z)
                                         
          EndIf
          
        Next 
        FinishMesh(#False)
              
EndProcedure


calcit()  ; calling the mandel 3D or julia generator function

;Main loop
Repeat
  Event = WindowEvent()
  
    
   RotateNode(Stars, 0, 1, 0, #PB_Relative)
   
   RenderWorld()
   FlipBuffers()

  ExamineKeyboard()
  
  
   If KeyboardPushed(#PB_Key_Escape)
      Quit = #True
    EndIf
    
    
Until Quit = #True Or Event = #PB_Event_CloseWindow
regarding lines, in addition to the CreateLine3D(...) function (look CreateLine3d.pb official example) , we can also create mesh like this:
CreateMesh(#mesh, #PB_Mesh_LineList , #PB_Mesh_Static)
and after that when we plot 2 consecutive points it will be connected by a line automatically
look example MeshManual2.pb also this thread: http://purebasic.fr/english/viewtopic.php?f=36&t=61049

EDIT:
here is how to plot Big points
suppose you want a point size 3
save this as bigpoint.material in the the same folder as the following code, since the Add3DArchive(".", #PB_3DArchive_FileSystem) instruct the engine to look at the code folder for textures, scripts, models, etc. and here
GetScriptMaterial(5, "def_Points") search for *.material text files for sentence material def_Points (we can use any other name such as material Big_Points)
look that the text file bigpoint.material have a line point_size 3
so every :
MeshVertexPosition(x, y, z)
MeshVertexColor(RGB(0,0,255))

will plot a big blue point. i should not forgot that Parse3DScripts() are essential to add.
note that we can zoom on/out rotate camera with mouse/keyboard around the Graphics to have the desirable view
bigPoints.material:

Code: Select all

material def_Points
{
   receive_shadows off
   technique
   {
      pass
      {
         point_size 3
         lighting off
         diffuse vertexcolour
      }
   }
}
sine plotting code

Code: Select all

Declare DrawBigPoints()

#CameraSpeed = 1

Enumeration
  #mesh
  #entity
  #tex
  #light
  #camera

EndEnumeration

ScreenX = GetSystemMetrics_(#SM_CXSCREEN)
ScreenY = GetSystemMetrics_(#SM_CYSCREEN)

InitEngine3D(#PB_Engine3D_DebugLog)
InitSprite()
InitKeyboard()
InitMouse()

OpenWindow(0,0,0,ScreenX,ScreenY,"3D curves Plot",#PB_Window_BorderLess|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,ScreenX,ScreenY,1,0,0,#PB_Screen_WaitSynchronization)

Add3DArchive(".", #PB_3DArchive_FileSystem)
Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)

Parse3DScripts()

KeyboardMode(#PB_Keyboard_AllowSystemKeys)

;Create Camera
CreateCamera(0,0,0,100,100)
MoveCamera(0,0,0,23,#PB_Absolute)
CameraLookAt(0, 0, 0, 0)
CameraBackColor(0, RGB(155,155,100))

;-Mesh

CreateMesh(0, #PB_Mesh_PointList, #PB_Mesh_Static)
  GetScriptMaterial(5, "def_Points")
  SetMeshMaterial(0, MaterialID(5))
  DisableMaterialLighting(5, #True)
  DrawBigPoints()
  FinishMesh(#True)
  SetMeshMaterial(0, MaterialID(5))
  CreateEntity(0, MeshID(0), MaterialID(5),0,0,0)

Repeat
  WindowEvent()
  
  If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.2
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.2
      EndIf
  ShowCursor_(0)
    
; Use arrow keys and mouse to rotate and fly in/out
  ExamineKeyboard()
      
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed
        Else
          KeyY = 0
        EndIf
      

      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      
  RenderWorld()
  
  FlipBuffers()

Until KeyboardPushed(#PB_Key_Escape) Or WindowEvent() = #PB_Event_CloseWindow

;main drawing routine
Procedure DrawBigPoints()
  Protected.l a, b, Nb
  Protected.w P1, P2, P3, P4
  Protected.f x, z
  NbX=600
  NbZ=600
  NbY=600
  
  ;xMin.f = -1 : yMin.f = -1: zMin.f = -1 : xMax.f = 1: yMax = 1 : zMax = 1
  xMin.f = -2*#PI : yMin.f = -2*#PI: zMin.f = -3 : xMax.f = 2*#PI: yMax = 2*#PI : zMax = 3
  ;xMin.f = -10 : zMin.f = -10 : xMax.f = 10: zMax = 10
  range = xMax - xMin
  step1.f = range / NbX
  x.f = xMin: z.f = 0 : y.f = yMin
      
    For a=0 To NbX
      
            ;y.f = x * x
            y.f = Sin(x)
                      
            MeshVertexPosition(x, y, z) 
            ;MeshVertexPosition(x*3, y*3, z) ;to amplify values
            MeshVertexColor(RGB(0,0,255))
      
      x.f + step1
    Next a
    
    
EndProcedure  

hgcoon
New User
New User
Posts: 9
Joined: Wed Feb 19, 2014 11:09 pm

Re: 3d point and line graphs in PB

Post by hgcoon »

Special thanks to ApplePi for the compilation of examples of OGRE. I am still studying and trying to follow all the elements of the examples. Alas, however, it is either going to take a long time, or I need a lot more information before I could put together these elements to do what I want. Perhaps allowing me to see the "includes" would help some of my confusion. But I'm still at the point of commenting out portions of the code in the examples and seeing just what they do...
I'd like to get to the point where I could set up axes (as in the MeshManual 2 example) and then plot series of points in the space and be able to use the pitch-yaw and roll my way through the result to see details of what happens in 3D, but I'm still not there in spite of the plethora of examples provided. I'm working on it...
Thank you very much indeed for getting me started!
hgc
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: 3d point and line graphs in PB

Post by applePi »

Hi hgcoon
i should mention the DK_PETER thread "Something beautiful" http://purebasic.fr/english/viewtopic.php?f=36&t=60270 it is plotting graphics also using MeshVertexPosition(...) and related functions. but his coding style are advanced.
there is a book (PureBasic - A Beginner's Guide) http://www.purearea.net/pb/download/PureBasicBook.pdf and even it is old but it is still great, it has a chapters about 2d + 3d graphics . you can install an older purebasic version 4.xx to try its examples look here http://purebasic.fr/english/viewtopic.php?f=14&t=37059 . after sometime you can adapt it to the new PB versions
its source code can be found in Archive.org: https://web.archive.org/web/20060813110 ... 20Code.zip
one of its examples i have adapted it to PB 5.31b1 and posted it here:
http://purebasic.fr/english/viewtopic.p ... 9&p=452618
i will check it tomorrow.
T4r4ntul4
Enthusiast
Enthusiast
Posts: 118
Joined: Tue Mar 04, 2014 4:15 pm
Location: Netherlands

Re: 3d point and line graphs in PB

Post by T4r4ntul4 »

hgcoon wrote: I searched without result for simple rotating cube code and the like. I can do these graphs easily in Mathematica but the interface with PB seems difficult -- maybe impossible.
just a simple rotating cube:

Code: Select all

If InitEngine3D()
 
  InitSprite()
  InitKeyboard()
  
  ; get screen size
  ExamineDesktops()
  DesktopW = DesktopWidth(0)-10
  DesktopH = DesktopHeight(0)-60
 
  OpenWindow(0, 0, 0, DesktopW, DesktopH, "Rotating cube")
    OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)

    ; cube creation
    CreateCube(1,35)
    CreateEntity(1, MeshID(1), #PB_Material_None)
    
    ;-Camera
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 150, #PB_Absolute)
    CameraBackColor(0, RGB(0, 0, 40))
   
    ;-Light
    CreateLight(0, RGB(255,255,255), -10, 60, 10)
    AmbientColor(RGB(90, 90, 90))
    
    ; game loop
    Repeat
      Event = WindowEvent()
      ExamineKeyboard()
      
      ; rotate cube
      RotateEntity(1, 0.5, 0.5, 0.5, #PB_Relative)
      
      RenderWorld()
     
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
 
End
doctornash
Enthusiast
Enthusiast
Posts: 130
Joined: Thu Oct 20, 2011 7:22 am

Re: 3d point and line graphs in PB

Post by doctornash »

Wondering if someone would be so kind as to suggest mods to this rotating cube code for the following:
-make the cube 'transparent' (such that all vertices are visible at all times)
-enable rotation in 'different directions' from the same viewing position
-capture the (x,y) pixel co-ordinates of the vertices as the cube rotates
Thanks muchly!
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: 3d point and line graphs in PB

Post by applePi »

doctornash ,
-make the cube 'transparent' (such that all vertices are visible at all times)
we need to give the cube a material with AlphaBlend, look lines 16-19
note that MaterialCullingMode(0,#PB_Material_NoCulling) is very important to see the other sides of the cube.
to see the cube as a wire uncomment line 30 CameraRenderMode(0, #PB_Camera_Wireframe)
to see thick lines compile with opengl subsystem from the compiler->compiler options
in this case glLineWidth_(4) in line 21 will have an effect since it is an opengl functions works over the Ogre_opengl corner side
tested with 5.46 LTS

Code: Select all

If InitEngine3D()
 
  InitSprite()
  InitKeyboard()
  
  ; get screen size
  ExamineDesktops()
  DesktopW = DesktopWidth(0)-10
  DesktopH = DesktopHeight(0)-60
 
  OpenWindow(0, 0, 0, DesktopW, DesktopH, "Rotating cube")
  OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)
  Add3DArchive(".", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
  
  CreateMaterial(0, LoadTexture(0, "Wood.jpg"))
  MaterialCullingMode(0,#PB_Material_NoCulling)
  MaterialBlendingMode(0, #PB_Material_AlphaBlend)
  SetMaterialColor(0, #PB_Material_DiffuseColor, RGBA(255, 255, 255,120))

  glLineWidth_(4) ; for opengl sybsystem only
    ; cube creation
    CreateCube(1,35)
    CreateEntity(1, MeshID(1), MaterialID(0))
    
    ;-Camera
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 150, #PB_Absolute)
    CameraBackColor(0, RGB(0, 0, 40))
    ;CameraRenderMode(0, #PB_Camera_Wireframe)

   
    ;-Light
    CreateLight(0, RGB(255,255,255), -10, 60, 10)
    AmbientColor(RGB(255, 255, 255))
    
    ; game loop
    Repeat
      Repeat
        Event = WindowEvent()
        Until Event = 0
      ExamineKeyboard()
      
      ; rotate cube
      RotateEntity(1, 0.5, 0.5, 0.5, #PB_Relative)
      
      RenderWorld()
     
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
 
End
-enable rotation in 'different directions' from the same viewing position
i don't understand what this means, but to
rotate around y, change line 43 to RotateEntity(1, 0, 0.5, 0, #PB_Relative)
around x: RotateEntity(1, 0.5, 0, 0, #PB_Relative)
around z: RotateEntity(1, 0, 0, 0.5, #PB_Relative)
to use #PB_Absolute parameter instead of #PB_Relative, use variables and increase its values continuously

-capture the ( x,y ) pixel co-ordinates of the vertices as the cube rotates
unfortunately RotateEntity does not trigger an info about the vertices positions
so we do another approach:
we get the mesh information while the cube rotates as described below, the built in cube have 24 vertices ( not 8 ) (but you can make a cube manually with 8 vertices only.
use function GetMeshData to drop the mesh data into MeshData structured array
apply a rotation formula to each of the 24 cube vertices, i don't know what the rotation formula around itself exactly so this is makes the cube travel in orbit

Code: Select all

For i=0 To 23 ; number of the cube vertices
 x.f=Cos(angle)*0.4
 z.f=Sin(angle)*0.4
 MeshData(i)\x + x
 MeshData(i)\z + z
Next
and the positions of vertex(0) printed with debug. then copy the debug info to notepad and inspect it
we use SetMeshData function to set the cube vertices to its new positions
note that we can use a function TransformMesh(1,0,0,0, 1,1,1,0,1,0)
uncomment it in line 48 and remove the lines 59 to 64:

Code: Select all

For i=0 To 23 ; number of the cube vertices
           x.f=Cos(angle)*0.4
           z.f=Sin(angle)*0.4
           MeshData(i)\x + x
           MeshData(i)\z + z
Next
in this case you will see a true rotation around itself with info about the vertex(0) positions
you can display the vertex 1 to 23 positions by changing :
for i=0 to 23
Debug "x= "+StrF(MeshData(i)\x)+" y= ....
next i

Code: Select all

If InitEngine3D()
 
  InitSprite()
  InitKeyboard()
  
  ; get screen size
  ExamineDesktops()
  DesktopW = DesktopWidth(0)-10
  DesktopH = DesktopHeight(0)-60
 
  OpenWindow(0, 0, 0, DesktopW, DesktopH, "Rotating cube")
  OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)
  Add3DArchive(".", #PB_3DArchive_FileSystem)
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
  
  CreateMaterial(0, LoadTexture(0, "Wood.jpg"))
  MaterialCullingMode(0,#PB_Material_NoCulling)
  MaterialBlendingMode(0, #PB_Material_AlphaBlend)
  SetMaterialColor(0, #PB_Material_DiffuseColor, RGBA(255, 255, 255,120))

  glLineWidth_(4) ; for opengl sybsystem only
    ; cube creation
    CreateCube(1,35)
    CreateEntity(1, MeshID(1), MaterialID(0))
    
    ;-Camera
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 150, #PB_Absolute)
    CameraBackColor(0, RGB(0, 0, 40))
    ;CameraRenderMode(0, #PB_Camera_Wireframe)

   
    ;-Light
    CreateLight(0, RGB(255,255,255), -10, 60, 10)
    AmbientColor(RGB(255, 255, 255))
    ;Debug MeshVertexCount(1)-1
    ;Debug SubMeshCount(1)
    
    Global Dim MeshData.PB_MeshVertex(0)
        
    Define.f x,y,z
  
    Repeat
Repeat
        Event = WindowEvent()
        Until Event = 0
      ExamineKeyboard()
      ; rotate cube
      ;RotateEntity(1, 0.0, 0.5, 0.0, #PB_Relative)
      ;TransformMesh(1,0,0,0, 1,1,1,0,1,0)
      ;UpdateMeshBoundingBox(1)
      
      RenderWorld()
     
      FlipBuffers()
  
      ;GetMeshData(#Mesh, SubMesh, DataArray(), Flags, FirstIndex, LastIndex)
      GetMeshData(1,0, MeshData(), #PB_Mesh_Vertex ,0, MeshVertexCount(1)-1)
          angle.f + 0.02
          
          For i=0 To 23 ; number of the cube vertices
           x.f=Cos(angle)*0.4
           z.f=Sin(angle)*0.4
           MeshData(i)\x + x
           MeshData(i)\z + z
          Next
        
        Debug "x= "+StrF(MeshData(0)\x)+" y= "+StrF(MeshData(0)\y)+" z= "+StrF(MeshData(0)\z)
        SetMeshData(1,0, MeshData(), #PB_Mesh_Vertex ,0, MeshVertexCount(1)-1)
       
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
 
End
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: 3d point and line graphs in PB

Post by DK_PETER »

As applePi stated - capturing the x,y pixel coords isn't possible,
but using nodes can get you the 3D coords needed when rotating.
In this example I've focused on the four corners on each side only.
In fact..You don't need the entity at all - here is is for viewing only
since all the data comes from nodes.

Code:

Code: Select all

Structure _vrot
  rox.f
  roy.f
  roz.f
EndStructure

Declare.i AttachNodes(CubeSize.f, Side.s = "front")

Global Dim n.i(3) ;Four corners on a single surface - add the rest if all vertex positions are needed.
Global mainnode.i, el.i, r._vrot

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  
  ; get screen size
  ExamineDesktops()
  DesktopW = DesktopWidth(0)
  DesktopH = DesktopHeight(0)
  
  OpenWindow(0, 0, 0, DesktopW, DesktopH, "Rotating cube")
  OpenWindowedScreen(WindowID(0), 0, 0, DesktopW, DesktopH, 0, 0, 0)

  CreateTexture(0, 1024, 1024, "Cubie")
  StartDrawing(TextureOutput(0))
  RoundBox(4, 4, 1016, 1016, 10, 10, $26FA22)
  RoundBox(24, 24, 976, 976, 10, 10, $0)
  StopDrawing()
  CreateMaterial(0, TextureID(0))
  MaterialCullingMode(0,#PB_Material_NoCulling)
  MaterialBlendingMode(0, #PB_Material_Add)

  CreateCube(0, 35)
  CreateEntity(0, MeshID(0), MaterialID(0))
  mainnode = CreateNode(#PB_Any,0, 0, 0)
  AttachNodeObject(mainnode, EntityID(0))
  
  For x.i = 0 To 3 : n(x) = CreateNode(#PB_Any) : Next x
  
  AttachNodes(35, "front") 
  
  ;-Camera
  CreateCamera(0, 0, 0, 100, 100)
  MoveCamera(0, 0, 0, 150, #PB_Absolute)
  CameraBackColor(0, RGB(0, 0, 40))

;   ;-Light
;   CreateLight(0, RGB(255,255,255), -10, 60, 10)
;   AmbientColor(RGB(255, 255, 255))
  
  r\rox = -0.2 : r\roy = 0.2 : r\roz = 0.4
  
  el = ElapsedMilliseconds()
  
  Repeat
    Repeat
      Event = WindowEvent()
    Until Event = 0
    ExamineKeyboard()
    For x = 0 To 3
      Debug "n(" + Str(x) + ") = " + StrF(NodeX(n(x))) + " , " + StrF(NodeY(n(x))) + " , " + StrF(NodeZ(n(x)))
    Next 
    If ElapsedMilliseconds() - el > 4000
      If Random(50) > 25 : r\rox = -0.2 : Else : r\rox = 0.2 : EndIf
      If Random(50) > 25 : r\roy = -0.2 : Else : r\roy = 0.2 : EndIf
      If Random(50) > 25 : r\roz = -0.2 : Else : r\roz = 0.2 : EndIf
      el = ElapsedMilliseconds()
    EndIf
    RotateNode(mainnode, r\rox, r\roy, r\roz, #PB_Relative)
    
    RenderWorld()
    
    FlipBuffers()
    
  Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
EndIf

End

Procedure.i AttachNodes(CubeSize.f, Side.s = "front")
  Protected x.i, nsize.f = CubeSize/2
  ;Attaching to one side only
  For x = 0 To 3 : DetachNodeObject(n(x), NodeID(mainnode)) : Next x
  Select LCase(Side)
    Case "front"
      MoveNode(n(0),-nsize, nsize, nsize, #PB_Absolute)
      MoveNode(n(1),nsize, nsize, nsize, #PB_Absolute)
      MoveNode(n(2),nsize, -nsize, nsize, #PB_Absolute)
      MoveNode(n(3),-nsize, -nsize, nsize, #PB_Absolute)
    Case "back"
      MoveNode(n(0),-nsize, nsize, -nsize, #PB_Absolute)
      MoveNode(n(1),nsize, nsize, -nsize, #PB_Absolute)
      MoveNode(n(2),Size, -Size, -Size, #PB_Absolute)
      MoveNode(n(3),-nsize, -nsize, -nsize, #PB_Absolute)
    Case "left"
      MoveNode(n(0),-nsize, nsize, -nsize, #PB_Absolute)
      MoveNode(n(1),-nsize, nsize, nsize, #PB_Absolute)
      MoveNode(n(2),-nsize, -nsize, nsize, #PB_Absolute)
      MoveNode(n(3),-nsize, -nsize, -nsize, #PB_Absolute)
    Case "right"
      MoveNode(n(0),nsize, nsize, -nsize, #PB_Absolute)
      MoveNode(n(1),nsize, -nsize, -nsize, #PB_Absolute)
      MoveNode(n(2),nsize, nsize, nsize, #PB_Absolute)
      MoveNode(n(3),nsize, -nsize, nsize, #PB_Absolute)
    Case "up"
      MoveNode(n(0),-nsize, nsize, -nsize, #PB_Absolute)
      MoveNode(n(1),nsize, nsize, -nsize, #PB_Absolute)
      MoveNode(n(2),nsize, nsize, nsize, #PB_Absolute)
      MoveNode(n(3),-nsize, nsize, nsize, #PB_Absolute)
    Case "down"
      MoveNode(n(0),-nsize, -nsize, -nsize, #PB_Absolute)
      MoveNode(n(1),nsize, -nsize, -nsize, #PB_Absolute)
      MoveNode(n(2),nsize, -nsize, nsize, #PB_Absolute)
      MoveNode(n(3),-nsize, -nsize, nsize, #PB_Absolute)
  EndSelect
  AttachNodeObject(mainnode, NodeID(n(0)))
  AttachNodeObject(mainnode, NodeID(n(1)))
  AttachNodeObject(mainnode, NodeID(n(2)))
  AttachNodeObject(mainnode, NodeID(n(3)))
  ProcedureReturn #True
EndProcedure
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Comtois
Addict
Addict
Posts: 1429
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: 3d point and line graphs in PB

Post by Comtois »

You can convert vertices position using ConvertLocalToWorldPosition() --> look at example "ConvertLocalToWorldPosition.pb" (C1 et C2 are vertices position)

once you get world position, you can get xy using CameraProjection --> look at example "CameraProjection.pb"
Please correct my english
http://purebasic.developpez.com/
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: 3d point and line graphs in PB

Post by DK_PETER »

Comtois wrote:You can convert vertices position using ConvertLocalToWorldPosition() --> look at example "ConvertLocalToWorldPosition.pb" (C1 et C2 are vertices position)

once you get world position, you can get xy using CameraProjection --> look at example "CameraProjection.pb"
I've never had a use for such a task - which is why it never occured to me to use CameraProjection, but I think I get it.
It shows that there's a nice solution to almost every task.
Me thinks I need to go through the examples once again. It's been far too long. :)
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: 3d point and line graphs in PB

Post by applePi »

i have suspected (somewhat) that doctornash means something like this, but can't remember at all what it is exactly, but i have eliminated this possibility since doctornash have posted the first time in the 3D section. searching my poor memory i find a forum subject which have used CameraProjecttion:
Edit Mesh with Mouse and Keyboard:
viewtopic.php?f=36&t=66555
after that i never come to the subject again.
but your example DK_PETER is nice and useful and relating to the general theme of this thread.
thanks also to Comtois
doctornash
Enthusiast
Enthusiast
Posts: 130
Joined: Thu Oct 20, 2011 7:22 am

Re: 3d point and line graphs in PB

Post by doctornash »

Thanks for your help! I've modified DK_PETER's code to display the (x,y) pixel coords of the 8 cube vertices (or at least I think I have). Applied CameraProjection to the 3D coords of the nodes like this:

Code: Select all

 Debug Str(x) + ";" + StrF(CameraProjectionX(0, NodeX(n(x)), NodeY(n(x)), NodeZ(n(x)))) + ";" +  StrF(CameraProjectionY(0, NodeX(n(x)), NodeY(n(x)), NodeZ(n(x))))
Didn't have to do ConvertLocalToWorldPosition() first in this case as the Node coords were already 'world positions'?

And yes, the way that a user can control the rate at which the cube rotates around each of the axes, and when it does so, is what I was imagining when I asked the question :D

Along the same lines, might I be so bold as to request an example of a simple shape 'created manually' (like a rotating triangular pyramid for instance) - to act as a basis for constructing more complex polyhedrons of my own?

Code: Select all

Structure _vrot
  rox.f
  roy.f
  roz.f
EndStructure

Declare.i AttachNodes(CubeSize.f)

Global Dim n.i(7) 
Global mainnode.i, el.i, r._vrot

If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  
  OpenWindow(0, 0, 0, 800, 620, "Rotating cube")
  OpenWindowedScreen(WindowID(0), 10, 10, 600,600, 0, 0, 0)

  CreateTexture(0, 1024, 1024)
  StartDrawing(TextureOutput(0))
  RoundBox(4, 4, 1016, 1016, 10, 10, $26FA22)
  RoundBox(24, 24, 976, 976, 10, 10, $0)
  StopDrawing()
  CreateMaterial(0, TextureID(0))
  MaterialCullingMode(0,#PB_Material_NoCulling)
  MaterialBlendingMode(0, #PB_Material_Add)

  CreateCube(0, 55)
  CreateEntity(0, MeshID(0), MaterialID(0))
  mainnode = CreateNode(#PB_Any,0, 0, 0)
  AttachNodeObject(mainnode, EntityID(0))
  
  For x.i = 0 To 7 : n(x) = CreateNode(#PB_Any) : Next x
  
  AttachNodes(55) 
  
  ;-Camera
  CreateCamera(0, 0, 0, 100, 100)
  MoveCamera(0, 0, 0, 150, #PB_Absolute)
  CameraBackColor(0, RGB(0, 0, 40))


  r\rox = -0.2 : r\roy = 0.2 : r\roz = 0.4
  
  Repeat
    Repeat
      Event = WindowEvent()
    Until Event = 0
    ExamineKeyboard()
    For x = 0 To 7
      Debug Str(x) + ";" + StrF(CameraProjectionX(0, NodeX(n(x)), NodeY(n(x)), NodeZ(n(x)))) + ";" +  StrF(CameraProjectionY(0, NodeX(n(x)), NodeY(n(x)), NodeZ(n(x))))
    Next 
    
      r\rox = 1 : r\roy = 0 : r\roz = 0.5
      
    RotateNode(mainnode, r\rox, r\roy, r\roz, #PB_Relative)
    
    RenderWorld()
    
    FlipBuffers()
    
  Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
EndIf

End

Procedure.i AttachNodes(CubeSize.f)
  Protected x.i, nsize.f = CubeSize/2
  For x = 0 To 7 : DetachNodeObject(n(x), NodeID(mainnode)) : Next x

      MoveNode(n(0),-nsize, nsize, nsize, #PB_Absolute)
      MoveNode(n(1),nsize, nsize, nsize, #PB_Absolute)
      MoveNode(n(2),nsize, -nsize, nsize, #PB_Absolute)
      MoveNode(n(3),-nsize, -nsize, nsize, #PB_Absolute)

      MoveNode(n(4),-nsize, nsize, -nsize, #PB_Absolute)
      MoveNode(n(5),nsize, nsize, -nsize, #PB_Absolute)
      MoveNode(n(6),nSize, -nSize, -nSize, #PB_Absolute)
      MoveNode(n(7),-nsize, -nsize, -nsize, #PB_Absolute)

  AttachNodeObject(mainnode, NodeID(n(0)))
  AttachNodeObject(mainnode, NodeID(n(1)))
  AttachNodeObject(mainnode, NodeID(n(2)))
  AttachNodeObject(mainnode, NodeID(n(3)))
  AttachNodeObject(mainnode, NodeID(n(4)))
  AttachNodeObject(mainnode, NodeID(n(5)))
  AttachNodeObject(mainnode, NodeID(n(6)))
  AttachNodeObject(mainnode, NodeID(n(7)))
  
  ProcedureReturn #True
EndProcedure
User avatar
Comtois
Addict
Addict
Posts: 1429
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: 3d point and line graphs in PB

Post by Comtois »

doctornash wrote:Along the same lines, might I be so bold as to request an example of a simple shape 'created manually' (like a rotating triangular pyramid for instance) - to act as a basis for constructing more complex polyhedrons of my own?

May be this can help ?

Code: Select all

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    ; Create a pyramid, manually
    ;
    
    CreateMesh(0, #PB_Mesh_TriangleList)
    
    ;Base
    MeshVertexPosition(-0.5,-0.5,0.5)
    MeshVertexNormal(0,0,0)
    MeshVertexTextureCoordinate(0,0)
    MeshVertexPosition(0.5,-0.5,0.5)
    MeshVertexNormal(0,0,0)
    MeshVertexTextureCoordinate(0,1)  
    MeshVertexPosition(0.5,-0.5,-0.5)
    MeshVertexNormal(0,0,0)
    MeshVertexTextureCoordinate(1,1)       
    MeshVertexPosition(-0.5,-0.5,-0.5)
    MeshVertexNormal(0,0,0)
    MeshVertexTextureCoordinate(1,0)  
    
    MeshFace(2,1,0)   
    MeshFace(0,3,2) 
    
    ;-Front
    AddSubMesh(#PB_Mesh_TriangleList)
    MeshVertexPosition(0.5,-0.5,0.5)
    MeshVertexNormal(0,0,0)
    MeshVertexTextureCoordinate(1,0)
    MeshVertexPosition(0.0,0.5,0.0)
    MeshVertexNormal(0,0,0)
    MeshVertexTextureCoordinate(0.5,0.5)           
    MeshVertexPosition(-0.5,-0.5,0.5)
    MeshVertexNormal(0,0,0)
    MeshVertexTextureCoordinate(0,0) 
    
    MeshFace(0,1,2)
    
    ;-Back
    AddSubMesh(#PB_Mesh_TriangleList)
    MeshVertexPosition(-0.5,-0.5,-0.5)
    MeshVertexNormal(0,0,0)
    MeshVertexTextureCoordinate(0,1) 
    MeshVertexPosition(0.0,0.5,0.0)
    MeshVertexNormal(0,0,0)
    MeshVertexTextureCoordinate(0.5,0.5) 
    MeshVertexPosition(0.5,-0.5,-0.5)
    MeshVertexNormal(0,0,0)
    MeshVertexTextureCoordinate(1,1) 
    
    MeshFace(0,1,2)
    
    ;-Left
    AddSubMesh(#PB_Mesh_TriangleList)
    MeshVertexPosition(-0.5,-0.5,0.5)
    MeshVertexNormal(0,0,0)
    MeshVertexTextureCoordinate(0,0) 
    MeshVertexPosition(0.0,0.5,0.0)
    MeshVertexNormal(0,0,0)
    MeshVertexTextureCoordinate(0.5,0.5) 
    MeshVertexPosition(-0.5,-0.5,-0.5)
    MeshVertexNormal(0,0,0)
    MeshVertexTextureCoordinate(0,1) 
    
    MeshFace(0,1,2)
    
    ;-Right
    AddSubMesh(#PB_Mesh_TriangleList)
    MeshVertexPosition(0.5,-0.5,-0.5)
    MeshVertexNormal(0,0,0)
    MeshVertexTextureCoordinate(1,1)
    MeshVertexPosition(0.0,0.5,0.0)
    MeshVertexNormal(0,0,0)
    MeshVertexTextureCoordinate(0.5,0.5)
    MeshVertexPosition(0.5,-0.5,0.5)
    MeshVertexNormal(0,0,0)
    MeshVertexTextureCoordinate(1,0)
    
    MeshFace(0,1,2)
    
    FinishMesh(#True)
    NormalizeMesh(0) 
    
    CreateMaterial(0, LoadTexture(0, "Geebee2.bmp"))
        
    CreateEntity(0, MeshID(0), MaterialID(0))
    ScaleEntity(0, 1, 0.5, 1)
    
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 2, #PB_Absolute)
    
    CreateLight(0, RGB(255,255,255), 300, 600, -100)
    AmbientColor(RGB(80, 80, 80))
    
    Repeat
      Screen3DEvents()
      
      ExamineMouse()
      
      ExamineKeyboard()
              
      RotateEntity(0, 1, 1, 1, #PB_Relative)
            
      RenderWorld()
      Screen3DStats()      
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End
Please correct my english
http://purebasic.developpez.com/
doctornash
Enthusiast
Enthusiast
Posts: 130
Joined: Thu Oct 20, 2011 7:22 am

Re: 3d point and line graphs in PB

Post by doctornash »

Thanks Comtois, yes I found that pyramid example too not long before you posted! :mrgreen:

In an attempt to try to get the pyramid to look like DK_Peter's transparent cube with the green edges, I commented out a couple of the 'material' lines in the pyramid example and pasted DK_Peter's 'material' code there instead. This has resulted in only the base looking green edged like it should, but the rest of the pyramid has disappeared. How to have that look correctly applied to all faces?

Code: Select all

#CameraSpeed = 1

IncludeFile #PB_Compiler_Home + "examples/3d/Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY

Define.f x, y, z, nx, ny, nz, u, v
Define.l Co
Define.w t1, t2, t3

If InitEngine3D()
  
  Add3DArchive(#PB_Compiler_Home + "examples/3d/Data/Textures", #PB_3DArchive_FileSystem)
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    ; Create a pyramid, manually.. See the DataSection, for more precisions
    ;
    
    Restore Pyramid
    
    CreateMesh(0, #PB_Mesh_TriangleList)
    
    ;Base
    For i = 0 To 3
      Read.f x : Read.f y : Read.f z
      Read.l Co
      Read.f u : Read.f v
      MeshVertexPosition(x, y, z)
      MeshVertexNormal(0, 0, 0)
      MeshVertexColor(Co)
      MeshVertexTextureCoordinate(u, v)
    Next
    
    For i = 0 To 1
      Read.w t1 : Read.w t2 : Read.w t3
      MeshFace(t1, t2, t3)
    Next
    
    ;Side
    For k=0 To 3
      
      AddSubMesh(#PB_Mesh_TriangleList)
      For i = 0 To 2
        Read.f x : Read.f y : Read.f z
        Read.l Co
        Read.f u : Read.f v
        MeshVertexPosition(x, y, z)
        MeshVertexNormal(0, 0, 0) 
        MeshVertexColor(Co)
        MeshVertexTextureCoordinate(u, v)
      Next i
      Read.w t1 : Read.w t2 : Read.w t3
      MeshFace(t1, t2, t3)
      
    Next
    
    FinishMesh(#True)
    NormalizeMesh(0) 
    
    UpdateMeshBoundingBox(0)
    
  ;CreateMaterial(0, LoadTexture(0, "Geebee2.bmp"))
  ;SetMaterialColor(0, #PB_Material_AmbientColor, #PB_Material_AmbientColors)
    
  CreateTexture(0, 1024, 1024)
  StartDrawing(TextureOutput(0))
  RoundBox(4, 4, 1016, 1016, 10, 10, $26FA22)
  RoundBox(24, 24, 976, 976, 10, 10, $0)
  StopDrawing()
  CreateMaterial(0, TextureID(0))
  MaterialCullingMode(0,#PB_Material_NoCulling)
  MaterialBlendingMode(0, #PB_Material_Add)
    
    
    CreateEntity(0, MeshID(0), MaterialID(0))
    ScaleEntity(0, 400, 200, 400)
    
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, 0, 0, 1000, #PB_Absolute)
    
    CreateLight(0, RGB(255,255,255), 300, 600, -100)
    AmbientColor(RGB(80, 80, 80))
    
    Repeat
      Screen3DEvents()
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
      EndIf
      
      If ExamineKeyboard()
        
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed 
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed 
        Else
          KeyY = 0
        EndIf
        
      EndIf
      
      RotateEntity(0, 1, 0, 0, #PB_Relative)
      
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      
      RenderWorld()
      Screen3DStats()      
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

DataSection
  Pyramid:
  ;Base
  Data.f -0.5,-0.5,0.5  ; position
  Data.l $FF0000        ; color
  Data.f 0,0            ; UVCoordinate
  
  Data.f 0.5,-0.5,0.5   ; position
  Data.l $FF0000        ; color 
  Data.f 0,1            ; UVCoordinate
  
  Data.f 0.5,-0.5,-0.5  ; position
  Data.l $FF0000        ; color
  Data.f 1,1            ; UVCoordinate
  
  Data.f -0.5,-0.5,-0.5 ; position
  Data.l $FF0000        ; color
  Data.f 1,0            ; UVCoordinate
  
  Data.w 2,1,0          ; Face 
  Data.w 0,3,2          ; Face 
  
  ;-Front
  Data.f 0.5,-0.5,0.5   ; position
  Data.l $FFFFFF        ; color
  Data.f 1,0            ; UVCoordinate
  
  Data.f 0.0,0.5,0.0
  Data.l $FFFFFF
  Data.f 0.5,0.5
  
  Data.f -0.5,-0.5,0.5
  Data.l $FFFFFF
  Data.f 0,0
  
  Data.w 0,1,2         ; Face
  
  ;-Back
  Data.f -0.5,-0.5,-0.5
  Data.l $FFFFFF
  Data.f 0,1
  
  Data.f 0.0,0.5,0.0
  Data.l $FFFFFF
  Data.f 0.5,0.5
  
  Data.f 0.5,-0.5,-0.5
  Data.l $FFFFFF
  Data.f 1,1
  
  Data.w 0,1,2
  
  ;-Left
  Data.f -0.5,-0.5,0.5
  Data.l $FFFFFF
  Data.f 0,0
  
  Data.f 0.0,0.5,0.0
  Data.l $FFFFFF
  Data.f 0.5,0.5
  
  Data.f -0.5,-0.5,-0.5
  Data.l $FFFFFF
  Data.f 0,1
  
  Data.w 0,1,2
  
  ;-Right
  Data.f 0.5,-0.5,-0.5
  Data.l $FFFFFF
  Data.f 1,1
  
  Data.f 0.0,0.5,0.0
  Data.l $FFFFFF
  Data.f 0.5,0.5
  
  Data.f 0.5,-0.5,0.5
  Data.l $FFFFFF
  Data.f 1,0
  
  Data.w 0,1,2
  
EndDataSection
Post Reply