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

Author Topic: Achiving some sort of projection texture mapping  (Read 4713 times)

0 Members and 1 Guest are viewing this topic.

Bimblorio

  • Newbie
  • *
  • Posts: 3
    • View Profile
Achiving some sort of projection texture mapping
« on: April 12, 2021, 06:57:39 pm »
I'm trying to achieve an effect, where repeated texture stays in place even if object (shape or sprite) is movig.
Is there any easy way to do it?
Is there any way to do it without using shaders?
Thanks in advance.
« Last Edit: April 12, 2021, 07:04:00 pm by Bimblorio »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: Achiving some sort of projection texture mapping
« Reply #1 on: April 13, 2021, 01:12:48 pm »
By "in place" do you mean that it's the same relative to the window or relative to the moving object?

Which object are you moving exactly?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Bimblorio

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Achiving some sort of projection texture mapping
« Reply #2 on: April 13, 2021, 06:45:16 pm »
By "in place" do you mean that it's the same relative to the window or relative to the moving object?
Relative to the window.

Which object are you moving exactly?
Shape

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Achiving some sort of projection texture mapping
« Reply #3 on: April 14, 2021, 03:39:35 am »
One way would be messing with UV coordinates each frame.

Or you can use a shader. Here's a quick test hack I just made:
Vertex shader:
void main()
{
    // transform the vertex position
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

    // transform the texture coordinates
    gl_TexCoord[0] = gl_Vertex/64;

    // forward the vertex color
    gl_FrontColor = gl_Color;
}
Fragment shader:
sampler2D texture;
void main()
{
    // lookup the pixel in the texture
    vec4 c = texture2D(texture, gl_TexCoord[0].xy);
    gl_FragColor = c;
}
 

Applied to a sprite, that made the sprite's texture (which was set to repeat) stay in place as the sprite moved.
The 64 in the vertex shader was the resolution of the sprite's texture.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Achiving some sort of projection texture mapping
« Reply #4 on: April 14, 2021, 09:19:00 am »
Or just set the sprite's textureRect to its global bounding rectangle whenever it moves ;)
Laurent Gomila - SFML developer

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Achiving some sort of projection texture mapping
« Reply #5 on: April 14, 2021, 11:05:17 am »
Well, sure, if you want to do it the easy and sensible way...
:)

Bimblorio

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Achiving some sort of projection texture mapping
« Reply #6 on: April 14, 2021, 05:23:32 pm »
Wow so many options, thank you everyone.
Ill choose something that sutes me.

 

anything