Advertisement
jovanovski

КГ Лаб1

Mar 23rd, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.81 KB | None | 0 0
  1. #include <windows.h>
  2. #include <gl\gl.h>
  3. #include <gl\glu.h>
  4. #include <gl\glut.h>
  5. #define _USE_MATH_DEFINES
  6. #include <math.h>
  7.  
  8.  
  9.  
  10. static void redraw(void);
  11. int main(int argc, char **argv);
  12.  
  13. int main(int argc, char **argv)
  14. {
  15.     glutInit(&argc,argv);
  16.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  17.     glutInitWindowSize(800,800);
  18.     glutCreateWindow("Шаховска Табла");
  19.  
  20.     glutDisplayFunc(redraw);
  21.  
  22.     glMatrixMode(GL_PROJECTION);                        //hello
  23.     gluPerspective(45, //view angle
  24.                         1.0,    //aspect ratio
  25.                         10.0, //near clip
  26.                         200.0);//far clip
  27.     glMatrixMode(GL_MODELVIEW);
  28.  
  29.     glutMainLoop();
  30.  
  31.     return 0;
  32. }
  33.  
  34.  
  35. static void tabla(){
  36.     int br = 0;
  37.     for(int i=0;i<8;i++){
  38.         for(int j=0;j<8;j++){
  39.             glBegin(GL_POLYGON);
  40.             if(br%2==0)
  41.                 glColor3f(1.0,1.0,1.0);
  42.             else
  43.                 glColor3f(0.3,0.3,0.3);
  44.             br++;
  45.             glVertex2f(i,j);
  46.             glVertex2f(i,j+1);
  47.             glVertex2f(i+1,j+1);
  48.             glVertex2f(i+1,j);
  49.             glEnd();
  50.         }
  51.         br++;
  52.     }
  53.  
  54. }
  55.  
  56. static void ramka(){
  57.           glBegin(GL_POLYGON);
  58.           glColor3f(0.5,0.5,0.5);
  59.           glVertex2f(-0.2,-0.2);
  60.           glVertex2f(8.2,-0.2);
  61.           glVertex2f(8.2,8.2);
  62.           glVertex2f(-0.2,8.2);
  63.  
  64.  
  65.           glEnd();
  66. }
  67.  
  68. static void crtajKrug(int s, int od, float r, float xk, float yk, bool crvena){
  69.     float x,y;
  70.     float radius = r;
  71.     yk*=-1;
  72.     glBegin(GL_POLYGON);
  73.     if(crvena)
  74.         glColor3f(1.0f,0.0f,0.0f);
  75.     else
  76.         glColor3f(0.0f,0.0f,1.0f);
  77.     if(od==0){
  78.     x = (float)radius * cos(359 * M_PI/180.0f)+xk;
  79.     y = (float)radius * sin(359 * M_PI/180.0f)+8+yk;
  80.     }
  81.     else{
  82.         x = (float)radius * cos(od * M_PI/180.0f)+xk;
  83.         y = (float)radius * sin(od * M_PI/180.0f)+8+yk;
  84.     }
  85.     for(int j = od; j <= s; j++)
  86.     {
  87.         glVertex2f(x,y);
  88.         x = (float)radius * cos(j * M_PI/180.0f)+xk;
  89.         y = (float)radius * sin(j * M_PI/180.0f)+8+yk;
  90.         glVertex2f(x,y);
  91.     }
  92.     glEnd();
  93. }
  94.  
  95.  
  96.  
  97. void DrawEllipse(float radiusX, float radiusY, float xk, float yk)
  98. {
  99.    int i;
  100.    yk*=-1;
  101.    glBegin(GL_POLYGON);
  102.  
  103.    for(i=0;i<80;i++)
  104.    {
  105.       float rad = (i*M_PI/180)+90;
  106.       glVertex2f(cos(rad)*radiusX+xk, sin(rad)*radiusY+8+yk);
  107.    }
  108.  
  109.    glEnd();
  110. }
  111.  
  112. static void crtajPiun(float x, float y, bool crvena){
  113.     crtajKrug(181, 0, 0.35, x+0.5,y+0.9, crvena);
  114.     crtajKrug(360, 0, 0.19, x+0.5,y+0.5, crvena);
  115.     crtajKrug(360, 0, 0.12, x+0.5,y+0.25, crvena);
  116. }
  117.  
  118. static void crtajTop(float xk, float yk, bool crvena){
  119.  
  120.     glBegin(GL_QUADS);
  121.     yk*=-1;
  122.  
  123.     if(crvena)
  124.         glColor3f(1.0,0.0,0.0);
  125.     else
  126.         glColor3f(0.0,0.0,1.0);
  127. //osnova
  128.     glVertex2f(xk+0.15,yk+7.1);
  129.     glVertex2f(xk+0.85,yk+7.1);
  130.     glVertex2f(xk+0.85,yk+7.2);
  131.     glVertex2f(xk+0.15,yk+7.2);
  132.  
  133. //dolen trapez
  134.     glVertex2f(xk+0.15,yk+7.2);
  135.     glVertex2f(xk+0.85,yk+7.2);
  136.     glVertex2f(xk+0.73,yk+7.35);
  137.     glVertex2f(xk+0.27,yk+7.35);
  138. //sredna kocka
  139.     glVertex2f(xk+0.27,yk+7.35);
  140.     glVertex2f(xk+0.73,yk+7.35);
  141.     glVertex2f(xk+0.73,yk+7.65);
  142.     glVertex2f(xk+0.27,yk+7.65);
  143. //goren trapez
  144.     glVertex2f(xk+0.27,yk+7.65);
  145.     glVertex2f(xk+0.73,yk+7.65);
  146.     glVertex2f(xk+0.85,yk+7.80);
  147.     glVertex2f(xk+0.15,yk+7.80);
  148. //kockicki
  149.     glVertex2f(xk+0.15,yk+7.80);
  150.     glVertex2f(xk+0.325,yk+7.80);
  151.     glVertex2f(xk+0.325,yk+7.90);
  152.     glVertex2f(xk+0.15,yk+7.90);
  153.  
  154.     glVertex2f(xk+0.4125,yk+7.80);
  155.     glVertex2f(xk+0.5875,yk+7.80);
  156.     glVertex2f(xk+0.5875,yk+7.90);
  157.     glVertex2f(xk+0.4125,yk+7.90);
  158.  
  159.     glVertex2f(xk+0.675,yk+7.80);
  160.     glVertex2f(xk+0.85,yk+7.80);
  161.     glVertex2f(xk+0.85,yk+7.90);
  162.     glVertex2f(xk+0.675,yk+7.90);
  163.  
  164.     glEnd();
  165. }
  166.  
  167. static void crtajLovec(float xk, float yk, bool crvena){
  168.  
  169.     crtajKrug(360, 0, 0.08,0.5+xk,0.2+yk, crvena);
  170.     crtajKrug(360, 0, 0.18,0.5+xk,0.43+yk, crvena);
  171.     crtajKrug(180, 0, 0.3, 0.5+xk,0.9+yk, crvena);
  172.     yk*=-1;
  173.  
  174.     glBegin(GL_POLYGON);
  175.     if(crvena)
  176.         glColor3f(1.0,0.0,0.0);
  177.     else
  178.         glColor3f(0.0,0.0,1.0);
  179.  
  180.     glVertex2f(0.35+xk,7.5+yk);
  181.     glVertex2f(0.3+xk,7.4+yk);
  182.     glVertex2f(0.4+xk,7.3+yk);
  183.     glVertex2f(0.6+xk,7.3+yk);
  184.     glVertex2f(0.7+xk,7.4+yk);
  185.     glVertex2f(0.65+xk,7.5+yk);
  186.     glEnd();
  187.  
  188. }
  189.  
  190. static void crtajKonj(float xk, float yk, bool crvena){
  191.  
  192.     if(crvena)
  193.         glColor3f(1.0,0.0,0.0);
  194.     else
  195.         glColor3f(0.0,0.0,1.0);
  196.     //Zaktivenost
  197.  
  198.     DrawEllipse(0.5,0.7,0.7+xk,0.7+yk);
  199.  
  200.     yk*=-1;
  201.     glBegin(GL_POLYGON);
  202.         glVertex2f(0.7+xk,7.1+yk);
  203.         glVertex2f(0.2+xk,7.1+yk);
  204.         glVertex2f(0.5+xk,7.5+yk);
  205.     glEnd();
  206.  
  207.     //Dolen triagolnik
  208.     glBegin(GL_POLYGON);
  209.         glVertex2f(0.47+xk,7.9+yk);
  210.         glVertex2f(0.2+xk,7.1+yk);
  211.         glVertex2f(0.5+xk,7.5+yk);
  212.     glEnd();
  213.  
  214.     //Usta
  215.     glBegin(GL_POLYGON);
  216.         glVertex2f(0.5+xk,7.5+yk);
  217.         glVertex2f(0.8+xk,7.35+yk);
  218.         glVertex2f(0.85+xk,7.45+yk);
  219.         glVertex2f(0.57+xk,7.92+yk);
  220.         glVertex2f(0.47+xk,7.92+yk);
  221.     glEnd();
  222.  
  223.     //Ostatok
  224.     glBegin(GL_POLYGON);
  225.         glVertex2f(0.57+xk,7.92+yk);
  226.         glVertex2f(0.47+xk,7.92+yk);
  227.         glVertex2f(0.1+xk,7.1+yk);
  228.         glVertex2f(0.57+xk,7.1+yk);
  229.     glEnd();
  230. }
  231.  
  232. static void crtajKralica(float xk, float yk, bool crvena){
  233.  
  234.     if(crvena)
  235.         glColor3f(1.0,0.0,0.0);
  236.     else
  237.         glColor3f(0.0,0.0,1.0);
  238.  
  239.     crtajKrug(360, 0, 0.166, 0.33+xk, 0.5+yk, crvena);
  240.     crtajKrug(360, 0, 0.166, 0.67+xk, 0.5+yk, crvena);
  241.     crtajKrug(360, 0, 0.08, 0.5+xk, 0.28+yk, crvena);
  242.  
  243.     crtajKrug(326, 215, 0.258, 0.51+xk, 0.65+yk, crvena);
  244.  
  245.     yk*=-1;
  246.  
  247.     glBegin(GL_QUADS);
  248.         glVertex2f(0.42+xk,7.72+yk);
  249.         glVertex2f(0.58+xk,7.72+yk);
  250.         glVertex2f(0.58+xk,7.32+yk);
  251.         glVertex2f(0.42+xk,7.32+yk);
  252.     glEnd();
  253.  
  254.     glBegin(GL_QUADS);
  255.         glVertex2f(0.194+xk,7.5+yk);
  256.         glVertex2f(0.82+xk,7.5+yk);
  257.         glVertex2f(0.72+xk,7.3+yk);
  258.         glVertex2f(0.294+xk,7.3+yk);
  259.  
  260.         glVertex2f(0.72+xk,7.3+yk);
  261.         glVertex2f(0.72+xk,7.2+yk);
  262.         glVertex2f(0.294+xk,7.2+yk);
  263.         glVertex2f(0.294+xk,7.3+yk);
  264.     glEnd();
  265.  
  266.  
  267.     glBegin(GL_LINES);
  268.         glVertex2f(0.5+xk,7.9+yk);
  269.         glVertex2f(0.5+xk,7.5+yk);
  270.  
  271.         glVertex2f(0.45+xk,7.86+yk);
  272.         glVertex2f(0.55+xk,7.86+yk);
  273.     glEnd();
  274.  
  275. }
  276.  
  277.  
  278. static void crtajKral(float xk, float yk, bool crvena){
  279.     if(crvena)
  280.         glColor3f(1.0,0.0,0.0);
  281.     else
  282.         glColor3f(0.0,0.0,1.0);
  283.  
  284.     crtajKrug(360, 0, 0.07, 0.84+xk, 0.3+yk, crvena);
  285.     crtajKrug(360, 0, 0.07, 0.67+xk, 0.25+yk, crvena);
  286.     crtajKrug(360, 0, 0.07, 0.50+xk, 0.2+yk, crvena);
  287.     crtajKrug(360, 0, 0.07, 0.33+xk, 0.25+yk, crvena);
  288.     crtajKrug(360, 0, 0.07, 0.16+xk, 0.3+yk, crvena);
  289.  
  290.     yk*=-1;
  291.  
  292.     glBegin(GL_QUADS);
  293.         glVertex2f(0.15+xk,7.1+yk);
  294.         glVertex2f(0.85+xk,7.1+yk);
  295.         glVertex2f(0.75+xk,7.2+yk);
  296.         glVertex2f(0.25+xk,7.2+yk);
  297.  
  298.  
  299.         glVertex2f(0.25+xk,7.2+yk);
  300.         glVertex2f(0.75+xk,7.2+yk);
  301.         glVertex2f(0.85+xk,7.4+yk);
  302.         glVertex2f(0.15+xk,7.4+yk);
  303.  
  304.     glEnd();
  305.  
  306.     glBegin(GL_TRIANGLES);
  307.         glVertex2f(0.15+xk,7.4+yk);
  308.         glVertex2f(0.29+xk,7.4+yk);
  309.         glVertex2f(0.16+xk,7.7+yk);
  310.  
  311.         glVertex2f(0.29+xk,7.4+yk);
  312.         glVertex2f(0.43+xk,7.4+yk);
  313.         glVertex2f(0.33+xk,7.75+yk);
  314.  
  315.         glVertex2f(0.43+xk,7.4+yk);
  316.         glVertex2f(0.57+xk,7.4+yk);
  317.         glVertex2f(0.50+xk,7.8+yk);
  318.  
  319.         glVertex2f(0.57+xk,7.4+yk);
  320.         glVertex2f(0.71+xk,7.4+yk);
  321.         glVertex2f(0.67+xk,7.75+yk);
  322.  
  323.         glVertex2f(0.71+xk,7.4+yk);
  324.         glVertex2f(0.85+xk,7.4+yk);
  325.         glVertex2f(0.84+xk,7.7+yk);
  326.  
  327.     glEnd();
  328.  
  329.  
  330.  
  331. }
  332.  
  333. static void redraw(void)
  334. {
  335.     glClear(GL_COLOR_BUFFER_BIT);
  336.  
  337.     glPushMatrix();
  338.         glTranslatef(-4,-4,-10);
  339.         //Crtaj ramka
  340.         ramka();
  341.  
  342.         //Ctraj tabla
  343.         tabla();
  344.  
  345.         //crveni piuni
  346.         for (int i=0; i<8;i++){
  347.         crtajPiun(i,1, true);}
  348.  
  349.         //plavi piuni
  350.         for (int i=0; i<8;i++){
  351.         crtajPiun(i,6, false);}
  352.  
  353.         //crtaj topovi
  354.         crtajTop(0,0,true);
  355.         crtajTop(7,0,true);
  356.         crtajTop(7,7,false);
  357.         crtajTop(0,7,false);
  358.  
  359.         //crtaj lovec
  360.         crtajLovec(2,0,true);
  361.         crtajLovec(5,0,true);
  362.         crtajLovec(2,7,false);
  363.         crtajLovec(5,7,false);
  364.  
  365.         //crtanje konj
  366.         crtajKonj(1,0,true);
  367.         crtajKonj(6,0,true);
  368.         crtajKonj(1,7,false);
  369.         crtajKonj(6,7,false);
  370.  
  371.         //crtanje kralica
  372.         crtajKralica(4,0,true);
  373.         crtajKralica(4,7,false);
  374.  
  375.         //crtanje kral
  376.         crtajKral(3,0,true);
  377.         crtajKral(3,7,false);
  378.  
  379.     glPopMatrix();
  380.     glutSwapBuffers();
  381.  
  382. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement