Thursday, September 29, 2011

Figure 53


$40 off Craftsman 5 Drawer Homeowner Tool Center with Riser and Parts Bins $99.99 on sale 10/2-10/8
Sears Deal of the Day: Thule Car Top Carrier, Quest
Wholesale Fleece Blankets
400950_BuddhaGroove.com-Culture Infused Living: Home Accents, Jewelry, Accessories from around the world!
400948_Culture Infused Living: Home Accents, Jewelry, and accessories from around the world. CulturalElemen
504427_Save up to 75% on batteries and accessories at Brooklyn Battery Works!
504080_10% OFF Orders of $75 or More from Journelle.com! Use Promo Code: NOV11 (Valid 11/1 - 11/30/11)
502239_Brand names, bargain prices at shoezoo.com. If you don't love your new shoes, return them for free!

400949_GaneshMall.com-Culture Infused Living: Home Accents, Jewelry, and accessories from around the world!



Figure 53
======================
Note: The rings keep on whirling.
======================


/*
//Author: Cheikh Drame
//To support, please do one of the following
//- Visit irap123.com many times a day
//- Join irap123.com
//- Buy my app irap123 (rhyming dictionary) available for android, and iphone. On windows phone, it is called iLyrics. If not, you can get the desktop version at www.irap123.com/apps/webstore/
//- Include irap123.com in your blogs, forums, facebook walls, email footers, comments
//- send a donation at my paypal address at cbdsigned.vevo@gmail.com
//- Buy a story at http://shop.pparzygnat.com
*/
// Rings.cpp : Defines the entry point for the console application.
//




/* rings.c
*
* To compile: cc -o rings rings.c -lGL -lGLU -lX11 -lglut -lXmu -lm
*
* Usage: rings
*
* Homework 4, Part 1: perspective, hierarchical coords, moving eye pos.
*
* Do a slow zoom on a bunch of rings (ala Superman III?)
*
* Philip Winston - 2/21/95
* pwinston@hmc.edu
* http://www.cs.hmc.edu/people/pwinston
*
*/

#include

#include
#include
#include

/* Some files do not define M_PI... */
#ifndef M_PI
#define M_PI 3.14159265
#endif

typedef enum {MENU_STARTOVER, MENU_ZOOM_OUT, MENU_STOP_RINGS, MENU_STOP_FADE,
MENU_START_RINGS, MENU_START_FADE, MENU_QUIT} MenuChoices;

typedef enum {NOTALLOWED, CONE, TORUS, INNERMAT, OUTTERMAT} DisplayLists;

#define STEPS 30

int Fade = 1; /* Start moving out */

float Axis = 0, AxisInc = (2.0 * M_PI / STEPS);

GLfloat InnerRad, OutterRad, Tilt, Trans, TransCone, Dist;

/* mainly computes the translation amount as a function of the
tilt angle and torus radii */
void myInit(void)
{
float sinoftilt;

InnerRad = 0.70;
OutterRad = 5.0;
Tilt = 15;
Dist = 10;

sinoftilt = sin(Tilt * M_PI*2/360);

Trans = (2*OutterRad + InnerRad) * sinoftilt + InnerRad +
((1 - sinoftilt) * InnerRad) - (InnerRad * 1/10);

TransCone = Trans + (OutterRad * sinoftilt + InnerRad);
}

/* I used code from the book's accnot.c as a starting point for lighting.
I have one positional light in center, then one directional */
void myglInit(void)
{
GLfloat light0_position[] = { 1.0, 0.2, 1.0, 0.0 };
GLfloat light1_position[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light1_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light1_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat lm_ambient[] = { 0.2, 0.2, 0.2, 1.0 };

glEnable(GL_NORMALIZE);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.2);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lm_ambient);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);

glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);

glFlush();
}

void myreshape(GLsizei w, GLsizei h)
{
glViewport(0,0,w,h);
glFlush();
}

/* setup display lists to change material for inner/outter rings and
to draw a single torus or cone */
void MakeDisplayLists(void)
{
GLfloat cone_diffuse[] = { 0.0, 0.7, 0.7, 1.0 };
GLfloat mat1_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat2_ambient[] = { 0.0, 0.0, 0.0, 0.0 };
GLfloat torus1_diffuse[] = { 0.7, 0.7, 0.0, 1.0 };
GLfloat torus2_diffuse[] = { 0.3, 0.0, 0.0, 1.0 };
GLfloat mat1_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat2_specular[] = { 0.5, 0.5, 0.5, 1.0 };

glNewList(INNERMAT, GL_COMPILE);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat1_specular);
glMaterialf(GL_FRONT, GL_SHININESS, 50.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, torus1_diffuse);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat1_ambient);
glEndList();

