Welcome to My OpenGL Web Page for CE304

 

SnapShot of Smooth Shaded Fancy Sine

 

 

 

*Run or Download my AVI*

(2.33MB)

Flat Spiral, Fancy Sine

*Run or Download my AVI - 4 Surfaces*

(1.25MB)

Flat Spiral, Fancy Sine, Swept Contracted

 Hypocycloid & Swept Epicycloid

*Or Download Zip file of both AVI*

(876KB)

 

***   Note that some tiny square pixels in the polygon animation is due to the capture in AVI Format.

Otherwise appeasr specular smoothed shaded in Visual C++ 6.0 or Unix Systems.  ***

 

CE304 Lab 2000/2001

OpenGL Lab Project
File: mycode.c

 

Date : October 2000

Author : Lam Wee Wah

Tutorial : T12

 

*OpenGL Source code – 2 Surfaces*

 

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

 

// Enable this line for Visual C++

#include <GL/glaux.h>

// Also enable all CALLBACK for Visual C++

 

/*#include "glaux.h"

#include <GL/gl.h>

#include <GL/glu.h>

*/

 

#define    SIZE 1.0

#define PI 3.141

 

float scaling = (float)1.0, k_var=0.0, k_max=1.0, alpha, beta;

int increment=3, formulae=0, rotate_flag=0, display_flag=0,

                animation_flag=0, swap_flag=0, shade_flag=1;

 

void CALLBACK NormalLight(void);

void CALLBACK EnableLight(void);

 

/**********************************************************************

                This function is called only once in the very beginning.

                It can initialize whatever you want.

                Right now, two angles are defined.

***********************************************************************/

static void Init()

{

                static GLfloat light_position[]={0.0,SIZE,5.0*SIZE,0.0};

                static GLfloat mat_ambient[]={0.0,1.8,0.0,1.0};

                static GLfloat mat_emission[]={0.0,0.0,0.0,1.0};

                static GLfloat mat_diffuse[]={0.0,0.8,0.0,1.0};

                static GLfloat mat_specular[]={0.0,1.0,0.0,1.0};

                static GLfloat mat_shininess[]={50.0};

               

                glLightfv(GL_LIGHT0,GL_POSITION,light_position);

                glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);

                glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);

                glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);

                glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);

                glMaterialfv(GL_FRONT,GL_EMISSION,mat_emission);

 

                alpha=(float)360.0;

                beta=(float)20.0;

}

 

/***********************************************************************

Main Drawing Function.

It is called by the system to draw every next buffer with the maximum

speed available.

************************************************************************/

static void CALLBACK Draw()

{             

                void MakeObject(void);

 

                glClearColor((float)0.0, (float)0.0, (float)0.0, (float)0.0);

                glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

                glPushMatrix();

                if (rotate_flag == 1)

                {

                                alpha=alpha-(float)increment;

                                if (alpha<=(float)0.0)

                                                alpha=(float)360.0;

                }

/* This translation is done for the visualization transformation  */

                glTranslatef((float)0.0,(float)0.0,-(float)(5.0*SIZE));

/* The following are rotations done about Y followed by rotation about X  */

                glRotatef(beta, (float)1.0, (float)0.0, (float)0.0);

                glRotatef(alpha, (float)0.0, (float)1.0, (float)0.0);

/* This could be a scalinging if you decide to scalinge. Right now we scalinge by 1,1,1 */

                glScalef(scaling,scaling,scaling);

/* Here, the MakeObject function is called. You have to change it first to draw

   the requested objects  */

                MakeObject();

                glFlush();

                glPopMatrix();

                auxSwapBuffers();

}

 

/**********************************************************************

                This function defines the object(s) to be drawn.

                Now, for wire-frame drawing, only polylines and lines are used.

                If you decided to draw shaded polygons, do not forget to

                define materials and lights in Init()

***********************************************************************/

void CALLBACK DrawAxis(void)

