Demo - Cubes

Everything related to 3D programming
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Demo - Cubes

Post by firace »

I like cubes 8)

Image

Code: Select all



; text$ = " oo "
 text$ = "pure"
; text$ = "peek"
; text$ = "cube"
; text$ = "1337"

;  wireframeMode = 1


Procedure.s BuildPixelString(String$, Char$="*", font.i=#PB_Ignore)   ;;; (infratec)
  
  Protected Result$, Line$, Img.i, StringWidth.i, StringHeight.i, y.i, x.i
  
  Img = CreateImage(#PB_Any, 1, 1)
  If Img
    If StartDrawing(ImageOutput(Img))
      DrawingFont(FontID(font))
      StringWidth = TextWidth(String$)
      StringHeight = TextHeight(String$)
      StopDrawing()
      FreeImage(Img)
      
      
      Img = CreateImage(#PB_Any, StringWidth, StringHeight)
      If Img
        If StartDrawing(ImageOutput(Img))
          DrawingFont(FontID(font))
          DrawText(0, 0, String$, $FFFFFF)
          For y = StringHeight - 1 To 0 Step -1 
            Line$ = ""
            For x = 0 To StringWidth - 1
              If Point(x, y) = 0
                Line$ + " "
              Else
                Line$ + Char$
              EndIf
            Next x
            Line$ = LSet(Line$, StringWidth, " ")
            Result$ + Line$ + #CRLF$
          Next y
          StopDrawing()
        EndIf
        FreeImage(Img)
      EndIf
    EndIf
  EndIf
  
  ProcedureReturn Result$
  
EndProcedure

Procedure.s pixelON(f$, X, Y)
  
  ProcedureReturn Mid(StringField(f$, Y + 1, #CRLF$), X + 1, 1)
  
EndProcedure


LoadFont(16, "Consolas", 12)

f$ = BuildPixelString(text$, "*", 16)  


InitEngine3D() : InitSprite()
InitKeyboard()

Structure Cubes
  ms.i
  ma.i
  tx.i
  id.i
  direction.i 
EndStructure

#NbrCubesX = 40
#NbrCubesY = 24
speed.f = 1.6   

Dim cubes.Cubes(#NbrCubesX, #NbrCubesY)

ExamineDesktops()

OpenWindow(0, 0, 0, 10, 10, "Cubes", #PB_Window_ScreenCentered|#PB_Window_Maximize|#PB_Window_BorderLess)
OpenWindowedScreen(WindowID(0), 10,10, DesktopWidth(0)-20, DesktopHeight(0)-20, #True, 0, 0)
CreateCamera(0, 0, 0, 100, 100)

MoveCamera(0, 10.5, 3.5, 0)

if wireframeMode = 1
  CameraRenderMode(0, #PB_Camera_Wireframe)
endif 

CreateLight(0, $FFFFFF, -3, -5, 0)

For y = 0 To #NbrCubesY
  For x = 0 To #NbrCubesX
    With Cubes(x,y)
      col = RGB(Random(255, 120), Random(255,120), Random(255, 120))
      \tx = CreateTexture(#PB_Any, 10, 10)
      StartDrawing(TextureOutput(\tx))
      Box(0, 0, 10, 10, col)
      
      StopDrawing()
      \ma = CreateMaterial(#PB_Any, TextureID(\tx)) 
      \ms = CreateCube(#PB_Any, 0.3)
      
      If PixelON(f$, x, y) = "*"       
        \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), x * 0.6, y * 0.6, -16)
      EndIf
      \direction = Random(50)
    EndWith
  Next x
Next y


Repeat
  Repeat : ev = WindowEvent() : Until ev = 0
  For y = 0 To #NbrCubesY
    For x = 0 To #NbrCubesX
      With cubes(x,y)
        If \id
          Select \direction
            Case 0 To 10
              RotateEntity(\id, speed, speed, speed, #PB_Relative)
            Case 11 To 20
              RotateEntity(\id, -speed, speed, speed, #PB_Relative)
            Case 21 To 30
              RotateEntity(\id, speed, speed, -speed, #PB_Relative)
            Case 31 To 40
              RotateEntity(\id, speed, -speed, speed, #PB_Relative)
            Default
              RotateEntity(\id, -speed, -speed, -speed, #PB_Relative)
          EndSelect
        EndIf
      EndWith
    Next x
  Next y
  
  RenderWorld()
  FlipBuffers()
  ExamineKeyboard()
  
Until KeyboardPushed(#PB_Key_Escape)

Last edited by firace on Mon Oct 30, 2023 9:50 pm, edited 3 times in total.
juergenkulow
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 25, 2019 10:18 am

Re: Demo - Cubes

Post by juergenkulow »

Code: Select all

 CompilerIf #PB_Compiler_OS=#PB_OS_Linux
   Structure Point
     x.i
     y.i
   EndStructure
 CompilerEndIf 
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Demo - Cubes

Post by Oso »

Very good, though it displays only a portion of the text for me. Reducing the font size moves it further to the left and displays fewer characters.

Image
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: Demo - Cubes

Post by firace »

Hmmm if someone could help center the text properly it would be great!
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: Demo - Cubes

Post by firace »

juergenkulow wrote: Mon Oct 30, 2023 11:22 am

Code: Select all

 CompilerIf #PB_Compiler_OS=#PB_OS_Linux
   Structure Point
     x.i
     y.i
   EndStructure
 CompilerEndIf 
Thanks, I've removed the reference to Point altogether, it wasn't needed.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Demo - Cubes

Post by Oso »

firace wrote: Mon Oct 30, 2023 11:45 am Hmmm if someone could help center the text properly it would be great!
I noticed that you were taking into account the size of the desktop here, but not sure where you are calculating the position for the text. Presumably you've calculated it based on your particular monitor size?

Code: Select all

OpenWindowedScreen(WindowID(0), 10,10, DesktopWidth(0)-20, DesktopHeight(0)-20, #True, 0, 0)
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: Demo - Cubes

Post by firace »

Oso wrote: Mon Oct 30, 2023 12:09 pm
firace wrote: Mon Oct 30, 2023 11:45 am Hmmm if someone could help center the text properly it would be great!
I noticed that you were taking into account the size of the desktop here, but not sure where you are calculating the position for the text. Presumably you've calculated it based on your particular monitor size?

Code: Select all

OpenWindowedScreen(WindowID(0), 10,10, DesktopWidth(0)-20, DesktopHeight(0)-20, #True, 0, 0)
I made this code a while ago... Seems I'm calculating positions (very poorly) in this line:

Code: Select all

        \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), basex + x * 0.6, basey + y * 0.6, -16)
But it would probably be smarter and easier to just center the whole scene?
But I have no clue how :)
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Demo - Cubes

Post by Oso »

firace wrote: Mon Oct 30, 2023 12:15 pm I made this code a while ago... Seems I'm calculating positions (very poorly) in this line:

Code: Select all

        \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), basex + x * 0.6, basey + y * 0.6, -16)
But it would probably be smarter and easier to just center the whole scene? But I have no clue how :)
Ah, okay it just requires a way to calculate the positions. I just had a quick try at changing the 0.6 to 0.4 and there's an interesting result. I'll keep trying :D

Image

EDIT: Actually I'm not getting the same result as before, even with the values returned to 0.6. Has the original code been changed?
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: Demo - Cubes

Post by firace »

Oso wrote: Mon Oct 30, 2023 12:35 pm
firace wrote: Mon Oct 30, 2023 12:15 pm I made this code a while ago... Seems I'm calculating positions (very poorly) in this line:

Code: Select all

        \id = CreateEntity(#PB_Any, MeshID(\ms), MaterialID(\ma), basex + x * 0.6, basey + y * 0.6, -16)
But it would probably be smarter and easier to just center the whole scene? But I have no clue how :)
Ah, okay it just requires a way to calculate the positions. I just had a quick try at changing the 0.6 to 0.4 and there's an interesting result. I'll keep trying :D

Image

EDIT: Actually I'm not getting the same result as before, even with the values returned to 0.6. Has the original code been changed?
Yes and I've just updated the code again :)
To make it simpler, position is now defined in this line:

Code: Select all

MoveCamera(0, 10.5, 3.5, 0)


But these are values are still manually chosen so that text is centered on my screen - so of course it would be better to calculate these values dynamically...

BTW, this is how the rendering is supposed to look:

Image

What version of PB do you have?
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Demo - Cubes

Post by Oso »

firace wrote: Mon Oct 30, 2023 12:56 pm What version of PB do you have?
I'm using PB 6.00. The results are more central now.

Image
User avatar
Caronte3D
Addict
Addict
Posts: 1361
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Demo - Cubes

Post by Caronte3D »

Oso, the cubes looks distorted on your screen
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Demo - Cubes

Post by Oso »

Caronte3D wrote: Mon Oct 30, 2023 3:14 pm Oso, the cubes looks distorted on your screen
Yes, they don't have solid sides, although they look better while they are rotating. I see identical results between Windows Server 2016 and also on a separate Windows 10 machine. The latter is via RDP, so it's understandably very slow.
User avatar
SPH
Enthusiast
Enthusiast
Posts: 571
Joined: Tue Jan 04, 2011 6:21 pm

Re: Demo - Cubes

Post by SPH »

Good 3D 🥰

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: Demo - Cubes

Post by firace »

Oso wrote: Mon Oct 30, 2023 3:19 pm
Caronte3D wrote: Mon Oct 30, 2023 3:14 pm Oso, the cubes looks distorted on your screen
Yes, they don't have solid sides, although they look better while they are rotating. I see identical results between Windows Server 2016 and also on a separate Windows 10 machine. The latter is via RDP, so it's understandably very slow.
Strange, perhaps a graphics driver issue?
For me it displays fine both on Windows 10 and Ubuntu (on separate ASUS laptops)
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Demo - Cubes

Post by Oso »

firace wrote: Mon Oct 30, 2023 9:45 pm Strange, perhaps a graphics driver issue? For me it displays fine both on Windows 10 and Ubuntu (on separate ASUS laptops)
You are probably correct firace, so I wouldn't be concerned that there's something wrong with your code. My Windows Server 2016 machine is using Microsoft's basic display driver, a generic driver. The other machine is a Linux CentOS server with Windows 10 running as a virtual machine, hence the graphics may be limited to some extent by that. A third machine that I tried while going to sleep last night :D is a Linux laptop, again with the same Windows 10 virtual machine guest, so none of these are ideal tests. I don't do graphics normally — perhaps why I was keen to try it.
Post Reply