VR-Forces 4.3.1 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
exampleOpenGLSimple

Table of Contents

Overview

This example shows how to create an OpenGL application. This particular application shows how to render a simple cube using OpenGL primitive set. The cube is lit by a single OpenGL light.

The VR-Vantage architecture consists of many layers and at its lowest level is OpenGL. Although it is rare that a developer will have to program at this level we provide a few examples anyway.

Example details

The example is organized into three main sections. First is a section that defines the shape of the cube and a helper function to draw it. The second section has functions for setting up and rendering the scene. Last is the main function which initializes OpenGL and then runs the main loop.

The geometry is defined by a set of vertices. The side of the cube is of length 3 units.

GLfloat cubeVert[8][3] =
{
{0.0f, 0.0f, 0.0f}, // 0 left front bottom
{3.0f, 0.0f, 0.0f}, // 1 right front bottom
{3.0f, 3.0f, 0.0f}, // 2 right front top
{0.0f, 3.0f, 0.0f}, // 3 left front top
{0.0f, 0.0f, -3.0f}, // 4 left back bottom
{3.0f, 0.0f, -3.0f}, // 5 right back bottom
{3.0f, 3.0f, -3.0f}, // 6 right back top
{0.0f, 3.0f, -3.0f} // 7 left back top
};

These vertices only define points in space, they do not define the faces of the geometry. Faces are defined by the order of vertices making up the edges of each face.

GLint cubeFace[6][4] =
{
{2, 6, 7, 3}, // top
{0, 4, 5, 1}, // bottom
{3, 7, 4, 0}, // left
{1, 5, 6, 2}, // right
{0, 1, 2, 3}, // front
{7, 6, 5, 4} // back
};

Each face is drawn by specifying the type of geometry primitive and the order of the vertices for that primitive.

// Draw the face using the vertices in cubeVert
// in the order defined by cubeFace[i] where i is
// the index of each face.
glBegin(GL_QUADS);
// Specify normal for each face of the cube.
glNormal3fv(&n[i][0]);
glVertex3fv(cubeVert[ cubeFace[i][0] ]);
glVertex3fv(cubeVert[ cubeFace[i][1] ]);
glVertex3fv(cubeVert[ cubeFace[i][2] ]);
glVertex3fv(cubeVert[ cubeFace[i][3] ]);
glEnd();

OpenGL allows us to specify upto 7 different lights. This section enables a single light (GL_LIGHT0) and specifies its properties.

// Specify light reflection property.
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
// Specify the light position.
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
// Enable a single OpenGL light.
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);

The main function is used to initialize OpenGL and specify which function callbacks to call when rendering before starting the main loop.

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Building the Example

VR-Vantage includes pre-built versions of the example application. To build it yourself, follow the instructions at Building VR-Vantage Examples, Applications, and Plug-ins.

Running the Example

This example is an application. You can run it by running ./bin/exampleOpenGLSimple.exe (on Windows) or ./bin/exampleOpenGLSimple (on Linux). For more information about running examples, please see Running Applications and Examples.

Learn More

Example Source Files


exampleOpenGLSimple.cxx

