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