Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


z-buffer discontinuity in shaders


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 amtri   Members   -  Reputation: 178

Like
0Likes
Like

Posted 23 December 2013 - 05:43 PM

Hello,

 

I would like to highlight edges based solely on z-buffer discontinuities. I can imagine this as an image posprocessing step after the frame and z-buffers are complete.

 

I have two questions:

 

1) What is a reasonable filter to use to detect edge pixels?

 

2) Can this be implemented in a shader? One of my questions here is whether I can access neighboring pixel information in the fragment shader.

 

Thanks.



Sponsor:

#2 richardurich   Members   -  Reputation: 1187

Like
4Likes
Like

Posted 23 December 2013 - 07:53 PM

You can access neighboring pixel information in a shader by using the texture function (texture2D is deprecated now) or even other functions like texelFetch. You can look at the documentation and use the "See Also" section from texture to get you started.

 

If you need examples of edge detection shaders, just google edge detection fragment shader. Taking a quick look at the Google results, it looks like you'll get enough results from that search.



#3 amtri   Members   -  Reputation: 178

Like
0Likes
Like

Posted 28 January 2014 - 06:02 PM

Richard,

 

Apologies for the delay, but I had to go do my homework first...

 

I've looked into edge detection fragment shader and many, many questions came up. I was hoping somebody would be able to help me make sense of all this.

 

1) To start with, I need to access the z-buffer values in the frament shader; not only for the current pixel, but for adjacent pixels also. From what I understand the only way to access z-buffer information in the fragment shader for adjacent pixels is to have the z-buffer written to a texture.

 

Could somebody confirm this? Sounds like a silly thing, given that the z-buffer is already in the graphics card memory, so I figured there should be a way to access the data - adjacent pixels included.

 

2) Assuming a texture is indeed needed, then there are 2 parts (or more) to this procedure: (a) creating the texture - i.e., the client code - and (b) using it in the shader. Can somebody show me the necessary client steps I will need to generate the texture? Also, as I rotate my model and redraw, I assume the texture I am creating gets automatically updated with whatever I do. Is this the case?

 

3) I think that once I get the client code done I can probably figure out what goes in the fragment shader, but if somebody has already done something similar and can give me a head start I would truly appreciate it.

 

Thanks. 



#4 BitMaster   Crossbones+   -  Reputation: 4248

Like
3Likes
Like

Posted 29 January 2014 - 10:12 AM

If you want to do interesting things like edge detection or other postprocessing effects then you must render your scene in a framebuffer object you created. The default framebuffer will not do. Also, you need to render your scene into textures, not renderbuffers. Have a loof at this page for an introduction. If you have any questions after reading that page, feel free to but I'd say most of your current questions are either answered or invalid.

#5 richardurich   Members   -  Reputation: 1187

Like
0Likes
Like

Posted 29 January 2014 - 04:33 PM

Any render to texture tutorial should work as a starting point I'd think. Or you can use the link BitMaster kindly provided.

 

I don't know any other way you'd want to do it, although I also don't know everything. And you definitely don't want code from a production code base even if anyone were allowed to provide that.



#6 amtri   Members   -  Reputation: 178

Like
0Likes
Like

Posted 29 January 2014 - 05:46 PM

Richard and BitMap,

 

Thanks for your responses. You have pointed me to enough material now that I need to go into a corner and do my homework. I'm sure there will be more questions later - hopefully, inteligent ones :-)!

 

Thanks again.



#7 L. Spiro   Crossbones+   -  Reputation: 14026

Like
0Likes
Like

Posted 29 January 2014 - 11:40 PM

1) What is a reasonable filter to use to detect edge pixels?

A sobel filter.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#8 amtri   Members   -  Reputation: 178

Like
0Likes
Like

Posted 30 January 2014 - 10:26 AM

Spiro,

 

Thanks for the suggestion. What I've settled for right now is a rather very simple algorithm: since I have the z-buffer information, then for every pixel I compute a "gradient" g by using the formula

 

g = z_(i,j) - 0.125*[ z_(i+i,j) + z_(i+1,j+1) + z_(i+1,j-1) + z_(i,j-1) + z_(i,j+1) + z_(i-1,j) + z_(i-1,j+1) + z_(i-1,j-1) ]

 

