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