Guest User

Untitled

a guest
Feb 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. /* program illustrates the use of the GLUT library for interfacing
  2. with a window system */
  3.  
  4. /* The program opens a window, clears it to black, then draws a
  5. box at the location of the mouse each time the left button is
  6. clicked. The right button exits the program. The program also
  7. reacts correctly when the window is moved or resized by clearing
  8. the new window to black.*/
  9.  
  10. #include <GL/glut.h>
  11.  
  12. /* globals */
  13.  
  14. GLsizei wh = 500, ww = 500; /* initial window size */
  15. GLfloat size = 3.0; /* half side length of square */
  16.  
  17.  
  18. void drawSquare(int x, int y)
  19. {
  20.  
  21. y=wh-y;
  22. //glColor3ub( (char) rand()%256, (char) rand()%256, (char) rand()%256);
  23. glBegin(GL_POLYGON);
  24. glVertex2f(x+size, y+size);
  25. glVertex2f(x-size, y+size);
  26. glVertex2f(x-size, y-size);
  27. glVertex2f(x+size, y-size);
  28. glEnd();
  29. glFlush();
  30. }
  31.  
  32.  
  33. /* reshaping routine called whenever window is resized or moved */
  34.  
  35. void myReshape(GLsizei w, GLsizei h)
  36. {
  37.  
  38. /* adjust clipping box */
  39.  
  40. glMatrixMode(GL_PROJECTION);
  41. glLoadIdentity();
  42. glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0);
  43. glMatrixMode(GL_MODELVIEW);
  44. glLoadIdentity();
  45.  
  46. /* adjust viewport and clear */
  47.  
  48. glViewport(0,0,w,h);
  49. glClear(GL_COLOR_BUFFER_BIT);
  50. glFlush();
  51.  
  52. /* set global size for use by drawing routine */
  53.  
  54. ww = w;
  55. wh = h;
  56. }
  57.  
  58. void myinit()
  59. {
  60. glClearColor (0.0, 0.0, 0.0, 1.0);
  61. glViewport(0,0,ww,wh);
  62. /* Pick 2D clipping window to match
  63. size of screen window. This choice avoids having to scale object
  64. coordinates each time window is resized. */
  65.  
  66. glMatrixMode(GL_PROJECTION);
  67. glLoadIdentity();
  68. glOrtho(0.0, (GLdouble) ww , 0.0, (GLdouble) wh , -1.0, 1.0);
  69.  
  70. /* set clear color to black and clear window */
  71.  
  72. glClearColor (0.0, 0.0, 0.0, 1.0);
  73. glClear(GL_COLOR_BUFFER_BIT);
  74. glFlush();
  75. }
  76. GLint ix,iy,jx,jy;
  77. void mouse(int btn, int state, int x, int y)
  78. {
  79.  
  80. if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
  81. {
  82. ix=x;iy=y;
  83. }
  84.  
  85. if(btn==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
  86. {
  87. jx=x;jy=y;
  88.  
  89. glColor3f(1.0,0.0,0.0);
  90.  
  91. glBegin(GL_POLYGON);
  92. glVertex2f(ix,iy);
  93. glVertex2f(ix,jy);
  94. glVertex2f(jx,jy);
  95. glVertex2f(jx,iy);
  96.  
  97. glEnd();
  98. glFlush();
  99. }
  100.  
  101.  
  102. }
  103.  
  104. /* display callback required by GLUT */
  105.  
  106. void display(void)
  107. {
  108.  
  109.  
  110. }
  111.  
  112. int main(int argc, char** argv)
  113. {
  114.  
  115. glutInit(&argc,argv);
  116. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
  117. glutCreateWindow("square");
  118. //myinit ();
  119. //glutReshapeFunc (myReshape);
  120. glutMouseFunc (mouse);
  121. //glutPassiveMotionFunc(drawSquare);
  122. glutDisplayFunc(display);
  123. glutMainLoop();
  124.  
  125. }
Add Comment
Please, Sign In to add comment