Then I subtract an equal amount from every color component based on the value of "g" above. I chose to only darken areas further away while leaving closer areas unaltered. It works very well for my needs.

 

Now what I'm grappling with is the fact that my full image contains not only true geometry, but also labels which I want to leave unaltered. My thought is to separate true geometry and everything else into two frame buffer objects (with z buffering on both), applying the image processing on the geometry buffer at swap time, then combining both buffers into the true frame buffer at the end.

 

I still don't know how to do this, but I'm pretty sure it's doable and not difficult; I just need to study FBOs which are still a novelty to me.

 

Thanks!



#9 LorenzoGatti   Crossbones+   -  Reputation: 2736

Like
0Likes
Like

Posted 31 January 2014 - 05:58 AM

[quote name='amtri' timestamp='1391099205' post='5127492']
Now what I'm grappling with is the fact that my full image contains not only true geometry, but also labels which I want to leave unaltered. My thought is to separate true geometry and everything else into two frame buffer objects (with z buffering on both), applying the image processing on the geometry buffer at swap time, then combining both buffers into the true frame buffer at the end.


You can simply draw the labels on top of the postprocessed true geometry, no need of another FBO,


Produci, consuma, crepa

#10 amtri   Members   -  Reputation: 178

Like
0Likes
Like

Posted 31 January 2014 - 10:13 AM

Lorenzo,

 

You are right: I could simply draw the labels on top of the postprocessed true geometry.

 

The problem is that I do not control the order in which graphics data comes in. It could be geometry/geometry/label/label, or it could be geometry/label/geometry/label, etc. So what I will do is to direct the geometry to one FBO, the labels to another, and in this way not worry about the order in which they come in.

 

When everything is done I post-process the true geometry, then add the FBO with the labels on top of the final post processed image.

 

I thought about enforcing some ordering for when geometry and label data is sent, but I simply have no control over it in the entire application; there's just too much going on in other parts of the code, so I need to accomodate it.

 

At swap time I'll combine them.

 

Thanks!



#11 amtri   Members   -  Reputation: 178

Like
0Likes
Like

Posted 31 January 2014 - 03:14 PM

OK. I got the color and the depth each written to a different texture in an FBO. For the color texture I use

glTexImage2D (GL_TEXTURE_2D,0,GL_RGBA,400,400,0,GL_RGBA,GL_UNSIGNED_BYTE,NULL);
glFramebufferTexture2D (GL_DRAW_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,geom_color,0);

and for the depth texture I use

glTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT24,400,400,0,GL_DEPTH_COMPONENT,GL_FLOAT,0);
glFramebufferTexture(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,geom_depth,0);

At the end I bind the 0 frame buffer and draw 2 triangles that cover the entire window, using the color texture to draw them. Everything works fine.

 

Now I would like to use a shader to post-process my geometry. I have 2 textures: one with the color and one with the depth. I want to use both in the shader: I will look at the value of the depth using a filter to detect an edge (something I've already done outside of a shader and I know it works well) and, depending on the value of the filter, I will use either the color from the color texture or just set the color to black to highlight the edge (I'm not worried about the edge algorithm now).

 

My question is: how do I setup the shader to use 2 textures at the same time. I'm truly confused by this: one texture contains a vec4; the other a single float; and both are used in the shader at the same time.

 

If somebody could give me some pointers on what needs to be done in the client and the shader to use these 2 textures at the same time I would appreciate it.

 

Thanks.



#12 richardurich   Members   -  Reputation: 1187

Like
0Likes
Like

Posted 31 January 2014 - 05:07 PM

 

If somebody could give me some pointers on what needs to be done in the client and the shader to use these 2 textures at the same time I would appreciate it.

 

Same thing you do for multitexturing, just don't blend in the shader. Here's a tutorial if you're unfamiliar with multitexturing.



#13 dpadam450   Members   -  Reputation: 933

Like
1Likes
Like

Posted 01 February 2014 - 12:25 PM

If you are using an edge detection algorithm, why not just use the dot product. If dot product of (normal,viewvec) < .01 or something, you can make the pixel black.



