• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
lonewolff

Trying to understand vertex shaders better

12 posts in this topic

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?

0

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
0

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.

2

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

0

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
0

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
1

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

1

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0