#include "basis.h"

static void Init(void );
static void CALLBACK Reshape(int width,int height);
static void CALLBACK Draw(void );

GLint windW, windH;
GLfloat params[4];

static void Init(void)
{
  glClearColor(1.0f, 0.96f, 0.866f, 1.0f);
}

static void CALLBACK Reshape(int width, int height)
{
  windW = (GLint)width;
  windH = (GLint)height;
}

static void CALLBACK DrawBorder(int left)
{
  GLint x;
  GLushort pattern;

  glColor3f(0.0f, 0.0f, 0.0f);
  if(!left){
    pattern = 0x1C47;
    x = windW/4;
  }
  else{
    pattern = 0x471C;
    x = -windW/4;
  }
  glPushMatrix();
    glGetFloatv(GL_LINE_WIDTH, &params[0]);
    glLineWidth(2.0f);
    glLineStipple(2, pattern);
    glEnable(GL_LINE_STIPPLE);
    glBegin(GL_LINES);
      glVertex2i(x, -windH/2);
      glVertex2i(x,  windH/2);
    glEnd();
    glDisable(GL_LINE_STIPPLE);
    glLineWidth(params[0]);
  glPopMatrix();
}

static void CALLBACK InitViewport(int x, int y, int width, int height)
{
  glViewport(x, y, width, height);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(-windW/4, windW/4, -windH/2, windH/2);
  glMatrixMode(GL_MODELVIEW);
}

static void CALLBACK DrawScene(void)
{
  int i;
  GLint aVertex[6][2];

  // Определяем координаты вершин для треугольников
  aVertex[0][0] = -windW/32*7;  aVertex[0][1] =  windH/3;
  aVertex[1][0] = -windW/16;    aVertex[1][1] =  windH/24*11;
  aVertex[2][0] =  windW/8;     aVertex[2][1] =  windH/24*10;
  aVertex[3][0] =  windW/32*7;  aVertex[3][1] =  windH/3;
  aVertex[4][0] =  windW/8;     aVertex[4][1] =  windH/24*7;
  aVertex[5][0] = -windW/16;    aVertex[5][1] =  windH/24*5;

  // Здесь вызываются команды рисования
  // ...

  glGetFloatv(GL_CURRENT_COLOR, params);

  glColor3f(1.0f, 0.0f, 1.0f);
  glBegin(GL_TRIANGLES);
    for (i = 0; i < 6; i++) {
      glVertex2iv(aVertex[i]);
    }
  glEnd();

  for(i = 0; i < 6; i++){
    aVertex[i][1] -= windH/3;
  }
  
  glColor3f(1.0f, 0.5f, 0.0f);
  glBegin(GL_TRIANGLE_STRIP);
    for (i = 0; i < 6; i++) {
      glVertex2iv(aVertex[i]);
    }
  glEnd();

  for(i = 0; i < 6; i++){
    aVertex[i][1] -= windH/3;
  }
  
  glColor3f(0.0f, 0.5f, 1.0f);
  glBegin(GL_TRIANGLE_FAN);
    for (i = 0; i < 6; i++) {
      glVertex2iv(aVertex[i]);
    }
  glEnd();
  
  glColor3f(params[0], params[1], params[2]);
}

static void CALLBACK Draw(void)
{
  glClear(GL_COLOR_BUFFER_BIT);

  InitViewport(0,0, windW/2, windH);
  DrawBorder(0);

  glColor3f(1.0, 0.0, 1.0);
  DrawScene();

  InitViewport(windW/2, 0, windW/2, windH);
  DrawBorder(1);
  
  glEnable(GL_POLYGON_SMOOTH);
  glColor3f(1.0, 0.0, 1.0);
  DrawScene();
  glDisable(GL_POLYGON_SMOOTH);

  glFinish();
  auxSwapBuffers();
}

void main(int argc, char **argv)
{
    windW = 500;
    windH = 300;
    auxInitPosition(150, 100, windW, windH);

    auxInitDisplayMode(AUX_RGB | AUX_DOUBLE);

    if (auxInitWindow("Треугольники, треугольники ...") == GL_FALSE) {
      auxQuit();
    }

    Init();

    auxExposeFunc((AUXEXPOSEPROC)Reshape);
    auxReshapeFunc((AUXRESHAPEPROC)Reshape);
    auxMainLoop(Draw);
}
