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