Article: OpenGL programming in Eclipse »
FERDY CHRISTANT - APR 26, 2007 (07:29:25 PM)
In an earlier article, I explained how to setup portable OpenGL programming in Visual Studio on Windows. Given my recent switch to Linux, I'm moving as much as I can to my Ubuntu setup. I've bend my mind around setting this up, and wasted many hours, only to find out how simple it really is. Goal is to setup OpenGL programming in Eclipse, with C++ as the programming language. Since there is little info on how to do this online, I'll hereby explain it, for others to take and for myself to remember should I ever have to do it again. Instructions should be similar, but vary a little, for other Linux distributions and IDEs.
Setting up Eclipse for C/C++ programming
Luckily, I have previously written an article about setting up Eclipse for C/C++ programming. By simply following the instructions in the article, you should be able to install Eclipse Callisto. The article also mentions that you need to install the C++ compiler and other tooling on top of it, since that is not part of Eclipse. For Ubuntu, this is a matter of installing g++ and gdb from the package manager. After that, you should be able to compile and run the helloworld example in forementioned example.
Installing GLUT and/or SDL
Before we continue the installation, we have a choice to make. On Linux, there are two major ways to do OpenGL programming, via GLUT or via SDL. In essence, both libraries make use of OpenGL, yet the way you interact with OpenGL commands differs. GLUT seems to be the most commonly used library, yet it has its limitation in the way you can control application flow. By contrast, the SDL library is sometimes more complex, but also more powerful. Both are simply a layer between your logic and the OpenGL API that in addition allow you to do window management and hardware interactions. Think of drawing screens, capturing keyboard input, loading images and playing sounds. For the remainder of this article, I will demonstrate how to setup both libraries and let you choose.
On my Ubuntu installation, SDL was already installed. I'm not sure of that is a default or not. To check what is installed, browse to your usr/include directory and check for the SDL directory. If it does not exist, use the package manager to install it.
To install GLUT, use the package manager to install the packages freeglut3 and freeglut3-dev. The library header files will be installed in /usr/include/GL. If you've made it this far, you are ready to put your setup to the test.
A simple GLUT project
Time to have some fun. In Eclipse, create a new project: File->New->Managed Make C++ Project. Name it "opengl_glut". After creation the project, you can expand it and browse through the includes. By default, everything in /usr/include is included in your project. This is good, this means that we can access the GLUT header files without any extra steps.
Before we can start the coding, there is one more thing to do. We need to tell the linker what libraries to include in the build. To do this, right-click on your project, choose properties. Next, choose C/C++ Build, and then Libraries under the GCC C++ Linker section. In the field libraries, add the value "glut". Click OK to confirm the project settings.
Finally, done with the setup. Time for some code. Create a new source file below your project and call it "main.cpp". Paste the following code in the file:
#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;
}
// Initialze 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();
}
Let's run it. Right-click on your project in the outline, and choose Run as -> Local C/C++ Application. You should now see a rotating cube:
A simple SDL project
Creating a SDL project is very similar to a GLUT project. There is one difference in the setup, instead of telling the linker to include the "glut" library, you tell it to include the "SDL", "GLU" and "GL" libraries:
The only other difference is of course in your code. Again create a "main.cpp" source file. This time, paste the following code in the file:
#include <GL/gl.h>
#include <GL/glu.h>
#include <SDL/SDL.h>
#define window_width 640
#define window_height 480
// Keydown booleans
bool key[321];
// Process pending events
bool events()
{
SDL_Event event;
if( SDL_PollEvent(&event) )
{
switch( event.type )
{
case SDL_KEYDOWN : key[ event.key.keysym.sym ]=true ; break;
case SDL_KEYUP : key[ event.key.keysym.sym ]=false; break;
case SDL_QUIT : return false; break;
}
}
return true;
}
void main_loop_function()
{
float angle;
while( events() )
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,0, -10);
glRotatef(angle, 0, 0, 1);
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();
SDL_GL_SwapBuffers();
// Check keypresses
if( key[SDLK_RIGHT] ){ angle-=0.5; }
if( key[SDLK_LEFT ] ){ angle+=0.5; }
}
}
// Initialze 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, 0.1, 100 );
glMatrixMode( GL_MODELVIEW );
}
int main()
{
// Initialize SDL with best video mode
SDL_Init(SDL_INIT_VIDEO);
const SDL_VideoInfo* info = SDL_GetVideoInfo();
int vidFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER;
if (info->hw_available) {vidFlags |= SDL_HWSURFACE;}
else {vidFlags |= SDL_SWSURFACE;}
int bpp = info->vfmt->BitsPerPixel;
SDL_SetVideoMode(window_width, window_height, bpp, vidFlags);
GL_Setup(window_width, window_height);
main_loop_function();
}
Run the project. Notice that the cube does not rotate initially, but you can using the left and right arrows of your keyboard.
Closing
This article is all about getting you started in OpenGL programming on Linux using Eclipse. I'll leave it up to you to actually learn what OpenGL can do and how you can use it, but trust me: the possibilities are endless and tons of fun :)
Disclaimers: This article assumes you have properly installed drivers for your video hard and that you have hardware acceleration working. This article is based upon a very helpful Ubuntu forum post, but narrowed down to the usage of Eclipse.