glNewList(OUTTERMAT, GL_COMPILE);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat2_specular);
glMaterialf(GL_FRONT, GL_SHININESS, 25.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, torus2_diffuse);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat2_ambient);
glEndList();

glNewList(CONE, GL_COMPILE);
glMaterialfv(GL_FRONT, GL_DIFFUSE, cone_diffuse);
glPushMatrix();
glTranslatef(0, -TransCone, 0);
glRotatef(90, 1, 0, 0);
glutSolidCone(OutterRad, 10, 8, 8);
glPopMatrix();
glEndList();

glNewList(TORUS, GL_COMPILE);
glPushMatrix();
glRotatef(90, 1, 0, 0);
glutSolidTorus(InnerRad, OutterRad, 15, 25);
glPopMatrix();
glEndList();
}

/* Draw three rings, rotated and translate so they look cool */
void DrawRings(float axis)
{
GLfloat x = sin(axis), y = cos(axis);

glPushMatrix();
glTranslatef(0, Trans, 0);
glRotatef(Tilt, x, 0, y);
glCallList(TORUS);
glPopMatrix();

glPushMatrix();
glRotatef(-Tilt, x, 0, y);
glCallList(TORUS);
glPopMatrix();

glPushMatrix();
glTranslatef(0, -Trans, 0);
glRotatef(Tilt, x, 0, y);
glCallList(TORUS);
glPopMatrix();
}

/* Draw the inner thing, then glScale and draw 3 huge rings */
void mydisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1, 1, -1, 1, 10, 1000);
gluLookAt(0, 0, Dist, 0, 0, 0, 0, 1, 0);

glMatrixMode(GL_MODELVIEW);

glCallList(INNERMAT);
DrawRings(Axis);
glCallList(CONE);

glCallList(OUTTERMAT);
glPushMatrix();
glScalef(10, 10, 10);
DrawRings(Axis/3);
glPopMatrix();

glutSwapBuffers();
glFlush();
}

/* rotate the axis and adjust position if nec. */
void myidle(void)
{
Axis += AxisInc;

if (Dist < 15 && Fade) /* start slow */
Dist += 0.1;
else if (Dist < 800 && Fade) /* don't go back too far */
Dist *= 1.005;

mydisplay();
}

/* nothing fancy */
void handlemenu(int value)
{
switch (value) {
case MENU_STARTOVER:
Dist = 10; Axis = 0; Fade = 1;
AxisInc = (2.0 * M_PI / STEPS);
glutChangeToMenuEntry(3, "Stop rings", MENU_STOP_RINGS);
glutChangeToMenuEntry(4, "Stop fade", MENU_STOP_FADE);
break;
case MENU_ZOOM_OUT:
Dist = 800;
break;
case MENU_STOP_RINGS:
AxisInc = 0;
glutChangeToMenuEntry(3, "Start rings", MENU_START_RINGS);
break;
case MENU_START_RINGS:
AxisInc = (2.0 * M_PI / STEPS);
glutChangeToMenuEntry(3, "Stop rings", MENU_STOP_RINGS);
break;
case MENU_STOP_FADE:
Fade = 0;
glutChangeToMenuEntry(4, "Start fade", MENU_START_FADE);
break;
case MENU_START_FADE:
Fade = 1;
glutChangeToMenuEntry(4, "Stop fade", MENU_STOP_FADE);
break;
case MENU_QUIT:
exit(0);
break;
}
}

void MenuInit(void)
{
glutCreateMenu(handlemenu);
glutAddMenuEntry("Start Over", MENU_STARTOVER);
glutAddMenuEntry("Zoom Out", MENU_ZOOM_OUT);
glutAddMenuEntry("Stop rings", MENU_STOP_RINGS);
glutAddMenuEntry("Stop fade", MENU_STOP_FADE);
glutAddMenuEntry("Quit", MENU_QUIT);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}

void
vis(int visible)
{
if (visible == GLUT_VISIBLE) {
glutIdleFunc(myidle);
} else {
glutIdleFunc(NULL);
}
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(512, 512);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("rings by irap123.com");

myInit();
myglInit();

MakeDisplayLists();
MenuInit();

glutReshapeFunc(myreshape);
glutDisplayFunc(mydisplay);
glutVisibilityFunc(vis);

glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}

No comments:

Post a Comment