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:

Sunday, March 2, 2014

Matrix Math using C

This is a program that performs matrices math, very straight forward matrix multiplication (dot product), scalar multiplication and the calculation of the transpose of a matrix.  Here is a good video of how to do these things by hand (link)

Each matrix is defined within the program and the program is structured to pass values by reference instead of value.  To print each result make sure to remove the /* */ comments from each labeled section.  Any questions let me know


 #include <stdio.h>  
 #define ROWSx 2  
 #define COlSx 3  
 #define ROWSy 3  
 #define COlSy 2  
 #define ROWSw 3  
 #define COlSw 2  
 void scalar_multiplication(int, int[][COlSy]);  
 void matrix_multiplication(int[][COlSx], int[][COlSy], int[][COlSx]);  
 void matrix_addition(int[][COlSy], int[][COlSw]);  
 void transpose_matrix(int[][COlSw], int[][ROWSw]);  
 int main() {  
      int i, j;  
      int scalarX = 10;  
      int matrixZ[ROWSy][COlSx];  
      int matrixX[ROWSx][COlSx] = {  
           { 2, 2, 2 },  
           { 4, 4, 10 }  
      };  
      int matrixY[ROWSy][COlSy] = {  
           { 4, 3 },  
           { 4, 3 },  
           { 2, 2 }  
      };  
      int matrixW[ROWSw][COlSw] = {  
           { 1, 2 },  
           { 3, 4 },  
           { 5, 6 }  
      };  
      int matrixM[COlSw][ROWSw];  
      //scalar_multiplication(scalarX, matrixY);  
      //matrix_multiplication(matrixX, matrixY, matrixZ);  
      // matrix_addition(matrixY, matrixW);  
      transpose_matrix(matrixW, matrixM);  
      printf("\nHere is your matrix:\n");  
      for (i = 0; i<COlSw; i++)  
      {  
           for (j = 0; j<ROWSw; j++)  
           {  
                printf("%d ", matrixM[i][j]);  
           }  
           printf("\n");  
      }  
 }  
 void matrix_addition(int Yptr[][COlSy], int Wptr[][COlSw])  
 {  
      int f, g;  
      for (g = 0; g < ROWSw; g++)  
      {  
           for (f = 0; f < COlSw; f++)  
           {  
                Wptr[g][f] = Yptr[g][f] + Wptr[g][f];  
           }  
      }  
 }  
 void scalar_multiplication(int Xptr, int Yptr[][COlSy])  
 {  
      int a,b;  
      for (a = 0; a < ROWSy; a++)  
      {  
           for (b = 0; b < COlSy; b++)   
           {  
                Yptr[a][b] = Xptr * Yptr[a][b];  
           }  
      }  
 }  
 void matrix_multiplication(int matXptr[][COlSx], int matYptr[][COlSy], int matZptr[][COlSx])  
 {  
      int c, d, e;  
      int sum;  
      for (c = 0; c < ROWSy; c++)        
      for (d = 0; d < COlSx; d++)        
            matZptr[ROWSy][COlSx] = 0; // resultant matrix is row_of_y by column_of_x size  
      for (c = 0; c < ROWSy; c++) {     // row of matrix Y  
           for (d = 0; d < COlSx; d++) {   // column of matrix X  
                sum = 0;  
                for (e = 0; e < COlSy; e++)  
                     sum += matYptr[c][e] * matXptr[e][d];  
                 matZptr[c][d] = sum;  
           }  
      }  
 }  
 void transpose_matrix(int matZpt[][COlSw], int transZpt[][ROWSw])   
 {  
      int f, g, h, i;  
      for (f = 0; f < COlSw; f++)  
      for (g = 0; g < ROWSw; g++)  
           transZpt[f][g] = 0; // transposed matrix is this dimension  
      for (h = 0; h < COlSw; h++)  
      {  
           for (i = 0; i < ROWSw; i++)  
           {  
                transZpt[h][i] = matZpt[i][h];  
           }  
      }  
 }