Comments: 89
Reviews: 45
Average rating:
Highest rating: 5
Lowest rating: 4
COMMENT: JOE
MAY 28, 21:55:45
COMMENT: GREG


JUN 3, 10:23:23 AM
I have really enjoyed working with Eclipse for doing Java programming. I was hoping there was a plugin that allowed me to use it's nice interface, handy automatic compiling, and especially CVS integration for OpenGL and C/C++ (I sign up for a new free 10MB CVS account at http://www.cvsdude.com for most major projects I attempt now, and it integrates easily into Eclipse).
Thanks for taking the time to put this online. It was the first Google result for "eclipse c++ opengl", and it was a perfect find.
Thanks again! «
COMMENT: LIAM

JUN 12, 15:26:30
I believe that programming games is key to learning how to become a good programmer. Until now I could not transfer my OpenGL skills from Windows/Visual C++ over to Linux. I kept losing interest before I had set up the environment I needed (IDE + API + compiler).
Tutorials like this one could help thousands of skilled programmers make it past that initial barrier and start making great 3D software for Linux!
Thanks again! «
COMMENT: SATISH VELLANKI

JUN 27, 04:45:14 AM
Thanks for the easy example.
I have a problem executing it.
I get (only) this error message on console:
DRM_I830_CMDBUFFER: -22
I am running on Ubuntu 7 with Beryl. I disabled Beryl and it works like a charm.
How can I make it work all the time?
Thanks in advance «
COMMENT: ZACHARY MANNING
JUL 7, 01:34:17 AM
Any thoughts? «
COMMENT: NATE
JUL 23, 09:17:43 PM
COMMENT: SNORTMAN
AUG 12, 04:18:06 AM
COMMENT: NE-YAWN

AUG 23, 09:59:30 AM
COMMENT: JEREMY
SEP 14, 08:30:36 PM
COMMENT: SARANN
OCT 18, 20:56:48
COMMENT: DENYOS
OCT 23, 07:30:08 PM
COMMENT: GOE
OCT 27, 16:30:36
COMMENT: CYBRID

