jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame^] | 1 | /* |
| 2 | ** blendxor.c - Demonstrates the use of the blend_logic_op |
| 3 | ** extension to draw hilights. Using XOR to draw the same |
| 4 | ** image twice restores the background to its original value. |
| 5 | */ |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <string.h> |
| 9 | #ifndef _WIN32 |
| 10 | #include <unistd.h> |
| 11 | #endif |
| 12 | #include <stdlib.h> |
| 13 | #include <GL/glut.h> |
| 14 | |
| 15 | |
| 16 | GLenum doubleBuffer; |
| 17 | int dithering = 0; |
| 18 | GLint windW, windH; |
| 19 | |
| 20 | static void Init(void) |
| 21 | { |
| 22 | glDisable(GL_DITHER); |
| 23 | glShadeModel(GL_FLAT); |
| 24 | } |
| 25 | |
| 26 | static void Reshape(int width, int height) |
| 27 | { |
| 28 | |
| 29 | windW = (GLint)width; |
| 30 | windH = (GLint)height; |
| 31 | |
| 32 | glViewport(0, 0, (GLint)width, (GLint)height); |
| 33 | |
| 34 | glMatrixMode(GL_PROJECTION); |
| 35 | glLoadIdentity(); |
| 36 | gluOrtho2D(0, 400, 0, 400); |
| 37 | glMatrixMode(GL_MODELVIEW); |
| 38 | } |
| 39 | |
| 40 | static void Key(unsigned char key, int x, int y) |
| 41 | { |
| 42 | |
| 43 | switch (key) { |
| 44 | case 27: |
| 45 | exit(1); |
| 46 | case 'd': |
| 47 | dithering = !dithering; |
| 48 | break; |
| 49 | default: |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | glutPostRedisplay(); |
| 54 | } |
| 55 | |
| 56 | static void Draw(void) |
| 57 | { |
| 58 | int i; |
| 59 | |
| 60 | glDisable(GL_BLEND); |
| 61 | |
| 62 | (dithering) ? glEnable(GL_DITHER) : glDisable(GL_DITHER); |
| 63 | |
| 64 | glClearColor(0.5, 0.6, 0.1, 1.0); |
| 65 | glClear(GL_COLOR_BUFFER_BIT); |
| 66 | |
| 67 | /* Draw background prims */ |
| 68 | glColor3f(0.1, 0.1, 1.0); |
| 69 | glBegin(GL_TRIANGLES); |
| 70 | glVertex2i(5, 5); |
| 71 | glVertex2i(130, 50); |
| 72 | glVertex2i(100, 300); |
| 73 | glEnd(); |
| 74 | glColor3f(0.5, 0.2, 0.9); |
| 75 | glBegin(GL_TRIANGLES); |
| 76 | glVertex2i(200, 100); |
| 77 | glVertex2i(330, 50); |
| 78 | glVertex2i(340, 400); |
| 79 | glEnd(); |
| 80 | |
| 81 | glEnable(GL_BLEND); |
| 82 | glBlendEquationEXT(GL_LOGIC_OP); |
| 83 | glLogicOp(GL_XOR); |
| 84 | |
| 85 | /* Draw a set of rectangles across the window */ |
| 86 | glColor3f(0.9, 0.2, 0.8); |
| 87 | for(i = 0; i < 400; i+=60) { |
| 88 | glBegin(GL_POLYGON); |
| 89 | glVertex2i(i, 100); |
| 90 | glVertex2i(i+50, 100); |
| 91 | glVertex2i(i+50, 200); |
| 92 | glVertex2i(i, 200); |
| 93 | glEnd(); |
| 94 | } |
| 95 | glFlush(); /* Added by Brian Paul */ |
| 96 | #ifndef _WIN32 |
| 97 | sleep(2); |
| 98 | #endif |
| 99 | |
| 100 | /* Redraw the rectangles, which should erase them */ |
| 101 | for(i = 0; i < 400; i+=60) { |
| 102 | glBegin(GL_POLYGON); |
| 103 | glVertex2i(i, 100); |
| 104 | glVertex2i(i+50, 100); |
| 105 | glVertex2i(i+50, 200); |
| 106 | glVertex2i(i, 200); |
| 107 | glEnd(); |
| 108 | } |
| 109 | glFlush(); |
| 110 | |
| 111 | |
| 112 | if (doubleBuffer) { |
| 113 | glutSwapBuffers(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static GLenum Args(int argc, char **argv) |
| 118 | { |
| 119 | GLint i; |
| 120 | |
| 121 | doubleBuffer = GL_FALSE; |
| 122 | |
| 123 | for (i = 1; i < argc; i++) { |
| 124 | if (strcmp(argv[i], "-sb") == 0) { |
| 125 | doubleBuffer = GL_FALSE; |
| 126 | } else if (strcmp(argv[i], "-db") == 0) { |
| 127 | doubleBuffer = GL_TRUE; |
| 128 | } else { |
| 129 | printf("%s (Bad option).\n", argv[i]); |
| 130 | return GL_FALSE; |
| 131 | } |
| 132 | } |
| 133 | return GL_TRUE; |
| 134 | } |
| 135 | |
| 136 | int main(int argc, char **argv) |
| 137 | { |
| 138 | GLenum type; |
| 139 | char *s; |
| 140 | char *extName = "GL_EXT_blend_logic_op"; |
| 141 | |
| 142 | glutInit(&argc, argv); |
| 143 | |
| 144 | if (Args(argc, argv) == GL_FALSE) { |
| 145 | exit(1); |
| 146 | } |
| 147 | |
| 148 | glutInitWindowPosition(0, 0); glutInitWindowSize( 400, 400); |
| 149 | |
| 150 | type = GLUT_RGB; |
| 151 | type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE; |
| 152 | glutInitDisplayMode(type); |
| 153 | |
| 154 | if (glutCreateWindow("Blend XOR") == GL_FALSE) { |
| 155 | exit(1); |
| 156 | } |
| 157 | |
| 158 | /* Make sure blend_logic_op extension is there. */ |
| 159 | s = (char *) glGetString(GL_EXTENSIONS); |
| 160 | if (!s) |
| 161 | exit(1); |
| 162 | if (strstr(s,extName) == 0) { |
| 163 | printf("Blend_logic_op extension is not present.\n"); |
| 164 | exit(1); |
| 165 | } |
| 166 | |
| 167 | Init(); |
| 168 | |
| 169 | glutReshapeFunc(Reshape); |
| 170 | glutKeyboardFunc(Key); |
| 171 | glutDisplayFunc(Draw); |
| 172 | glutMainLoop(); |
| 173 | return 0; |
| 174 | } |