{

/* Set color to red  */

    glColor3f((float)1.0, (float)0.0, (float)0.0);

 

/* Coordinate axes - three lines  */

                glBegin(GL_LINES);

/* CL_LINES defines segments of straight line connecting vertices v1 with v2,

   v3 with v4, v5 with v6, etc. */

     

                glVertex3f(-(float)(2.0*SIZE),(float)0,(float)0);

                glVertex3f((float)(2.0*SIZE),(float)0,(float)0);

 

/* Set color to green  */

                glColor3f((float)0.0, (float)1.0, (float)0.0);

 

                glVertex3f((float)0,-(float)(2.0*SIZE),(float)0);

                glVertex3f((float)0,(float)(2.0*SIZE),(float)0);

 

/* Set color to purple  */

                glColor3f((float)1.0, (float)0.0, (float)1.0);

 

                glVertex3f((float)0,(float)0,-(float)(2.0*SIZE));

                glVertex3f((float)0,(float)0,(float)(2.0*SIZE));

                glEnd();

                glFlush();

}

 

/**********************************************************************

                Parametric Equations number 8 and 20.

***********************************************************************/

 

// Flat Spiral

void Formulae8(GLfloat fi,GLfloat teta,GLfloat *x,GLfloat *y,GLfloat*z)

{

                *x=0.25*fi*cos(teta);

                *y=0.25*teta-0.5;

                *z=0.25*fi*sin(teta);

} 

 

// Fancy Sine

void Formulae20(GLfloat fi,GLfloat teta,GLfloat *x,GLfloat *y,GLfloat*z)

{

                *x=0.2*teta*sin(fi);

                *y=0.2*fi*sin(teta);

                *z=0.2*fi*cos(teta);

} 

 

/**********************************************************************

                Return a vertex for either of the formulae chosen

        and the time at t (depends whether animation is required)

***********************************************************************/

void GetFormulae(GLfloat fi,GLfloat teta,GLfloat p[3])

{

                GLfloat p1[3],p2[3];

 

                // p1 is for formulae8 and p2 is for formulae20

 

                Formulae8(fi,teta,&p1[0],&p1[1],&p1[2]);

                Formulae20(fi,teta,&p2[0],&p2[1],&p2[2]);

               

                /* Returning the vertex.

                   if k is 0 then return vertex for formuale8

                   otherwise if t =1 the vertex return is for formulae20 */

                p[0] = (1-k_var)*p1[0]+p2[0]*k_var;

                p[1] = (1-k_var)*p1[1]+p2[1]*k_var;

                p[2] = (1-k_var)*p1[2]+p2[2]*k_var;

}

 

/******************************************************************/

 

void Normalize(GLfloat x,GLfloat y,GLfloat z)

{

                glNormal3f(x,y,z);

                glVertex3f(x,y,z);

}

/******************************************************************/

 

void MakeObject()

