0

I've been trying to do some masking in OpenGL and GLSL, but the resources I've found online usually assume that textures and their masks have the same dimensions and no transformations (Images and mask in OpenGL ES 2.0)

I have a mask (pink circle) that has been translated and I have a texture (red square) that can be further translated and rotated behind the mask. I would like to display the portion of the red square under the pink circle.

enter image description here

The other questions I've seen either reuse the existing texture coordinate because the texture and mask are the same dimensions, or they have an additional texture coordinate for the mask (https://gamedev.stackexchange.com/questions/93893/opengl-positional-2d-texture-masking) that I'm not sure how they got.

How can I apply distinct transformations to a mask and texture of different dimensions and only display the overlap? I've considered doing something similar to a shadow map, rendering any mask textures first, getting the depth map, and then sampling that in my fragment shader whenever I want to mask something. I have a shadow map implemented so that would allow me to reuse some code. Is that a good solution?

Exudes
  • 103
  • 1
  • 9
  • Just sample mask texture in the fragment shader and discard pixels if the value is not opaque. Then sample color texture and output its value. – Michael Nastenko Apr 20 '20 at 10:55
  • The issue I have is calcuating the coordinates I need to sample in the mask texture. Unless I'm misunderstanding something, the texCoord I'm using to sample the square will not give the same position in screen space when I sample the circle (e.g. 0,0 returning the top left corner of each, which don't overlap in my example) – Exudes Apr 20 '20 at 20:17
  • `I have a mask (pink circle) that has been translated and I have a texture (red square) that can be further translated and rotated behind the mask. ` - means you must use different texture coordinates for sampling. Since it's just simple translation and translation+rotation you can have one input and transform for each texture in shader. – Michael Nastenko Apr 20 '20 at 22:22
  • `The issue I have is calcuating the coordinates I need to sample in the mask texture. Unless I'm misunderstanding something, the texCoord I'm using to sample the square will not give the same position in screen space when I sample the circle (e.g. 0,0 returning the top left corner of each, which don't overlap in my example)` I think you misunderstood the concept. Fragment shader calculates texture coordinates in one pixel. So you must calculate what tesCoord you want for each texture separately. It'll easier to help if you could share your fragment shader code. – Michael Nastenko Apr 25 '20 at 00:12

0 Answers0