' http://nehe.gamedev.net/
/*
* This Code Was Created By Jeff Molofee 2000
* Lesson5: 3D Shapes
* Using freeglut
* Modified and ported to OxygenBasic
*/
$ FileName "Lesson5.exe"
'include "$/inc/RTL32.inc"
'include "$/inc/console.inc"
include "../include/freeglut.inc"
// Global variables
' Title in windowed mode
string title = "NeHe's OpenGL Framework - 3D Shapes"
int windowWidth = 640 ' Windowed mode's width
int windowHeight = 480 ' Windowed mode's height
int windowPosX = 50 ' Windowed mode's top-left corner x
int windowPosY = 50 ' Windowed mode's top-left corner y
bool fullscreen = false // Fullscreen mode ON/OFF
GLfloat rtri // Angle For The Triangle
GLfloat rquad // Angle For The Quad
int millsec = 15; // refresh interval in milliseconds
sub initGL() // All setup for OpenGL goes Here
glShadeModel(GL_SMOOTH) // Enable smooth shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f) // Black background
glClearDepth(1.0f) // Depth buffer setup
glEnable(GL_DEPTH_TEST) // Enables depth testing
glDepthFunc(GL_LEQUAL) // Type of depth testing to do
glEnable(GL_COLOR_MATERIAL)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST) // Really nice perspective calculation
end sub
sub display() // Create The Display Function
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(-1.5f,0.0f,-6.0f) // Move Left 1.5 Units And Into The Screen 6.0
glRotatef(rtri,0.0f,1.0f,0.0f) // Rotate The Triangle On The Y axis
glPushMatrix()
glBegin(GL_TRIANGLES) // Start Drawing A Triangle
glColor3f(1.0f,0.0f,0.0f) // Red
glVertex3f( 0.0f, 1.0f, 0.0f) // Top Of Triangle (Front)
glColor3f(0.0f,1.0f,0.0f) // Green
glVertex3f(-1.0f,-1.0f, 1.0f) // Left Of Triangle (Front)
glColor3f(0.0f,0.0f,1.0f) // Blue
glVertex3f( 1.0f,-1.0f, 1.0f) // Right Of Triangle (Front)
glColor3f(1.0f,0.0f,0.0f) // Red
glVertex3f( 0.0f, 1.0f, 0.0f) // Top Of Triangle (Right)
glColor3f(0.0f,0.0f,1.0f) // Blue
glVertex3f( 1.0f,-1.0f, 1.0f) // Left Of Triangle (Right)
glColor3f(0.0f,1.0f,0.0f) // Green
glVertex3f( 1.0f,-1.0f, -1.0f) // Right Of Triangle (Right)
glColor3f(1.0f,0.0f,0.0f) // Red
glVertex3f( 0.0f, 1.0f, 0.0f) // Top Of Triangle (Back)
glColor3f(0.0f,1.0f,0.0f) // Green
glVertex3f( 1.0f,-1.0f, -1.0f) // Left Of Triangle (Back)
glColor3f(0.0f,0.0f,1.0f) // Blue
glVertex3f(-1.0f,-1.0f, -1.0f) // Right Of Triangle (Back)
glColor3f(1.0f,0.0f,0.0f) // Red
glVertex3f( 0.0f, 1.0f, 0.0f) // Top Of Triangle (Left)
glColor3f(0.0f,0.0f,1.0f) // Blue
glVertex3f(-1.0f,-1.0f,-1.0f) // Left Of Triangle (Left)
glColor3f(0.0f,1.0f,0.0f) // Green
glVertex3f(-1.0f,-1.0f, 1.0f) // Right Of Triangle (Left)
glEnd() // Done Drawing The Pyramid
glLoadIdentity() // Reset The Current Modelview Matrix
glTranslatef(1.5f,0.0f,-7.0f) // Move Right 1.5 Units And Into The Screen 7.0
glRotatef(rquad,1.0f,0.0f,0.0f) // Rotate The Quad On The X axis
glColor3f(0.5f,0.5f,1.0f); // Set The Color To Blue One Time Only
glBegin(GL_QUADS) // Draw A Quad
glColor3f(0.0f,1.0f,0.0f) // Set The Color To Green
glVertex3f( 1.0f, 1.0f,-1.0f) // Top Right Of The Quad (Top)
glVertex3f(-1.0f, 1.0f,-1.0f) // Top Left Of The Quad (Top)
glVertex3f(-1.0f, 1.0f, 1.0f) // Bottom Left Of The Quad (Top)
glVertex3f( 1.0f, 1.0f, 1.0f) // Bottom Right Of The Quad (Top)
glColor3f(1.0f,0.5f,0.0f) // Set The Color To Orange
glVertex3f( 1.0f,-1.0f, 1.0f) // Top Right Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f, 1.0f) // Top Left Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f,-1.0f) // Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0f,-1.0f,-1.0f) // Bottom Right Of The Quad (Bottom)
glColor3f(1.0f,0.0f,0.0f) // Set The Color To Red
glVertex3f( 1.0f, 1.0f, 1.0f) // Top Right Of The Quad (Front)
glVertex3f(-1.0f, 1.0f, 1.0f) // Top Left Of The Quad (Front)
glVertex3f(-1.0f,-1.0f, 1.0f) // Bottom Left Of The Quad (Front)
glVertex3f( 1.0f,-1.0f, 1.0f) // Bottom Right Of The Quad (Front)
glColor3f(1.0f,1.0f,0.0f) // Set The Color To Yellow
glVertex3f( 1.0f,-1.0f,-1.0f) // Top Right Of The Quad (Back)
glVertex3f(-1.0f,-1.0f,-1.0f) // Top Left Of The Quad (Back)
glVertex3f(-1.0f, 1.0f,-1.0f) // Bottom Left Of The Quad (Back)
glVertex3f( 1.0f, 1.0f,-1.0f) // Bottom Right Of The Quad (Back)
glColor3f(0.0f,0.0f,1.0f) // Set The Color To Blue
glVertex3f(-1.0f, 1.0f, 1.0f) // Top Right Of The Quad (Left)
glVertex3f(-1.0f, 1.0f,-1.0f) // Top Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f,-1.0f) // Bottom Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f, 1.0f) // Bottom Right Of The Quad (Left)
glColor3f(1.0f,0.0f,1.0f) // Set The Color To Violet
glVertex3f( 1.0f, 1.0f,-1.0f) // Top Right Of The Quad (Right)
glVertex3f( 1.0f, 1.0f, 1.0f) // Top Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f, 1.0f) // Bottom Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f,-1.0f) // Bottom Right Of The Quad (Right)
glEnd() // Done Drawing The Quad
glPopMatrix()
rtri += 0.4f // Increase The Rotation Variable For The Triangle
rquad -= 0.3f // Decrease The Rotation Variable For The Quad
glutSwapBuffers() // Swap the buffers not to be left with a frozen Screen (will need task manager)
end sub
/* Called back when timer expired */
sub timer(int value)
glutPostRedisplay() // Post re-paint request to activate display()
glutTimerFunc(millsec, @timer, 0) // next timer call milliseconds later
end sub
// Reshaping handler (required even in fullscreen mode)
sub reshape(int w, int h)
if h = 0 then h = 1 // Prevent divide by zero
GLfloat aspect = (GLfloat)w / (GLfloat)h;
glViewport(0, 0, w, h) // Reset the current viewport
glMatrixMode(GL_PROJECTION) // Select the projection matrix
glLoadIdentity() // Reset the projection matrix
// Calculate the aspect ratio and set the clipping Volume
gluPerspective(45.0f, aspect, 0.1f, 100.0f)
glMatrixMode(GL_MODELVIEW) // Select the modelview matrix
glLoadIdentity() // Reset the modelview matrix
end sub
// Keyboard handler (Normal keys)
sub keyboard(int key, int x, int y)
select key
case 27 // When Escape Is Pressed...
glutExit() // Exit The Program
case else
end select
end sub
sub savePos()
windowPosX = glutGet(GLUT_WINDOW_X) ' Save parameters for restoring later
windowPosY = glutGet(GLUT_WINDOW_Y)
windowWidth = glutGet(GLUT_WINDOW_WIDTH)
windowHeight = glutGet(GLUT_WINDOW_HEIGHT)
end sub
// Keyboard Handler for special keys (Like arrow Keys and function keys)
sub arrow_keys(int a_keys, int x, int y) // Create Special Function (required for arrow keys)
select a_keys
case GLUT_KEY_F1 // Is F1 Being Pressed?
// We can switch between windowed mode and fullscreen mode
fullscreen = not fullscreen // Toggle fullscreen flag
if fullscreen then
savePos() // save position and size
glutFullScreen() // Change in fullscreen mode
else
glutReshapeWindow(windowWidth, windowHeight) // Switch into windowed mode
' glutPositionWindow(windowPosX, windowPosY) // Position top-left
end if
case GLUT_KEY_UP // When Up Arrow Is Pressed...
savePos() // save position and size
glutFullScreen() // Go Into Full Screen Mode
fullscreen = -1 // fullscreen flag true
case GLUT_KEY_DOWN: // When Down Arrow Is Pressed...
glutReshapeWindow(windowWidth, windowHeight) // Switch into windowed mode
' glutPositionWindow(windowPosX, windowPosY) // Position top-left
fullscreen = 0 // fullscreen flag false
case else
end select
end sub
sub main()
glutInit(0,0)
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE) // Display Mode
glutInitWindowSize(windowWidth, windowHeight) // If glutFullScreen wasn't called this is the window size
glutInitWindowPosition(windowPosX, windowPosY) // Window position
glutCreateWindow(title) // Window Title
glutDisplayFunc(@display) // callbacks
glutReshapeFunc(@reshape)
glutKeyboardFunc(@keyboard)
glutSpecialFunc(@arrow_keys)
' either
' glutIdleFunc(@display)
' or better
glutTimerFunc(0, @timer, 0) // First timer call immediately
initGL() // OpenGL setup
glutMainLoop() // Initialize The Main Loop
end sub
main()