0

I'm working with OpenGL >3.3 and I was wondering if there is a way to find out if a fragment belongs to the background easily in the fragment Shader. (I'm working with #Version 330).
I'm using a multipass rendering, and I have to work with neighbors of a fragment that are actually not the background... (I'm working with pointcloud). So I tried to do :

uniform vec3 backGroundColor;
float dx = 1/screenwidth;  
float dy = 1/screenHeight;
for (int i=-3; i<3; ++i){
  for (int j=-3; j<3; ++j){
    vec3 frag = texture(screenTexture, TexCoords + vec2(i*dx, j*dy)).xyz;
    if (frag.x != backGroundColor.x && frag.y != backGroundColor.y && frag.z != backGroundColor.z)
        do something...
    else
        do other Things...
  }
} 

But I believe this is underproductive... (And not that right because of float precisions ...) Is there any better way to achieve what I'm trying to?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Raph Schim
  • 528
  • 7
  • 30
  • You could always check the z-buffer, but it would double the number of texture read. – Guillaume Racicot Mar 09 '17 at 14:46
  • Since i'm not native english, I didn't understand if it's a typo or not "but I would double the number of texture read". Did you mean "but **It** would double the number of texture read"? Maybe it's the best way... – Raph Schim Mar 09 '17 at 14:49
  • Corrected. Sorry, I'm not a native English speaker too. – Guillaume Racicot Mar 09 '17 at 14:50
  • Well, I will maybe do so. But I first have to learn how to use 2 "textures" in the same fragment shader ... Thanks – Raph Schim Mar 09 '17 at 14:54
  • Define "belongs to the background." Do you mean a pixel which hasn't been written yet? And how are you reading from neighboring "fragments" at all? That sounds like reading from the same image you're writing to, [which is *explicitly* UB](https://www.khronos.org/opengl/wiki/Memory_Model#Framebuffer_objects) unless you're extremely careful about it. – Nicol Bolas Mar 09 '17 at 16:07
  • Since I'm working on pointCloud, a point belongs to the background if it is not a point of my pointCloud, so if my background has color (0.5,0.5,0.5), a pixel belongs to the background if its color is (0.5,0.5,0.5). I'm reading from neighbors fragments with `texture(screenTexture, TexCoords + vec2(i*dx, j*dy).xyz`, is it bad? – Raph Schim Mar 10 '17 at 07:29
  • With dx and dy respectively 1/screenWidth and 1/screenheight – Raph Schim Mar 10 '17 at 07:42

0 Answers0