Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // BOX Movement using up-down-left-right key of the keyboard.
- #include <gl/glut.h>
- #include <stdio.h>
- #include <math.h>
- // Initial size of graphics window.
- int WIDTH = 800;
- int HEIGHT = 600;
- // Current size of window.
- int width = WIDTH;
- int height = HEIGHT;
- int noOfDisplayCalls=0;
- float offset = 0;
- int dir = 0;
- int noOfTimer=0;
- /**
- * TIMERSEC int
- * the amount of time (in milisecond) after which animate function is called
- */
- const int TIMERMSECS = 5000;
- const int BOX_WIDTH = 100;
- const int BOX_HEIGHT = 30;
- int boxleft = 350, boxtop=200;
- void display ()
- {
- // printf("calling display(%d)\n",++noOfDisplayCalls);
- // Function that clears the window. If you don't want to clear the view window, don't call glClear function. make it as a comment statement.
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // --------------
- // YOUR CODE ...
- // --------------
- glColor3f(.5, .7, .1);
- glLineWidth(1);
- glBegin(GL_TRIANGLES);
- //if (dir==1)
- glVertex2f(width/(double)WIDTH*100+offset,height/(double)HEIGHT*310);
- //else
- //glVertex2f(width/(double)WIDTH*30+offset,height/(double)HEIGHT*50+offset);
- glVertex2f(width/(double)WIDTH*100+offset,height/(double)HEIGHT*290);
- glVertex2f(width/(double)WIDTH*350,height/(double)HEIGHT*300);
- //glVertex2f(400+offset,600);//
- glBegin(GL_TRIANGLES);
- //if (dir==1)
- glVertex2f(width/(double)WIDTH*360,height/(double)HEIGHT*100+offset);
- //else
- //glVertex2f(width/(double)WIDTH*30+offset,height/(double)HEIGHT*50+offset);
- glVertex2f(width/(double)WIDTH*340,height/(double)HEIGHT*100+offset);
- glVertex2f(width/(double)WIDTH*350,height/(double)HEIGHT*300);
- glEnd();
- glutSwapBuffers();
- }
- void mouseMovement(int mx, int my)
- {
- // --------------
- // YOUR CODE ...
- // --------------
- //printf("(%d,%d)\n",mx,my);
- glutPostRedisplay();
- }
- void mouseAction(int button, int state, int x, int z)
- // Record cursor position in response to mouse button action
- {
- switch(button){
- case GLUT_LEFT_BUTTON:
- if (state == GLUT_DOWN) {
- printf("Clicked\n");
- }
- else if(state==GLUT_UP){
- printf("Released\n");
- }
- break;
- }
- } // end mouse
- // Respond to window resizing, preserving proportions.
- // Parameters give new window size in pixels.
- void reshapeMainWindow (int newWidth, int newHeight)
- {
- printf("Calling Reshape()\n");
- // ----------------------------
- // DO NOT ALTER THIS FUNCTION ...
- // ----------------------------
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- glOrtho( 0, newWidth, 0, newHeight, 0, 1 );
- glViewport( 0, 0, newWidth, newHeight );
- width = newWidth;
- height = newHeight;
- glMatrixMode(GL_MODELVIEW);
- //glutDisplayFunc(display);//
- glutPostRedisplay();
- }
- // Respond to graphic character keys.
- // Parameters give key code and mouse coordinates.
- void graphicKeys (unsigned char key, int x, int y)
- {
- printf("Calling graphicKeys()\n");
- switch (key) {
- case 'p':{
- offset++;
- break;
- }
- case 'h':
- // --------------
- // YOUR CODE ...
- // --------------
- break;
- case 27: // Escape
- exit(0);
- default:
- printf("%c\n",key);
- }
- glutPostRedisplay();
- }
- // Respond to function keys.
- // Parameters give key code and mouse coordinates.
- void functionKeys (int key, int x, int y)
- {
- switch (key)
- {
- case GLUT_KEY_UP:
- boxtop +=10;
- break;
- case GLUT_KEY_DOWN:
- boxtop -=10;
- break;
- case GLUT_KEY_LEFT:
- boxleft -= 10;
- break;
- case GLUT_KEY_RIGHT:
- boxleft += 10;
- break;
- }
- glutPostRedisplay();
- }
- void timer(int value) {
- printf("calling timer(%d)\n",++noOfTimer);
- // --------------
- // YOUR CODE ...
- // --------------
- glutTimerFunc(500, timer, 0);
- glutPostRedisplay();
- }
- void doThisAlways()
- {
- //printf("Calling doThisAlways()\n");
- if(dir==1)
- {
- if(offset==500)
- {
- dir=0;
- }
- offset+=2.5;
- }
- else if(dir==0)
- {
- if(offset==0)
- {
- dir=1;
- }
- offset-=2.5;
- }
- glutPostRedisplay();
- }
- int main()
- {
- // GLUT initialization.
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize(width, height);
- glutCreateWindow("GLUT Skeleton Program");
- // Register call backs.
- glutDisplayFunc(display);
- glutReshapeFunc(reshapeMainWindow);
- glutKeyboardFunc(graphicKeys);
- glutSpecialFunc(functionKeys);
- glutPassiveMotionFunc(mouseMovement);
- glutMouseFunc(mouseAction);
- glutIdleFunc(doThisAlways);
- glutTimerFunc(500, timer, 0);
- // Enter GLUT loop.
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement