opengl with eclipse

5
8/20/2019 Opengl With Eclipse http://slidepdf.com/reader/full/opengl-with-eclipse 1/5 Configuring Eclipse and freeglut on Windows and Ubuntu Linux to start programming in OpenGL Gerhard Burger February 19, 2010 1 Installation When you start to program in OpenGL, you might need a decent Integrated Development Environ- ment (IDE). Being free, open-source and platform independent, Eclipse ( http://www.eclipse. org/) is an excellent choice. Although officially a Java Development Environment, Eclipse can work with C, C++, COBOL, Python, Perl, PHP, and other programming languages by means of an extensible plug-in system. This section will offer a step-by-step guide on how to configure Eclipse for C++ and OpenGL on Windows and Ubuntu (Linux). At the time of writing the version of Eclipse is 3.5.1 (Galileo), and I used Windows 7 Ultimate x64 and Linux Mint 8 32bit (based on Ubuntu 9.10). Getting everything to work on Ubuntu is easier and less time consuming, so if you wanted to try Linux at some point, now might be the right time. 1.1 Installing Eclipse  Go to  http://www.eclipse.org/downloads/. At the bottom of the page there is an entry for Eclipse Classic, choose the version corresponding to your operating system. 1 Download the file and extract using your favorite extraction tool. Eclipse does not have to be installed so you can move the ‘eclipse’ folder to a location of your liking and start working.  Since Eclipse is working we move on to configure Eclipse for using C/C++. Start Eclipse and go to ‘help’ and select ‘Install New Software...’. Under ‘Programming Languages’ select and install the package called ‘Eclipse C/C++ Development Tools’. After the installation is done restart Eclipse when prompted. 1.2 Installing a compiler Since installing a compiler is different for Windows and Ubuntu, both are explained separately. 1.2.1 Windows As compiler for Windows we will use MinGW. MinGW is a contraction of “Minimalist GNU for Windows”, and is a port of the GNU Compiler Collection (GCC), and GNU Binutils. 1 On Ubuntu you can also install Eclipse using the standard package repositories 1

Upload: lee-chan-peter

Post on 07-Aug-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Opengl With Eclipse

8/20/2019 Opengl With Eclipse

http://slidepdf.com/reader/full/opengl-with-eclipse 1/5

Configuring Eclipse and freeglut on Windows and Ubuntu Linux to

start programming in OpenGL

Gerhard Burger

February 19, 2010

1 Installation

When you start to program in OpenGL, you might need a decent Integrated Development Environ-ment (IDE). Being free, open-source and platform independent, Eclipse (http://www.eclipse.

org/) is an excellent choice. Although officially a Java Development Environment, Eclipse canwork with C, C++, COBOL, Python, Perl, PHP, and other programming languages by meansof an extensible plug-in system. This section will offer a step-by-step guide on how to configureEclipse for C++ and OpenGL on Windows and Ubuntu (Linux). At the time of writing the versionof Eclipse is 3.5.1 (Galileo), and I used Windows 7 Ultimate x64 and Linux Mint 8 32bit (based onUbuntu 9.10). Getting everything to work on Ubuntu is easier and less time consuming, so if youwanted to try Linux at some point, now might be the right time.

1.1 Installing Eclipse

−  Go to   http://www.eclipse.org/downloads/. At the bottom of the page there is an entryfor Eclipse Classic, choose the version corresponding to your operating system.1 Download

the file and extract using your favorite extraction tool. Eclipse does not have to be installedso you can move the ‘eclipse’ folder to a location of your liking and start working.

−  Since Eclipse is working we move on to configure Eclipse for using C/C++. Start Eclipseand go to ‘help’ and select ‘Install New Software...’. Under ‘Programming Languages’ selectand install the package called ‘Eclipse C/C++ Development Tools’. After the installation isdone restart Eclipse when prompted.

1.2 Installing a compiler

Since installing a compiler is different for Windows and Ubuntu, both are explained separately.

1.2.1 Windows

As compiler for Windows we will use MinGW. MinGW is a contraction of “Minimalist GNU forWindows”, and is a port of the GNU Compiler Collection (GCC), and GNU Binutils.

1On Ubuntu you can also install Eclipse using the standard package repositories

1

Page 2: Opengl With Eclipse

