Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <GL/glut.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #define PI 3.1415
- GLfloat angle=45.0f;
- GLfloat cubesize;
- boolean option=false;
- void demo_menu(int id){
- if(id==3)
- exit(0);
- else if (id==1) {
- option=false;
- }
- else if (id==2){
- option=true;
- }
- glutPostRedisplay();
- }
- void timer(){
- glutPostRedisplay();
- angle+=0.2;
- if(angle>=360.0) angle-=360.0;
- cubesize=3+2*fabs(sin(angle*PI/180));
- }
- void myinit(void)
- {
- typedef GLfloat point[3];
- /* attributes */
- glEnable(GL_BLEND);
- glClearColor(1.0, 1.0, 1.0, 0.0); /* white background */
- glColor3f(0.0, 0.0, 1.0); /* draw in red */
- /* set up viewing */
- /* 500 x 500 window with origin lower left */
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-100.0,100.0,-100.0,100.0,-200.0,200.0);
- glMatrixMode(GL_MODELVIEW);
- glNewList(1,GL_COMPILE);
- point vertices[4]= {{1.0,1.0,1.0},{1.0,-1.0,1.0},{-1.0,-1.0,1.0},{-1.0,1.0,1.0}};
- glBegin(GL_POLYGON);
- glVertex3fv(vertices[0]);
- glVertex3fv(vertices[1]);
- glVertex3fv(vertices[2]);
- glVertex3fv(vertices[3]);
- glEnd();
- glEndList();
- }
- void cubefunc() {
- glLoadIdentity();
- if (option){
- glTranslatef(0,0,-22);
- glRotatef(angle,1,0,1);
- glTranslatef(0,0,-88);
- }
- else{
- glTranslatef(0,0,-110);
- glRotatef(angle,1,0,1);
- }
- glScalef(cubesize,cubesize,cubesize);
- }
- void pleures(){
- glColor3f(1.0,0.0,0.0); //red
- glCallList(1); //z
- glPushMatrix();
- glRotatef(90,1,0,0);//-y
- glColor3f(1.0,0.0,1.0); //purple
- glCallList(1);
- glColor3f(1.0,1.0,0.0); //yellow
- glRotatef(90, 1,0,0);
- glCallList(1); // -z
- glColor3f(0.0,0.0,1.0); //blue
- glRotatef(90, 1,0,0);
- glCallList(1); // y
- glPopMatrix();
- glColor3f(0.0,1.0,0.0); //green
- glPushMatrix();
- glRotatef(90,0,1,0);
- glCallList(1);//-x
- glColor3f(0.0,1.0,1.0); //cyan
- glRotatef(180,0,1,0);
- glCallList(1);//-x
- glPopMatrix();
- }
- void display( void )
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- /* define a point data type */
- glColor3f(0.0,0.0, 1.0);
- glLoadIdentity();
- glTranslatef(0,0,-88);
- //glCallList(1);
- //glPushMatrix();
- cubefunc();
- pleures();
- //glPopMatrix();
- glFlush();
- }
- int main(int argc, char** argv)
- {
- /* Standard GLUT initialization */
- glutInit(&argc,argv);
- glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); /* default, not needed */
- // glDrawBuffer(GL_BACK) ;
- glutInitWindowSize(500.0,500.0); /* 500 x 500 pixel window */
- glutInitWindowPosition(0,0); /* place window top left on display */
- glutCreateWindow("Moving square"); /* window title */
- glutDisplayFunc(display); /* display callback invoked when window opened */
- myinit(); /* set attributes */
- glEnable(GL_DEPTH_TEST);
- glutIdleFunc(timer);
- glutCreateMenu(demo_menu);
- glutAddMenuEntry("a)",1);
- glutAddMenuEntry("b)",2);
- glutAddMenuEntry("Exit",3);
- glutAttachMenu(GLUT_RIGHT_BUTTON);
- glutMainLoop(); /* enter event loop */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement