Search Projects

Thursday, September 7, 2017

"Ray" OpenGL Sample Program in Visual Studio

There are many OpenGL sample program  written in early days of OpenGL Development. These Sample Program or sample code written by many of the authors in top university.

In this post were are going to talk about one such sample code written by David Bucciarelli ( This article aims to improvise knowledge of seekers, who want to learn opengl programming with sample code. The download source code from the link in the post for this opengl sample code along with mesa version uploaded to github. Do give author the credit if your are using this opengl sample code in your work projects etc.

Download Sample Code                 Github Mesa Version


The OpenGL Sample Program has been tested in Microsoft Visual Studio Ultimate 2010 and will works in almost all the other version as well. It can be executed on other platforms like linux, mac os etc with some modification.

The program consist of  mainly a light source and a spherical mirror placed on square strips. There are other sections like help section and two maps one for texture of plane and other for texture of sphere.

The Program is user interactive, allow use to give input and get desire output. The user interaction has been code with keyboard functionality of OpenGL.

"Ray" OpenGL Sample Program in Visual Studio

2. The OpenGL Sample Program

The Ray V.10 Written by David Bucciarelli is our OpenGL sample program. You need to have MS visual studio to get started with. Also you need to setup the OpenGL environment to execute the apis provided by them for this opengl sample code. Learn how to setup opengl in MS visual studio in window.

Header Files - First of all include the header files, including glut in the program as we do in "hello world opengl". Thereafter write down the global variable which goes on the way as program progress.

Functions - This program have many functions including the basic opengl function like init, reshape, key and main. We have other functions which is created for drawing the objects. Since we required these function to do our work they are called and used in program.

User Interaction - The key board and special key function is written so user can give input and interact with our program.

3. Coding

There is two object - light source and mirror for reflection. We are going to draw both of them with the help of sphere. Hence we need to call the function for drawing sphere. First of all define the radius for the sphere and then also define the width and height for texture (strips) and it's reflection. Also define the frame rate as we are going to use it as well.

Now we will see all the function one by one (excludes basic functions).

gettime(void) - with use of glutget function this function used to check the status of glut and time with GLUT_ELAPSED_TIME.

calcposobs(void) - defines the position (direction) of observer.

special(int k, int x, int y) - define specil keyboard functions for left, right, up and down navigation keys.

key(unsigned char k, int x, int y) - normal user interaction functions assign to different keys.

printstring(void *font, char *string) -  to print the string.

printhelp(void) - use printstring() and display help box. Font we are using is GLUT_BITMAP_HELVETICA (12 and 18 size).

seelight(float p[3],float dir[3]) - the light source.

colorcheckmap(float ppos[3], float c[3]) - this draw the plane map.

updatecheckmap(int slot) - update plane map with change cause by user interaction.

updatereflectmap(int slot) - update reflection map with change cause by user interaction.

drawbase(void) - Our base is square strips or checkers, this function draws it.

drawobj(void) - draw objects.

dojoy(void)- for joystick functions.

updatemaps(void) - Update maps

draw(void) - Finally draws everything on the screen by rendering objects.

initspherepos(void) - details the sphere position.

inittextures(void) - for textures

initdlists(void) - lists

4. User Interactions

The user interaction in this program is given below -

h - Togle Help

f - Togle Fog

b - Togle Back face culling

p - Togle Wire frame

Arrow Keys (navigation key) - Rotate in left, right, up and down respectively to respective keys

a - Increase velocity

z - Decrease velocity
j - Togle jostick control (Joystick control available)
(No Joystick control available)

To move the light source: s - left, d - right, e - far, x - near, w - down r - up

To move the mirror sphere: j - left, k - right, i - far, m - near, u - down o - up

1 - Togle the plane texture map window

2 - Togle the sphere texture map window

5. Final Words

The ray - OpenGLSample Program on compilation and execution will show a checker with one mirror in sphere shape and a light source. There is also two maps in left and right. These maps show the plane and reflection textures. A helps popup like appears in beginning itself that give glimpse of all user interactions in the program. The Youtube video above is showing demo of all the functionality of this program.

If you are keen to learn and become an game programmer this program is especially relevant to you. Also in point of view of exam or project preparation is opengl sample code is an additional help.

Finally, do download this program and comment about it in the comment box.

No comments:

Post a Comment