8/20/2019 Opengl With Eclipse

http://slidepdf.com/reader/full/opengl-with-eclipse 2/5

−  To download MinGW go to   http://sourceforge.net/projects/mingw/files/ and down-load the latest   MinGW-x.x.x.exe. Also download the latest   MSYS-x.x.x.exe  which you canfind under ‘MSYS Base System’.

−  First install MinGW using the current packages and make sure to select only the ‘MinGWbase tools’ and the ‘g++ compiler’. Be careful not to install to a directory containing spaces.

−  Then install MSYS making sure you fill in the path to MinGW correctly using forward slashes(/).

−  Go to Environment Variables in Windows and add the bin directory of both MinGW andMINSYS to the PATH variable in System variables. In my case I had to add:

;C:\msys\1.0\bin;C:\mingw\bin

−  Finally open a command prompt (cmd) and test the installation with the following commands:

 make --version

g++ --version

−   Now you are ready to execute your first program in C++. In Eclipse go to ‘Window   →Open Perspective  →  Other’ and choose ‘C/C++’. Go to ‘File  →  New  →  C++ Project’. Inthe window that opens, enter a project name and choose ‘Hello World C++ Project’ under‘Executable’. If you don’t have a folder called ‘Executable’ uncheck ‘Show project typesand toolchains only if they are supported on the platform’. Now you also have to select atoolchain for your project, choose ‘MinGW GCC’. Click Finish. If you did everything right,your project is automatically build and a ‘Debug’ folder is created with an  .exe executable.

−  Now right click on your project and choose ‘Properties’ and go to ‘’Run/Debug Settings’. If 

there are no configurations available, create a new one using the default settings. Exit theproperties.Important!  You have to repeat this step for every new project you create.

−  Now right click on your project and choose ‘Run As  →   1 Local C/C++ Application’. Nowthe building is done again and the program executed in the console showing

Hello World!!!

meaning you just successfully compiled and ran your first program.2 If you are asked abouta debugger, choose ‘gdb/mi’, although the others will probably work as well.

2If you are receiving the error “Launch Failed. Binary Not Found.” you have probably selected the wrongtoolchain. You can create a new project with the correct toolchain, but there is a quicker way to fix this problem:right click on your project folder and choose ‘Properties’. Go to ‘C/C++ Build→ Tool Chain Editor’. Change your‘Current toolchain’ to ‘MinGW GCC’. Now go to ‘Settings’ and select the tab ‘Binary Parser’. Uncheck all but check‘PE Windows Parser’. Exit the properties and try to run again.

2

Page 3: Opengl With Eclipse

8/20/2019 Opengl With Eclipse

http://slidepdf.com/reader/full/opengl-with-eclipse 3/5

1.2.2 Ubuntu

For Ubuntu you have to install the GCC C++ compiler, which is contained in the packagebuild-essential.

−  Open a terminal and enter:

sudo apt-get install build-essential

Or, go to the ‘Synaptic Package Manager’ and enter   essential   in the search field. Selectbuild-essential  and press apply.

−  Open a terminal and test the installation with the following commands:

 make --version

g++ --version

−   Now you are ready to execute your first program in C++. In Eclipse go to ‘Window   →

Open Perspective  →  Other’ and choose ‘C/C++’. Go to ‘File  →  New  →  C++ Project’. Inthe window that opens, enter a project name and choose ‘Hello World C++ Project’ under‘Executable’. Choose ‘Linux GCC’. Click Finish. If you did everything right, your project isautomatically build and a ‘Debug’ folder is created with an  .exe  executable.

−  Now right click on your project and choose ‘Run As  →   1 Local C/C++ Application’. Nowthe building is done again and the program executed in the console showing

Hello World!!!

meaning you just successfully compiled and ran your first program.

1.3 Installing freeglut

Now that we can program in C++ we need to install freeglut. freeglut is an open-source alternativeto the GLUT (OpenGL Utility Toolkit) library. Mark Kilgard wrote GLUT originally to supportthe sample programs of the OpenGL ‘Redbook’, the official guide to learning OpenGL. freeglut wasdeveloped because the most recent version of the GLUT library (3.7) dates back to August 1998,and needed improvement.Again installing freeglut is very different on Windows and Ubuntu, so they are explained separately.

