Jump to content
  • Advertisement
Sign in to follow this  
DividedByZero

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.

If you intended to correct an error in the post then please contact us.

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?

vertex_shader_01.png

How would you go about doing something like this?

Share this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
Share on other sites

Thanks for your answer.

 

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

 

[edit]

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

 

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 this post


Link to post
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 this post


Link to post
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.

//
// Simple passthrough vertex shader
//
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 smile.png

 

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?

 

[edit]

Screenshot of what is happening

 

shader_prob.png

Edited by lonewolff

Share this post


Link to post
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 this post


Link to post
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. smile.png

 

Reading and re-reading your post to try and absorb what is happening here.

 

Thanks for the explanation though. smile.png

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!