How do I implement two translations and a scale operation in GLSL?

…衆ロ難τιáo~ 提交于 2020-04-17 22:39:30

问题


I am trying to implement the following transformation.

My original world-space coordinates are (2D) x=1586266800 and y=11812

I want:

  1. the bottom left corner of the OpenGL image to represent coordinates (1586266800, 11800)
  2. the top right corner of the OpenGL image to represent coordinates (1586267400, 11900)

In order to do that I plan to join three transformation matrices:

  1. Translate to the origin of coordinates x=1586266800 and y=11800
  2. Scale to have a width of 600 and a height of 100
  3. Translate again -1.0f and -1.0f so the center of the OpenGL is at the bottom left.

I use the following transformation matrices:

Translation Matrix:

| 1 0 0 tx |
| 0 1 0 ty |
| 0 0 1 tz |
| 0 0 0 1  |

Scale Matrix:

| sx  0  0  0 |
|  0 sy  0  0 |
|  0  0 sz  0 |
|  0  0  0  1 |

In Octave I can implement the transformation as follows, multiplying three matrices:

>> candle
candle =
  1586266800
       11812
           0
           1
>> translation1
translation1 =
    1   0   0   -1586266800
    0   1   0        -11800
    0   0   1             0
    0   0   0             1

>> scale
scale =
   0.00333333333333333      0   0   0
                     0   0.02   0   0
                     0      0   1   0
                     0      0   0   1

(where `0.0033333 = 2/600` and `0.02 = 2/100`)

>> translation2
translation2 =
    1   0   0   -1
    0   1   0   -1
    0   0   1    0
    0   0   0    1

>> translation2*scale*translation1*candle
ans =

                    -1
    -0.759999999999991
                     0
                     1

Which translates the point to the right place in a -1.0f,1.0f OpenGL screen.

Now I am trying to replicate that in my Geometry shader, which receives the original world-space coordinates from the vertex shader.

I tried this:

#version 330 core

layout (points) in;
layout (line_strip, max_vertices = 12) out;

in uint gs_in_y[];
in uint gs_in_x[];

uniform uint xOrigin;
uniform uint xScaleWidth;
uniform uint yOrigin;
uniform uint yScaleWidth;

void main()
{

    // TRANSLATION MATRIX
    // [ 1 0 0 tx ]
    // [ 0 1 0 ty ]
    // [ 0 0 1 tz ]
    // [ 0 0 0 1  ]
    // mat3 m = mat3(
    //  1.1, 2.1, 3.1, // first column (not row!)
    //  1.2, 2.2, 3.2, // second column
    //  1.3, 2.3, 3.3  // third column
    //  );
    mat4 translation = mat4(
        1.0f, 0, 0, -xOrigin,
        0, 1.0f, 0, -yOrigin,
        0, 0, 1.0f, 0,
        0, 0, 0, 1.0f
    );


    // SCALE MATRIX
    // [ sx  0  0  0 ]
    // [  0 sy  0  0 ]
    // [  0  0 sz  0 ]
    // [  0  0  0  1 ]
    mat4 scale = mat4(
        2.0/xScaleWidth, 0, 0, 0,
        0, 2.0f/yScaleWidth, 0, 0,
         0,  0, 1.0f,  0,
         0,  0,  0,  1.0f
    );

    // FINAL TRANSLATION
    mat4 translationGl = mat4(
        1.0f,      0,    0,   -1.0f,
           0,   1.0f,    0,   -1.0f,
           0,      0, 1.0f,   0,
           0,      0,    0,   1.0f
    );

    gl_Position = translationGl * scale * translation * vec4(gs_in_x[0], gs_in_y[0], 0.0, 1.0);
    EmitVertex();    
    gl_Position = translationGl * scale * translation * vec4(gs_in_x[0]+30, gs_in_y[0], 0.0, 1.0); 
    EmitVertex();    
    EndPrimitive();

}

来源:https://stackoverflow.com/questions/61138427/how-do-i-implement-two-translations-and-a-scale-operation-in-glsl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!