1.3.1 Windows

For Windows you have to download and extract Martin Payne’s binaries for MinGW which are

available at   http://www.transmissionzero.co.uk/software/freeglut-devel/.

−  Copy the file   freeglut.dll  to

C:\Windows\System32

−  Copy the contents of  include\GL  to the same folder of your MinGW installation, typically

3

Page 4: Opengl With Eclipse

8/20/2019 Opengl With Eclipse

http://slidepdf.com/reader/full/opengl-with-eclipse 4/5

C:\MinGW\include\GL

−  Finally, also copy the contents of  lib to the same folder of your MinGW installation, typically

C:\MinGW\lib

−  Open or restart Eclipse. Now we need to link the freeglut binaries to our compiler. Rightclick on you project and select ‘Properties’. Go to ‘C/C++ Build  → Settings’ and select thetab ‘Tool Settings’. Now select ‘Libraries’ under ‘MinGW C++ Linker’. Add the followingentries to the libraries:

glu32

opengl32

freeglut

Important!  You also need to repeat this step for every project you create.

−  You should now be able to run the example program in Sec.  1.4.  Create a new empty projectwith the correct toolchain and properties. Create a new source file (with extension   .cpp)

and copy paste the code into your source file. Now save, and run the program. If nothinghappens, check the instructions on the run and debug settings in Sec.  1.2.1.

−   Important!  Remember that if you want to share your program with other people, they needto have the same   freeglut.dll as you do.

1.3.2 Ubuntu

Again, it is easier to install freeglut in Ubuntu than it is in Windows.

−  Open a terminal and enter:

sudo apt-get install freeglut3 freeglut3-dev

Or, go to the ‘Synaptic Package Manager’ and enter  glut in the search field. Select  freeglut3

and   freeglut3-dev and press apply.

−  Open or restart Eclipse. Now we need to link the freeglut binaries to our compiler. Rightclick on you project and select ‘Properties’. Go to ‘C/C++ Build  → Settings’ and select thetab ‘Tool Settings’. Now select ‘Libraries’ under ‘GCC C++ Linker’. Add the following entryto the libraries:

glut

Important!  You need to repeat this step for every project you create.

−  You should now be able to run the example program in Sec.  1.4.  Create a new empty projectwith the correct toolchain and properties. Create a new source file (with extension .cpp) andcopy paste the code into your source file. Now save, and run the program.

1.4 Sample Program

Fig. 1  contains a sample program to test your configuration:

4

Page 5: Opengl With Eclipse

8/20/2019 Opengl With Eclipse

http://slidepdf.com/reader/full/opengl-with-eclipse 5/5

#include <GL/glut.h>

#define window_width 640

#define window_height 480

// Main loop

void main_loop_function()

{ // Z angle

static float angle;

// Clear color (screen)

// And depth (used internally to block obstructed objects)

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Load identity matrix

glLoadIdentity();

// Multiply in translation matrix

glTranslatef(0,0, -10);

// Multiply in rotation matrix

glRotatef(angle, 0, 0, 1);

// Render colored quad

glBegin(GL_QUADS);

glColor3ub(255, 000, 000); glVertex2f(-1, 1);

glColor3ub(000, 255, 000); glVertex2f( 1, 1);

glColor3ub(000, 000, 255); glVertex2f( 1, -1);

glColor3ub(255, 255, 000); glVertex2f(-1, -1);

glEnd();

// Swap buffers (color buffers, makes previous render visible)

glutSwapBuffers();

// Increase angle to rotate

angle+=0.25;

}// Initialize OpenGL perspective matrix

void GL_Setup(int width, int height)

{

glViewport( 0, 0, width, height );

glMatrixMode( GL_PROJECTION );

glEnable( GL_DEPTH_TEST );

gluPerspective( 45, (float)width/height, .1, 100 );

glMatrixMode( GL_MODELVIEW );

}

// Initialize GLUT and start main loop

int main(int argc, char** argv) {glutInit(&argc, argv);

glutInitWindowSize(window_width, window_height);

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);

glutCreateWindow("GLUT Example!!!");

glutIdleFunc(main_loop_function);

GL_Setup(window_width, window_height);

glutMainLoop();

}

Figure 1: A sample program to test your configuration.