/******************************************************************************
** Copyright (c) 2012 MAK Technologies, Inc.
** All rights reserved.
******************************************************************************/
// This example creates a simple cube whose left back top corner is located at
// origin. A single OpenGL directional light is used. And the lighting has diffuse
// reflection property. To see the effects of lighting on the cube, face normals
// are specified which are of unit length.
#ifdef _WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
// Red diffuse light.
GLfloat lightDiffuse[] = {1.0, 0.0, 0.0, 1.0};
// Directional light vector specifying infinite light location.
GLfloat lightPosition[] = {1.0, 1.0, 1.0, 0.0};
// Vertices are defined to specify the corners of the cube.
// Notice that these vertices define a cube of side length
// 3 units with its left back top vertex as origin (x=0, y=0, z=0).
GLfloat cubeVert[8][3] =
{
{0.0f, 0.0f, 0.0f}, // 0 left front bottom
{3.0f, 0.0f, 0.0f}, // 1 right front bottom
{3.0f, 3.0f, 0.0f}, // 2 right front top
{0.0f, 3.0f, 0.0f}, // 3 left front top
{0.0f, 0.0f, -3.0f}, // 4 left back bottom
{3.0f, 0.0f, -3.0f}, // 5 right back bottom
{3.0f, 3.0f, -3.0f}, // 6 right back top
{0.0f, 3.0f, -3.0f} // 7 left back top
};
// Normals for the 6 faces of a cube.
GLfloat n[6][3] =
{
{ 0.0, -1.0, 0.0}, // top
{ 0.0, 1.0, 0.0}, // bottom
{-1.0, 0.0, 0.0}, // left
{ 1.0, 0.0, 0.0}, // right
{ 0.0, 0.0, 1.0}, // front
{ 0.0, 0.0, -1.0} // back
};
// Arrays of indices to the vertices define the faces of the cube.
// Each sub-array defines the order of verticies making up one
// face of the cube. There is one sub-array for each face.
GLint cubeFace[6][4] =
{
{2, 6, 7, 3}, // top
{0, 4, 5, 1}, // bottom
{3, 7, 4, 0}, // left
{1, 5, 6, 2}, // right
{0, 1, 2, 3}, // front
{7, 6, 5, 4} // back
};
// The function to draw a cube. This function draws each face of the
// cube as a GL_QUAD (a four point polygon) using the vertices defined
// above, ordered by the indices of the cube-face.
void drawCube()
{
// Render cube in red color.
glColor3f(1.0f, 0.0f, 0.0f);
// For each face in cubeFace...
for (int i = 0; i < 6; ++i)
{
// Draw the face using the vertices in cubeVert
// in the order defined by cubeFace[i] where i is
// the index of each face.
glBegin(GL_QUADS);
// Specify normal for each face of the cube.
glNormal3fv(&n[i][0]);
glVertex3fv(cubeVert[ cubeFace[i][0] ]);
glVertex3fv(cubeVert[ cubeFace[i][1] ]);
glVertex3fv(cubeVert[ cubeFace[i][2] ]);
glVertex3fv(cubeVert[ cubeFace[i][3] ]);
glEnd();
}
}
// This function is the workhorse that draws the scene. It first positions
// the eyepoint to see the objects in the scene. Then it positions and draws
// the cube. It then tells OpenGL to render the scene.
// This function is repeatedly called to refresh and redraw the
// scene.
void display()
{
// Set the matrix mode for geometry
glMatrixMode(GL_MODELVIEW);
// Position the eyepoint
glLoadIdentity();
gluLookAt(7.0f, 7.0f, 7.0f, // eyepoint
0.0f, 0.0f, -3.0f, // target at (0,0,-1)
0.0f, 0.0f, 1.0f); // up is in positive Z direction
// Clear the buffers each frame
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Draw the cube
drawCube();
// Swap buffers, flush rendering, and request this function called again
glutSwapBuffers();
glFlush();
glutPostRedisplay();
}
// This function (re)initializes the viewport to the scene.
// When called it sets the viewport to the scene to fill the window, and
// sets the view to be perspective (objects get smaller the further away
// they are from the eye-point). This function is called once when the
// window is created and again everytime the window is resized.
void reshape(int width, int height)
{
// Size the viewport to fill the window
glViewport(0, 0, width, height);
// Setup to be a perspective view
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (float)width / (float)height, 0.1f, 50.0f);
}
// This function initialize how the scene will be displayed in the window.
// This is only called once for the application.
void init()
{
// Set the display mode to use double-buffering, a color buffer
// and a depth buffer.
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
// Specify an initial window size.
glutInitWindowSize(300, 300);
// Create a window to draw in and give it a window title
glutCreateWindow("Render Cube with lighting");
// Set the color that when clearing the screen
glClearColor(0.5f, 0.5f, 0.5f, 0.0f);
// Specify light reflection property.
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
// Specify the light position.
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
// Enable a single OpenGL light.
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
// Use depth buffering for hidden surface culling
glEnable(GL_DEPTH_TEST);
}
// The main function initializes OpenGL and how the window is displayed,
// sets a function to be called if the window is resized, sets a function
// to call to draw the scene, then starts the main rendering loop.
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Document ID: Generated on Wed Jul 29 00:55:00 EDT 2015 from SVN revision 155257
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)