NOV 15, 20:03:07
Thanks in advance. «
COMMENT: UMAIR AHMAD KHAN
NOV 24, 05:55:56 AM
i tried this tutorial on suse 10.3 and eclipse 3.3.0 eu when i installed the plugin there was no managed make c++ option and i was getting same error as mentioned in this comment.
"I keep getting a an error about not having a binary when I got to run locally?"
create an empty project right click and then properties -> c/c++ build then setting and under tool setting tab you can find gcc C++ linker settings and can add library
also as there is no managed make project so you need to right click -> make target -> create write name and target for make file
right click again -> make target -> build you can see the target name you mentioned in previous step.
press build
now run it will not give any error.
well i am noob on linux so i most prolly over doing few steps that may not be required. but it worked so me happy
«
COMMENT: FELIPE
FEB 7, 2008 - 11:16:02 PM
COMMENT: MACIEK
MAR 11, 2008 - 11:39:37 PM
I had this problem with CDT 4.0.2. In 4.0.3 there is no "managed" project but the respective fields are already present. «
COMMENT: GG
MAR 19, 2008 - 09:35:51 AM
:) «
COMMENT: ---
MAR 22, 2008 - 05:01:35 AM
COMMENT: SAUS
APR 8, 2008 - 09:30:07 PM
COMMENT: SAUS
APR 8, 2008 - 09:48:30 PM
COMMENT: RYAN
APR 12, 2008 - 02:48:55 AM
COMMENT: ASMUND
APR 21, 2008 - 10:31:33 AM
COMMENT: TANAWAT TASSANA
APR 23, 2008 - 04:09:43 AM
COMMENT: TIBOLOGY
MAY 1, 2008 - 07:45:31 PM
COMMENT: CHRIS
MAY 25, 2008 - 11:33:18 PM
COMMENT: CORY
JUN 5, 2008 - 05:12:20 AM
COMMENT: CURRAN


JUN 5, 2008 - 17:29:49
http://lifeofaprogrammergeek.blogspot.com/2008/06/opengl-glut-and-glew-in-eclipse.html
Thanks again! It was a huge help! «
COMMENT: THGC
JUL 21, 2008 - 16:03:20
COMMENT: NICK

JUL 26, 2008 - 11:09:03 AM
COMMENT: SWTSVN
SEP 3, 2008 - 08:48:36 PM
could u also tell us hwo to install glew in windows eclipse? «
COMMENT: RICHARD THOMPSON

SEP 7, 2008 - 11:32:28
COMMENT: RICHARD THOMPSON

SEP 7, 2008 - 11:33:22
COMMENT: V
SEP 8, 2008 - 08:44:56
COMMENT: SIMON
SEP 12, 2008 - 02:47:33 AM
COMMENT: MAURO
SEP 16, 2008 - 01:14:14 PM
I spent hours and hours trying to figure out what librarys to include. I was using GLUT and including GL instead of simply including "glut" :S «
COMMENT: VANGUARD2K
SEP 21, 2008 - 08:53:50 AM
Although i have programmed cpp several times i have never managed to set up eclipse correctly (maybe partly because of lazyness).
With this guide i had no problem to run the test programs. Nice!
Thanks for it! «
COMMENT: LEONARD
OCT 12, 2008 - 02.56.52
freeglut (/home/myname/workspace/firstglut/Debug/firstglut): Unable to create direct context rendering for window 'GLUT Example!!!'
This may hurt performance.
X Error of failed request: BadMatch (invalid parameter attributes)
Major opcode of failed request: 161 (GLX)
Minor opcode of failed request: 5 (X_GLXMakeCurrent)
Serial number of failed request: 37
Current serial number in output stream: 37
Locking assertion failure. Backtrace:
#0 /usr/lib/libxcb-xlib.so.0 [0xb6ce4767]
#1 /usr/lib/libxcb-xlib.so.0(xcb_xlib_lock+0x2e) [0xb6ce481e]
#2 /usr/lib/libX11.so.6 [0xb7a6e518]
#3 /usr/lib/libX11.so.6(XESetCloseDisplay+0x31) [0xb7a518d1]
#4 /usr/lib/libGL.so.1 [0xb7bee7e9]
Maybe I have to switch output from console to a graphical output?!!
Also when I right click on a c++ project and "Run as" I can't find the option C/C++ local application....I don't know why. Right now eclipse is a mystery for me...some suggestion?
Bye «
COMMENT: LEONARD
OCT 12, 2008 - 11.24.20
COMMENT: Z
NOV 5, 2008 - 04:16:38 DU.
COMMENT: JEREMY
DEC 21, 2008 - 10:23:20 AM
COMMENT: KP
JAN 28, 2009 - 09:23:18 PM
COMMENT: MM
JAN 30, 2009 - 06:37:09 AM
COMMENT: SREEKAR

