]> git.tdb.fi Git - geometrycompositor.git/commitdiff
Use a more 3d-y approach for generating the monitor mesh
authorMikko Rasa <tdb@tdb.fi>
Sun, 13 Dec 2015 11:53:59 +0000 (13:53 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 13 Dec 2015 12:14:27 +0000 (14:14 +0200)
It's easier to grasp, especially when more corrections are implemented.

The coordinates for cylinder correction are now spaced evenly by angle
rather than X coordinate.  This also fixes incorrect texture coordinates.

source/main.c

index 04e662025dbf2f6eaf98e2dec919a28708544c6c..08be9dda8e8f8224f384939342fa07db2cc48ec5 100644 (file)
@@ -465,49 +465,75 @@ void update_monitor_vertices(CompositedScreen *screen, CompositedMonitor *monito
        unsigned x, y;
        unsigned i;
        float aspect;
-       float cyl_w_sq;
+       float cyl_radius;
+       float cyl_arc;
        float sin_ksv;
        float cos_ksv;
+       float distance;
+       float eye[3];
+       float look[3];
 
        t = monitor->tessellation;
 
        data_size = (t+1)*(t+1)*4*sizeof(float);
        vertex_data = (float *)malloc(data_size);
+
        aspect = (float)monitor->width/monitor->height;
-       cyl_w_sq = 0.25f-(0.5f-monitor->cylinder_depth)*(0.5f-monitor->cylinder_depth);
+
+       if(monitor->cylinder_depth)
+       {
+               cyl_radius = (monitor->cylinder_depth*monitor->cylinder_depth+0.25f)/(2.0f*monitor->cylinder_depth);
+               cyl_arc = 2.0f*asin(0.5f/cyl_radius);
+       }
+
        sin_ksv = monitor->keystone_vertical/sqrt(1.0f+monitor->keystone_vertical*monitor->keystone_vertical);
        cos_ksv = sqrt(1.0f-sin_ksv*sin_ksv);
+       distance = monitor->perspective+sin_ksv*((sin_ksv>0)-monitor->vertical_center)/aspect;
+
+       eye[0] = 0.0f;
+       eye[1] = (monitor->vertical_center-0.5f)/aspect+sin_ksv*distance;
+       eye[2] = cos_ksv*distance;
+
+       look[0] = 0.0f;
+       look[1] = -sin_ksv;
+       look[2] = -cos_ksv;
+
        for(y=0; y<=t; ++y)
                for(x=0; x<=t; ++x)
                {
-                       float xf, yf, z, rz;
-                       float scale;
                        float *v;
 
-                       xf = (float)x/t;
-                       yf = (float)y/t;
-
-                       v = vertex_data+(y*(t+1)+x)*4;
-                       v[2] = (monitor->x+xf*monitor->width)/screen->width;
-                       v[3] = ((float)screen->height-monitor->y-monitor->height+yf*monitor->height)/screen->height;
-
-                       z = sqrt(0.25f-(xf-0.5f)*(xf-0.5f)*4.0f*cyl_w_sq)-0.5f+monitor->cylinder_depth;
-                       if(monitor->keystone_vertical>0)
+                       v = vertex_data+(y*(t+1)+x)*3;
+                       v[0] = (float)x/t-0.5f;
+                       v[1] = ((float)y/t-0.5f)/aspect;
+                       v[2] = 0;
+                       if(monitor->cylinder_depth)
                        {
-                               rz = (1.0f-yf)/aspect*sin_ksv+z*cos_ksv;
-                               yf = 1.0f-((1.0f-yf)*cos_ksv-z*aspect*sin_ksv);
-                               z = rz;
+                               v[2] = (1.0f-cos(v[0]*cyl_arc))*cyl_radius-monitor->cylinder_depth;
+                               v[0] = sin(v[0]*cyl_arc)*cyl_radius;
                        }
-                       else if(monitor->keystone_vertical<0)
-                       {
-                               rz = z*cos_ksv-yf/aspect*sin_ksv;
-                               yf = yf*cos_ksv+z*aspect*sin_ksv;
-                               z = rz;
-                       }
-                       scale = monitor->perspective/(monitor->perspective+z);
-                       v[0] = 0.5f+(xf-0.5f)*scale;
-                       v[1] = monitor->vertical_center+(yf-monitor->vertical_center)*scale;
                }
+
+       for(y=t; y<=t; --y)
+               for(x=t; x<=t; --x)
+               {
+                       float *v;
+                       float px, py, pz;
+                       float scale;
+
+                       v = vertex_data+(y*(t+1)+x)*3;
+                       px = v[0]-eye[0];
+                       py = (v[0]-eye[0])*look[0] - (v[1]-eye[1])*look[2] + (v[2]-eye[2])*look[1];
+                       pz = (v[0]-eye[0])*look[0] + (v[1]-eye[1])*look[1] + (v[2]-eye[2])*look[2];
+                       scale = monitor->perspective/pz;
+
+                       v = vertex_data+(y*(t+1)+x)*4;
+                       v[0] = px*scale+0.5f;
+                       v[1] = py*aspect*scale+monitor->vertical_center;
+                       v[2] = (monitor->x+(float)x*monitor->width/t)/screen->width;
+                       v[3] = 1.0f-(monitor->y+(1.0f-(float)y/t)*monitor->height)/screen->height;
+               }
+
        glBindBuffer(GL_ARRAY_BUFFER, monitor->vertex_buffer);
        glBufferData(GL_ARRAY_BUFFER, data_size, vertex_data, GL_STATIC_DRAW);
        glBindBuffer(GL_ARRAY_BUFFER, 0);