{

                GLfloat p1[3];

                GLfloat var_angle,fi,fi2,teta,teta2;

 

                fi2 = PI*2;

                teta2 = PI*2;

               

                // Compute current interval angle size.

                // if Scaling is big, then intervals should be more

                // to enable us to see the shape of the wireframe.

                // (13.0+(int)(scaling*20)) is the number of intervals

                // var_angle is the angle size.

               

                var_angle = PI/(13.0+(int)(scaling*20));

                               

                if (!display_flag){

                                DrawAxis();

                                /* Set color to green  */

                                glColor3f((float)0.0, (float)0.0, (float)1.0);

                                for (fi=0;fi<=fi2;fi+=var_angle){

                                                glBegin(GL_LINE_STRIP);

                                                for (teta=0; teta<=teta2;teta+=var_angle){                                                           

                                                                GetFormulae(fi,teta,p1);

                                                                glVertex3f(p1[0],p1[1],p1[2]);

                                                }

                                                glEnd();

                                }

                                for (teta=0; teta<=teta2;teta+=var_angle){

                                                glBegin(GL_LINE_STRIP);

                                                for (fi=0;fi<=fi2;fi+=var_angle){

                                                                GetFormulae(fi,teta,p1);

                                                                glVertex3f(p1[0],p1[1],p1[2]);

                                                }

                                                glEnd();

                                }

                }

                else{

                                glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);

                                if (display_flag==1)

                                                glShadeModel(GL_FLAT);

                                else

                                                glShadeModel(GL_SMOOTH);

                                NormalLight();

                                DrawAxis();

                                EnableLight();

 

                                for (teta=0; teta<=teta2;teta+=var_angle){

                                                glBegin(GL_QUADS);

                                                for (fi=0;fi<=fi2;fi+=var_angle){

                                                                GetFormulae(fi,teta,p1);

                                                                Normalize(p1[0],p1[1],p1[2]);              

                                                                GetFormulae(fi,teta+var_angle,p1);

                                                                Normalize(p1[0],p1[1],p1[2]);

                                                                GetFormulae(fi+var_angle,teta+var_angle,p1);

                                                                Normalize(p1[0],p1[1],p1[2]);

                                                                GetFormulae(fi+var_angle,teta,p1);

                                                                Normalize(p1[0],p1[1],p1[2]);

                                                }

                                                glEnd();

                                }

                }

                glFlush();

                if (animation_flag){

                                if (!swap_flag)

                                {

                                                k_var+=0.01*k_max; //expand

                                                if (k_var>1.0)

                                                                k_var=1.0;

                                                if (formulae==8)

                                                {

                                                    if(k_var>=1.0)

                                                                    swap_flag = 1.0;

                                                    else

                                                                    swap_flag = 0.0;

                                                }

                                                else if(k_var>=1.0)

                                                {             

                                                                k_var=0.0;

                                                                if((formulae+1)>2)

                                                                                formulae=1;

                                                                else         

                                                                                formulae+=1;

                                                               

                                                }

                                }

                                else

                                {

                                                k_var-=0.01*k_max; // contract

                                                if (k_var<=0.0)

                                                {

                                                    k_var=0.0;

                                                    swap_flag = 0.0;

                                                }

                                                else

                                                    swap_flag = 1.0;

                                               

                                }

                }

               

               

} /* MakeObject */

 

/******************************************************************/

 

void CALLBACK Close(void)

{              auxQuit();}

 

void CALLBACK parametric8(void)

{              formulae=8;             animation_flag=0;   k_var=0;}

 

void CALLBACK parametric20(void)

{              formulae=8;             animation_flag=0;   k_var=1;}

 

void CALLBACK KeepOnRotate(void)

{              if(rotate_flag==1)

                                rotate_flag=0;

                else

                                rotate_flag=1;

}

 

void CALLBACK RotateUp(void)

{              if(beta<=3)

                                beta=360.0;

                else

                                beta-=(float)3.0;

}

 

void CALLBACK RotateDown(void)

{              if(beta>=360)

                                beta=0.0;

                else

                                beta+=(float)3.0;

}

 

void CALLBACK RotateLeft(void)

{              if(alpha<=3)

                                alpha=360.0;

                else

                                alpha-=(float)3.0;

}

 

void CALLBACK RotateRight(void)

{              if(alpha>=360)

                                alpha=0.0;

                else

                                alpha+=(float)3.0;

}

 

void CALLBACK EnableLight(void)                     // extra lighting to illumimnate polygons

{              glEnable(GL_LIGHTING);

                glEnable(GL_LIGHT0);

                glDepthFunc(GL_LESS);

                glEnable(GL_DEPTH_TEST);

                glEnable(GL_NORMALIZE);

}

 

void CALLBACK NormalLight(void)                   // normal lighting to illumimnate polygons

{              glDisable(GL_LIGHTING);

                glDisable(GL_LIGHT0);

                glDisable(GL_DEPTH_TEST);

                glDisable(GL_NORMALIZE);}

 

void CALLBACK FlatShaded(void)

{    display_flag=1;}

 

void CALLBACK SmoothShaded(void)

{    display_flag=2;}

 

void CALLBACK WireFrame(void)

{    display_flag=0; NormalLight();  }

 

void CALLBACK FastAnimate(void)

{              increment=increment>10?10:increment+1;            k_max=k_max>=10?100:k_max+2;}

 

void CALLBACK SlowAnimate(void)

{              increment=increment>1?increment-1:1; k_max=k_max<4?2:k_max-9;}

 

void CALLBACK ScaleUp(void)

{              scaling+=0.1;}

 

void CALLBACK ScaleDown(void)

{              scaling=scaling>0.2?scaling-0.1:0.1;}

 

void CALLBACK Animate(void)

{              swap_flag = 0;        animation_flag = 1; k_var=0.0;               k_max=1;}

 

