/* --plan.c--                                    */
/* Uebung 3 zu Einfuehrung in die Computergrafik */
/*                                               */
/* Zeichnet einen Schaltplan in ein Fenster      */

#include <stdio.h>
#include <math.h>
#include <GL/glos.h>
#include <GL/glut.h>

#define pi 3.1415926535898

void drawWire (void) {
        glBegin (GL_LINES);
        glVertex2f (0.0, 0.0);
        glVertex2f (1.0, 0.0);
        glEnd ();
        glTranslatef (1.0, 0.0, 0.0);
        glFlush ();
}

void drawSwitch (void) {     
        glBegin (GL_LINE_STRIP);
		glVertex2f (0.0, 0.0);
        glVertex2f (0.2, 0.0);
        glVertex2f (1.4, 0.46);
        glEnd ();

        glBegin (GL_LINES);
        glVertex2f (1.4, 0.0);
        glVertex2f (1.5, 0.0);
        glEnd ();
        glTranslatef (1.5, 0.0, 0.0);
        glFlush ();
}

void drawResistor (void) {
        glBegin (GL_LINES);
        glVertex2f (0.0, 0.0);
        glVertex2f (0.1, 0.0);
        glEnd ();

        glBegin (GL_LINE_STRIP);
        glVertex2f (0.1, -0.25);
        glVertex2f (0.1, 0.25);       
		glVertex2f (0.9, 0.25);
		glVertex2f (0.9, -0.25);
        glVertex2f (0.1, -0.25);
        glEnd ();

        glBegin (GL_LINES);
        glVertex2f (0.9, 0.0);
        glVertex2f (1.0, 0.0);
        glEnd ();

        glTranslatef (1.0, 0.0, 0.0);
        glFlush ();
}

void drawCondenser (void) {
        glBegin (GL_LINES);
        glVertex2f (0.0, 0.0);
        glVertex2f (0.4, 0.0);
        glEnd ();

        glBegin (GL_LINES);
        glVertex2f (0.4, -0.5);
        glVertex2f (0.4, 0.5);
        glEnd ();            
		        
		glBegin (GL_LINES);
        glVertex2f (0.6, -0.5);
        glVertex2f (0.6, 0.5);
        glEnd ();

        glBegin (GL_LINES);
        glVertex2f (0.6, 0.0);
        glVertex2f (1.0, 0.0);
        glEnd ();

        glTranslatef (1.0, 0.0, 0.0);
        glFlush ();
}

void drawPower (void) {
        glBegin (GL_LINES);
        glVertex2f (0.0, 0.0);
        glVertex2f (0.4, 0.0);
        glEnd ();

        glBegin (GL_LINES);
        glVertex2f (0.4, -0.35);
        glVertex2f (0.4, 0.35);       
		       
		glBegin (GL_LINES);
        glVertex2f (0.6, -0.5);
        glVertex2f (0.6, 0.5);
        glEnd ();

        glBegin (GL_LINES);
        glVertex2f (0.6, 0.0);
        glVertex2f (1.0, 0.0);
        glEnd ();
        
		glTranslatef (1.0, 0.0, 0.0);
        glFlush ();
}

void drawLamp (void) {
        int i;

        glBegin (GL_LINES);
        glVertex2f (0.0, 0.0);
        glVertex2f (0.1, 0.0);
        glEnd ();   
		
		glBegin (GL_LINE_STRIP);
        for (i=0; i < 360; i++)
			glVertex2f (0.5 - 0.4 * cos (i / 180.0 * pi), 0.4 * sin (i / 180.0 * pi));
        glEnd ();

        glBegin (GL_LINES);
        glVertex2f (0.5 - 0.4 * cos (pi / 4.0), 0.4 * sin (pi / 4.0));
        glVertex2f (0.5 - 0.4 * cos (pi / 4.0 * 5.0), 0.4 * sin (pi / 4.0 * 5.0));
        glEnd ();

        glBegin (GL_LINES);
        glVertex2f (0.5 - 0.4 * cos (-pi / 4.0), 0.4 * sin (-pi / 4.0));
        glVertex2f (0.5 - 0.4 * cos (pi / 4.0 * 3.0), 0.4 * sin (pi / 4.0 * 3.0));
        glEnd ();

        glBegin (GL_LINES);
        glVertex2f (0.9, 0.0);
        glVertex2f (1.0, 0.0);
        glEnd ();
        glTranslatef (1.0, 0.0, 0.0);
}

void drawLampAndSwitch (void) {
		drawWire ();
        glPushMatrix ();
   
        glRotatef (-90.0, 0.0, 0.0, 1.0);
        drawWire ();

        glRotatef (90.0, 0.0, 0.0, 1.0);
        glScalef (0.5, 0.5, 1.0);
        drawWire ();

        glScalef (2.0, 2.0, 1.0);
        drawCondenser ();

        drawResistor ();

        glScalef (0.7, 0.7, 1.0);
        drawWire ();

        glScalef (1.0, 1.45, 1.0);
        glRotatef (90.0, 0.0, 0.0, 1.0);
		drawWire ();

        glPopMatrix ();
        drawSwitch ();

		glScalef (2.0, 2.0, 1.0);
		drawWire ();
        
		glScalef (0.45, 0.52, 1.0);
        drawLamp ();

        glScalef (0.39, 0.53, 1.0);
        drawWire ();

        glScalef (2.0, 2.0, 1.0);
}

void drawPlan (void) {
        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glColor3f (0.0, 0.0, 0.0);
		glScalef (35.25, 37.5, 1.0);
        glTranslatef (-3.25, -3.0, 0.0);
     
        glRotatef (90.0, 0.0, 0.0, 1.0);
        glScalef (0.5, 0.5, 1.0);    
		drawWire ();

        glScalef (4.0, 4.0, 1.0);

        drawResistor ();

		glScalef (0.25, 0.25, 1.0);
		drawWire ();

        glScalef (2.0, 2.0, 1.0);
        drawPower ();

        glScalef (2.15, 2.0, 1.0);
        drawWire ();

		glRotatef (-90.0, 0.0, 0.0, 1.0);
        glScalef (1.5, 1.5, 1.0);
        drawWire ();

        glScalef (1.0 / 3.0, 1.0 / 3.0, 1.0);
        glPushMatrix (); 
			
        drawResistor ();

        glScalef (1.5, 1.5, 1.0);    
        drawWire ();

        glScalef (2.0 / 3.0, 2.0 / 3.0, 1.0);
        glRotatef (-90.0, 0.0, 0.0, 1.0);
        drawLampAndSwitch ();

        glRotatef (-90.0, 0.0, 0.0, 1.0);
        glScalef (5.0, 1.0, 1.0);
		drawWire ();

        glPopMatrix ();

        glRotatef (-90.0, 0.0, 0.0, 1.0);
        drawLampAndSwitch ();

        glFlush (); 
}

int main (int argc, char **argv) {
        glutInit(&argc, argv); 
		glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
        glutInitWindowSize (400, 300);
        glutInitWindowPosition (300, 200);
        glutCreateWindow ("plan | created by Raimund Neumueller");
        printf ("Ok.\n");

		glMatrixMode (GL_PROJECTION);
        glLoadIdentity ();
        glOrtho (-200.0, 200.0, -150.0, 150.0, -1.0, 1.0);
        
		glClearColor (1.0, 1.0, 1.0, 0.0);
        glutDisplayFunc (drawPlan);
        glutMainLoop ();
        return 0;               
}
