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},
{3.0f, 0.0f, 0.0f},
{3.0f, 3.0f, 0.0f},
{0.0f, 3.0f, 0.0f},
{0.0f, 0.0f, -3.0f},
{3.0f, 0.0f, -3.0f},
{3.0f, 3.0f, -3.0f},
{0.0f, 3.0f, -3.0f}
};
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},
{0, 4, 5, 1},
{3, 7, 4, 0},
{1, 5, 6, 2},
{0, 1, 2, 3},
{7, 6, 5, 4}
};
Each face is drawn by specifying the type of geometry primitive and the order of the vertices for that primitive.
glBegin(GL_QUADS);
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.
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
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[])
{
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
#ifdef _WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
GLfloat lightDiffuse[] = {1.0, 0.0, 0.0, 1.0};
GLfloat lightPosition[] = {1.0, 1.0, 1.0, 0.0};
GLfloat cubeVert[8][3] =
{
{0.0f, 0.0f, 0.0f},
{3.0f, 0.0f, 0.0f},
{3.0f, 3.0f, 0.0f},
{0.0f, 3.0f, 0.0f},
{0.0f, 0.0f, -3.0f},
{3.0f, 0.0f, -3.0f},
{3.0f, 3.0f, -3.0f},
{0.0f, 3.0f, -3.0f}
};
GLfloat n[6][3] =
{
{ 0.0, -1.0, 0.0},
{ 0.0, 1.0, 0.0},
{-1.0, 0.0, 0.0},
{ 1.0, 0.0, 0.0},
{ 0.0, 0.0, 1.0},
{ 0.0, 0.0, -1.0}
};
GLint cubeFace[6][4] =
{
{2, 6, 7, 3},
{0, 4, 5, 1},
{3, 7, 4, 0},
{1, 5, 6, 2},
{0, 1, 2, 3},
{7, 6, 5, 4}
};
void drawCube()
{
glColor3f(1.0f, 0.0f, 0.0f);
for (int i = 0; i < 6; ++i)
{
glBegin(GL_QUADS);
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();
}
}
void display()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(7.0f, 7.0f, 7.0f,
0.0f, 0.0f, -3.0f,
0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawCube();
glFlush();
}
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (float)width / (float)height, 0.1f, 50.0f);
}
{
glClearColor(0.5f, 0.5f, 0.5f, 0.0f);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
}
int main(int argc, char *argv[])
{
return 0;
}