void CALLBACK StopAnimate(void)

{              animation_flag = 0;}

 

void CALLBACK Cont_Animate(void)

{              animation_flag = 1;}

 

void CALLBACK Org_Position(void)

{              alpha=(float)360.0; beta=(float)20.0; scaling=(float)1.0;}

 

 

/******************************************************************

In this function the projection transformation is defined.

Currently, it is perspective projection. It can be orthographic

projection as well if you use the commented glOrtho instead of

glFrustum

*******************************************************************/

void CALLBACK Reshape(GLsizei w, GLsizei h)

{

    glMatrixMode (GL_PROJECTION);               

    glLoadIdentity ();

/*  You may use this glOrtho in place of glFrustrum if you do not

    like perspective projection   

    glOrtho(-2.0*SIZE, 2.0*SIZE, -2.0*SIZE, 2.0*SIZE,

                                0.0*SIZE, 10.0*SIZE);

*/

    glFrustum(-(float)2.0*SIZE, (float)2.0*SIZE, -(float)2.0*SIZE,

                                (float)2.0*SIZE, (float)3.0*SIZE, (float)40.0*SIZE);

    glMatrixMode(GL_MODELVIEW);

    glViewport((int)0, (int)0, w, h);

}

 

/******************************************************************

This function calls Draw if there is nothing else to call

*******************************************************************/

static void CALLBACK Idle(void)

{              Draw();}

 

/******************************************************************

This is the main function.

It defines the display modes, the initial window position and its

size. It also manages all the events that can be handled by your

program. currently, only two events are handled. They are <Esc> and

<S> keys pressed. You may use gltk library instead of glaux if

you like it more.

*******************************************************************/

//int main( int argc, char *argv[] )

void main()

{

    auxInitDisplayMode(AUX_DOUBLE | AUX_RGB | AUX_DIRECT);

    auxInitPosition(0, 0, 400, 400);

    if(auxInitWindow("CE304 - OpenGL") == GL_FALSE) {auxQuit();}

 

    Init();

   

    //auxExposeFunc(Reshape);

    //auxReshapeFunc(Reshape);

   

    // Enable these 2 lines for Visual C++

    auxExposeFunc((AUXEXPOSEPROC)Reshape);

    auxReshapeFunc((AUXRESHAPEPROC)Reshape);

   

    auxKeyFunc (AUX_ESCAPE, Close);

    auxKeyFunc (AUX_1, parametric8);

    auxKeyFunc (AUX_2, parametric20);

    auxKeyFunc (AUX_UP, RotateUp);

    auxKeyFunc (AUX_DOWN, RotateDown);

    auxKeyFunc (AUX_LEFT, RotateLeft);

    auxKeyFunc (AUX_RIGHT, RotateRight);

    auxKeyFunc (AUX_Z, ScaleUp);

    auxKeyFunc (AUX_z, ScaleDown);

    auxKeyFunc (AUX_R | AUX_r, KeepOnRotate);

    auxKeyFunc (AUX_A | AUX_a, Animate);

    auxKeyFunc (AUX_F | AUX_f, FastAnimate);

    auxKeyFunc (AUX_S | AUX_s, SlowAnimate);

    auxKeyFunc (AUX_T | AUX_t, StopAnimate);

    auxKeyFunc (AUX_C | AUX_c, Cont_Animate);

    auxKeyFunc (AUX_p, FlatShaded);

    auxKeyFunc (AUX_P, SmoothShaded);

    auxKeyFunc (AUX_W | AUX_w, WireFrame);

    auxKeyFunc (AUX_O | AUX_o, Org_Position);

   

    //auxKeyFunc (AUX_L, EnableLight);

    //auxKeyFunc (AUX_l, NormalLight);

               

    auxIdleFunc(Idle);

    auxMainLoop(Draw);

   

   // return(0); 

}

 

 

*OpenGL Source code – 4 Surfaces*

 

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

 

// Enable this line for Visual C++

#include <GL/glaux.h>

// Also enable all CALLBACK for Visual C++

 

/*#include "glaux.h"

#include <GL/gl.h>

#include <GL/glu.h>

*/

 

#define    SIZE 1.0

#define PI 3.141

 

