1 #define GL_GLEXT_PROTOTYPES
10 typedef struct GeometryCorrection
13 float keystone_vertical;
15 float curvature_depth;
16 float vertical_center;
20 typedef struct InteractiveView
23 GLXContext glx_context;
25 Atom geometry_data_atom;
32 static unsigned short frustum_indices[] = { 2, 1, 3, 0, 1, 0xFFFF, 3, 4, 2, 0, 4, 0xFFFF };
33 static float view_matrix[] =
35 0.70711f, 0.23570f, -0.66667f, 0.0f,
36 0.0f, 0.94281f, 0.33333f, 0.0f,
37 0.70711f, -0.23570f, 0.66667f, 0.0f,
38 -0.2f, 0.0f, -2.0f, 1.0f
40 static float projection_matrix[] =
42 2.0f, 0.0f, 0.0f, 0.0f,
43 0.0f, 2.6667f, 0.0f, 0.0f,
44 0.0f, 0.0f, -1.1f, -1.0f,
45 0.0f, 0.0f, -1.05f, 0.0f
48 static const char *vshader_src =
50 "uniform mat4 view;\n"
54 " gl_Position = view*vertex;\n"
57 static const char *gshader_src =
59 "layout(triangles) in;\n"
60 "layout(triangle_strip, max_vertices=3) out;\n"
61 "uniform mat4 projection;\n"
65 " vec3 v0 = gl_in[0].gl_Position.xyz;\n"
66 " vec3 v1 = gl_in[1].gl_Position.xyz;\n"
67 " vec3 v2 = gl_in[2].gl_Position.xyz;\n"
68 " vec3 tri_normal = normalize(cross(v1-v0, v2-v0));\n"
69 " for(int i=0; i<3; ++i)\n"
71 " normal = tri_normal;\n"
72 " gl_Position = projection*gl_in[i].gl_Position;\n"
78 static const char *fshader_src =
81 "out vec4 frag_color;\n"
84 " frag_color = vec4(vec3(0.5+clamp(normal.z, 0.0, 1.0)*0.5), 1.0);\n"
87 static const char *flat_vshader_src =
89 "uniform mat4 view;\n"
90 "uniform mat4 projection;\n"
94 " gl_Position = projection*view*vertex;\n"
97 static const char *flat_fshader_src =
99 "uniform vec4 color;\n"
100 "out vec4 frag_color;\n"
103 " frag_color = color;\n"
107 GeometryCorrection *get_corrections(Display *display)
111 Atom correction_atom;
114 unsigned long overflow;
115 unsigned long names_length;
117 unsigned long values_length;
119 unsigned ncorrections;
121 GeometryCorrection *corrections;
124 root = DefaultRootWindow(display);
125 monitors_atom = XInternAtom(display, "_MSP_GEOMETRY_CORRECTION_MONITORS", False);
126 correction_atom = XInternAtom(display, "_MSP_GEOMETRY_CORRECTION", False);
128 XGetWindowProperty(display, root, monitors_atom, 0, 64, False, XA_STRING,
129 &prop_type, &prop_format, &names_length, &overflow, (unsigned char **)&names);
130 if(prop_type!=XA_STRING || prop_format!=8)
133 XGetWindowProperty(display, root, correction_atom, 0, 64, False, XA_INTEGER,
134 &prop_type, &prop_format, &values_length, &overflow, (unsigned char **)&values);
135 if(prop_type!=XA_INTEGER || prop_format!=16)
141 ncorrections = (names_length>0);
142 for(i=0; i<names_length; ++i)
145 if(ncorrections*5>values_length)
146 ncorrections = values_length/5;
147 corrections = (GeometryCorrection *)malloc((ncorrections+1)*sizeof(GeometryCorrection));
150 for(i=0; i<ncorrections; ++i)
154 namelen = strlen(name_ptr);
155 corrections[i].monitor_name = (char *)malloc(namelen+1);
156 strcpy(corrections[i].monitor_name, name_ptr);
158 corrections[i].keystone_vertical = values[i*5]/4096.0f;
159 corrections[i].curvature_type = values[i*5+1];
160 corrections[i].curvature_depth = values[i*5+2]/4096.0f;
161 corrections[i].vertical_center = values[i*5+3]/4096.0f;
162 corrections[i].perspective = values[i*5+4]/4096.0f;
164 name_ptr += namelen+1;
167 corrections[ncorrections].monitor_name = NULL;
175 void set_corrections(Display *display, GeometryCorrection *corrections)
177 unsigned ncorrections;
185 Atom correction_atom;
189 for(i=0; corrections[i].monitor_name; ++i)
192 total_len += strlen(corrections[i].monitor_name);
195 names = (char *)malloc(total_len+ncorrections);
196 values = (short *)malloc(ncorrections*5*sizeof(short));
198 for(i=0; i<ncorrections; ++i)
200 strcpy(name_ptr, corrections[i].monitor_name);
201 name_ptr += strlen(name_ptr)+1;
203 values[i*5] = corrections[i].keystone_vertical*4096;
204 values[i*5+1] = corrections[i].curvature_type;
205 values[i*5+2] = corrections[i].curvature_depth*4096;
206 values[i*5+3] = corrections[i].vertical_center*4096;
207 values[i*5+4] = corrections[i].perspective*4096;
210 root = DefaultRootWindow(display);
211 monitors_atom = XInternAtom(display, "_MSP_GEOMETRY_CORRECTION_MONITORS", False);
212 correction_atom = XInternAtom(display, "_MSP_GEOMETRY_CORRECTION", False);
213 XChangeProperty(display, root, monitors_atom, XA_STRING, 8, PropModeReplace, (unsigned char *)names, total_len+ncorrections-1);
214 XChangeProperty(display, root, correction_atom, XA_INTEGER, 16, PropModeReplace, (unsigned char *)values, ncorrections*5);
220 unsigned create_shader(GLenum type, const char *src)
224 shader = glCreateShader(type);
225 glShaderSource(shader, 1, &src, NULL);
226 glCompileShader(shader);
231 unsigned create_program(unsigned *shaders, unsigned nshaders)
236 program = glCreateProgram();
237 for(i=0; i<nshaders; ++i)
238 glAttachShader(program, shaders[i]);
239 glLinkProgram(program);
241 glUseProgram(program);
242 glUniformMatrix4fv(glGetUniformLocation(program, "view"), 1, GL_FALSE, view_matrix);
243 glUniformMatrix4fv(glGetUniformLocation(program, "projection"), 1, GL_FALSE, projection_matrix);
248 int initialize_view(Display *display, InteractiveView *view)
254 GLXFBConfig *configs;
257 XSetWindowAttributes win_attr;
260 if(!glXQueryVersion(display, &major_ver, &minor_ver) || major_ver<1 || (major_ver==1 && minor_ver<4))
264 attribs[i++] = GLX_DOUBLEBUFFER;
266 attribs[i++] = GLX_DEPTH_SIZE;
270 configs = glXChooseFBConfig(display, DefaultScreen(display), attribs, &nconfigs);
271 if(!configs || !nconfigs)
274 vi = glXGetVisualFromFBConfig(display, configs[0]);
275 root = DefaultRootWindow(display);
276 win_attr.colormap = XCreateColormap(display, root, vi->visual, AllocNone);
277 view->window = XCreateWindow(display, root, 0, 0, 1024, 768, 0, vi->depth, InputOutput, vi->visual, CWColormap, &win_attr);
278 XStoreName(display, view->window, "Geometry correction control");
279 XMapWindow(display, view->window);
281 view->glx_window = glXCreateWindow(display, configs[0], view->window, NULL);
282 view->glx_context = glXCreateNewContext(display, configs[0], GLX_RGBA_TYPE, NULL, True);
283 glXMakeContextCurrent(display, view->glx_window, view->glx_window, view->glx_context);
288 view->shaders[0] = create_shader(GL_VERTEX_SHADER, vshader_src);
289 view->shaders[1] = create_shader(GL_GEOMETRY_SHADER, gshader_src);
290 view->shaders[2] = create_shader(GL_FRAGMENT_SHADER, fshader_src);
291 view->programs[0] = create_program(view->shaders, 3);
292 view->shaders[3] = create_shader(GL_VERTEX_SHADER, flat_vshader_src);
293 view->shaders[4] = create_shader(GL_FRAGMENT_SHADER, flat_fshader_src);
294 view->programs[1] = create_program(view->shaders+3, 2);
295 glUniform4f(glGetUniformLocation(view->programs[1], "color"), 0.0f, 0.6f, 1.0f, 1.0f);
297 glGenBuffers(2, view->buffers);
298 glBindBuffer(GL_ARRAY_BUFFER, view->buffers[0]);
299 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), NULL);
300 glEnableVertexAttribArray(0);
301 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, view->buffers[1]);
303 glEnable(GL_PRIMITIVE_RESTART);
304 glPrimitiveRestartIndex(0xFFFF);
306 glEnable(GL_DEPTH_TEST);
307 glDepthFunc(GL_LEQUAL);
309 view->geometry_data_atom = XInternAtom(display, "_MSP_GEOMETRY_CORRECTION_DATA", False);
310 XSelectInput(display, view->window, KeyPressMask|ExposureMask);
315 void request_geometry_data(Display *display, InteractiveView *view, GeometryCorrection *target)
317 XChangeProperty(display, view->window, view->geometry_data_atom, XA_STRING, 8, PropModeReplace, (unsigned char *)target->monitor_name, strlen(target->monitor_name));
318 XConvertSelection(display, view->geometry_data_atom, view->geometry_data_atom, view->geometry_data_atom, view->window, CurrentTime);
321 void update_view(Display *display, InteractiveView *view, XSelectionEvent *event)
325 unsigned long overflow;
326 unsigned long data_length;
327 short *geometry_data;
328 unsigned tessellation;
332 unsigned short *index_data;
336 XGetWindowProperty(display, event->requestor, event->property, 0, 32262, False, XA_INTEGER,
337 &prop_type, &prop_format, &data_length, &overflow, (unsigned char **)&geometry_data);
339 tessellation = geometry_data[0];
340 nvertices = 5+(tessellation+1)*(tessellation+1);
342 vertex_data = (float *)malloc(nvertices*3*sizeof(float));
343 for(i=0; i<nvertices*3; ++i)
344 vertex_data[i] = geometry_data[1+i]/4096.0f;
346 view->nelements = tessellation*((tessellation+1)*2+1)-1;
348 nindices = 12+view->nelements;
349 index_data = (unsigned short *)malloc(nindices*sizeof(unsigned short));
350 memcpy(index_data, frustum_indices, sizeof(frustum_indices));
352 for(y=0; y<(int)tessellation; ++y)
355 index_data[i++] = 0xFFFF;
356 for(x=0; x<=(int)tessellation; ++x)
358 index_data[i++] = 5+(y+1)*(tessellation+1)+x;
359 index_data[i++] = 5+y*(tessellation+1)+x;
363 glBufferData(GL_ARRAY_BUFFER, nvertices*3*sizeof(float), vertex_data, GL_STATIC_DRAW);
364 glBufferData(GL_ELEMENT_ARRAY_BUFFER, nindices*sizeof(unsigned short), index_data, GL_STATIC_DRAW);
368 XFree(geometry_data);
371 int interactive(Display *display, GeometryCorrection *corrections, GeometryCorrection *target)
373 InteractiveView view;
377 initialize_view(display, &view);
378 request_geometry_data(display, &view, target);
388 XNextEvent(display, &event);
394 keysym = XLookupKeysym(&event.xkey, 0);
395 if(keysym==XK_Escape)
400 else if(update_pending)
402 else if(keysym==XK_q)
403 target->keystone_vertical += 1.0f/64;
404 else if(keysym==XK_a)
405 target->keystone_vertical -= 1.0f/64;
406 else if(keysym==XK_w)
407 target->curvature_depth += 1.0f/256;
408 else if(keysym==XK_s)
409 target->curvature_depth -= 1.0f/256;
410 else if(keysym==XK_e)
411 target->vertical_center += 1.0f/32;
412 else if(keysym==XK_d)
413 target->vertical_center -= 1.0f/32;
414 else if(keysym==XK_r)
415 target->perspective += 1.0f/16;
416 else if(keysym==XK_f)
417 target->perspective -= 1.0f/16;
418 else if(keysym==XK_z)
419 target->curvature_type = target->curvature_type%2+1;
423 set_corrections(display, corrections);
424 request_geometry_data(display, &view, target);
428 case SelectionNotify:
429 if(event.xselection.property==view.geometry_data_atom)
431 update_view(display, &view, &event.xselection);
440 printf("event %d\n", event.type);
446 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
447 glUseProgram(view.programs[0]);
448 glDrawElements(GL_TRIANGLE_STRIP, view.nelements, GL_UNSIGNED_SHORT, (void *)(12*sizeof(unsigned short)));
449 glUseProgram(view.programs[1]);
450 glDrawElements(GL_LINE_STRIP, 11, GL_UNSIGNED_SHORT, NULL);
451 glXSwapBuffers(display, view.glx_window);
458 int main(int argc, char **argv)
461 GeometryCorrection *corrections;
464 if(argc!=2 && argc!=7)
466 fprintf(stderr, "Usage: %s <monitor> [<keystone> <curvature> <depth> <vcenter> <perspective>]\n", argv[0]);
470 display = XOpenDisplay(NULL);
472 corrections = get_corrections(display);
475 for(i=0; corrections[i].monitor_name; ++i)
476 if(!strcmp(corrections[i].monitor_name, argv[1]))
482 if(!corrections || !corrections[i].monitor_name)
486 corrections = (GeometryCorrection *)realloc(corrections, (i+2)*sizeof(GeometryCorrection));
487 namelen = strlen(argv[1]);
488 corrections[i].monitor_name = (char *)malloc(namelen+1);
489 strcpy(corrections[i].monitor_name, argv[1]);
490 corrections[i+1].monitor_name = NULL;
491 corrections[i].keystone_vertical = 0.0f;
492 corrections[i].curvature_type = 1;
493 corrections[i].curvature_depth = 0.0f;
494 corrections[i].vertical_center = 0.5f;
495 corrections[i].perspective = 1.0f;
499 interactive(display, corrections, &corrections[i]);
502 if(!strcmp(argv[3], "cylindrical"))
503 corrections[i].curvature_type = 1;
504 else if(!strcmp(argv[3], "spherical"))
505 corrections[i].curvature_type = 2;
508 fprintf(stderr, "Invalid curvature\n");
511 corrections[i].keystone_vertical = strtod(argv[2], NULL);
512 corrections[i].curvature_depth = strtod(argv[4], NULL);
513 corrections[i].vertical_center = strtod(argv[5], NULL);
514 corrections[i].perspective = strtod(argv[6], NULL);
516 set_corrections(display, corrections);
519 XCloseDisplay(display);
520 for(i=0; corrections[i].monitor_name; ++i)
521 free(corrections[i].monitor_name);