# Trying to understand vertex shaders better

This topic is 2019 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi Guys,

Now that I understand fragment shaders (to a reasonable degree), it is time for me to move on to vertex shaders

So, now I am playing with a basic vertex shader and focusing on this line (along with a 256 x 256 solid colour spite, so we are on the same page).

vec4 object_space_pos=vec4(in_Position.x,in_Position.y,in_Position.z,1.0);

Adding and subtracting numbers to in_Position.n let's you move the object around simply enough.

What if you wanted to skew the sprite? For example, have the top of the quad stay where it is but move the bottom of the quad to turn it into a parallelogram?

How would you go about doing something like this?

##### Share on other sites

x' = x + y * skewFactor

Note that a 4x4 matrix can trivially contain skew factors for 3d geometry:

1 0.5 0 0

0 1 0 0

0 0 1 0

0 0 0 1

Given the skew would be 0.5*y, this would effectively encode the transformation in your example (assuming y would go top to bottom). If "y" goes from bottom to top, simply negate the skew factor, and optionally add 0.5 to the first coefficient of the last row (x to w) to translate/bias the geometry.

##### Share on other sites

But, I am not understanding how to implement this in code?

Oooh got it working (in a way - got enough to play with it now anyway   )

varying vec2 v_vTexcoord;
varying vec4 v_vColour;
void main()
{
//vec4 object_space_pos = vec4( in_Position.x, in_Position.y+in_Position.y/2.0, in_Position.z,1.0);

vec4 object_space_pos=vec4(in_Position.x,in_Position.y,in_Position.z,1.0);

// How much we want to skew each axis by
float xSkew = 0.4;
float ySkew = 0.0;
// Create a transform that will skew our texture coords
mat4 trans = mat4(
1.0       , tan(xSkew), 0.0,  0.0,
tan(ySkew), 1.0,        0.0,  0.0,
0.0       , 0.0,        1.0,   0.0,
0.0       , 0.0,      0.0,    1.1
);

gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos * trans;

v_vColour = in_Colour;
v_vTexcoord = in_TextureCoord;
}

Thanks again!

Edited by lonewolff

##### Share on other sites

I recommend using a math library like GLM. Then you can pass the value of the created matrix to the shader via an uniform.

##### Share on other sites

I recommend using a math library like GLM. Then you can pass the value of the created matrix to the shader via an uniform.

Thanks for that. I'll take a look :)

##### Share on other sites

The only thing with this is that the object is now not positioned on the screen where I am asking it to be anymore.

I have two sprites that is aligned with each other (one above the other).

So, the second one should be located at sprite1.x & sprite1.y+256 but, after applying the shader to the second sprite it is located at roughly sprite1.x-200 give or take.

//
//
attribute vec3 in_Position;                  // (x,y,z)
//attribute vec3 in_Normal;                  // (x,y,z)     unused in this shader.
attribute vec4 in_Colour;                    // (r,g,b,a)
attribute vec2 in_TextureCoord;              // (u,v)
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
void main()
{
vec4 object_space_pos=vec4(in_Position.x,in_Position.y,in_Position.z,1.0);

float xSkew=-0.5;
float ySkew=0.0;
// Create a transform that will skew our texture coords
mat4 trans = mat4(   1.0,        tan(xSkew), 0.0,    0.0,
tan(ySkew), 1.0,        0.0,    0.0,
0.0,        0.0,        1.0,    0.0,
0.0,        0.0,        0.0,    1.0);

gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * trans * object_space_pos;

v_vColour = in_Colour;
v_vTexcoord = in_TextureCoord;
}

If you could shed an eye over my code that would be awesome

Do I need to multiply the negative tan(xSkew) with the original x position to compensate? Or have I just muliplied something the wrong way?

Screenshot of what is happening

Edited by lonewolff

##### Share on other sites

The only thing with this is that the object is now not positioned on the screen where I am asking it to be anymore.

No, it is where you told it to be, you just likely didn't realize how you modified the location.

The computer is correctly executing your code (it is doing what you asked), you likely didn't understand what the changes meant.

The formula you put in above modifies the basis vectors. Your transformation matrix re-defines what "up" and "over" mean (but you didn't modify "forward"). You have fundamentally modified what it means to be on the XY plane. There are six values you might need to modify in the transformation matrix to add a shear. (You can change XY, XZ, YX, YZ, ZX, ZY shears.) Every one of them modifies how coordinates are interpreted by redefining the basis vectors.

You have added to both the x component and the y component.  So the X position will now have an additional value of tan(xSkew) relative to what was formerly 'up' (or whatever your y axis pointed), and the Y position will now have an additional value of tan(ySkew) relative to what was formerly 'side' (whichever direction the x axis points)

Up is no longer up, it is "up and a little bit extra depending on how to the side I am" and side is no longer side, it is "side and a little bit extra depending on how up I am".

It may not be what you wanted, but it is what you wrote in the code..

There are very good reasons computer graphics study is usually put off until after linear algebra. You need to actually understand the math. Trying to gain an understanding by guesswork will likely be a very painful path.

Edited by frob
Slight clarification

##### Share on other sites

No, it is where you told it to be, you just likely didn't realize how you modified the location.

The computer is correctly executing your code (it is doing what you asked), you likely didn't understand what the changes meant.

Very very likely. Infact, I don't doubt it one bit.

Thanks for the explanation though.

##### Share on other sites

Thanks for that. I'll be reading up on this for sure.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 9
• 15
• 9
• 11