FEB 1, 2009 - 07:10:55 AM
thanks «
COMMENT: MARTON BIRO
MAR 10, 2009 - 09:34:58 DU.
COMMENT: BART
JUN 23, 2009 - 10:20:15 PM
COMMENT: GURIA
JUN 28, 2009 - 15:48:55
COMMENT: JACOB
JUL 3, 2009 - 10:16:12 PM
COMMENT: EDGAR
JUL 23, 2009 - 10:33:28 PM
You Rock!
Had no problem at all
Thanks!
«
COMMENT: LINDSAY

AUG 7, 2009 - 11:11:06 AM
COMMENT: MARTIN FROM GERMANY
OCT 14, 2009 - 15:53:12
COMMENT: BJ
NOV 15, 2009 - 02:07:46 PM
COMMENT: JOHN
NOV 17, 2009 - 03:37:16 AM
Solution was to click on Project up in the menu option, and then do a Build All from there.
Then go back and do a Run As «
COMMENT: DANGERLEM
NOV 29, 2009 - 03:32:43 PM
COMMENT: DEWESH KUMAR

DEC 29, 2009 - 11:08:17 AM
thanks for that.
How to get glaux for linux?
Thanks in advance.......... «
COMMENT: ANDREAS
JAN 2, 2010 - 06:31:26 PM
1337 thumbs up! «
COMMENT: MAN

JAN 13, 2010 - 12:36:25 AM
COMMENT: TASH
JAN 17, 2010 - 09:02:34 AM
Saved me uncountable hours
Cheers
«
COMMENT: RUSSELL THACKSTON


JAN 27, 2010 - 03:46:03 PM
COMMENT: MARIJA
FEB 9, 2010 - 11:09:46 AM
This is great!!!!
I love to work in eclipse and I'm new in openGL
so this is great combination!!!!
Works for me:)))
«
COMMENT: MERGE
FEB 12, 2010 - 23:35:26
COMMENT: DUCK
FEB 19, 2010 - 16:33:12
COMMENT: BLAH
FEB 28, 2010 - 11:02:09 AM
COMMENT: DMITRIY P.
MAR 21, 2010 - 21:31:02
COMMENT: NOSICLIN
MAR 23, 2010 - 12:47:20 AM
COMMENT: DEEP
APR 18, 2010 - 12:53:44 PM
X Error of failed request: BadRequest (invalid request code or no such operation)
Major opcode of failed request: 143 (GLX)
Minor opcode of failed request: 19 (X_GLXQueryServerString)
Serial number of failed request: 11
Current serial number in output stream: 11
Please help me to figure out the problem. «
COMMENT: ANONYMOUS
APR 25, 2010 - 03:47:17
COMMENT: ASHOK J

MAY 22, 2010 - 04:29:31 AM
Thanks a lot.
There is a small correction for the libraries needed to make the first programing running.
It was not working with just adding glut into the libraries. I had to add GL and GLU also to make it working.
Even with that error .. your work is awsome.
thanks for your effort.
regards,
ashok. «
COMMENT: ALBERT
MAY 29, 2010 - 12:43:11 AM
this entire time i was missing the 'freeglut3-dev' package. I spent days manually trying to track down missing libraries.
those were two of the most important packages (along with their dependencies). If you didn't have those two packages installed; program won't compile. «
COMMENT: VV
JUN 15, 2010 - 07:53:12 AM
COMMENT: CUONG

