Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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.

  • You cannot reply to this topic
12 replies to this topic

#1 DarkRonin   Members   -  Reputation: 616

Like
0Likes
Like

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?

vertex_shader_01.png

How would you go about doing something like this?



Sponsor:

#2 Nik02   Crossbones+   -  Reputation: 2915

Like
3Likes
Like

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


#3 DarkRonin   Members   -  Reputation: 616

Like
0Likes
Like

Posted 08 April 2014 - 10:34 PM

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


#4 ProtectedMode   Members   -  Reputation: 1278

Like
2Likes
Like

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.



#5 DarkRonin   Members   -  Reputation: 616

Like
0Likes
Like

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 :)



#6 DarkRonin   Members   -  Reputation: 616

Like
0Likes
Like

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.

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


#7 frob   Moderators   -  Reputation: 22693

Like
1Likes
Like

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 write about assorted stuff.


#8 DarkRonin   Members   -  Reputation: 616

Like
1Likes
Like

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. smile.png

 

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

 

Thanks for the explanation though. smile.png



#9 NightCreature83   Crossbones+   -  Reputation: 3030

Like
1Likes
Like

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, Mad Max

#10 DarkRonin   Members   -  Reputation: 616

Like
0Likes
Like

Posted 09 April 2014 - 01:40 AM

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



#11 DarkRonin   Members   -  Reputation: 616

Like
0Likes
Like

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. :)



#12 NightCreature83   Crossbones+   -  Reputation: 3030

Like
0Likes
Like

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, Mad Max

#13 DarkRonin   Members   -  Reputation: 616

Like
0Likes
Like

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 cool.png

 

[edit]

If you want to see what I am trying to achieve I have uploaded my exe here http://www.win32developer.com/downloads/shadow3.zip

 

Arrow keys to control movement and escape to quit.

 

Thanks guys :)


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





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.



PARTNERS