• Home
  • About
  • Tech
  • Projects
  • Contact
TjWallas.DevOps().Labs>

OpenGL Development on windows 7 . freeglut + eclipse + GNU C/C++ Compiler MinGW ftw

12/11/2010

 
Because I am a visual studio hater. And face it, The GNU C/C++ compiler is the best C Compiler ever made ! so I thought why not Eclipse + GNU compiler ftw. The trick was how to use GNU C Compiler on windows. Another trick was how to set these outdated OpenGL libraries to work on windows 7 x64 with eclipse + GNU C Compiler.

MinGW GNU C,C++ Compilers & freeglut

First of all, you need to install MinGW. Which is a minimal GNU platform on windows and it allows you to install many GNU tools including the GNU C and C++ Compilers. Download their automated installer from here. Make sure you are connected to the internet before running the downloaded exe file because it will download the necessary packages for installation process. Also make sure you are running it as an administrator (Right click -> "Run as Administrator"). 


During the installation, you'll be prompted to select which packages you want to install. Make sure you check the C Compiler and the C++ Compiler. Other than that don't mess with the installation settings. It might ruin your installation (At least it did for me when I tried to change the installation path).


Next, you'll need to download the freeglut libraries which are way dated than the very old OpenGL libraries because freeglut is opensource and is still alive. You can either download the source and compile your own package (Which is what I did). Or, you can download this very nice pre-compiled zip archive from here


All you have to do Next is to extract this zip archive anywhere then:
  1. Copy the freeglut.dll to “C:\Windows\System32″ or "C:\Windows\SysWOW64" if you are an x64 user.
  2. Copy the contents of include\GL to “C:\MinGW\include\GL”
  3. Copy the contents of lib to “C:\MinGW\lib”
If it all goes well, the following command in your command prompt should show an output:
 gcc --version
 g++ --version 

Eclipse for C/C++ Devs

Now, all what's left is to download and configure eclipse for C/C++ and OpenGL development.
1. Download and extract Eclipse for C/C++ from here .
2. Run Eclipse as Administrator then File -> New C++ Project.
3. Uncheck "Show project type and toolchains ...."
4. Make sure MinGW GCC is selected from the list of toolchains on the right.

5. Enter a name for your project and then click finish.
6. Right click your project main folder from the project explorer and click properties.


7. From the left menu expand C/C++ build and click settings.
8. From the submenu that appears Click Libraries, which is found under MinGW C++ linker add the following libraries using the tiny add button at the top-right: "glu32" , "opengl32" , "freeglut" (Without the quotes). Then finally, click ok.
Note that u'll need to repeat these steps for every project you create. Now, eclipse is properly configured with freeglut. The next thing u need to do is creating a new c++ source file so: File -> New ->Source file. Then enter a name for you new file. Maybe test.cpp.


Copy and paste the following code and hit the green run button at the top or press ctrl + F11.
#include <windows.h>
#include <Gl.h>
#include <GL/glut.h>

void myInit(void) {
	glClearColor(1.0, 1.0, 1.0, 0.0);
	glColor3f(0.0f, 0.0f, 0.0f);
	glPointSize(4.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}
void myDisplay(void) {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	//First Line
	glLineWidth(8.0f);

	glBegin(GL_LINE_STRIP);
	glColor3ub(255, 0, 0);
	glVertex2i(40, 380);
	glColor3ub(0, 255, 0);
	glVertex2i(600, 380);

	glEnd();

	//Second Line
	glLineWidth(8.0f);

	glBegin(GL_LINE_STRIP);
	glColor3ub(0, 0, 255);
	glVertex2i(40, 250);
	glColor3ub(0, 255, 0);
	glVertex2i(600, 250);

	glEnd();

	//Third Line
	glLineWidth(8.0f);

	glBegin(GL_LINE_STRIP);
	glColor3ub(255, 0, 0);
	glVertex2i(40, 120);
	glColor3ub(0, 0, 255);
	glVertex2i(600, 120);

	glEnd();

	glFlush();
}

int main(int argc, char** argv) {
	//GLUT & OpenGL
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(640, 480);
	glutInitWindowPosition(100, 150);
	glutCreateWindow("Hello OpenGL");
	glutDisplayFunc(myDisplay);
	myInit();
	glutMainLoop();

}
If you did it right, you should see an output similar to this:
Credit: Some images were taken from this post by elleestCrimi

Comments are closed.
Powered by Create your own unique website with customizable templates.