JUN 30, 2010 - 02:21:38 AM
Howver, I've got a linker error even though I followed exactly what described in the tut:
"undefined reference to gluPerspective"
It took me a few days to find out that the solution is to add 'GLU' in the Libraries section together with 'glut' (GLU must be capital). «
COMMENT: CODER


AUG 25, 2010 - 06:54:06 PM
COMMENT: MIKE
SEP 19, 2010 - 18:10:49
COMMENT: ED
OCT 30, 2010 - 08:56:53 AM
COMMENT: ALEX
NOV 17, 2010 - 01:02:47 AM
COMMENT: JAMES
JAN 5, 2011 - 07:06:19 AM
COMMENT: HAHN


FEB 3, 2011 - 04:18:25
and thanks to Alex.. i have same problems, can't compile that source. «
COMMENT: OSCAR WILDE

MAR 22, 2011 - 11:29:58 PM
HAHAHAHAHAHAHAHAHAHA! I'm using HTML! Neener Neener! «
COMMENT: PAULO CHEADI HADDAD FILHO
APR 3, 2011 - 10:35:46 PM
Great article!
«
COMMENT: BRUNO HENRY

APR 16, 2011 - 03:21:31
COMMENT: MIGUEL ORTIZ
JUN 22, 2011 - 05:29:17
When you add libraries in your project, you should add -lglut and -lGLU, that means add GLU in "Libraries" option when you are using eclipse «
COMMENT: MARTIN R
OCT 27, 2011 - 20:39:26
COMMENT: LINH

DEC 7, 2011 - 09:36:28 AM
COMMENT: TOMIG90
JAN 6, 2012 - 01:58:18 DU.
I do the tutorial on ubuntu 10.04 LTS.
I've got a problem:
Every glut method is OK, but every "glu*" is undefined reference: (
Even when i have this 3 line:
#include
#include
#include
gluPerspective or gluOrtho2d still don't work for me.
( i even tried to copy the name from the header, but it's still bad:(
someone any idea?
PS: sorry for my bad english :) «
COMMENT: AUTOGLITCH
JAN 8, 2012 - 05:47:01 PM
I've got the latest version of eclipse and here is my step-by-step:
-Open your project
-Right click on the Project Folder in the "project explorer" window
-Select "Properties"
-Go to C/C++ Build > Settings
-Under the "Tools Settings" tab scroll down and expand "GCC C++ Linker"
-Select "Libraries"
-The top box is "Libraries (-l)". you should have both "glut" and "GLU" listed. If either are not then click the add button and type them in without quotes.
Save and recompile. I hope this helps. It's been a beast trying to learn linux, C++ and OpenGL all at ones :-) «
COMMENT: DEXTER
JAN 11, 2012 - 01:34:59 PM
make: *** No rule to make target `sample1'. opengl_glut line 0 C/C++ Problem""
i encountered the above error while following the above mentioned steps.....
pls help.... «
COMMENT: SSY
FEB 11, 2012 - 16:55:16
Howver, I've got a linker error even though I followed exactly what described in the tut:
"undefined reference to gluPerspective"
It took me a few days to find out that the solution is to add 'GLU' in the Libraries section together with 'glut' (GLU must be capital). "
Thanks your comment has been super useful, and thanks for this tutorial too! «
COMMENT: RAY

FEB 20, 2012 - 05:38:39 PM
COMMENT: MARCELO
APR 18, 2012 - 23:01:46
Im using Ubuntu 11.04.
The package that i use are: freeglut3-dev for glut and libsdl1.2-dev for sdl.
In the example code for glut, the words for include in library session is "glut" and "GLU". However, the error "undefined reference to `gluPerspective'" is showed.
In the example for SDL, if i include the "GL" in libraries another error is formed while build "undefined reference to `glMatrixMode'". Just remove the reference to "GL" in libraries and everything works fine.
Thnks very much. «
COMMENT: ASH
JAN 31, 2013 - 09:27:46 AM
Thanks «