float scaling = (float)1.0, k_var=0.0, k_max=1.0, alpha, beta;

int increment=3, formulae=0, rotate_flag=0, display_flag=0,

                animation_flag=0, swap_flag=0, shade_flag=1;

 

void CALLBACK NormalLight(void);

void CALLBACK EnableLight(void);

 

/**********************************************************************

                This function is called only once in the very beginning.

                It can initialize whatever you want.

                Right now, two angles are defined.

***********************************************************************/

static void Init()

{

                static GLfloat light_position[]={0.0,SIZE,5.0*SIZE,0.0};

                static GLfloat mat_ambient[]={0.0,1.8,0.0,1.0};

                static GLfloat mat_emission[]={0.0,0.0,0.0,1.0};

                static GLfloat mat_diffuse[]={0.0,0.8,0.0,1.0};

                static GLfloat mat_specular[]={0.0,1.0,0.0,1.0};

                static GLfloat mat_shininess[]={50.0};

               

                glLightfv(GL_LIGHT0,GL_POSITION,light_position);

                glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);

                glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);

                glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);

                glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);

                glMaterialfv(GL_FRONT,GL_EMISSION,mat_emission);

 

                alpha=(float)360.0;

                beta=(float)20.0;

}

 

/***********************************************************************

Main Drawing Function.

It is called by the system to draw every next buffer with the maximum

speed available.

************************************************************************/

static void CALLBACK Draw()

{              

                void MakeObject(void);

 

                glClearColor((float)0.0, (float)0.0, (float)0.0, (float)0.0);

                glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

                glPushMatrix();

                if (rotate_flag == 1)

                {

                                alpha=alpha-(float)increment;

                                if (alpha<=(float)0.0)

                                                alpha=(float)360.0;

                }

/* This translation is done for the visualization transformation  */

                glTranslatef((float)0.0,(float)0.0,-(float)(5.0*SIZE));

/* The following are rotations done about Y followed by rotation about X  */

                glRotatef(beta, (float)1.0, (float)0.0, (float)0.0);

                glRotatef(alpha, (float)0.0, (float)1.0, (float)0.0);

/* This could be a scalinging if you decide to scalinge. Right now we scalinge by 1,1,1 */

                glScalef(scaling,scaling,scaling);

/* Here, the MakeObject function is called. You have to change it first to draw

   the requested objects  */

                MakeObject();

                glFlush();

                glPopMatrix();

                auxSwapBuffers();

}

 

/**********************************************************************

                This function defines the object(s) to be drawn.

                Now, for wire-frame drawing, only polylines and lines are used.

                If you decided to draw shaded polygons, do not forget to

                define materials and lights in Init()

***********************************************************************/

void CALLBACK DrawAxis(void)

{

/* Set color to red  */

    glColor3f((float)1.0, (float)0.0, (float)0.0);

 

/* Coordinate axes - three lines  */

                glBegin(GL_LINES);

/* CL_LINES defines segments of straight line connecting vertices v1 with v2,

   v3 with v4, v5 with v6, etc. */

     

                glVertex3f(-(float)(2.0*SIZE),(float)0,(float)0);

                glVertex3f((float)(2.0*SIZE),(float)0,(float)0);

 

/* Set color to green  */

                glColor3f((float)0.0, (float)1.0, (float)0.0);

 

                glVertex3f((float)0,-(float)(2.0*SIZE),(float)0);

                glVertex3f((float)0,(float)(2.0*SIZE),(float)0);

 

/* Set color to purple  */

                glColor3f((float)1.0, (float)0.0, (float)1.0);

 

                glVertex3f((float)0,(float)0,-(float)(2.0*SIZE));

                glVertex3f((float)0,(float)0,(float)(2.0*SIZE));

                glEnd();

                glFlush();

}

 

/**********************************************************************

                Parametric Equations number 8 and 20.

***********************************************************************/

 

// Flat Spiral

void Formulae8(GLfloat fi,GLfloat teta,GLfloat *x,GLfloat *y,GLfloat*z)

{

                *x=0.25*fi*cos(teta);

                *y=0.25*teta-0.5;

                *z=0.25*fi*sin(teta);

} 

 

// Fancy Sine

