• 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
Meitii Metsla

Shader blending ( Get previous pixel )

16 posts in this topic

Hello.

 

I want to control the way opengl blend transparent surfaces.

 

Vertex shader

#version 120
varying vec2 texcoord;
 
void main()
{
    gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
    texcoord = gl_MultiTexCoord0.xy;
}

Fragment shader

#version 120
uniform sampler2D img;
varying vec2 texcoord;

void main() 
{
	vec4 tex = texture2D ( img, texcoord );
	gl_FragColor = tex;
}

Those are my basic texture rendering codes.
What i need is to get somehow the pixel what is behind that thing what is correctly in shader.

It would be so good to have control over it. 
Also readed the basics of shaders, did not get any ideas how to do it.

Reading on internet, they said that opengl offers some blend functions. Already tried them.
Its nothing wrong with them but the way they act doesnt pleasure me. I really need to controll how pixels are blended.
Is there someone wise who have solution to my problem?
Thanks!

0

Share this post


Link to post
Share on other sites

That is a really good source yet not exactly what i wanted.
Il give you code example:

#version 120
uniform sampler2D img;
varying vec2 texcoord;

void main() 
{
	vec4 tex = texture2D ( img, texcoord );
        vec4 behindpixel = ?
	gl_FragColor = (behindpixel * 0.5 ) + (tex * 0.5 );
}

In theory i can blend pixels without using glBlendFunc.
Getting the that pixel what is behind will give me so much control. I can make so many new nice blending types.
 

If i can somehow know where the pixel will be in screen ( the pixel origin in screen ( x, y coords ) )
Then i can store them and use them. Also the order will not matter ( if the back pixel is rendered first or the front one )
I can figure it out somehow.

Edited by Gyiove
0

Share this post


Link to post
Share on other sites

AFAIK fragment shaders cannot read framebuffers directly. However, you can use an FBO with a texture attached as color target in a first pass, and bind those texture for sampling in a second pass. That should give you the access you need.

 

Just for curiosity, what are those kinds of blending you are not able to create with the standard blend-functions?

Edited by haegarr
0

Share this post


Link to post
Share on other sites

Nope, that can't be done. You can't read and write to the same texture due to how desktop GPUs work. It can however be done on tile based mobile GPUs.

0

Share this post


Link to post
Share on other sites

AFAIK fragment shaders cannot read framebuffers directly. However, you can use an FBO with a texture attached as color target in a first pass, and bind those texture for sampling in a second pass. That should give you the access you need.

I'm familiar with that way and im afraid that it will be ineffective.
Not sure how fast the function is but still believe that there are some other ways, there must be something.

 

 

Just for curiosity, what are those kinds of blending you are not able to create with the standard blend-functions?

 

For example one is that if you are looking out of the window, pixels will be different color but the window texture will be white for example.

0

Share this post


Link to post
Share on other sites

Untitled-1.png

That thing what make the wall yellow. 
The color depens on that texture what is right now in the fragment shader.
As far, you guys have been really helpful, im glad you want to help me. Thank you so much!
Anyway, i'm really beginner, yet dont know the ways and functions.
I've tried all options of glBlendfunc yet did not succeed in creating the effect you see up, right in the corner.

0

Share this post


Link to post
Share on other sites


I've tried all options of glBlendfunc yet did not succeed in creating the effect you see up, right in the corner.

 

Did you call glEnable(GL_BLEND) anywhere? This is definately doable with standard blending, people would have gotten insane if they required to pingpong rendertargets in order for simple glass effects.

0

Share this post


Link to post
Share on other sites

The one ohter problem is:

2014-03-09_193421.png

 

// vertex shader

#version 120
varying vec2 texcoord;
 
void main()
{
    gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
    texcoord = gl_MultiTexCoord0.xy;
}
#version 120
uniform sampler2D img;
varying vec2 texcoord;

void main() 
{
	vec4 tex = texture2D ( img, texcoord );
	if( tex.r < 0.3 && tex.g < 0.3 && tex.b > 0.6 ) tex *= 0.0;
	else tex.a = 1.0;
	gl_FragColor = tex;
}

		glUniform1i(glGetUniformLocation(program,"img"),0);
		glBindTexture(GL_TEXTURE_2D, triangles[a].texture);
		glActiveTexture(GL_TEXTURE0);
		if(triangles[a].texture == 1) // The first texture is not supposed to be transparent
		{
			glDisable(GL_BLEND);
		}
		else
		{
			glEnable (GL_BLEND);
			glBlendFunc(GL_ONE, GL_ONE);
		}

	
		glBegin(GL_TRIANGLES);
		for( c = 0; c < 3; c++ )
		{
			glTexCoord2d(triangles[a].uvmap[c][0], -triangles[a].uvmap[c][1]);
			glNormal3d(triangles[a].normals[c][0], triangles[a].normals[c][1], triangles[a].normals[c][2]);
			glVertex3d(-triangles[a].vertex[c][1], triangles[a].vertex[c][2],triangles[a].vertex[c][0]);
		}
		glEnd();

