Wednesday, April 16, 2014

OpenGL Program

OpenGL is an API for rendering vector graphics, it's awesome and there's some code out there written in Python, C, C++ with some nice examples. It was developed in 90s and used in CAD tools, simulation renderings, video games and other great stuff. Anyways I developed a very straight forward piece of code that draws 3 triangles. The utility toolkit can be downloaded here:

http://www.opengl.org/resources/libraries/glut/

or

http://user.xmission.com/~nate/glut.html

(for windows)

I used Cygwin to compile my code.

#include <stdio.h>  
#include <GL/gl.h>
#include <GL/glut.h>

#define KEY_ESCAPE 27 // definition for exiting program with key
#define RAFAEL 127 // definition added to exit program with the key delete as well.

void display();
void ThisInit();
void keyboardesc(unsigned char, int, int);

/* argc is the number of arguments passed to the executable, argv contains the actual arguments
   argv is a pointer to an array of null terminated strings, and argv is how large the array is 
   like such:
 c:\test.exe hello world
 Then argc=3
 argv[0]="c:\test.exe"
 argv[1]="hello"
 argv[2]="world"   
*/

/* GLUT set up and initialization */
int main(int argc, char **argv) 
{
 glutInit(&argc, argv);         // GLUT initialization
 glutInitWindowSize(500, 500);       // set window size
 glutInitWindowPosition(100, 100);      // set window position
 glutCreateWindow("OpenGL Window");                                              // create window with specific title
 glutDisplayFunc(display);        // register display function
 glutKeyboardFunc(keyboardesc);       // register Keyboard Handler
 
 ThisInit();         
 glutMainLoop();              // run GLUT mainloop    
 return 0;
}

/* Initialization function for the window with OpenGl settings*/
void ThisInit() {
 glClearColor(1.0, 1.0, 1.0, 0.0);  /* red,gree,blue,alpha */
 glShadeModel(GL_SMOOTH);
 glEnable(GL_BLEND);
 glEnable(GL_TEXTURE_2D);
}

/* display function draws what the window contains
   This functions gets called repeatedly by GLUT's
   main loop
*/
void display() {
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);  //display mode
 glLoadIdentity();// load identity matrix
 glColor3f(0.0f, 1.0f, 0.0f); //green color
  glBegin(GL_TRIANGLES);//start drawing triangles
  glColor3f(0.0f, 1.0f, 1.0f);
  glVertex3f(-1.0f, -0.25f, 0.0f);//triangle one first vertex
  glColor3f(0.0f, 1.0f, 1.0f);
  glVertex3f(-0.5f, -0.25f, 0.0f);//triangle one second vertex
  glColor3f(1.0f, 0.0f, 1.0f);
  glVertex3f(-0.75f, 0.25f, 0.0f);//triangle one third vertex
  //drawing second triangle
  glVertex3f(-0.5f, -0.25f, 0.0f);//triangle two first vertex
  glColor3f(1.0f, 1.0f, 0.0f);
  glVertex3f(0.5f, -0.25f, 0.0f);//triangle two second vertex
  glVertex3f(0.0f, 0.25f, 0.0f);//triangle two third vertex
  //drawing third triangle
  glVertex3f(0.5f, -0.25f, 0.0f);//triangle three first vertex
  glColor3f(1.0f, 0.0f, 0.0f);
  glVertex3f(1.0f, -0.25f, 0.0f);//triangle three second vertex
  glVertex3f(0.75f, 0.25f, 0.0f);//triangle three third vertex
  glEnd();//end drawing of triangles
 glFlush();
}

/* key board exit program*/
void keyboardesc(unsigned char key, int x, int y)
{
 switch (key)
 {
 case KEY_ESCAPE:
   exit(0);
   break;
 case RAFAEL:
   exit(0);
   break;
  default:
   break;
 }
}

When compiling your code you need to link the proper library by doing this:

gcc 1.c -o nameoffile.c -o nameofouput -lglut

the output should look like this: