Multiple shaders in 3D space

Started by matanui159, April 15, 2014, 00:17:14

Previous topic - Next topic

matanui159

I saw this topic on java-gaming.org http://www.java-gaming.org/index.php?topic=29590.0

But it seems like this is just rendering to a 2D image then using shaders on that

But how do I use multiple shaders in a 3D space? Is it possible?

Also is it a good idea for a lighting engine to use a shader per light so I can have infinite amounts of lights?
ALGORITHM
A word used by programmers when they do not want to explain what they did.

WEE :)

DapperPixpy

It sounds like what you're looking for is Deferred Shading. (http://en.wikipedia.org/wiki/Deferred_shading). Also, I recommend you brush up on your knowledge of Frame Buffer Objects. You may render to a texture with them, but this does not mean that their textures cannot be viewed as though it was a three dimensional object.

For example, in a tutorial for a glow effect using GLSL and OpenGLES(http://devmaster.net/posts/3100/shader-effects-glow-and-bloom), what they refer to as a "glow map" is three dimensional data rendered to a 2D texture (Using a FBO, With GLSL shader effects for blur) and is then rendered onto the 3D scene as a fullscreen quad (Or, just as well, a pair of triangles) for a glow effect.
Currently working on a 2D game. I plan to have it out in (//TODO: Make time estimation.)

matanui159

I read it up and supposedly all I do is render all the info (like depth, color, etc.) to FBOs, then use a per pixel shader to render that to the next set of shaders, and so on...

Quote from: DapperPixpy on April 15, 2014, 01:42:13
... You may render to a texture with them, but this does not mean that their textures cannot be viewed as though it was a three dimensional object...

To me, it is a 3D world, but you can't really do much with that 3D world other than look at it as a texture
ALGORITHM
A word used by programmers when they do not want to explain what they did.

WEE :)

Cornix

Your screen is also showing you 2D images and yet these appear as if they were 3D worlds.

quew8

Having read your post several times through I think I know what you want. You want to apply multiple shaders to a 3D object ie apply the transform from one shader, then apply the transform from the second etc... Am I right?

To my knowledge there is no way to do that - you have to create a new shader combining all of them. But it can be very useful. So useful that I created a library to combine "effects" together to make shaders and shoved it all into an XML file format for good measure. If you think it would help and don't mind trawling through my code, I'm happy to share.

matanui159

Quote from: quew8 on April 16, 2014, 16:33:05
...
To my knowledge there is no way to do that - you have to create a new shader combining all of them. But it can be very useful. So useful that I created a library to combine "effects" together to make shaders and shoved it all into an XML file format for good measure. If you think it would help and don't mind trawling through my code, I'm happy to share.

But there's just one problem with that...
If I want an infinite amount of lights, I can't do that because GLSL much have a size for arrays (I'm pretty sure).
So my idea was one shader pass for each light
I like DapperPixpy's idea of deffered shading



Quote from: Cornix on April 16, 2014, 06:49:06
Your screen is also showing you 2D images and yet these appear as if they were 3D worlds.
I know, I'm just noting out that you can't put a shader in a 3D space over a 2D image

Thx for all your help ;D
ALGORITHM
A word used by programmers when they do not want to explain what they did.

WEE :)

matanui159

I just had a thought today (hopefully it's not to late)...

How do I get the position with deferred shading?
Becuase I can get the Z using the z-buffer but how do I get the X and Y?
I can't trust the position on the screen because the game may be (and most likely will be) using a perspective or fustrum view...

So how do I get the position?
ALGORITHM
A word used by programmers when they do not want to explain what they did.

WEE :)

DapperPixpy

Some GLSL Pseudocode (Fragment shader)-
uniform vec4 viewport;//x, y, width, height (x,y,z,w)

void main(){
	vec2 pos_xy = gl_FragCoord.xy * viewport.zw + viewport.xy;
	float z = <get Z position>;
	vec3 position_world = vec3(pos_xy, z);
}
Currently working on a 2D game. I plan to have it out in (//TODO: Make time estimation.)

matanui159

DapperPixpy, I had a look at your code but theres just one problem...

Quote from: matanui159 on April 26, 2014, 00:50:10
I can't trust the position on the screen because the game may be (and most likely will be) using a perspective or fustrum view...

Because on a perspective view the x and y on the screen is different from the x and y in real space...
ALGORITHM
A word used by programmers when they do not want to explain what they did.

WEE :)

matanui159

I'm currently guessing that no one knows how to get the position in deferred shading...
ALGORITHM
A word used by programmers when they do not want to explain what they did.

WEE :)

spasi

Search for screen-space to world-space transformations (e.g. this).

Basically you need to get a screen-space point (x/y from gl_FragCoord, z from depth texture), transform it to normalized device coordinates (brings x/y to -1..1 range), then multiply by the inverse of the MVP matrix.