Objectives

Install Eclipse

Hello C++

Create a new C++ application, selecting the "Hello World" app from the //File->New->C++ Project// menu:

Subsequently, to run the application, just press one of the launch icons:

Hello OpenGL

Eclipse will generate an empty source file:

#ifdef WIN32
#include <GL/gl.h>
#include <GL/glu.h>
#include "freeglut.h"
#endif

#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <Glut/glut.h>
#endif
void renderScene(void)
{
  glClear( GL_COLOR_BUFFER_BIT);

  glFlush();
}

void setupRC(void)
{
  glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}

int main(int argc, char* argv[])
{
  glutInit(&amp;argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
  glutCreateWindow("Hello OpenGL");
  glutDisplayFunc(renderScene);

  setupRC();

  glutMainLoop();

  return 0;
}
**** Internal Builder is used for build               ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -ohelloopengl.o ..\helloopengl.cpp
..\helloopengl.cpp:4:22: freeglut.h: No such file or directory
..\helloopengl.cpp: In function `int main(int, char**)':
..\helloopengl.cpp:28: error: `glutInit' was not declared in this scope
..\helloopengl.cpp:29: error: `GLUT_SINGLE' was not declared in this scope
..\helloopengl.cpp:29: error: `GLUT_RGBA' was not declared in this scope
..\helloopengl.cpp:29: error: `glutInitDisplayMode' was not declared in this scope
..\helloopengl.cpp:30: error: `glutCreateWindow' was not declared in this scope
..\helloopengl.cpp:31: error: `glutDisplayFunc' was not declared in this scope
..\helloopengl.cpp:35: error: `glutMainLoop' was not declared in this scope
..\helloopengl.cpp:28: warning: unused variable 'glutInit'
..\helloopengl.cpp:29: warning: unused variable 'GLUT_SINGLE'
..\helloopengl.cpp:29: warning: unused variable 'GLUT_RGBA'
..\helloopengl.cpp:29: warning: unused variable 'glutInitDisplayMode'
..\helloopengl.cpp:30: warning: unused variable 'glutCreateWindow'
..\helloopengl.cpp:31: warning: unused variable 'glutDisplayFunc'
..\helloopengl.cpp:35: warning: unused variable 'glutMainLoop'
Build error occurred, build is stopped
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"helloopengl.d" -MT"helloopengl.d" -o"helloopengl.o" "../helloopengl.cpp"
Finished building: ../helloopengl.cpp

Building target: HelloOpenGL
Invoking: MacOS X C++ Linker
g++  -o "HelloOpenGL"  ./helloopengl.o
Undefined symbols:
  "_glFlush", referenced from:
      renderScene()     in helloopengl.o
  "_glutDisplayFunc", referenced from:
      _main in helloopengl.o
  "_glutInitDisplayMode", referenced from:
      _main in helloopengl.o
  "_glutCreateWindow", referenced from:
      _main in helloopengl.o
  "_glutMainLoop", referenced from:
      _main in helloopengl.o
  "_glutInit", referenced from:
      _main in helloopengl.o
  "_glClearColor", referenced from:
      setupRC()    in helloopengl.o
  "_glClear", referenced from:
      renderScene()     in helloopengl.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [HelloOpenGL] Error 1

Drawing a Square

void setupRC(void)
{
  glClearColor(0.0f, 0.0f, 1.0f, 1.0f);

  gluOrtho2D(-100.0, 100.0, -100, 100.0);
}
void renderScene(void)
{
  glClear( GL_COLOR_BUFFER_BIT);

  glColor3f(1.0f, 0.0f, 0.0f);
  glRectf(-25.0f, 25.0f, 25.0f, -25.0f);

  glFlush();
}

Exercises

1. Reconfigure the Project Structure

Review the two projects you have created, exploring them within eclipse:

and on the file system:

Refactor the opengl project such that the source is in a src subdirectory, like the c++ application. To do this, you will need to: - Create a "source directory" called src in eclipse

Your objective is to have a project structure like this:

2. Introduce a utilities project

Create a new project to contain some utility classes. Call the project "utils". It should initially contain a single leader file "libopengl.h". Put the includes from the existing open gl source into into it:

#ifdef WIN32
#include <windows.h>
#include <gl\glu.h>
#include "glut.h"
#endif

#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <Glut/glut.h>
#endif

Then replace these lines in your C++ programe with:

#include "libopengl.h"

There are many ways of doing this within eclipse. Most involve exiting the project properties, and exploring the project paths for the build. The following screen here gives the game away...

3. Explore the OpenGL API