/*
   Test of interpenetrating planes with alpha-channel colors, no lighting.

   Example for the color module of the graphics course
   © 2000, Steve Cunningham
*/

#include "glut.h"
#include <stdlib.h>
#include <math.h>

static GLfloat angle = 0.0;

void myinit(void);
void display( void );
void reshape(int w,int h);
void keyboard(unsigned char key, int x, int y);
void animate(void);

void myinit(void)
{
        //glClearColor(0.5,0.5,0.5,0.5);
        glDisable(GL_DEPTH_TEST); // allow z-buffer display
        glEnable(GL_BLEND); // allow alpha testing
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

void display( void )
{

/* define a point data type */

    typedef GLfloat point3[3];

    point3 plane0[4]={{-1.0, 0.0, -1.0},   // X-Z plane
                      {-1.0, 0.0,  1.0},
                      { 1.0, 0.0,  1.0},
                      { 1.0, 0.0, -1.0} };

    point3 plane1[4]={{ 0.0, -1.0, -1.0},  // Y-Z plane
                      { 0.0, -1.0,  1.0},
                      { 0.0,  1.0,  1.0},
                      { 0.0,  1.0, -1.0} };

    point3 plane2[4]={{-1.0, -1.0, 0.0},   // X-Y plane
                      {-1.0,  1.0, 0.0},
                      { 1.0,  1.0, 0.0},
                      { 1.0, -1.0, 0.0} };

    GLfloat color0[]={1.0, 0.0, 0.0, 0.33},
    		color1[]={0.0, 1.0, 0.0, 0.5},
            color2[]={0.0, 0.0, 1.0, 1.0};  // R=0, G=1, B=2

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    glRotatef(angle, 1.0, 1.0, -1.0); /* rotate model */

/*  draw our planes */

    glColor4fv(color2);		// blue
    glBegin(GL_QUADS);		// X-Y plane
      glVertex3fv(plane2[0]);
      glVertex3fv(plane2[1]);
      glVertex3fv(plane2[2]);
      glVertex3fv(plane2[3]);
    glEnd();

    glColor4fv(color1);		// green
    glBegin(GL_QUADS);		// Y-Z plane
      glVertex3fv(plane1[0]);
      glVertex3fv(plane1[1]);
      glVertex3fv(plane1[2]);
      glVertex3fv(plane1[3]);
    glEnd();

    glColor4fv(color0);		// red
    glBegin(GL_QUADS);		// X-Z plane
      glVertex3fv(plane0[0]);
      glVertex3fv(plane0[1]);
      glVertex3fv(plane0[2]);
      glVertex3fv(plane0[3]);
    glEnd();

glPopMatrix();  /* undo last rotation you set */
    glutSwapBuffers();
 }

void reshape(int w,int h)
{
        glViewport(0,0,(GLsizei)w,(GLsizei)h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(60.0,1.0,1.0,30.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        //           eye point   center of view       up
        gluLookAt(1.8, 1.8, 1.8, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void keyboard(unsigned char key, int x, int y)
{
        if (key)
        {
           angle += 10.0; /* update angle for rotation in display */
           glutPostRedisplay(); /* perform display again */
}}

void main(int argc, char** argv)
{

/* Standard GLUT initialization */

        glutInit(&argc,argv);
//      initialize for double buffering, RGB color, and depth tests
        glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
        glutInitWindowSize(500,500);
        glutInitWindowPosition(70,70);
        glutCreateWindow("partially transparent planes");
        glutDisplayFunc(display);
        glutReshapeFunc(reshape);
        glutKeyboardFunc(keyboard); // enable keyboard callback

        myinit();
        glutMainLoop();
}