Clone of mesa.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. * Test rubber-band selection box w/ logicops and blend.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #include <GL/glew.h>
  8. #include <GL/glut.h>
  9. #include "readtex.c"
  10. #define IMAGE_FILE "../images/arch.rgb"
  11. static int ImgWidth, ImgHeight;
  12. static GLenum ImgFormat;
  13. static GLubyte *Image = NULL;
  14. static int Win;
  15. static int Width = 512, Height = 512;
  16. struct rect
  17. {
  18. int x0, y0, x1, y1;
  19. };
  20. static struct rect OldRect, NewRect;
  21. static GLboolean ButtonDown = GL_FALSE;
  22. static GLboolean LogicOp = 0*GL_TRUE;
  23. static GLboolean RedrawBackground = GL_TRUE;
  24. static const GLfloat red[4] = {1.0, 0.2, 0.2, 1.0};
  25. static const GLfloat green[4] = {0.2, 1.0, 0.2, 1.0};
  26. static const GLfloat blue[4] = {0.2, 0.2, 1.0, 1.0};
  27. /*
  28. * Draw rubberband box in front buffer
  29. */
  30. static void
  31. DrawRect(const struct rect *r)
  32. {
  33. glDrawBuffer(GL_FRONT);
  34. if (LogicOp) {
  35. glLogicOp(GL_XOR);
  36. glEnable(GL_COLOR_LOGIC_OP);
  37. }
  38. else {
  39. glEnable(GL_BLEND);
  40. glBlendFunc(GL_ONE, GL_ONE);
  41. glBlendEquation(GL_FUNC_SUBTRACT);
  42. }
  43. glColor3f(1, 1, 1);
  44. glLineWidth(3.0);
  45. glBegin(GL_LINE_LOOP);
  46. glVertex2i(r->x0, r->y0);
  47. glVertex2i(r->x1, r->y0);
  48. glVertex2i(r->x1, r->y1);
  49. glVertex2i(r->x0, r->y1);
  50. glEnd();
  51. glDisable(GL_COLOR_LOGIC_OP);
  52. glDisable(GL_BLEND);
  53. glDrawBuffer(GL_BACK);
  54. }
  55. static void
  56. PrintString(const char *s)
  57. {
  58. while (*s) {
  59. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  60. s++;
  61. }
  62. }
  63. static void
  64. DrawBackground(void)
  65. {
  66. char s[100];
  67. sprintf(s, "[L/B] %s mode. Use mouse to make selection box.",
  68. LogicOp ? "LogicOp" : "Blend");
  69. glClear(GL_COLOR_BUFFER_BIT);
  70. glWindowPos2i((Width - ImgWidth) / 2, (Height - ImgHeight) / 2);
  71. glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, Image);
  72. glWindowPos2i(10, 10);
  73. PrintString(s);
  74. glutSwapBuffers();
  75. }
  76. static void
  77. Draw(void)
  78. {
  79. if (RedrawBackground) {
  80. DrawBackground();
  81. }
  82. if (ButtonDown) {
  83. if (!RedrawBackground)
  84. DrawRect(&OldRect); /* erase old */
  85. DrawRect(&NewRect); /* draw new */
  86. OldRect = NewRect;
  87. }
  88. RedrawBackground = GL_FALSE;
  89. }
  90. static void
  91. Reshape(int width, int height)
  92. {
  93. Width = width;
  94. Height = height;
  95. glViewport(0, 0, width, height);
  96. glMatrixMode(GL_PROJECTION);
  97. glLoadIdentity();
  98. glOrtho(0, Width, Height, 0, -1, 1); /* Inverted Y! */
  99. glMatrixMode(GL_MODELVIEW);
  100. glLoadIdentity();
  101. RedrawBackground = GL_TRUE;
  102. }
  103. static void
  104. Key(unsigned char key, int x, int y)
  105. {
  106. (void) x;
  107. (void) y;
  108. switch (key) {
  109. case 'b':
  110. case 'B':
  111. LogicOp = GL_FALSE;
  112. break;
  113. case 'l':
  114. case 'L':
  115. LogicOp = GL_TRUE;
  116. break;
  117. case 27:
  118. glutDestroyWindow(Win);
  119. exit(0);
  120. break;
  121. }
  122. RedrawBackground = GL_TRUE;
  123. glutPostRedisplay();
  124. }
  125. static void
  126. SpecialKey(int key, int x, int y)
  127. {
  128. (void) x;
  129. (void) y;
  130. switch (key) {
  131. case GLUT_KEY_UP:
  132. break;
  133. case GLUT_KEY_DOWN:
  134. break;
  135. case GLUT_KEY_LEFT:
  136. break;
  137. case GLUT_KEY_RIGHT:
  138. break;
  139. }
  140. glutPostRedisplay();
  141. }
  142. static void
  143. MouseMotion(int x, int y)
  144. {
  145. if (ButtonDown) {
  146. NewRect.x1 = x;
  147. NewRect.y1 = y;
  148. glutPostRedisplay();
  149. }
  150. }
  151. static void
  152. MouseButton(int button, int state, int x, int y)
  153. {
  154. if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
  155. ButtonDown = GL_TRUE;
  156. RedrawBackground = GL_TRUE;
  157. NewRect.x0 = NewRect.x1 = x;
  158. NewRect.y0 = NewRect.y1 = y;
  159. OldRect = NewRect;
  160. }
  161. else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
  162. ButtonDown = GL_FALSE;
  163. }
  164. glutPostRedisplay();
  165. }
  166. static void
  167. Init(void)
  168. {
  169. Image = LoadRGBImage(IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat);
  170. if (!Image) {
  171. printf("Couldn't read %s\n", IMAGE_FILE);
  172. exit(0);
  173. }
  174. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  175. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  176. }
  177. int
  178. main(int argc, char *argv[])
  179. {
  180. glutInit(&argc, argv);
  181. glutInitWindowSize(Width, Height);
  182. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  183. Win = glutCreateWindow(argv[0]);
  184. glewInit();
  185. glutReshapeFunc(Reshape);
  186. glutKeyboardFunc(Key);
  187. glutSpecialFunc(SpecialKey);
  188. glutMotionFunc(MouseMotion);
  189. glutMouseFunc(MouseButton);
  190. glutDisplayFunc(Draw);
  191. Init();
  192. glutPostRedisplay();
  193. glutMainLoop();
  194. return 0;
  195. }