administration mode

 

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. 

BOOKMARK THIS CONTENT
del.icio.us technorati digg Furl YahooMyWeb Reddit NewsVine

Comments: 39
Reviews: 21
Average rating: rating
Highest rating: 5
Lowest rating: 4

COMMENT: JOE rating

MAY 28, 21:55:45

comment » Exactly what I needed! Thanks! «

COMMENT: GREG emailhomepagerating

JUN 3, 10:23:23 AM

comment » Thanks for this easy to read, easy to follow tutorial!

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 homepagerating

JUN 12, 15:26:30

comment » Excellent article! Now I can cross off one more item on my list of things I could only do in Windows.

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 emailhomepage

JUN 27, 04:45:14 AM

comment » Hi,

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 email

JUL 7, 01:34:17 AM

comment » I keep getting a an error about not having a binary when I got to run locally?

Any thoughts? «

COMMENT: NATE

JUL 23, 09:17:43 PM

comment » I had the same problem as Satish with DRM_I830_CMDBUFFER: -22 on the glut tutorial, but the SDL project works great. I'm new to 3d programming so any help would be appreciated. «

COMMENT: SNORTMAN rating

AUG 12, 04:18:06 AM

comment » very useful, thank you for a straight forward tutorial «

COMMENT: NE-YAWN emailrating

AUG 23, 09:59:30 AM

comment » Thanks a LOT.It was indeed very helpful. 01 01 «

COMMENT: JEREMY

SEP 14, 08:30:36 PM

comment » Thank you so much this is exactly what I needed!!! «

COMMENT: SARANN rating

OCT 18, 20:56:48

comment » You're amazing :-D I was looking for something to guide me through this process last year and in the end just gave up. Thank you!! «

COMMENT: DENYOS rating

OCT 23, 07:30:08 PM

comment » Tnx! This was exactly what i needed to get me started! Great Job! «

COMMENT: GOE rating

OCT 27, 16:30:36

comment » Thank you very much. You are my saver. «

COMMENT: CYBRID emailrating

NOV 15, 20:03:07

comment » I found your article very useful, but I've faced a problem I'm not able to solve, If you could help me, I would be very grateful. I've setupped SDL in Eclipse 3.3 with MinGW; and that works OK, but I also want to use SDL with OpenGL, and I cannot find witch libraries I should include in the linker params.

Thanks in advance. «

COMMENT: UMAIR AHMAD KHAN

NOV 24, 05:55:56 AM

comment » i have seen few comments asking for errors while running the code. let me tell you the fix.

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. 02

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 18 «

COMMENT: FELIPE

FEB 7, 2008 - 11:16:02 PM

comment » Thanks for this. I really agree with what someone said above. Little and sometimes even simple barriers like not being able to integrate an IDE with some framework, etc., is what keeps people from trying out programming in Linux. And the same goes for many other topics on Linux.... «

COMMENT: MACIEK

MAR 11, 2008 - 11:39:37 PM

comment » @UMAIR AHMAD KHAN

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 rating

MAR 19, 2008 - 09:35:51 AM

comment » thank you for your help. Now I am programming with OpenGl under linux.

:) «

COMMENT: --- rating

MAR 22, 2008 - 05:01:35 AM

comment » Wow, this was exactly what I needed. Setting up OpenGL development with Eclipse on Ubuntu. And it worked. Hooray! «

COMMENT: SAUS

APR 8, 2008 - 09:30:07 PM

comment » I am having a bit of a problem with using the c++ project. All the opengl references are undefined. Odd thing is that if I put them in a c project all is well. I figure that opengl was compiled with a c compiler and not able to be used in g++. How do I fix this though?

05 «

COMMENT: SAUS

APR 8, 2008 - 09:48:30 PM

comment » Ok I have been doing a bit more research and I have found that the problem is with Eclipse. For some reason Eclipse does not like something with my C++ project. I will do some more fiddling and report the fix if I can come up with it. «

COMMENT: RYAN

APR 12, 2008 - 02:48:55 AM

comment » This was super helpful, thank you. «

COMMENT: ASMUND

APR 21, 2008 - 10:31:33 AM

comment » Thankyou! Just what I needed. «

COMMENT: TANAWAT TASSANA rating

APR 23, 2008 - 04:09:43 AM

comment » Thank you for this awesome tutorial!!! 18 «

COMMENT: TIBOLOGY rating

MAY 1, 2008 - 07:45:31 PM

comment » Cool, Thanx... «

COMMENT: CHRIS

MAY 25, 2008 - 11:33:18 PM

comment » Finally! After searching all day, reading reading this article solved my problems. Thanks! «

COMMENT: CORY rating

JUN 5, 2008 - 05:12:20 AM

comment » Thanks a metric ton! I just left my cozzy windows world and I was starting to get worried :) «

COMMENT: CURRAN emailhomepagerating

JUN 5, 2008 - 17:29:49

comment » This guide was very useful, thanks a lot! For those interested - have posted an up to date version of it (including GLEW also) for the latest versions of Eclipse and Ubuntu on my blog

http://lifeofaprogrammergeek.blogspot.com/2008/06/opengl-glut-and-glew-in-eclipse.html

Thanks again! It was a huge help! «

COMMENT: THGC rating

JUL 21, 2008 - 16:03:20

comment » Great job dude, worked fine for me, thanks «

COMMENT: NICK emailhomepage

JUL 26, 2008 - 11:09:03 AM

comment » Thanks a lot. This guide really helped me to begin with eclipse and opengl. «

COMMENT: SWTSVN

SEP 3, 2008 - 08:48:36 PM

comment » hi

could u also tell us hwo to install glew in windows eclipse? «

COMMENT: RICHARD THOMPSON emailhomepage

SEP 7, 2008 - 11:32:28

comment » Awesome guide man, I've wanted to do something like this in Linux for a long time, I'm new to the eclipse IDE and I love it, everything worked fine after a couple of tweaks, again, thanks for everything! 01 «

COMMENT: RICHARD THOMPSON emailrating

SEP 7, 2008 - 11:33:22

comment » Submit as Awesome «

COMMENT: V rating

SEP 8, 2008 - 08:44:56

comment » Just great, thanks. 18 «

COMMENT: SIMON rating

SEP 12, 2008 - 02:47:33 AM

comment » awesome!!! 18 «

COMMENT: MAURO

SEP 16, 2008 - 01:14:14 PM

comment » Thanks!!!!!

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 rating

SEP 21, 2008 - 08:53:50 AM

comment » Thanks a lot for this nice overview.

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

comment » Hi, I'm noob with Eclipse, I did all what you sad but I received a lot of error like those:

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 » Hi everybody, I solve my problem re-installing the latest nvidia drivers for my ubuntu, bye and thanks for this guide 01 «

COMMENT: Z rating

NOV 5, 2008 - 04:16:38 DU.

comment » Thanks a lot, now it works for me too! «

RATE THIS CONTENT (OPTIONAL)
Was this document useful to you?
 
rating Awesome
rating Good
rating Average
rating Poor
rating Useless
CREATE A NEW COMMENT
required field
required field HTML is not allowed. Hyperlinks will automatically be converted.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30