Spinning Pyramid, Rotating Cube - Open GL

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Spinning Pyramid, Rotating Cube - Open GL

Post by BackupUser »

Code updated For 5.20+

Restored from previous forum. Originally posted by LJ.

Code: Select all

; NeHe Lesson 5 OpenGL Tutorial
; Translated to PureBasic by Lance Jepsen
; http://nehe.gamedev.net/

IncludeFile "OpenGL.pbi"

Global rtri.f
Global rquad.f

Procedure InitGL()
  glClearColor_(0.0, 0.0, 0.0, 0.0)
  glDepthFunc_(#GL_LESS)
  glEnable_(#GL_DEPTH_TEST)
  glShadeModel_(#GL_SMOOTH)
  
  glMatrixMode_(#GL_PROJECTION)
  glLoadIdentity_()
  
  gluPerspectivef_(45.0, 640/480, 0.1, 100.0)
  glMatrixMode_(#GL_MODELVIEW)
EndProcedure

Procedure DrawScene(hDC)
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  glLoadIdentity_()
  glTranslatef_(-1.5, 0.0, -6.0)
  glRotatef_(rtri,0.0,1.0,0.0)
  
  glBegin_(#GL_TRIANGLES)
  
  ;Front Face of Pyramid
  glColor3f_(1.0,0.0,0.0); // Red
  glVertex3f_( 0.0, 1.0, 0.0); // Top Of Triangle (Front)
  glColor3f_(0.0,1.0,0.0); // Green
  glVertex3f_(-1.0,-1.0, 1.0); // Left Of Triangle (Front)
  glColor3f_(0.0,0.0,1.0); // Blue
  glVertex3f_( 1.0,-1.0, 1.0); // Right Of Triangle (Front)
  
  ;Right Face of Pyramid
  glColor3f_(1.0,0.0,0.0); // Red
  glVertex3f_( 0.0, 1.0, 0.0); // Top Of Triangle (Right)
  glColor3f_(0.0,0.0,1.0); // Blue
  glVertex3f_(1.0,-1.0, 1.0); // Left Of Triangle (Right)
  glColor3f_(0.0,1.0,0.0); // Green
  glVertex3f_(1.0,-1.0, -1.0); // Right Of Triangle (Right)
  
  ;Back Face of Pyramid
  glColor3f_(1.0,0.0,0.0); // Red
  glVertex3f_( 0.0, 1.0, 0.0); // Top Of Triangle (Back)
  glColor3f_(0.0,1.0,0.0); // Green
  glVertex3f_(1.0,-1.0, -1.0); // Left Of Triangle (Back)
  glColor3f_(0.0,0.0,1.0); // Blue
  glVertex3f_(-1.0,-1.0, -1.0); // Right Of Triangle (Back)
  
  ;Left Face of Pyramid
  glColor3f_(1.0,0.0,0.0); // Red
  glVertex3f_( 0.0, 1.0, 0.0); // Top Of Triangle (Left)
  glColor3f_(0.0,0.0,1.0); // Blue
  glVertex3f_(-1.0,-1.0,-1.0); // Left Of Triangle (Left)
  glColor3f_(0.0,1.0,0.0); // Green
  glVertex3f_(-1.0,-1.0, 1.0); // Right Of Triangle (Left)
  glEnd_(); // Done Drawing The Pyramid
  
  glLoadIdentity_();
  glTranslatef_(1.5,0.0,-7.0); // Move Right And Into The Screen
  
  glRotatef_(rquad,1.0,1.0,1.0); // Rotate The Cube On X, Y & Z
  
  glBegin_(#GL_QUADS); // Start Drawing The Cube
  
  ;Top Face of Cube
  glColor3f_(0.0,1.0,0.0); // Set The Color To Green
  glVertex3f_( 1.0, 1.0,-1.0); // Top Right Of The Quad (Top)
  glVertex3f_(-1.0, 1.0,-1.0); // Top Left Of The Quad (Top)
  glVertex3f_(-1.0, 1.0, 1.0); // Bottom Left Of The Quad (Top)
  glVertex3f_( 1.0, 1.0, 1.0); // Bottom Right Of The Quad (Top)
  
  ;Bottom Face of Cube
  glColor3f_(1.0,0.5,0.0); // Set The Color To Orange
  glVertex3f_( 1.0,-1.0, 1.0); // Top Right Of The Quad (Bottom)
  glVertex3f_(-1.0,-1.0, 1.0); // Top Left Of The Quad (Bottom)
  glVertex3f_(-1.0,-1.0,-1.0); // Bottom Left Of The Quad (Bottom)
  glVertex3f_( 1.0,-1.0,-1.0); // Bottom Right Of The Quad (Bottom)
  
  ;Front Face of Cube
  glColor3f_(1.0,0.0,0.0); // Set The Color To Red
  glVertex3f_( 1.0, 1.0, 1.0); // Top Right Of The Quad (Front)
  glVertex3f_(-1.0, 1.0, 1.0); // Top Left Of The Quad (Front)
  glVertex3f_(-1.0,-1.0, 1.0); // Bottom Left Of The Quad (Front)
  glVertex3f_( 1.0,-1.0, 1.0); // Bottom Right Of The Quad (Front)
  
  ;Back Face of Cube
  glColor3f_(1.0,1.0,0.0); // Set The Color To Yellow
  glVertex3f_( 1.0,-1.0,-1.0); // Bottom Left Of The Quad (Back)
  glVertex3f_(-1.0,-1.0,-1.0); // Bottom Right Of The Quad (Back)
  glVertex3f_(-1.0, 1.0,-1.0); // Top Right Of The Quad (Back)
  glVertex3f_( 1.0, 1.0,-1.0); // Top Left Of The Quad (Back)
  
  ;Left Face of Cube
  glColor3f_(0.0,0.0,1.0); // Set The Color To Blue
  glVertex3f_(-1.0, 1.0, 1.0); // Top Right Of The Quad (Left)
  glVertex3f_(-1.0, 1.0,-1.0); // Top Left Of The Quad (Left)
  glVertex3f_(-1.0,-1.0,-1.0); // Bottom Left Of The Quad (Left)
  glVertex3f_(-1.0,-1.0, 1.0); // Bottom Right Of The Quad (Left)
  
  ;Right Face of Cube
  glColor3f_(1.0,0.0,1.0); // Set The Color To Violet
  glVertex3f_( 1.0, 1.0,-1.0); // Top Right Of The Quad (Right)
  glVertex3f_( 1.0, 1.0, 1.0); // Top Left Of The Quad (Right)
  glVertex3f_( 1.0,-1.0, 1.0); // Bottom Left Of The Quad (Right)
  glVertex3f_( 1.0,-1.0,-1.0); // Bottom Right Of The Quad (Right)
  glEnd_(); // Done Drawing The Quad
  
  rtri = rtri + 0.6; // Increase The Rotation Variable For The Triangle
  rquad = rquad - 0.45; // Decrease The Rotation Variable For The Quad
  
EndProcedure

pfd.PIXELFORMATDESCRIPTOR

hWnd = OpenWindow(0, 0, 0, 800, 580, "OpenGL Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

hdc = GetDC_(hWnd)

pfd\nSize = SizeOf(PIXELFORMATDESCRIPTOR)
pfd\nVersion = 1
pfd\dwFlags = #PFD_SUPPORT_OPENGL | #PFD_DOUBLEBUFFER | #PFD_DRAW_TO_WINDOW
pfd\iLayerType = #PFD_MAIN_PLANE
pfd\iPixelType = #PFD_TYPE_RGBA
pfd\cColorBits = 24
pfd\cDepthBits = 16

pixformat = ChoosePixelFormat_(hdc, pfd)
SetPixelFormat_(hdc, pixformat, pfd)
hrc = wglCreateContext_(hdc)
wglMakeCurrent_(hdc,hrc)

InitGL()

While Quit = 0
  
  Repeat
    EventID = WindowEvent()
    
    Select EventID
      Case #PB_Event_CloseWindow
        Quit = 1
    EndSelect
    
  Until EventID = 0
  
  DrawScene(hDC)
  SwapBuffers_(hDC)
  
Wend

BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by LJ.

Compiles to only 14K .exe with no external requirements such as DirectX!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by dige.

Where i can download the opengl.pbi ?

cya dige
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Kale.

You may already have it, see:

C:\program files\purebasic\examples\Sources - Advanced\OpenGL Cube\opengl.pbi

:)

--Kale

In love with PureBasic! :)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by VPureBasic.
Originally posted by LJ

Compiles to only 14K .exe with no external requirements such as DirectX!
And you can reduce it again by...

1) Not declare pdf.PIXELFORMATDESCRIPTOR

2) Replace all this
;hdc = GetDC_(hWnd)

;pfd\nSize = SizeOf(PIXELFORMATDESCRIPTOR)
;pfd\nVersion = 1
;pfd\dwFlags = #PFD_SUPPORT_OPENGL | #PFD_DOUBLEBUFFER | #PFD_DRAW_TO_WINDOW
;pfd\iLayerType = #PFD_MAIN_PLANE
;pfd\iPixelType = #PFD_TYPE_RGBA
;pfd\cColorBits = 24
;pfd\cDepthBits = 16

;pixformat = ChoosePixelFormat_(hdc, pfd)
;SetPixelFormat_(hdc, pixformat, pfd)
;hrc = wglCreateContext_(hdc)
;wglMakeCurrent_(hdc,hrc)

...by only use this function:
InitOpenGLContext( 0 )

... and replace this line
SwapBuffers(hdc)
by
SwapOpenGLContext(0).


Roger
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Yep, you should try the lib of Roger which already handle camera and some other goodies.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by dmoc.

I think Roger's clashes with the Orge lib? I could be wrong, just remember trying it recently. Also, how to get fullscreen?

PS: Fred, will there eventually be an opengl option in PBOrge?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ebs.

I tried to run this program (original version, not using Roger's library) on Win XP Pro with an nVidia GeForce2 and I get a black window. I have OpenGL.pbi and the GLU32F library. Please tell me what I'm missing!

Thanks,
Eric
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by dmoc.

Try adjusting the "glTranslatef" parameters
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by LJ.

Ebs:
The GLU32F library file isn't necessary but this shouldn't be a problem. Try running Fred's OpenGL Cube in your C:\program files\purebasic\examples\Sources - Advanced\OpenGL Cube\opengl.pbi
folder. If Fred's code works, then try creating a new project, paste in the code I posted above, and save in the same folder. This way you are in the same folder as the opengl.pbi file. Conversely you can copy the opengl.pbi file to your project folder.

Finally, the resolution of the OpenGL screen is hardcoded so make sure your desktop is set to display 800 x 600 resolution.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ebs.

dmoc,

That did it! I changed it to

Code: Select all

glTranslatef_(0.0, 0.0, 0.0)
and it works. Can you explain why?

Thanks,
Eric
Originally posted by dmoc

Try adjusting the "glTranslatef" parameters
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by dmoc.

I haven't got time to read carefully through LJ/NeHe's code above but I assume things simply move out of view. Part of getting in the right mindset with OpenGL is that you don't place an obj at (x,y,z) but instead move the frame-of reference. An analogy is you as person are always static and the whole world moves around you.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by LJ.

Ebs:
the glTranslatef_(-1.5, 0.0, -6.0) is the camera movement. I suspect that different graphics cards handle the glTranslatef_(-1.5, 0.0, -6.0) a little different. I noticed some had the same problem with Fred's OpenGL cube code and by changing the variable ZOOM some who had just a black screen were then able to see the cube. I'm new at OpenGL but I suspect there is a better way to code the glTranslatef_(-1.5, 0.0, -6.0) command so that it is more graphic card independent friendly. Glad you got it working.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by VPureBasic.
Originally posted by dmoc

I think Roger's clashes with the Ogre lib? I could be wrong, just remember trying it recently. Also, how to get fullscreen?
Hi Dmoc,

Here's how you can set a fullscreen window, like 640 x 480 x 16

Code: Select all

If IsScreenMode( 640,480,16 )
     SetScreenMode()
EndIf

OpenWindow( 0,0,0,640,480,#PB_Window_BorderLess,"" )

Repeat
Until WaitWindowEvent() = 513

ResetScreenMode()

End
Roger
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by dmoc.

Hi Roger, first to clear-up my comment about a possible clash, I have checked again and there isn't any problem. I must have got your lib mixed up with something else.

Re your full-screen example, are these commands part of your lib? If so the version on the Resources site does not have them.
Post Reply