OpenGL rotae Cube in Cube wiht toggling transparency

Share your advanced PureBasic knowledge/code with the community.
SMaag
Enthusiast
Enthusiast
Posts: 385
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

OpenGL rotae Cube in Cube wiht toggling transparency

Post by SMaag »

I played a little with the OpenGL to get firm with it.
With the help of ChatGPT, the rusult is a Cube in Cube rotation with toggleable transparency.

Code: Select all

; Demo OpenGL rotate a Cube in a Cube with toggleable transparency

EnableExplicit

Enumeration eMenu
  #Menu_Main
  #Menu_Transparency
EndEnumeration

Global AngleOuter.f = 0.0
Global AngleInner.f = 0.0
Global width = 800
Global height = 600
Global Flag_Transparent = #True

; IIFn with Numeric retrurn 
; use it: numVar = IIF(a=b, 1, 2)
Macro IIFn(_Expression, _TruePart, _FalsePart)
  (Bool(_Expression) * _TruePart + Bool(Not(_Expression)) * _FalsePart)    
EndMacro

Procedure DrawInnerCube()
  ; --------------------------
  ; INNER SOLID CUBE
  ; --------------------------
  
  glPushMatrix_()

  glRotatef_(AngleInner, 1.0, 0.0, 1.0)
  
  ; Scale cube 20% bigger (previously 0.6)
  glScalef_(0.72, 0.72, 0.72)  ; 0.6 * 1.2 = 0.72

  glDisable_(#GL_BLEND)
  glDepthMask_(#GL_TRUE)

  glBegin_(#GL_QUADS)

    glColor3f_(1,0.5,0)
    glVertex3f_(-1,-1, 1)
    glVertex3f_( 1,-1, 1)
    glVertex3f_( 1, 1, 1)
    glVertex3f_(-1, 1, 1)
  
    glColor3f_(0,1,0.5)
    glVertex3f_(-1,-1,-1)
    glVertex3f_(-1, 1,-1)
    glVertex3f_( 1, 1,-1)
    glVertex3f_( 1,-1,-1)
  
    glColor3f_(0.5,0,1)
    glVertex3f_(-1,-1,-1)
    glVertex3f_(-1,-1, 1)
    glVertex3f_(-1, 1, 1)
    glVertex3f_(-1, 1,-1)
  
    glColor3f_(1,1,0)
    glVertex3f_(1,-1,-1)
    glVertex3f_(1, 1,-1)
    glVertex3f_(1, 1, 1)
    glVertex3f_(1,-1, 1)
  
    glColor3f_(1,0,1)
    glVertex3f_(-1,1,-1)
    glVertex3f_(-1,1, 1)
    glVertex3f_( 1,1, 1)
    glVertex3f_( 1,1,-1)
  
    glColor3f_(0,1,1)
    glVertex3f_(-1,-1,-1)
    glVertex3f_( 1,-1,-1)
    glVertex3f_( 1,-1, 1)
    glVertex3f_(-1,-1, 1)

  glEnd_()

  glPopMatrix_()
  
EndProcedure

Procedure DrawOuterCube(xTransparent=#True)
  ; -------------------------------------
  ; OUTER CUBE (TOGGLEABLE TRANXPARENCY)
  ; -------------------------------------
  
  Protected ColAlpha.f
  
  glPushMatrix_()
  
  glRotatef_(AngleOuter, 1.0, 1.0, 0.0)

  If xTransparent
    glEnable_(#GL_BLEND)
    glDepthMask_(#GL_FALSE)
  Else
    glDisable_(#GL_BLEND)
    glDepthMask_(#GL_TRUE)
  EndIf

  glBegin_(#GL_QUADS)
  
    ColAlpha = IIFn(xTransparent, 0.4, 1.0)
    ; Front
    glColor4f_(1,0,0, ColAlpha)
    glVertex3f_(-1,-1, 1)
    glVertex3f_( 1,-1, 1)
    glVertex3f_( 1, 1, 1)
    glVertex3f_(-1, 1, 1)
  
    ; Back
    glColor4f_(0,1,0, ColAlpha)
    glVertex3f_(-1,-1,-1)
    glVertex3f_(-1, 1,-1)
    glVertex3f_( 1, 1,-1)
    glVertex3f_( 1,-1,-1)
  
    ; Left
    glColor4f_(0,0,1, ColAlpha)
    glVertex3f_(-1,-1,-1)
    glVertex3f_(-1,-1, 1)
    glVertex3f_(-1, 1, 1)
    glVertex3f_(-1, 1,-1)
  
    ; Right
    glColor4f_(1,1,0, ColAlpha)
    glVertex3f_( 1,-1,-1)
    glVertex3f_( 1, 1,-1)
    glVertex3f_( 1, 1, 1)
    glVertex3f_( 1,-1, 1)
  
    ; Top
    glColor4f_(1,0,1, ColAlpha)
    glVertex3f_(-1, 1,-1)
    glVertex3f_(-1, 1, 1)
    glVertex3f_( 1, 1, 1)
    glVertex3f_( 1, 1,-1)
  
    ; Bottom
    glColor4f_(0,1,1, ColAlpha)
    glVertex3f_(-1,-1,-1)
    glVertex3f_( 1,-1,-1)
    glVertex3f_( 1,-1, 1)
    glVertex3f_(-1,-1, 1)

  glEnd_()

  glDepthMask_(#GL_TRUE)

  glPopMatrix_()
  
EndProcedure

If OpenWindow(0, 100, 100, width, height, "OpenGL rotate Cube in Cube", #PB_Window_SystemMenu)

  ; -------- MENU --------
  If CreateMenu(#Menu_Main, WindowID(0))
    MenuTitle("Flags")
    MenuItem(#Menu_Transparency, "Transparency")
    SetMenuItemState(#Menu_Main, #Menu_Transparency, #True)
  EndIf

  OpenGLGadget(0, 0, 0, width, height, #PB_OpenGL_Keyboard)

  ; --- OpenGL Setup ---
  glEnable_(#GL_DEPTH_TEST)
  glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA)

  Repeat
    Define Event = WindowEvent()

    ; -------- MENU EVENTS --------
    If Event = #PB_Event_Menu
      Select EventMenu()
        Case #Menu_Transparency
          Flag_Transparent ! 1
          SetMenuItemState(#Menu_Main, #Menu_Transparency, Flag_Transparent)
      EndSelect
    EndIf

    glViewport_(0, 0, width, height)

    glClearColor_(0.05, 0.05, 0.1, 1.0)
    glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)

    ; -------- Projection --------
    glMatrixMode_(#GL_PROJECTION)
    glLoadIdentity_()
    gluPerspective_(45.0, width/height, 0.1, 100.0)

    ; -------- ModelView --------
    glMatrixMode_(#GL_MODELVIEW)
    glLoadIdentity_()
    glTranslatef_(0.0, 0.0, -6.0)

    DrawInnerCube()
    DrawOuterCube(Flag_Transparent)
    
    ; Animate
    AngleOuter + 0.6
    AngleInner - 1.0

    SetGadgetAttribute(0, #PB_OpenGL_FlipBuffers, #True)

  Until Event = #PB_Event_CloseWindow

EndIf

threedslider
Enthusiast
Enthusiast
Posts: 610
Joined: Sat Feb 12, 2022 7:15 pm

Re: OpenGL rotae Cube in Cube wiht toggling transparency

Post by threedslider »

Nice work @SMaag !

Thanks for sharing.
davidow
User
User
Posts: 13
Joined: Mon Mar 20, 2023 7:34 am
Location: Uttoxeter, UK

Re: OpenGL rotae Cube in Cube wiht toggling transparency

Post by davidow »

@SMaag,

Very nice.
Thank you for sharing.
miso
Enthusiast
Enthusiast
Posts: 712
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: OpenGL rotae Cube in Cube wiht toggling transparency

Post by miso »

Cool example, thanks for sharing!
Post Reply