The one reason why i really wanted to get that pixel what is behind.
Tried all those blending flags what are in the page you gave.

0

Share this post


Link to post
Share on other sites


gl_FragColor = (behindpixel * 0.5 ) + (tex * 0.5 );

 

What you're trying to do is pretty much standard alpha blending. GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA, then output 0.5 for your alpha component.

 

I'm not sure exactly what you're trying to explain with your picture above, but it kind of seems like you want the areas around the tree leaves to be completely transparent? The same alpha blending flags will work for that too - except that you'll still be writing to the depth buffer for those completely transparent pixels, so they'll block stuff from being drawn behind them.

 

In that case, what you're looking for is alpha testing, not alpha blending.

2

Share this post


Link to post
Share on other sites

The one reason why i really wanted to get that pixel what is behind.
Tried all those blending flags what are in the page you gave.
 

 

You have to disable the depth buffer for this:


GL_CHECK(glDisable(GL_DEPTH_TEST));
GL_CHECK(glDepthMask(false));

and afterwards reenable


GL_CHECK(glEnable(GL_DEPTH_TEST));
GL_CHECK(glDepthMask(true));

Never use the zbuffer with alpha-blended stuff, it just doesn't work.

 

EDIT:

 

You can actually leave on GL_DEPTH_TEST and just set glDepthMask to false, otherwise your alphablended stuff will probably overlap your opaque geometry. Also, if you have a texture that is eigther fully opague and on some pixels fully transparent, you can just leave z-buffering like normal and just use alpha-testing by adding this line to your shader after the texture read:

if(tex.a <= 0.0001f)
discard;
Edited by Juliean
1

Share this post


Link to post
Share on other sites

Thank you all, it is working!
I still have one more question. Does the directx allow me to do that what i asked here?

( that getting pixel behind the thing ... )

Never worked with directx but after reading some source code and information from internet i did not found anything about saying yes or no.
More like no because the thing what im looking for doesnt exists maybe or i just dont know how do search because i dont really know how its called what im looking for.
 

Edited by Gyiove
0

Share this post


Link to post
Share on other sites

It's still unclear exactly what feature you're talking about, but all the things talked about on this thread (alpha blending, alpha test, render targets) are supported in DirectX, so I'm sure you'll be able to do pretty much the same thing.

0

Share this post


Link to post
Share on other sites

What if i render all the opaque triangles, create texture a from rendered screen,

 

then render transparent triangle, create texture b from screen where that one triangle were rendered and then

render quad with the texture a blended with b.

( so i have then control over the pixels what are behind the transparent object, i can control everything )
I will continue this thing with the ohter transparent triangles, one by one.
Need to find a way how do i know that the transparent triangle is or should be visible but thats really small problem right now and im having some ideas yet they are useless right now.
 

How bad is this idea?
Should i do it or find the other way?

 

 

 

0

Share this post


Link to post
Share on other sites

Thank you all, it is working!
I still have one more question. Does the directx allow me to do that what i asked here?

( that getting pixel behind the thing ... )

Never worked with directx but after reading some source code and information from internet i did not found anything about saying yes or no.
More like no because the thing what im looking for doesnt exists maybe or i just dont know how do search because i dont really know how its called what im looking for.
 

 

If I understand correctly, you're asking if there's any way to do a fully programmable blend. Sadly the answer is no. There's a very full explanation here: http://fgiesen.wordpress.com/2011/07/12/a-trip-through-the-graphics-pipeline-2011-part-9/ (halfway down, "Aside: Why no programmable blend"), but basically, it's a restriction of the GPU design rather than a restriction imposed by OpenGL/D3D. In fact, the entire http://fgiesen.wordpress.com/2011/07/09/a-trip-through-the-graphics-pipeline-2011-index/ series is rather excellent, and recommended reading to all.

 

Actually, I believe fully programmable blends are possible on certain PowerVR chips, for instance the one used by the Vita, but that uses a very different architecture to your desktop GPU.

 

Ultimately, the standard fixed function blend options, while limited, are really very powerful, and you should make every effort to fully understand what can be achieved by them before thinking about ping-ponging between buffers as suggested in your last post.

0

Share this post


Link to post
Share on other sites


AFAIK fragment shaders cannot read framebuffers directly

Oh well, today I stumbled over an extension for OpenGLES, originating from Apple but nowadays being an EXT extension that allows the fragment shader to read the active framebuffer at the current fragment location:

    EXT_shader_framebuffer_fetch

 

Perhaps not being an option for the OP (and obviously not needed anyhow), it shows that actually a programmed blending is possible on some platforms.

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