void Formulae20(GLfloat fi,GLfloat teta,GLfloat *x,GLfloat *y,GLfloat*z)

{

                *x=0.2*teta*sin(fi);

                *y=0.2*fi*sin(teta);

                *z=0.2*fi*cos(teta);

} 

 

/* Swept Contracted Hypocycloid */

void Formulae14(GLfloat fi,GLfloat teta,GLfloat *x,GLfloat *y,GLfloat*z)

{

                *x=0.1*(3*cos(fi)+0.8*cos(3*fi));

                *y=0.2*cos(teta)*(5+3*sin(fi)-0.8*sin(3*fi));

                *z=0.2*sin(teta)*(5+3*sin(fi)-0.8*sin(3*fi));

} 

 

/* Swept Epicycloid */

void Formulae15(GLfloat fi,GLfloat teta,GLfloat *x,GLfloat *y,GLfloat*z)

{

                *x=0.15*(4*cos(fi)-cos(4*fi));

                *y=0.15*cos(teta)*(6+4*sin(fi)-sin(4*fi));

                *z=0.15*sin(teta)*(6+4*sin(fi)-sin(4*fi));

} 

 

/**********************************************************************

                Return a vertex for either of the formulae chosen

        and the time at t (depends whether animation is required)

***********************************************************************/

void GetFormulae(GLfloat fi,GLfloat teta,GLfloat p[3])

{

                GLfloat p1[3],p2[3];

 

                // p1 is for formulae8 and p2 is for formulae20

 

                //Formulae8(fi,teta,&p1[0],&p1[1],&p1[2]);

                //Formulae20(fi,teta,&p2[0],&p2[1],&p2[2]);

                                switch (formulae) {

                                case 1: Formulae8(fi,teta,&p1[0],&p1[1],&p1[2]);

                                                                Formulae20(fi,teta,&p2[0],&p2[1],&p2[2]);

                                                                break;

                                case 2: Formulae20(fi,teta,&p1[0],&p1[1],&p1[2]);

                                                                Formulae14(fi,teta,&p2[0],&p2[1],&p2[2]);

                                                                break;

                                case 3: Formulae14(fi,teta,&p1[0],&p1[1],&p1[2]);

                                                                Formulae15(fi,teta,&p2[0],&p2[1],&p2[2]);

                                                                break;

                }

                /* Returning the vertex.

                   if k is 0 then return vertex for formuale8

                   otherwise if t =1 the vertex return is for formulae20 */

                p[0] = (1-k_var)*p1[0]+p2[0]*k_var;

                p[1] = (1-k_var)*p1[1]+p2[1]*k_var;

                p[2] = (1-k_var)*p1[2]+p2[2]*k_var;

}

 

/******************************************************************/

 

void Normalize(GLfloat x,GLfloat y,GLfloat z)

{

                glNormal3f(x,y,z);

                glVertex3f(x,y,z);

}

/******************************************************************/

 

void MakeObject()

