Programming openGL in Linux

OpenGL is a graphics library that can be used universally i.e various platforms like Linux, Windows, Mac . I will basically concentrate on linux. We will write down a C program to display a pencil using openGL
You can get the C source file here
pencil in c

The program will start with adding up the following header files

#include<stdio.h>
#include<stdlib.h>
#include<GL/glu.h>
#include<GL/glx.h>
#include<X11/Xlib.h>

we used stdio.h and stdlib.h because we need functions like printf() and exit().
In this we use Xlib. Xlib is a library which is used to manage applications under X window system. Xlib is a network oriented. This means An application which is running on computer A can send its graphical output to computer B, which is located somewhere else in the network (the network can be a LAN as well as the internet), and can receive events like keyboard or mouse input from computer B. This requires that a program called the “X-Server” is running on both computers.In linux we start a X server by typing startx in terminal. But most probably it dosent need to start up manually.It automatically starts when we log in our computer

Next we will use some variables like

Display *dpy;
Window root;
GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
XVisualInfo *vi;
Colormap cmap;
XSetWindowAttributes swa;
Window win;
GLXContext glc;
XWindowAttributes gwa;
XEvent xev;

Their use wil be cleared as we go deep in this program. Lets get to the drawing part of this program :P

Its better to make a different function for this part and we will call it from the main function afterwards. As we are going to draw a pencil lets name it pencil.

void pencil()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1., 1., -1., 1., 1., 20.);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0., 0., 10., 0., 0., 0., 0., 1., 0.);

We used glclearcolor() functions as it sets the background color white.
glMatrixMode() sets the current matrix mode.there are basically four modes to choose from. They are
GL_MODELVIEW
Applies subsequent matrix operations to the modelview matrix stack.
GL_PROJECTION
Applies subsequent matrix operations to the projection matrix stack.
GL_TEXTURE
Applies subsequent matrix operations to the texture matrix stack.
GL_COLOR
Applies subsequent matrix operations to the color matrix stack.

//tip of pencil
//upper part
glBegin(GL_QUADS);
glColor3f(0. ,0., 0.); glVertex3f(-.65,.90 ,0.);
glColor3f(0. ,0., 0.); glVertex3f(-.62,.95 ,0.);
glColor3f(0. ,0., 0.); glVertex3f(-.59,.90 ,0.);
glColor3f(0. ,0., 0.); glVertex3f(-.65,.90 ,0.);
//lowerpart
glColor3f(1. ,0.5, 0.); glVertex3f(-.65, .90, 0.);
glColor3f(1. ,0.5, 0.); glVertex3f(-.59, .90, 0.);
glColor3f(1. ,0.5, 0.); glVertex3f(-.50, .75, 0.);
glColor3f(1. ,0.5, 0.); glVertex3f(-.75, .75, 0.);

glEnd();

It creates the upper tip of the pencil. glColor3f specifies which color to use for a particular vertex.It uses the RGB mode.the value of R,G,B can be between 0 and 1.1
glVertex3f sets teh coordinate x,y,z

//lower black rectangle
glBegin(GL_QUADS);
glColor3f(0., 0., 0.); glVertex3f(-.75, -.75, .0);
glColor3f(0., 0., 0.); glVertex3f(-.50, -.75, .0);
glColor3f(0., 0., 0.); glVertex3f(-.50, .75, .0);
glColor3f(0., 0., 0.); glVertex3f(-.75, .75, .0);

glEnd();

//lower red part
glBegin(GL_QUADS);
glColor3f(1. ,0., 0.); glVertex3f(-.75, -.75, 0.);
glColor3f(1. ,0., 0.); glVertex3f(-.50, -.75, 0.);
glColor3f(1. ,0., 0.); glVertex3f(-.50, -.85, 0.);
glColor3f(1. ,0., 0.); glVertex3f(-.75, -.85, 0.);
glEnd();
}

by this our drawing is over. Now lets concentrate on the main function.

Now just we have to call our pencil() function from main function. To get knowledge about what to write in main function please refer to the orignal article.
Orignal article

About these ads

2 thoughts on “Programming openGL in Linux

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s