Hello Guest

Multiple shaders in 3D space

  • 10 Replies
  • 8908 Views
*

Offline matanui159

  • *
  • 30
  • KABOOM
Multiple shaders in 3D space
« on: April 15, 2014, 00:17:14 »
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 :)

Re: Multiple shaders in 3D space
« Reply #1 on: April 15, 2014, 01:42:13 »
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.
« Last Edit: April 15, 2014, 02:36:48 by DapperPixpy »
Currently working on a 2D game. I plan to have it out in (//TODO: Make time estimation.)

*

Offline matanui159

  • *
  • 30
  • KABOOM
Re: Multiple shaders in 3D space
« Reply #2 on: April 15, 2014, 23:13:49 »
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...

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

*

Offline Cornix

  • *****
  • 488
Re: Multiple shaders in 3D space
« Reply #3 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.

*

Offline quew8

  • *****
  • 569
  • Because Square Eyes Look More Real
Re: Multiple shaders in 3D space
« Reply #4 on: April 16, 2014, 16:33:05 »
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.

*

Offline matanui159

  • *
  • 30
  • KABOOM
Re: Multiple shaders in 3D space
« Reply #5 on: April 17, 2014, 23:27:49 »
...
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



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 :)

*

Offline matanui159

  • *
  • 30
  • KABOOM
Re: Multiple shaders in 3D space
« Reply #6 on: April 26, 2014, 00:50:10 »
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 :)

Re: Multiple shaders in 3D space
« Reply #7 on: April 26, 2014, 20:32:43 »
Some GLSL Pseudocode (Fragment shader)-
Code: [Select]
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);
}
« Last Edit: April 26, 2014, 21:16:53 by DapperPixpy »
Currently working on a 2D game. I plan to have it out in (//TODO: Make time estimation.)

*

Offline matanui159

  • *
  • 30
  • KABOOM
Re: Multiple shaders in 3D space
« Reply #8 on: April 28, 2014, 04:18:30 »
DapperPixpy, I had a look at your code but theres just one problem...

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 :)

*

Offline matanui159

  • *
  • 30
  • KABOOM
Re: Multiple shaders in 3D space
« Reply #9 on: May 02, 2014, 12:04:56 »
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 :)

*

Offline spasi

  • *****
  • 2258
    • WebHotelier
Re: Multiple shaders in 3D space
« Reply #10 on: May 02, 2014, 13:33:09 »
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.