{

                GLfloat p1[3];

                GLfloat var_angle,fi,fi2,teta,teta2;

 

                fi2 = PI*2;

                teta2 = PI*2;

               

                // Compute current interval angle size.

                // if Scaling is big, then intervals should be more

                // to enable us to see the shape of the wireframe.

                // (13.0+(int)(scaling*20)) is the number of intervals

                // var_angle is the angle size.

               

                var_angle = PI/(13.0+(int)(scaling*20));

                               

                if (!display_flag){

                                DrawAxis();

                                /* Set color to green  */

                                glColor3f((float)0.0, (float)0.0, (float)1.0);

                                for (fi=0;fi<=fi2;fi+=var_angle){

                                                glBegin(GL_LINE_STRIP);

                                                for (teta=0; teta<=teta2;teta+=var_angle){                                                           

                                                                GetFormulae(fi,teta,p1);

                                                                glVertex3f(p1[0],p1[1],p1[2]);

                                                }

                                                glEnd();

                                }

                                for (teta=0; teta<=teta2;teta+=var_angle){

                                                glBegin(GL_LINE_STRIP);

                                                for (fi=0;fi<=fi2;fi+=var_angle){

                                                                GetFormulae(fi,teta,p1);

                                                                glVertex3f(p1[0],p1[1],p1[2]);

                                                }

                                                glEnd();

                                }

                }

                else{

                                glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);

                                if (display_flag==1)

                                                glShadeModel(GL_FLAT);

                                else

                                                glShadeModel(GL_SMOOTH);

                                NormalLight();

                                DrawAxis();

                                EnableLight();

 

                                for (teta=0; teta<=teta2;teta+=var_angle){

                                                glBegin(GL_QUADS);

                                                for (fi=0;fi<=fi2;fi+=var_angle){

                                                                GetFormulae(fi,teta,p1);

                                                                Normalize(p1[0],p1[1],p1[2]);              

                                                                GetFormulae(fi,teta+var_angle,p1);

                                                                Normalize(p1[0],p1[1],p1[2]);

                                                                GetFormulae(fi+var_angle,teta+var_angle,p1);

                                                                Normalize(p1[0],p1[1],p1[2]);

                                                                GetFormulae(fi+var_angle,teta,p1);

                                                                Normalize(p1[0],p1[1],p1[2]);

                                                }

                                                glEnd();

                                }

                }

                glFlush();

                if (animation_flag){

                                if (!swap_flag)

                                {

                                                k_var+=0.01*k_max; //expand

                                                if (k_var>1.0)

                                                                k_var=1.0;

                                                if (formulae==8)

                                                {

                                                    if(k_var>=1.0)

                                                                    swap_flag = 1.0;

                                                    else

                                                                    swap_flag = 0.0;

                                                }

                                                else if(k_var>=1.0)

                                                {              

                                                                k_var=0.0;

                                                                if((formulae+1)>4)

                                                                                formulae=1;

                                                                else         

                                                                                formulae+=1;

                                                               

                                                }

                                }

                                else

                                {

                                                k_var-=0.01*k_max; // contract

                                                if (k_var<=0.0)

                                                {

                                                    k_var=0.0;

                                                    swap_flag = 0.0;

                                                }

                                                else

                                                    swap_flag = 1.0;

                                               

                                }

                }

               

               

} /* MakeObject */

 

/******************************************************************/

 

void CALLBACK Close(void)

{               auxQuit();}

 

void CALLBACK parametric8(void)

{               formulae=1;           animation_flag=0;                k_var=0;}

 

void CALLBACK parametric20(void)

{               formulae=1;           animation_flag=0;                k_var=1;}

 

void CALLBACK KeepOnRotate(void)

{               if(rotate_flag==1)

                                rotate_flag=0;

                else

                                rotate_flag=1;

}

 

void CALLBACK RotateUp(void)

{               if(beta<=3)

                                beta=360.0;

                else

                                beta-=(float)3.0;

}

 

void CALLBACK RotateDown(void)

{               if(beta>=360)

                                beta=0.0;

                else

                                beta+=(float)3.0;

}

 

void CALLBACK RotateLeft(void)

{               if(alpha<=3)

                                alpha=360.0;

                else

                                alpha-=(float)3.0;

}

 

void CALLBACK RotateRight(void)

{               if(alpha>=360)

                                alpha=0.0;

                else

                                alpha+=(float)3.0;

}

 

void CALLBACK EnableLight(void)                    // extra lighting to illumimnate polygons

{               glEnable(GL_LIGHTING);

                glEnable(GL_LIGHT0);

                glDepthFunc(GL_LESS);

                glEnable(GL_DEPTH_TEST);

                glEnable(GL_NORMALIZE);

}

 

void CALLBACK NormalLight(void)                   // normal lighting to illumimnate polygons

{               glDisable(GL_LIGHTING);

                glDisable(GL_LIGHT0);

                glDisable(GL_DEPTH_TEST);

                glDisable(GL_NORMALIZE);}

 

void CALLBACK FlatShaded(void)

{    display_flag=1;}

 

void CALLBACK SmoothShaded(void)

{    display_flag=2;}

 

void CALLBACK WireFrame(void)

{    display_flag=0; NormalLight();  }

 

void CALLBACK FastAnimate(void)

{               increment=increment>10?10:increment+1;         k_max=k_max>=10?100:k_max+2;}

 

void CALLBACK SlowAnimate(void)

{               increment=increment>1?increment-1:1;              k_max=k_max<4?2:k_max-9;}

 

