• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
amtri

z-buffer discontinuity in shaders

16 posts in this topic

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.

0

Share this post


Link to post
Share on other sites

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. 

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites

[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,

0

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

 

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.

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

 

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.

0

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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...

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0