#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <GL/glut.h>
#define H1 0
#define H2 1
#define H3 2
#define H4 3
#define H5 4
#define QUIT 5
//GLenum doubleBuffer;
float rotX = 0.0, rotY = 0.0;
static int spin = 0;
static int obj = 0;
static int begin;
float st=0.1;
void
menu_select(int item)
{
if (item == QUIT)
exit(0);
obj = item;
glutPostRedisplay();
}
void
movelight(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state ==
GLUT_DOWN) {
begin = x;
}
}
void
motion(int x, int y)
{
spin = (spin + (x - begin)) % 360;
begin = x;
glutPostRedisplay();
}
void
myinit(void)
{
GLfloat specular1[] = {0.727811, 0.626959, 0.626959};
glEnable(GL_DEPTH_TEST); // cho phep loai bo doi
tuong bi che
glDepthFunc(GL_LESS);
glHint( GL_PERSPECTIVE_CORRECTION_HINT,
GL_NICEST );
GLfloat amb_light[] = { 0.1, 0.1, 0.1, 1.0 };
GLfloat diffuse[] = { 0.6, 0.6, 0.6, 1 };
GLfloat specular[] = { 0.7, 0.7, 0.3, 1 };
glLightModelfv( GL_LIGHT_MODEL_AMBIENT,
amb_light );
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse );
glLightfv( GL_LIGHT0, GL_SPECULAR, specular );
glEnable( GL_LIGHT0 );
glEnable( GL_COLOR_MATERIAL );
glShadeModel( GL_SMOOTH );
glLightModeli( GL_LIGHT_MODEL_TWO_SIDE,
GL_FALSE );
glDepthFunc( GL_LEQUAL );
glEnable( GL_DEPTH_TEST );
glEnable(GL_LIGHTING);
//glEnable(GL_LIGHT0);
glMaterialfv(GL_FRONT, GL_SPECULAR, specular1);
glMaterialf(GL_FRONT, GL_SHININESS, 0.6*128.0);
glClearColor(0.5, 0.5, 0.5, 0.5);
}
//
static void Key(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
}
}
static void SpecialKey(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_UP:
rotX -= 20.0;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN:
rotX += 20.0;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT:
rotY -= 20.0;
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT:
rotY += 20.0;
glutPostRedisplay();
break;
}
}
void
output(GLfloat x, GLfloat y, char *format,...) // Huong Dan
Su Dung tren man hinh chinh
{
va_list args;
char buffer[200], *p;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
glPushMatrix();
glTranslatef(x, y, 0);
for (p = buffer; *p; p++)
glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
glPopMatrix();
}
//
void
display(void)
{
GLfloat position[] =
{0.0, 1.0, 0.5, 0.0};
glClear(GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(0.0, 0.0, -5.0);
glPushMatrix();
glRotated((GLdouble) spin, 0.0, 1.0, 0.0);
glRotated(0.0, 1.0, 0.0, 0.0);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glTranslated(0.0, 0.0,2);
glDisable(GL_LIGHTING);
glColor3f(1.0, 0.0, 0.0);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glutSolidSphere (0.1 , 10 , 10 );
glPopMatrix();
//
switch (obj) {
case H1:
glColor3f(0,0.75,0.5);
glRotatef(rotY, 0.0,1.0,0.0);
glRotatef(rotX, 1.0,0.0,0.0);
glutSolidTeapot(1.0); // VE AM TRA
glPopMatrix();
break;
case H2:
glPushMatrix();
glColor3f(0.25,0.4,0.7);
glRotatef(rotY, 0.0,1.0,0.0);
glRotatef(rotX, 1.0,0.0,0.0);
glutSolidTorus(0.275, 0.85, 20, 20);
glPopMatrix();
break;
case H3:
glPushMatrix();
glColor3f(0.0,0.25,0.5);
glRotatef(rotY, 0.0,1.0,0.0);
glRotatef(rotX, 1.0,0.0,0.0);
glutSolidIcosahedron();
glPopMatrix();
break;
case H4:
glPushMatrix();
glColor3f(0.0,0.25,0.5);
glRotatef(rotY, 0.0,1.0,0.0);
glRotatef(rotX, 1.0,0.0,0.0);
glutSolidSphere (1 , 32 , 32 );
glPopMatrix();
break;
}
glPopMatrix();
glPushAttrib(GL_ENABLE_BIT);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, 3000, 0, 3000);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
output(50, 2800, "Huong Dan: khoi cau do la NGUON
SANG");
output(50, 2650, "Click chuot phai de hien menu");
output(50, 2450, "Re chuot trai de thay doi huong chieu
sang");
output(50, 2250, "Nhan 4 phim mui ten de xoay hinh");
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glPopAttrib();
glutSwapBuffers();
}
void
myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40.0, (GLfloat) w / (GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int
main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB |
GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Bai Tap Lon KTDH");
myinit();
glutMouseFunc(movelight);
glutMotionFunc(motion);
glutReshapeFunc(myReshape);
glutKeyboardFunc(Key);
glutSpecialFunc(SpecialKey);
glutDisplayFunc(display);
glutCreateMenu(menu_select);
glutAddMenuEntry("Am Tra", H1);
glutAddMenuEntry("Hinh Xuyen", H2);
glutAddMenuEntry("Hinh Nhi Thap Phuong", H3);
glutAddMenuEntry("Hinh Cau", H4);
glutAddMenuEntry("Thoat", QUIT);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
return 0;
}