void CALLBACK ScaleUp(void)

{               scaling+=0.1;}

 

void CALLBACK ScaleDown(void)

{               scaling=scaling>0.2?scaling-0.1:0.1;}

 

void CALLBACK Animate(void)

{               swap_flag = 0;        animation_flag = 1;              k_var=0.0;              k_max=1;}

 

void CALLBACK StopAnimate(void)

{               animation_flag = 0;}

 

void CALLBACK Cont_Animate(void)

{               animation_flag = 1;}

 

void CALLBACK Org_Position(void)

{               alpha=(float)360.0;               beta=(float)20.0; scaling=(float)1.0;}

 

 

/******************************************************************

In this function the projection transformation is defined.

Currently, it is perspective projection. It can be orthographic

projection as well if you use the commented glOrtho instead of

glFrustum

*******************************************************************/

void CALLBACK Reshape(GLsizei w, GLsizei h)

{

    glMatrixMode (GL_PROJECTION);             

    glLoadIdentity ();

/*  You may use this glOrtho in place of glFrustrum if you do not

    like perspective projection   

    glOrtho(-2.0*SIZE, 2.0*SIZE, -2.0*SIZE, 2.0*SIZE,

                                0.0*SIZE, 10.0*SIZE);

*/

    glFrustum(-(float)2.0*SIZE, (float)2.0*SIZE, -(float)2.0*SIZE,

                                (float)2.0*SIZE, (float)3.0*SIZE, (float)40.0*SIZE);

    glMatrixMode(GL_MODELVIEW);

    glViewport((int)0, (int)0, w, h);

}

 

/******************************************************************

This function calls Draw if there is nothing else to call

*******************************************************************/

static void CALLBACK Idle(void)

{               Draw();}

 

/******************************************************************

This is the main function.

It defines the display modes, the initial window position and its

size. It also manages all the events that can be handled by your

program. currently, only two events are handled. They are <Esc> and

<S> keys pressed. You may use gltk library instead of glaux if

you like it more.

*******************************************************************/

//int main( int argc, char *argv[] )

void main()

{

    auxInitDisplayMode(AUX_DOUBLE | AUX_RGB | AUX_DIRECT);

    auxInitPosition(0, 0, 400, 400);

    if(auxInitWindow("CE304 - OpenGL") == GL_FALSE) {auxQuit();}

 

    Init();

   

    //auxExposeFunc(Reshape);

    //auxReshapeFunc(Reshape);

   

    // Enable these 2 lines for Visual C++

    auxExposeFunc((AUXEXPOSEPROC)Reshape);

    auxReshapeFunc((AUXRESHAPEPROC)Reshape);

   

    auxKeyFunc (AUX_ESCAPE, Close);

    auxKeyFunc (AUX_1, parametric8);

    auxKeyFunc (AUX_2, parametric20);

    auxKeyFunc (AUX_UP, RotateUp);

    auxKeyFunc (AUX_DOWN, RotateDown);

    auxKeyFunc (AUX_LEFT, RotateLeft);

    auxKeyFunc (AUX_RIGHT, RotateRight);

    auxKeyFunc (AUX_Z, ScaleUp);

    auxKeyFunc (AUX_z, ScaleDown);

    auxKeyFunc (AUX_R | AUX_r, KeepOnRotate);

    auxKeyFunc (AUX_A | AUX_a, Animate);

    auxKeyFunc (AUX_F | AUX_f, FastAnimate);

    auxKeyFunc (AUX_S | AUX_s, SlowAnimate);

    auxKeyFunc (AUX_T | AUX_t, StopAnimate);

    auxKeyFunc (AUX_C | AUX_c, Cont_Animate);

    auxKeyFunc (AUX_p, FlatShaded);

    auxKeyFunc (AUX_P, SmoothShaded);

    auxKeyFunc (AUX_W | AUX_w, WireFrame);

    auxKeyFunc (AUX_O | AUX_o, Org_Position);

   

    //auxKeyFunc (AUX_L, EnableLight);

    //auxKeyFunc (AUX_l, NormalLight);

               

    auxIdleFunc(Idle);

    auxMainLoop(Draw);

   

   // return(0); 

}