View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Trying to understand vertex shaders better

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

### #1DarkRonin  Members

Posted 08 April 2014 - 08:50 PM

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?

Win32 Developer
One Of Them - Martial arts game that is mid development.

### #2Nik02  Members

Posted 08 April 2014 - 09:12 PM

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.

Niko Suni

### #3DarkRonin  Members

Posted 08 April 2014 - 10:34 PM

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, 08 April 2014 - 10:47 PM.

Win32 Developer
One Of Them - Martial arts game that is mid development.

### #4ProtectedMode  Members

Posted 08 April 2014 - 10:40 PM

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

### #5DarkRonin  Members

Posted 08 April 2014 - 11:13 PM

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

Win32 Developer
One Of Them - Martial arts game that is mid development.

### #6DarkRonin  Members

Posted 08 April 2014 - 11:32 PM

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, 09 April 2014 - 12:12 AM.

Win32 Developer
One Of Them - Martial arts game that is mid development.

### #7frob  Moderators

Posted 09 April 2014 - 12:56 AM

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, 09 April 2014 - 02:03 AM.
Slight clarification

Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.

### #8DarkRonin  Members

Posted 09 April 2014 - 01:02 AM

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.

Win32 Developer
One Of Them - Martial arts game that is mid development.

### #9NightCreature83  Members

Posted 09 April 2014 - 01:28 AM

If you need an online math resource this one is decent: http://www.euclideanspace.com/maths/algebra/index.htm

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max

### #10DarkRonin  Members

Posted 09 April 2014 - 01:40 AM

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

Win32 Developer
One Of Them - Martial arts game that is mid development.

### #11DarkRonin  Members

Posted 09 April 2014 - 06:06 PM

@frob (or anyone) - Still reading up of Matrices. And I can sort of envisage what is going on here.

But, any clues on how I can correct the positioning would be gratly appreciated.

Win32 Developer
One Of Them - Martial arts game that is mid development.

### #12NightCreature83  Members

Posted 10 April 2014 - 02:20 AM

dont offset the Y, or atleast give an equal opposit signed translation on the y in the matrix. And by that I mean a single offset not an increasing one.

Edited by NightCreature83, 10 April 2014 - 02:20 AM.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max

### #13DarkRonin  Members

Posted 13 April 2014 - 05:27 PM

I have tried compensating with adding the following line (prior to calling the shader - angle being the xSkew amount).

x=obj_green.x+(tan(angle)*obj_green.y);

It improves the issue, but still not right.

It is a bummer as, this shader is the only part of my game that uses a shader. The whole game is 2D based. Thus, why I haven't ever done 3D mathematics before.

Any ideas on how to get the x co-ordatines to line up would be beyond awesome

Arrow keys to control movement and escape to quit.

Thanks guys

Edited by lonewolff, 13 April 2014 - 06:24 PM.

Win32 Developer
One Of Them - Martial arts game that is mid development.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.