www.pudn.com > OpenGL.rar > VR.cpp
#include <math.h>
#include <stdio.h>
#include <GL/glut.h>
#define TRUE 1
#define FALSE 0
/* Magic numbers for relationship b/w cylinder head and crankshaft. */
#define MAGNITUDE 120
#define PHASE 270.112
#define FREQ_DIV 58
#define ARC_LENGHT 2.7
#define ARC_RADIUS 0.15
/* Dimensions of texture image. */
#define IMAGE_WIDTH 64
#define IMAGE_HEIGHT 64
/* Step to be taken for each rotation. */
#define ANGLE_STEP 10
/* Rotation angles */
GLdouble view_h = 225, view_v = 0,head_angle = 0;
GLint crank_angle = 0;
/* Crank rotation step. */
GLdouble crank_step = 5;
/* Toggles */
GLshort shaded = TRUE, anim = FALSE;
GLshort texture = FALSE, transparent = FALSE;
GLshort light1 = TRUE, light2 = FALSE;
/* Storage for the angle look up table and the texture map */
GLdouble head_look_up_table[361];
GLubyte image[IMAGE_WIDTH][IMAGE_HEIGHT][3];
GLint list_flywheel_shaded = 4;
GLint list_flywheel_texture = 8;
/* Variable used in the creaton of glu objects */
GLUquadricObj *obj;
int temp=1;
void myBox(GLdouble x, GLdouble y, GLdouble z)
{
glPushMatrix();
glScalef(x, y, z);
if (shaded)
glutSolidCube(1);
else
glutWireCube(1);
//getchar();
glPopMatrix();
}
void myCylinder(GLUquadricObj * object, GLdouble outerRadius, GLdouble innerRadius, GLdouble lenght)
{
glPushMatrix();
gluCylinder(object, outerRadius, outerRadius, lenght, 20, 1);
glPushMatrix();
glRotatef(180, 0.0, 1.0, 0.0);
gluDisk(object, innerRadius, outerRadius, 20, 1);
glPopMatrix();
glTranslatef(0.0, 0.0, lenght);
gluDisk(object, innerRadius, outerRadius, 20, 1);
glPopMatrix();
}
void draw_flywheel(void)
{
glPushMatrix();
glColor4f(0.5, 0.5, 1.0, 1.0);
glRotatef(90, 0.0, 1.0, 0.0);
myCylinder(obj, 0.625, 0.08, 0.5);
glPopMatrix();
}
void draw_crankbell(void)
{
glPushMatrix();
glColor4f(1.0, 0.5, 0.5, 1.0);
glRotatef(90, 0.0, 1.0, 0.0);
myCylinder(obj, 0.3, 0.08, 0.12);
glColor4f(0.5, 0.1, 0.5, 1.0);
glTranslatef(0.0, 0.2, 0.0);
myCylinder(obj, 0.06, 0.0, 0.34);
glTranslatef(0.0, 0.0, 0.22);
glRotatef(90, 0.0, 1.0, 0.0);
glPopMatrix();
}
void draw_crank(void)
{
glPushMatrix();
glRotatef(crank_angle, 1.0, 0.0, 0.0);
glPushMatrix();
glRotatef(90, 0.0, 1.0, 0.0);
glTranslatef(0.0, 0.0, -1.0);
myCylinder(obj, 0.08, 0.0, 1.4);
glPopMatrix();
glPushMatrix();
glTranslatef(0.28, 0.0, 0.0);
draw_crankbell();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.77, 0.0, 0.0);
if (shaded) {
if (texture)
glCallList(list_flywheel_texture);
else
glCallList(list_flywheel_shaded);
} else
draw_flywheel();
glPopMatrix();
glPopMatrix();
}
void display(void)
{
int pass;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
if (transparent) {
glEnable(GL_ALPHA_TEST);
pass = 2;
} else {
glDisable(GL_ALPHA_TEST);
pass = 0;
}
/* Rotate the whole model */
glRotatef(view_h, 0, 1, 0);
glRotatef(view_v, 1, 0, 0);
do {
glPushMatrix();
glTranslatef(0.5, 1.4, 0.0);
// draw_cylinder_head();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0, -0.8, 0.0);
draw_crank();
glPopMatrix();
} while (pass > 0);
glDepthMask(GL_TRUE);
glutSwapBuffers();
glPopMatrix();
}
void animation(void)
{
if ((crank_angle += crank_step) >= 360)
crank_angle = 0;
head_angle = head_look_up_table[crank_angle];
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 's':
if (anim) {
glutIdleFunc(0);
anim = FALSE;
} else {
glutIdleFunc(animation);
anim = TRUE;
}
break;
case 'k':
if (texture &amt;&amt; !shaded);
else
break;
default:
return;
}
glutPostRedisplay();
}
void make_image(void)
{
int i, j, c;
for (i = 0; i < IMAGE_WIDTH; i++)
{
for (j = 0; j < IMAGE_HEIGHT; j++)
{
c = ((((i &amt; 0x8) == 0) ^ ((j &amt; 0x8)) == 0)) * 255;
image[i][j][0] = (GLubyte) c;
image[i][j][1] = (GLubyte) c;
image[i][j][2] = (GLubyte) c;
}
}
}
void make_table(void)
{
GLint i;
GLdouble k;
for (i = 0, k = 0.0; i < 360; i++, k++)
{
head_look_up_table[i] =
MAGNITUDE * atan(
(ARC_RADIUS * sin(PHASE - k / FREQ_DIV)) /
((ARC_LENGHT - ARC_RADIUS * cos(PHASE - k / FREQ_DIV))));
}
}
void myinit(void)
{
GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_shininess[] = {50.0};
GLfloat light_position1[] = {1.0, 1.0, 1.0, 0.0};
GLfloat light_position2[] = {-1.0, 1.0, 1.0, 0.0};
glClearColor(0.0, 0.0, 0.0, 0.0);
obj = gluNewQuadric();
make_table();
make_image();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, 3, IMAGE_WIDTH,
IMAGE_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE,
image);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); //
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position1);
glLightfv(GL_LIGHT1, GL_POSITION, light_position2);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glDisable(GL_ALPHA_TEST);
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glShadeModel(GL_SMOOTH);
glNewList(list_flywheel_shaded, GL_COMPILE);
draw_flywheel();
glEndList();
glNewList(list_flywheel_texture, GL_COMPILE);
draw_flywheel();
glEndList();
gluQuadricTexture(obj, GL_FALSE);
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (GLfloat) w / (GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0); /* viewing transform */
glScalef(1.5, 1.5, 1.5);
}
int main(int argc, char **argv)
{
puts("VR homework ÍòÓ¨");
puts("start to rotate: 's'");
glutInitWindowSize(400, 400);
glutInit(&amt;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutCreateWindow("Miniature Steam Engine by Troy Robinette");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myinit();
glutReshapeFunc(myReshape);
glutMainLoop();
return 0;
}