Exemplo n.º 1
0
        /// 正規化キューブマップの生成
        public static TextureCube CreateNormalizeCubeTexture(int width)
        {
            int         height = width;
            TextureCube nc     = new TextureCube(width, false, PixelFormat.Rgba);

            nc.SetFilter(TextureFilterMode.Linear,
                         TextureFilterMode.Linear,
                         TextureFilterMode.Linear);
            nc.SetWrap(TextureWrapMode.ClampToEdge, TextureWrapMode.ClampToEdge);

            // +X
            {
                byte[] pixsPX = new byte[width * height * 4];
                byte[] pixsNX = new byte[width * height * 4];

                byte[] pixsPY = new byte[width * height * 4];
                byte[] pixsNY = new byte[width * height * 4];

                byte[] pixsPZ = new byte[width * height * 4];
                byte[] pixsNZ = new byte[width * height * 4];

                for (int v = 0; v < width; v++)
                {
                    float y = (float)(v + v - height) / (float)height;
                    for (int u = 0; u < width; u++)
                    {
                        float x = (float)(u + u - width) / (float)width;

                        float r = 1.0f / (float)Math.Sqrt((x * x) + (y * y) + 1.0f);
                        float s = x * r;
                        float t = y * r;

                        Vector3 vn = new Vector3(r, s, t);
                        vn = vn.Normalize();

                        r = (r / 2.0f + 0.5f);
                        s = (s / 2.0f + 0.5f);
                        t = (t / 2.0f + 0.5f);

                        int ofst = (4 * u) + ((v * 4) * width);

                        pixsPX[ofst + 0] = (byte)(255 * r);
                        pixsPX[ofst + 1] = (byte)(255 * -t);
                        pixsPX[ofst + 2] = (byte)(255 * -s);
                        pixsPX[ofst + 3] = 0xFF;

                        pixsNX[ofst + 0] = (byte)(255 * -r);
                        pixsNX[ofst + 1] = (byte)(255 * -t);
                        pixsNX[ofst + 2] = (byte)(255 * s);
                        pixsNX[ofst + 3] = 0xFF;

                        pixsPY[ofst + 0] = (byte)(255 * s);
                        pixsPY[ofst + 1] = (byte)(255 * r);
                        pixsPY[ofst + 2] = (byte)(255 * t);
                        pixsPY[ofst + 3] = 0xFF;

                        pixsNY[ofst + 0] = (byte)(255 * s);
                        pixsNY[ofst + 1] = (byte)(255 * -r);
                        pixsNY[ofst + 2] = (byte)(255 * -t);
                        pixsNY[ofst + 3] = 0xFF;

                        pixsPZ[ofst + 0] = (byte)(255 * s);
                        pixsPZ[ofst + 1] = (byte)(255 * -t);
                        pixsPZ[ofst + 2] = (byte)(255 * r);
                        pixsPZ[ofst + 3] = 0xFF;

                        pixsNZ[ofst + 0] = (byte)(255 * -s);
                        pixsNZ[ofst + 1] = (byte)(255 * -t);
                        pixsNZ[ofst + 2] = (byte)(255 * -r);
                        pixsNZ[ofst + 3] = 0xFF;
                    }
                }
                nc.SetPixels(0, TextureCubeFace.PositiveX, pixsPX);
                nc.SetPixels(0, TextureCubeFace.NegativeX, pixsNX);

                nc.SetPixels(0, TextureCubeFace.PositiveY, pixsPY);
                nc.SetPixels(0, TextureCubeFace.NegativeY, pixsNY);

                nc.SetPixels(0, TextureCubeFace.PositiveZ, pixsPZ);
                nc.SetPixels(0, TextureCubeFace.NegativeZ, pixsNZ);
            }


            return(nc);
        }
Exemplo n.º 2
0
        /// 正規化キューブマップの生成
        public static TextureCube CreateNormalizeCubeTexture( int width )
        {
            int height = width;
            TextureCube nc = new TextureCube( width, false, PixelFormat.Rgba );
            nc.SetFilter( TextureFilterMode.Linear,
                      TextureFilterMode.Linear,
                      TextureFilterMode.Linear );
            nc.SetWrap( TextureWrapMode.ClampToEdge, TextureWrapMode.ClampToEdge );

            // +X
            {
            byte[] pixsPX = new byte[ width * height * 4 ];
            byte[] pixsNX = new byte[ width * height * 4 ];

            byte[] pixsPY = new byte[ width * height * 4 ];
            byte[] pixsNY = new byte[ width * height * 4 ];

            byte[] pixsPZ = new byte[ width * height * 4 ];
            byte[] pixsNZ = new byte[ width * height * 4 ];

            for( int v = 0; v < width; v++ ){
                float y = (float)(v + v - height) / (float)height;
                for( int u = 0; u < width; u++ ){
                    float x = (float)(u + u - width) / (float)width;

                    float r = 1.0f / (float)Math.Sqrt( (x * x) + (y * y) + 1.0f);
                    float s = x * r;
                    float t = y * r;

                    Vector3 vn = new Vector3( r, s, t );
                    vn = vn.Normalize();

                    r = (r / 2.0f + 0.5f);
                    s = (s / 2.0f + 0.5f);
                    t = (t / 2.0f + 0.5f);

                    int ofst = (4 * u) + ((v * 4) * width);

                    pixsPX[ ofst + 0 ] = (byte)(255 * r);
                    pixsPX[ ofst + 1 ] = (byte)(255 * -t);
                    pixsPX[ ofst + 2 ] = (byte)(255 * -s);
                    pixsPX[ ofst + 3 ] = 0xFF;

                    pixsNX[ ofst + 0 ] = (byte)(255 * -r);
                    pixsNX[ ofst + 1 ] = (byte)(255 * -t);
                    pixsNX[ ofst + 2 ] = (byte)(255 * s);
                    pixsNX[ ofst + 3 ] = 0xFF;

                    pixsPY[ ofst + 0 ] = (byte)(255 * s);
                    pixsPY[ ofst + 1 ] = (byte)(255 * r);
                    pixsPY[ ofst + 2 ] = (byte)(255 * t);
                    pixsPY[ ofst + 3 ] = 0xFF;

                    pixsNY[ ofst + 0 ] = (byte)(255 * s);
                    pixsNY[ ofst + 1 ] = (byte)(255 * -r);
                    pixsNY[ ofst + 2 ] = (byte)(255 * -t);
                    pixsNY[ ofst + 3 ] = 0xFF;

                    pixsPZ[ ofst + 0 ] = (byte)(255 * s);
                    pixsPZ[ ofst + 1 ] = (byte)(255 * -t);
                    pixsPZ[ ofst + 2 ] = (byte)(255 * r);
                    pixsPZ[ ofst + 3 ] = 0xFF;

                    pixsNZ[ ofst + 0 ] = (byte)(255 * -s);
                    pixsNZ[ ofst + 1 ] = (byte)(255 * -t);
                    pixsNZ[ ofst + 2 ] = (byte)(255 * -r);
                    pixsNZ[ ofst + 3 ] = 0xFF;

                }
            }
            nc.SetPixels( 0, TextureCubeFace.PositiveX, pixsPX );
            nc.SetPixels( 0, TextureCubeFace.NegativeX, pixsNX );

            nc.SetPixels( 0, TextureCubeFace.PositiveY, pixsPY );
            nc.SetPixels( 0, TextureCubeFace.NegativeY, pixsNY );

            nc.SetPixels( 0, TextureCubeFace.PositiveZ, pixsPZ );
            nc.SetPixels( 0, TextureCubeFace.NegativeZ, pixsNZ );
            }

            return nc;
        }