Setting View Matrix For CubeMap Rendering


Recently, some guys asked me how to set the view matrix to create dynamic CubeMaps of a scene (and eventually use it for things like dynamic Reflection or PointLight Shadow Generation).
We did not have this on PloobsEngine, so i decided to implement and then tell how do i achieved that.
The idea is actually very very simple, you just need to point your camera to the 6 “cube” orientations and render the scene then store the result in the cubemap +P. The only trick is to associate each cube face with the right View Matrix.

The following pseudo-Code uses the classic CreateLookAt to generate the View Matrices (i XNA conventions for Matrices, if you are using other plataforms like Opengl, you can go to try and error or something more clever like creating a custom scene with textures saying “this is Negative Y”, “this is Positive Z” … on each corner or finally you can discover exactely how the coordinates system of your plataform really works =P).

public void RenderSceneToTextureCube(RenderTargetCube renderTargetCube, Color backGroundColor, IWorld world,
            ref Vector3 camPos, ref Matrix projection, GameTime gt)
        {
             viewMatrix = new Matrix();
            // Render our cube map, once for each cube face( 6 times ).
            for (int i = 0; i < 6; i++)
            {
                // render the scene to all cubemap faces
                CubeMapFace cubeMapFace = (CubeMapFace)i;

                switch (cubeMapFace)
                {
                    case CubeMapFace.NegativeX:
                        {
                            viewMatrix = Matrix.CreateLookAt(camPos, camPos + Vector3.Left, Vector3.Up);
                            break;
                        }
                    case CubeMapFace.NegativeY:
                        {
                            viewMatrix = Matrix.CreateLookAt(camPos, camPos + Vector3.Down, Vector3.Forward);
                            break;
                        }
                    case CubeMapFace.NegativeZ:
                        {
                            viewMatrix = Matrix.CreateLookAt(camPos, camPos + Vector3.Backward, Vector3.Up);
                            break;
                        }
                    case CubeMapFace.PositiveX:
                        {
                            viewMatrix = Matrix.CreateLookAt(camPos, camPos + Vector3.Right, Vector3.Up);
                            break;
                        }
                    case CubeMapFace.PositiveY:
                        {
                            viewMatrix = Matrix.CreateLookAt(camPos, camPos + Vector3.Up, Vector3.Backward);
                            break;
                        }
                    case CubeMapFace.PositiveZ:
                        {
                            viewMatrix = Matrix.CreateLookAt(camPos, camPos + Vector3.Forward, Vector3.Up);
                            break;
                        }
                }                
                // Set the cubemap render target, using the selected face
                SetCubeRenderTarget(renderTargetCube, cubeMapFace);
                Clear(backGroundColor);
                RenderScene(world, gt, ref viewMatrix, ref projection);
                RestorePreviousRenderTarget();
            }
                        
        }

  1. #1 by data cabling denver on 21 de julho de 2017 - 12:46 pm

    You ave got a really great layout for the blog i want it to utilize on my web page as well

  2. #2 by Auto Care and Service on 21 de julho de 2017 - 12:53 pm

    I have been absent for some time, but now I remember why I used to love this blog. Thanks , I will try and check back more often. How frequently you update your web site?

  3. #3 by hermes bracelet replica on 21 de julho de 2017 - 1:41 pm

    Your product ended up being at such a great rates I never ever considered on top quality is hence excellent. It is gorgeous. Your mama might adore they concerning Xmas early morning where she starts present therefore appearance as though I devoted much more, then again cost was just ideal!!

  4. #4 by http://www.cheapmoncler.xyz/ on 21 de julho de 2017 - 1:42 pm

    I have purchased this one brand name of bracelet a few occasions. Every single one is very sweet, created very well, cannot tarnish then significant depending on what a person you buy to whom people render they on.

  5. #5 by Cartier Love Necklace Replica on 21 de julho de 2017 - 1:44 pm

    I’ve bought this one brand name concerning bracelet some period. Every single a person is ultra adorable, has made well, cannot tarnish and important depending on which one particular you purchase additionally that your provide they and.

  6. #6 by Cartier Rings on 21 de julho de 2017 - 1:45 pm

    I have purchased our brand to bracelet various instances. Every one is ultra adorable, established very well, cannot tarnish and/or significant based on that one you purchase to just who people present information technology and.

  7. #7 by 360 frontal on 21 de julho de 2017 - 2:26 pm

    Adore my new 360 frontal https://www.youtube.com/watch?v=ny8rUpI_98I! They may be sooo wonderful!

  8. #8 by Anti Wrinkles on 21 de julho de 2017 - 2:54 pm

    Muchos Gracias for your article. Fantastic.

1 429 430 431
(não será publicado)