Tags

, ,

When a newbie wants to get hands in opengl, he feels lost. He doesn’t get the place where and how to start. In this article I gonna discuss how to get started with opengl on a Linux host.
I gonna use Ubuntu Linux distro as my host environmeet. Following are the prerequisites to compile my Hello World program. Let’s have a glimpse of these.

1. GCC : Ubuntu is shipped with the GCC compiler, which we would use for compiling our source code.
2. Make Uitility : Make utility is basically used to manage the source code of the project. It is a command line tool. Ubuntu is shipped with this as well.
3. Freeglut : Freeglut is basically an API to perform basic opengl drawing and rendering those on the screen. It is the open source version of the GLUT(OpenGL Utility Toolkit). Ubuntu is not shipped with the Freeglut. So you need to intall this package on you host machine. You can do this easily by executing following command if you are connected the the Internet.

sudo apt-get install freeglut3-dev

4. GLU : It is a open source library consists of a number of functions that use the base OpenGL library to provide higher-level drawing routines from the more primitive routines that OpenGL provides. You can install this libaray by executing the following command.

sudo apt-get install libGLU

Now we are ready to write some code. First of all create a folder with the name of your choice in which we gonna put our source file main.c.
The following is the content of the file main.c.

#include 
#include 


GLfloat xrot, yrot, zrot;

void resize(int width, int height)
{
    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0f, (GLfloat)width/(GLfloat)height, 1.0f, 1000.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    glEnable(GL_CULL_FACE);
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glTranslatef(0.0f,0.0f,-5.0f);
	glRotatef(xrot,1.0f,0.0f,0.0f);
	glRotatef(yrot, 0.0f, 1.0f, 0.0f);
	glRotatef(zrot, 0.0f, 0.0f, 1.0f);
	glBegin(GL_QUADS);

		glColor3f(0.0f,1.0f,0.0f);
		glVertex3f( 1.0f, 1.0f,-1.0f);
		glVertex3f(-1.0f, 1.0f,-1.0f);
		glVertex3f(-1.0f, 1.0f, 1.0f);
		glVertex3f( 1.0f, 1.0f, 1.0f);

		glColor3f(1.0f,0.5f,0.0f);
		glVertex3f( 1.0f,-1.0f, 1.0f);
		glVertex3f(-1.0f,-1.0f, 1.0f);
		glVertex3f(-1.0f,-1.0f,-1.0f);
		glVertex3f( 1.0f,-1.0f,-1.0f);

		glColor3f(1.0f,0.0f,0.0f);
		glVertex3f( 1.0f, 1.0f, 1.0f);
		glVertex3f(-1.0f, 1.0f, 1.0f);
		glVertex3f(-1.0f,-1.0f, 1.0f);
		glVertex3f( 1.0f,-1.0f, 1.0f);

		glColor3f(1.0f,1.0f,0.0f);
		glVertex3f( 1.0f,-1.0f,-1.0f);
		glVertex3f(-1.0f,-1.0f,-1.0f);
		glVertex3f(-1.0f, 1.0f,-1.0f);
		glVertex3f( 1.0f, 1.0f,-1.0f);

		glColor3f(0.0f,0.0f,1.0f);
		glVertex3f(-1.0f, 1.0f, 1.0f);
		glVertex3f(-1.0f, 1.0f,-1.0f);
		glVertex3f(-1.0f,-1.0f,-1.0f);
		glVertex3f(-1.0f,-1.0f, 1.0f);

		glColor3f(1.0f,0.0f,1.0f);
		glVertex3f( 1.0f, 1.0f,-1.0f);
		glVertex3f( 1.0f, 1.0f, 1.0f);
		glVertex3f( 1.0f,-1.0f, 1.0f);
		glVertex3f( 1.0f,-1.0f,-1.0f);

	glEnd();

	xrot += 0.2f;
	yrot += 0.1f;
	zrot += 0.3f;

    glutSwapBuffers();
}

void key(unsigned char key, int x, int y)
{
    switch (key)
    {
        case 27 :
            exit(0);
            break;
        default:
            break;
    }

    glutPostRedisplay();
}

void idle()
{
    glutPostRedisplay();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("HELLO WORLD");
    glutReshapeFunc(resize);
    glutDisplayFunc(display);
    glutKeyboardFunc(key);
    glutIdleFunc(idle);

    glutMainLoop();
}

If you are working on opengl first this you might not get much out of this. But don’t worry our intention is to just run our first opengl code. In my next articles I gonna explain you this in detail.

Now we gonna write a simple makefile which would be used by the make utility to compile main.c. We can also compile the main.c without makefile by executing the following command.

gcc -g -o hello main.c -lglut -lGLU

But use of the makefile makes it easy to compile and manage a large source by writing all details of the code in the makefile. Without makefile it is hard to manage a large number of source file directly on command line. Following is the makefile named Makefile to compile the main.c.

all:
	gcc -g -o hello main.c -lglut -lGLU

Now we are ready to compile the code we have written. Now just open terminal window. Change the present working directory to the directory you have created by following command.

cd path-of-your-directory

Now execute command.

make 

If the compilation would get done successfully ‘hello’ an executable file would get generated. Now run the ‘hello’ executable by typing following commmand on the terminal.

./hello

and here is the HelloWorld program running on your screen.
HelloOpenGL

Advertisements