Exemplo n.º 1
0
        private SceneObject CreateSkyBoxObject()
        {
            if (!Gl.CurrentExtensions.TextureCubeMap_ARB)
            {
                return(null);
            }

            SceneObjectGeometry skyboxObject = new SceneObjectGeometry();

            #region Texture

            OpenGL.Objects.Image[] cubeMap = new OpenGL.Objects.Image[6];

            using (Stream imageStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("HelloObjects.Data.PiazzaDelPopolo.posx.jpg"))
                cubeMap[0] = ImageCodec.Instance.Load(imageStream, ImageFormat.Jpeg);
            using (Stream imageStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("HelloObjects.Data.PiazzaDelPopolo.negx.jpg"))
                cubeMap[1] = ImageCodec.Instance.Load(imageStream, ImageFormat.Jpeg);
            using (Stream imageStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("HelloObjects.Data.PiazzaDelPopolo.posy.jpg"))
                cubeMap[2] = ImageCodec.Instance.Load(imageStream, ImageFormat.Jpeg);
            using (Stream imageStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("HelloObjects.Data.PiazzaDelPopolo.negy.jpg"))
                cubeMap[3] = ImageCodec.Instance.Load(imageStream, ImageFormat.Jpeg);
            using (Stream imageStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("HelloObjects.Data.PiazzaDelPopolo.posz.jpg"))
                cubeMap[4] = ImageCodec.Instance.Load(imageStream, ImageFormat.Jpeg);
            using (Stream imageStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("HelloObjects.Data.PiazzaDelPopolo.negz.jpg"))
                cubeMap[5] = ImageCodec.Instance.Load(imageStream, ImageFormat.Jpeg);

            foreach (OpenGL.Objects.Image image in cubeMap)
            {
                image.FlipVertically();
            }

            TextureCube cubeMapTexture = new TextureCube();
            cubeMapTexture.Create(PixelLayout.RGB24, cubeMap);

            #endregion

            #region State

            skyboxObject.LocalModel.Scale(170.0f);

            skyboxObject.ObjectState.DefineState(new DepthTestState(DepthFunction.Less));
            skyboxObject.ObjectState.DefineState(new CullFaceState(FrontFaceDirection.Cw, CullFaceMode.Back));

            ShaderUniformState skyboxUniformState = new ShaderUniformState("OpenGL.Skybox");
            skyboxUniformState.SetUniformState("glo_CubeMapTexture", cubeMapTexture);
            skyboxObject.ObjectState.DefineState(skyboxUniformState);

            #endregion

            #region Program

            skyboxObject.ProgramTag = ShadersLibrary.Instance.CreateProgramTag("OpenGL.Skybox");
            skyboxObject.Program.Create(_Context);

            #endregion

            #region Vertex Array

            // Define vertex arrays
            skyboxObject.VertexArray = new VertexArrayObject();
            skyboxObject.VertexArray.SetArray(_CubeArrayPosition, VertexArraySemantic.Position);
            skyboxObject.VertexArray.SetElementArray(PrimitiveType.Triangles);
            skyboxObject.VertexArray.Create(_Context);

            #endregion

            return(skyboxObject);
        }