#14 amtri   Members   -  Reputation: 178

Like
0Likes
Like

Posted 03 February 2014 - 11:31 AM

 

If you are using an edge detection algorithm, why not just use the dot product. If dot product of (normal,viewvec) < .01 or something, you can make the pixel black.

 

It's not as simple as it sounds. My image consists of hundreds of thousands of triangles, many of them in the exact same orientation but just slightly parallel to each other. Sometimes I may have dozens of layers of closely spaced parallel surfaces and it's difficult to see them. They are all oriented the same way, with the same normal, so the best way to distinguish them is to highlight their edges. I could, in theory, just draw the line segment defining the feature edges, but that would take longer than just doing an image processing with a shader - which is why I'm going the shader route.



#15 amtri   Members   -  Reputation: 178

Like
0Likes
Like

Posted 03 February 2014 - 05:53 PM

I could really use some help with what I believe is very basic. I created a framebuffer object, attached a depth and a color texture to it, and drew 2 triangles along with the text "Hello World" on top. I then took the 2 textures, extracted the data from them just to check that they were correct. They were: in one I see the RGB values, and in the other I see a single floating point number with values as expected given my triangles.

 

Then I bind the "0" framebuffer and draw 2 triangles to the screen with the color texture; not a problem. I see the image just as expected, so I'm confident that my textures are ok (at least the color one since I'm drawing with it).

 

Now comes the part I don't understand: all I'm trying to do is to use a shader to display the same image: no manipulation at this point as I'm getting my feet wet with textures.

 

This is what my vertex shader looks like:

void main()
{
   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
   gl_TexCoord[0] = gl_MultiTexCoord0;
}

And this is my fragment shader:

uniform sampler2D colortex;
void main()
{
   gl_FragColor = texture2D(colortex,gl_TexCoord[0].st);
}

The shader program compiles and links with no errors.

 

In the client code I have

GLuint colortexloc = glGetUniformLocation (prog,"colortex");
glUniform1i (colortexloc,0);

Everything else is the same. I expected the image to be displayed just as before. Can somebody point towards something that's obviously wrong or missing? If I comment out the "glUseProgram" call then the image is properly displayed again. So it does look like a problem with the trivial shader code above.

 

Any help is appreciated. Thanks!



#16 amtri   Members   -  Reputation: 178

Like
0Likes
Like

Posted 05 February 2014 - 01:50 PM

I've made some more progress on my own, and ran into a more concrete issue: I have both a 1D and a 2D texture on unit 0 (GL_TEXTURE0). I use one or the other by using glEnable/glDisable.

 

In my shader I have a sampler2D and a sampler1D. Both are assigned to unit 0.

 

Whenever I switch from the 1D/2D textures I send a uniform flag indicating which texture is being used. If it's the 1D texture I use texture1D using as first argument the sampler1D variable, and as second argument gl_TexCoord[0].s; if it's the 2D texture I use texture2D using as first argument the sampler2D variable, and as second argument gl_TexCoord[0].st.

 

My problem is that the 2D texture does not show.

 

By setting the output color in the fragment shader to vec4(gl_TexCoord[0].s,gl_TexCoord[0].t,0.,1.) I can tell that the 2D texture coordinates are correct. So my problem is that somehow switching to texture2D(sampler2D,...) after displaying the using texture1D(sampler1D,...) is not working.

 

If I turn off the shader program then the images are displayed as expected, so the client program is correct.

 

Any thoughts? Have I misunderstood how a single unit is to be used with multiple textures?

 

Thanks.



#17 amtri   Members   -  Reputation: 178

Like
0Likes
Like

Posted 05 February 2014 - 01:56 PM

One more piece of information: I am drawing the 1D texture first, then the 2D texture.

 

If I reverse the order, then my 2D texture works fine, but my 1D texture does not.

 

And I know for a fact that my uniform flag indicating which texture I'm using is being passed down correctly, and it is using sampler1D for the 1D texture and sampler2D for the 2D texture. But, for some reason, after the first texture is drawn - either 1D or 2D - the second one in the same unit is not drawn.

 

Not clear why this is the case...






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS