Cube in OpenGL

Everything related to 3D programming
threedslider
User
User
Posts: 87
Joined: Sat Feb 12, 2022 7:15 pm

Cube in OpenGL

Post by threedslider »

Hi all,

I am new here in english forum and I would like to share to you my old code as posted in french forum :)

Hope you like it.

Happy coding !

Code: Select all

; Created and improved by threedslider 12/28/2021

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  CompilerIf Subsystem("OpenGL") = #False
    MessageRequester("Error", "Please set the subsystem to OpenGL")
    End
  CompilerEndIf
CompilerEndIf

InitSprite()
InitKeyboard()


OpenWindow(0, 0, 0, 800, 600, "OpenGL : Cube for test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)


glMatrixMode_(#GL_PROJECTION)
glLoadIdentity_() 
gluPerspective_(45.0, 800/600, 1.0, 60.0)
glMatrixMode_(#GL_MODELVIEW)
glTranslatef_(-1/4, 0, -4)
glShadeModel_(#GL_SMOOTH)
glEnable_(#GL_LIGHT0)
glEnable_(#GL_LIGHTING)
glEnable_(#GL_COLOR_MATERIAL)
glEnable_(#GL_DEPTH_TEST)
glEnable_(#GL_CULL_FACE)   
glViewport_(0, 0, 800, 600)


Repeat
glClearColor_(0.7, 0.7, 0.7, 0) ; background color
glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  Repeat
    event = WindowEvent()
    Select Event
      Case #PB_Event_CloseWindow
        quit = 1
     EndSelect
   Until event = 0
   
  
   
   glBegin_(#GL_QUADS)
    glColor3f_(0.3, 0.3, 1.0)
    glNormal3f_( 0.0, 0.0, 1.0)
    glVertex3f_( 0.5, 0.5, 0.5) 
    glVertex3f_(-0.5, 0.5, 0.5)
    glVertex3f_(-0.5,-0.5, 0.5) 
    glVertex3f_( 0.5,-0.5, 0.5)
    
    glColor3f_(0.0, 0.0, 1.0)
    glNormal3f_( 0.0, 0.0,-1.0)
    glVertex3f_(-0.5,-0.5,-0.5) 
    glVertex3f_(-0.5, 0.5,-0.5)
    glVertex3f_( 0.5, 0.5,-0.5) 
    glVertex3f_( 0.5,-0.5,-0.5)
    
    glColor3f_(1.0, 0.0, 0.0)
    glNormal3f_( 0.0, 1.0, 0.0)
    glVertex3f_( 0.5, 0.5, 0.5) 
    glVertex3f_( 0.5, 0.5,-0.5)
    glVertex3f_(-0.5, 0.5,-0.5)
    glVertex3f_(-0.5, 0.5, 0.5)
    
    glColor3f_(0.0, 1.0, 0.0)
    glNormal3f_( 0.0,-1.0, 0.0)
    glVertex3f_(-0.5,-0.5,-0.5)
    glVertex3f_( 0.5,-0.5,-0.5)
    glVertex3f_( 0.5,-0.5, 0.5) 
    glVertex3f_(-0.5,-0.5, 0.5)
    
    glColor3f_(1.0, 0.0, 1.0)
    glNormal3f_( 1.0, 0.0, 0.0)
    glVertex3f_( 0.5, 0.5, 0.5) 
    glVertex3f_( 0.5,-0.5, 0.5)
    glVertex3f_( 0.5,-0.5,-0.5) 
    glVertex3f_( 0.5, 0.5,-0.5)
    
    glColor3f_(1.0, 1.0, 0.0)
    glNormal3f_(-1.0, 0.0, 0.0)
    glVertex3f_(-0.5,-0.5,-0.5) 
    glVertex3f_(-0.5,-0.5, 0.5)
    glVertex3f_(-0.5, 0.5, 0.5) 
    glVertex3f_(-0.5, 0.5,-0.5)
  glEnd_();
		
	glTranslatef_(1/4,1/4,0)
	glRotatef_(1.0, 0.5, 1.0, 0.2)
	glTranslatef_(-1/4,-1/4,0)
	
 
 FlipBuffers()
 
 ExamineKeyboard()
 
Until KeyboardPushed(#PB_Key_Escape) Or quit = 1
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 627
Joined: Fri Dec 04, 2015 9:26 pm

Re: Cube in OpenGL

Post by skinkairewalker »

thanks :D
threedslider
User
User
Posts: 87
Joined: Sat Feb 12, 2022 7:15 pm

Re: Cube in OpenGL

Post by threedslider »

You are welcome ;)
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Cube in OpenGL

Post by davido »

@threedslider,
Thank you, very much.
I once wondered how to do this, now I know. :D

Worked straight off on my MacBook m1, PB-5.73 LTS.
DE AA EB
threedslider
User
User
Posts: 87
Joined: Sat Feb 12, 2022 7:15 pm

Re: Cube in OpenGL

Post by threedslider »

davido wrote: Sun Feb 13, 2022 7:44 pm @threedslider,
Thank you, very much.
I once wondered how to do this, now I know. :D

Worked straight off on my MacBook m1, PB-5.73 LTS.
Glad that it worked for you and in hope I will make more for stuff with OpenGL ;)
Post Reply