Cube Maps

4c998261e88d6d5b58c70305749b1ee0
0
xTiming 101 Mar 26, 2012 at 17:59 shadows

Hi guys,

I have a question regarding nVIDIA Cg shader language (or I guess any shader language for that matter, but I am using Cg.) Is it possible to create a cubemap for point light shadows in the vertex/pixel shader itself assuming you have the position of a given light, or must the cube map be generated outside your shader and passed in? I ask because I have all the necessary data available in my shaders however to get the data in my C++ code would result in me going through with some potentially pointless work.

I want to see if there is a way to construct the cube map per light in the shader and just do my calculations from there, essentially putting all my cube map code into the shader.

Thanks!

6 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Mar 26, 2012 at 20:13

To generate a cube map on the GPU, you would need to do separate draw calls to first generate the cubemap and then use it. That is, you’d bind each cube map render target and draw your scene into it, using a standard vertex shader and a pixel shader that just returns zero (since only depth is important for shadow mapping, not color). You have to do this 6 times, once for each cube face, unless you use a geometry shader to replicate the triangles into the 6 render targets on the GPU.

Then, you’d bind your main render target and draw your scene again using a pixel shader that samples the cubemap to determined how much the pixel is shadowed and incorporate that into the usual lighting/shading calculations.

So you can render the cube map on the GPU, but you do have to do some extra work on the CPU to set everything up. The cubemap cannot be generated and used entirely by shaders without any CPU involvement at all.

88dc730f0f71e55be39de0ad103bd9ff
0
Alienizer 109 Mar 26, 2012 at 22:04

I thought it was as simpe as binding each 6 side textures using GL_TEXTURE_CUBE_MAP_(POSITIVE/NEGATIVE)_(XYZ) then enable GL_TEXTURE_CUBE_MAP?

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Mar 26, 2012 at 22:19

If you already have a cube map image created that you’re loading from a file or something, then it’s just like binding any other texture. The OP was asking about dynamically rendering your own cubemap, not loading a pre-existing one.

4c998261e88d6d5b58c70305749b1ee0
0
xTiming 101 Mar 27, 2012 at 12:49

Thanks for the reply Reed..

That answers my question! :)

Do the cube maps have to be generated from the point of view of a given light, whether done on CPU or GPU, or can a generic cube map be made and then applied and transformed to work for a light at any position?

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Mar 27, 2012 at 21:01

Could you render a “generic” image of your game world and then transform it to work for any camera position? No, because the world looks different from different places. :) Shadow maps are just views of the game world from a camera placed at the same position as the light source. If you want your shadows to look correct at all you must render them from the correct position.

4c998261e88d6d5b58c70305749b1ee0
0
xTiming 101 Mar 28, 2012 at 11:46

Right, got it.

Thanks again Reed!