Wednesday, June 27, 2012

Chess in OpenGL Complete Code



   1:  #include<Windows.h>
   2:  #include<glut.h>   
   3:   
   4:  void Draw()
   5:  {
   6:      glClear(GL_COLOR_BUFFER_BIT);
   7:      float b=-0.99;             //  WIDTH
   8:      float c=-0.74;            //   WIDTH
   9:      float s=-0.99;           //    HEIGHT    
  10:      float e=-0.74;           //    HEIGHT
  11:      int a=0;  
  12:   
  13:      for(int o=0; o<8;o++)  
  14:      {
  15:          if(o==0 || o==2 || o==4 || o==6 || o==8 )
  16:              {   
  17:          for(a=0; a<8; a++)
  18:                     {
  19:                         glBegin(GL_POLYGON);
  20:                         glVertex3f(b,s,0.0);
  21:                         glVertex3f(c,s,0.0);   
  22:                         glVertex3f(c,e,0.0);
  23:                         glVertex3f(b,e,0.0);
  24:   
  25:                          if( a==0 ||  a==2 || a==4 || a==6 || a==8 )
  26:                             {
  27:                                glColor3f(0.0,0.0,0.0);
  28:                             }
  29:                          else
  30:                             glColor3f(1.0,1.0,1.0);
  31:                             glEnd();
  32:                             b=b+0.25;       
  33:                             c=c+0.25;                                  
  34:                             glFlush();
  35:                     }
  36:          b=-0.99;              //back to left position
  37:          c=-0.74;              //back to left position
  38:          }
  39:   
  40:          else if(o==1 || o==3 || o==5 || o==7 || o==9)
  41:              {       
  42:                  glColor3f(0.0,0.0,0.0);
  43:                  for(a=0; a<8; a++)
  44:                          {
  45:                              glBegin(GL_POLYGON);
  46:                              glVertex3f(b,s,0.0);
  47:                              glVertex3f(c,s,0.0);      
  48:                              glVertex3f(c,e,0.0);              
  49:                              glVertex3f(b,e,0.0);   
  50:   
  51:                              if( a==0 ||  a==2 || a==4 || a==6 || a==8)
  52:                                  {
  53:                                     glColor3f(1.0,1.0,1.0);
  54:                                  }
  55:                      else
  56:                          glColor3f(0.0,0.0,0.0);
  57:                          glEnd();
  58:                          b=b+0.25;      
  59:                          c=c+0.25;         
  60:                          glFlush();
  61:                          }   
  62:              }
  63:          b=-0.99;                
  64:          c=-0.74;                         
  65:          e=e+0.25;                 
  66:          s=s+0.25;                  
  67:          glColor3f(1.0,1.0,1.0);
  68:      }   
  69:  }
  70:   
  71:  void Initialize()
  72:  {
  73:      glClearColor(0.0,0.0,0.0,0.0);
  74:      glMatrixMode(GL_PROJECTION);
  75:      glLoadIdentity();
  76:      glOrtho(2.0,2.0,2.0,2.0,2.0,2.0);       
  77:  }
  78:   
  79:  int main(int iArgc,char** cppArgv)
  80:  {
  81:      glutInit(&iArgc,cppArgv);
  82:      glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  83:      glutInitWindowPosition(200,200);
  84:      glutCreateWindow("Chess By Muhammad Taqi");
  85:      Initialize();
  86:      glutDisplayFunc(Draw);
  87:      glutMainLoop();
  88:      return 0;
  89:  }
 
 

Wednesday, June 6, 2012

How to use DevC++ for OpenGl

Hello in this tutorial I'm gonna show you how you can use OpenGL in DevC++, I will also show how you can add libraries and properly link them to your project.

1. First Download the file from the link below, and extract it

Download

2. There are 3 files in this folder, we have to copy these files to their proper directory

glut.h
Copy this file to X:\Dev-Cpp\include\GL

libglut32.a
Copy this file to X:\Dev-Cpp\lib

glut32.dll

Copy this file to X:\Windows\System32 (link might be different for different OS)

3. Now open up your DevC++ Compiler

4. Click on File>>New>>Project

 5. Select Empty project, give your project a name and select C++ Project, and finally Click on OK

6. Now Click on File>> Source File

Now we have to link libraries So..

7. Now in the menu bar Click on project>> Project Option >>Parameters

 Here we need to add libraries for our code to work, So click on "Add Library or Object"

  • libopengl32.a
  • libglu32.a
  • libglut32.a  ( All these 3 files are located in X:\Dev-Cpp\lib)
Import them in the series as they are... Final look should be like this

9. Click on OK

10. Now copy the following code in your source file

#include<windows.h>
#include<gl/Gl.h>
#include<gl/glut.h>

void myInit(void)
{
     glClearColor(1.0,1.0,1.0,00);
     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);
     glBegin(GL_POINTS);
     glVertex2i(100,30);   
          glVertex2i(100,130);
               glVertex2i(150,130);
               glEnd();
               glFlush(); 
}
main(int argc, char ** argv)
{
     glutInit(&argc,argv);
     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
     glutInitWindowSize(640,480);
     glutInitWindowPosition(100,150);
     glutCreateWindow("My First desigh");
     glutDisplayFunc(myDisplay);
     myInit();
     glutMainLoop();
 }

11. Compile and execute it,and it should work