Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Vertex Shader transformation matrix and pixel coordinates  (Read 1022 times)

0 Members and 1 Guest are viewing this topic.

vdweller

  • Newbie
  • *
  • Posts: 2
    • View Profile
Vertex Shader transformation matrix and pixel coordinates
« on: May 07, 2020, 01:33:48 pm »
Hello,

Newcomer to sfml. I installed it this week and managed to port some code that creates various GUI widgets with container capabilities in order to test both my C++ and SFML understanding. I managed to get it work 100%. Now I am moving on to shaders. I have experience with shaders only from GameMaker so I'd like a few pointers for the following question:

I am writing a simple test shader to move a vertex X pixels in the x-axis and Y pixels in the y-axis. I found out that SFML vertices operate in some sort of normalized coordinates. A solution was to convert pixels to the coordinate space that SFML uses and transform them before passing them as a uniform. Example:

shader.setUniform("pos",sf::Glsl::Vec2(200.f*(2.f/1024.f),0.f));

Assuming a window size of 1024, this indeed moves the vertex 200 pixels to the right.

My question is: I'd like some help on how to set up a matrix or some other workaround to simply pass pixels to any shader without translating them to normalized coords in the uniform. As an example, I'd like to write:

shader.setUniform("pos",sf::Glsl::Vec2(200.f,0.f));

Of course, in the shader I could add:

gl_Position.x+=pos.x*(2./1024.);

Where 1024 the x-resolution which can also be a uniform.

However this requires also passing the window size/resolution to each shader. What should I do to set up a transformation matrix which substitutes gl_ModelViewProjectionMatrix that works with pixel coordinates directly?

Hapax

  • Hero Member
  • *****
  • Posts: 3349
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Vertex Shader transformation matrix and pixel coordinates
« Reply #1 on: May 08, 2020, 09:53:53 pm »
I would say that the simplest approach would be to move the vertices by a distance in the co-ordinate system used by the vertices themselves.
So, if you want to be positioning vertices using pixels, you could set the view to match the window. Then, 200 in co-ordinate space is also 200 in pixels.

That said, although I don't know what SFML's transformation matrix is exactly (I suppose I could go look ;D), I would guess that it converts co-ordinates from the view to OpenGL's standard (normalised) co-ordinates (-1, -1) - (1, 1). So, it'll use the view size for this. If you need pixels (window size) involved, you'd need to involve them. I would say that your conversion is a lot simpler than re-inventing the transformation matrix to include window size, especially since this would affect all vertices positions into that system so changing the view to match the window would be the simplest solution ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

vdweller

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Vertex Shader transformation matrix and pixel coordinates
« Reply #2 on: May 09, 2020, 02:05:35 pm »
Understood. Thanks!