Esempio n. 1
0
        public void CreateSkyBox(RShader skyBoxShader, RTexture3D skyBoxTexture)
        {
            sky = RScene.Instance.CreateMeshBuilder("skybox");
            sky.CreateBox(Vector3.Zero, Vector3.One, true);
            sky.Matrix     = Matrix.Identity;
            sky.DepthWrite = false;
            RMaterial skyBoxMaterial = new RMaterial("skybox");

            skyBoxMaterial.Shader = skyBoxShader;
            skyBoxMaterial.SetTexture(RTextureLayer.DIFFUSE, skyBoxTexture);
            sky.Material = skyBoxMaterial;
        }
Esempio n. 2
0
        public void CreateSkyBox(RTexture3D skyBoxTexture)
        {
            skybox = new RShader();
            skybox.Load(@"
#include ""headers.glsl""
uniform mat4 proj : PROJECTION;
uniform mat4 mv : MODELVIEW;

smooth out vec3 eye;

void main()
{
    mat3 imv = mat3(inverse(mv));
    mat4 inverseProjection = inverse(proj);
    vec3 unprojected = (inverseProjection * vec4(r_Position, 1.0)).xyz;
    eye = imv * unprojected;

    gl_Position = vec4(r_Position, 1.0);
}
", @"

smooth in vec3 eye;
uniform samplerCube diffuse;
out vec4 fragColor;
void main()
{
    fragColor = texture(diffuse, eye);
}
", null);
            sky = RScene.Instance.CreateMeshBuilder("skybox");
            sky.CreateBox(Vector3.Zero, Vector3.One, true);
            sky.Matrix     = Matrix.Identity;
            sky.DepthWrite = false;
            RMaterial skyBoxMaterial = new RMaterial("skybox");

            skyBoxMaterial.Shader = skybox;
            skyBoxMaterial.SetTexture(RTextureLayer.DIFFUSE, skyBoxTexture);
            sky.Material = skyBoxMaterial;
        }
Esempio n. 3
0
        public static void LoadSource(this RMesh rmesh, string filename)
        {
            AssimpContext context = new AssimpContext();

            context.SetConfig(new Assimp.Configs.FBXImportAllMaterialsConfig(true));
            context.SetConfig(new Assimp.Configs.FBXImportAllGeometryLayersConfig(true));
            context.SetConfig(new Assimp.Configs.MultithreadingConfig(2));
            context.SetConfig(new Assimp.Configs.FBXStrictModeConfig(false));
            if (!context.IsImportFormatSupported(Path.GetExtension(filename)))
            {
                throw new ReactorException("Attempted to load a model that Assimp doesn't know how to load");
            }
            LogStream log = new LogStream((msg, user) => {
                RLog.Info(msg.Remove(msg.Length - 2));
            });

            log.Attach();
            int   platform = (int)Environment.OSVersion.Platform;
            Scene scene    = context.ImportFile(filename,
                                                PostProcessSteps.FindInvalidData |
                                                PostProcessSteps.GenerateSmoothNormals |
                                                PostProcessSteps.Triangulate |
                                                PostProcessSteps.GenerateUVCoords |
                                                PostProcessSteps.CalculateTangentSpace |
                                                PostProcessSteps.PreTransformVertices);

            if (scene.HasMeshes)
            {
                foreach (Mesh mesh in scene.Meshes)
                {
                    if (!mesh.HasVertices)
                    {
                        continue;
                    }
                    RMeshPart rmeshpart = RMeshPart.Create <RMeshPart> ();


                    RVertexData [] data = new RVertexData [mesh.VertexCount];

                    List <int> indicesList = new List <int> ();

                    if (mesh.HasFaces)
                    {
                        foreach (Face face in mesh.Faces)
                        {
                            indicesList.AddRange(face.Indices.ToArray());
                            foreach (int index in face.Indices)
                            {
                                Vector3D p = mesh.Vertices [index];
                                data [index].Position = new Vector3(p.X, p.Y, p.Z);
                                if (mesh.HasTextureCoords(0))
                                {
                                    Vector3D t = mesh.TextureCoordinateChannels [0] [index];
                                    data [index].TexCoord = new Vector2(t.X, -t.Y);
                                }

                                if (mesh.HasNormals)
                                {
                                    Vector3D n = mesh.Normals [index];
                                    data [index].Normal = new Vector3(n.X, n.Y, n.Z);
                                }

                                if (mesh.HasTangentBasis)
                                {
                                    Vector3D b = mesh.BiTangents [index];
                                    Vector3D t = mesh.Tangents [index];
                                    data [index].Bitangent = new Vector3(b.X, b.Y, b.Z);
                                    data [index].Tangent   = new Vector3(t.X, t.Y, t.Z);
                                }
                            }
                        }
                    }



                    RVertexBuffer vbuffer = new RVertexBuffer(typeof(RVertexData), mesh.VertexCount, RBufferUsage.WriteOnly, true);


                    RIndexBuffer ibuffer = new RIndexBuffer(typeof(int), indicesList.Count, RBufferUsage.WriteOnly);
                    ibuffer.SetData(indicesList.ToArray());

                    vbuffer.SetData <RVertexData> (data);

#if WINDOWS
                    var separator = "\\";
#else
                    var separator = "/";
#endif

                    rmeshpart.VertexBuffer = vbuffer;
                    rmeshpart.IndexBuffer  = ibuffer;

                    RMaterial material = new RMaterial(rmesh.Name + ":Material");

                    if (scene.HasMaterials)
                    {
                        Material mat = scene.Materials[mesh.MaterialIndex];
                        material.Shininess = mat.Shininess;
                        material.SetColor(RMaterialColor.DIFFUSE, new RColor(mat.ColorDiffuse.R, mat.ColorDiffuse.G, mat.ColorDiffuse.B, mat.ColorDiffuse.A));
                        if (mat.HasTextureDiffuse)
                        {
                            var        texFileName = Path.GetFileName(mat.TextureDiffuse.FilePath.Replace("\\", separator).Replace("/", separator));
                            RTexture2D tex         = (RTexture2D)RTextures.Instance.CreateTexture <RTexture2D>(texFileName, "/textures/" + texFileName.ToLower());
                            tex.Bind();
                            tex.GenerateMipmaps();
                            tex.SetTextureMagFilter(RTextureMagFilter.Linear);
                            tex.SetTextureMinFilter(RTextureMinFilter.LinearMipmapLinear);
                            tex.SetTextureWrapMode(RTextureWrapMode.Repeat, RTextureWrapMode.Repeat);
                            tex.Unbind();
                            material.SetTexture(RTextureLayer.DIFFUSE, tex);
                        }
                        if (mat.HasTextureNormal)
                        {
                            var        texFileName = Path.GetFileName(mat.TextureNormal.FilePath.Replace("\\", separator).Replace("/", separator));
                            RTexture2D tex         = (RTexture2D)RTextures.Instance.CreateTexture <RTexture2D>(texFileName, "/textures/" + texFileName.ToLower());
                            tex.Bind();
                            tex.GenerateMipmaps();
                            tex.SetTextureMagFilter(RTextureMagFilter.Linear);
                            tex.SetTextureMinFilter(RTextureMinFilter.LinearMipmapLinear);
                            tex.SetTextureWrapMode(RTextureWrapMode.Repeat, RTextureWrapMode.Repeat);
                            tex.Unbind();
                            material.SetTexture(RTextureLayer.NORMAL, tex);
                        }
                        if (mat.HasTextureAmbient)
                        {
                            var        texFileName = Path.GetFileName(mat.TextureAmbient.FilePath.Replace("\\", separator).Replace("/", separator));
                            RTexture2D tex         = (RTexture2D)RTextures.Instance.CreateTexture <RTexture2D>(texFileName, "/textures/" + texFileName.ToLower());
                            tex.SetTextureMagFilter(RTextureMagFilter.Linear);
                            tex.SetTextureMinFilter(RTextureMinFilter.LinearMipmapLinear);
                            tex.SetTextureWrapMode(RTextureWrapMode.Repeat, RTextureWrapMode.Repeat);
                            material.SetTexture(RTextureLayer.AMBIENT, tex);
                        }
                        if (mat.HasTextureHeight)
                        {
                            var        texFileName = Path.GetFileName(mat.TextureHeight.FilePath.Replace("\\", separator).Replace("/", separator));
                            RTexture2D tex         = (RTexture2D)RTextures.Instance.CreateTexture <RTexture2D>(texFileName, "/textures/" + texFileName.ToLower());
                            tex.SetTextureMagFilter(RTextureMagFilter.Linear);
                            tex.SetTextureMinFilter(RTextureMinFilter.LinearMipmapLinear);
                            tex.SetTextureWrapMode(RTextureWrapMode.Repeat, RTextureWrapMode.Repeat);
                            material.SetTexture(RTextureLayer.HEIGHT, tex);
                        }
                        if (mat.HasTextureEmissive)
                        {
                            var        texFileName = Path.GetFileName(mat.TextureEmissive.FilePath.Replace("\\", separator).Replace("/", separator));
                            RTexture2D tex         = (RTexture2D)RTextures.Instance.CreateTexture <RTexture2D>(texFileName, "/textures/" + texFileName.ToLower());
                            tex.SetTextureMagFilter(RTextureMagFilter.Linear);
                            tex.SetTextureMinFilter(RTextureMinFilter.LinearMipmapLinear);
                            tex.SetTextureWrapMode(RTextureWrapMode.Repeat, RTextureWrapMode.Repeat);
                            material.SetTexture(RTextureLayer.GLOW, tex);
                        }
                        if (mat.HasTextureSpecular)
                        {
                            var        texFileName = Path.GetFileName(mat.TextureSpecular.FilePath.Replace("\\", separator).Replace("/", separator));
                            RTexture2D tex         = (RTexture2D)RTextures.Instance.CreateTexture <RTexture2D>(texFileName, "/textures/" + texFileName.ToLower());
                            tex.SetTextureMagFilter(RTextureMagFilter.Linear);
                            tex.SetTextureMinFilter(RTextureMinFilter.LinearMipmapLinear);
                            tex.SetTextureWrapMode(RTextureWrapMode.Repeat, RTextureWrapMode.Repeat);
                            material.SetTexture(RTextureLayer.SPECULAR, tex);
                        }
                    }
                    rmeshpart.Material = material;
                    rmesh.Parts.Add(rmeshpart);
                }
                //return rmesh;
            }
            //return null;
            if (rmesh.Parts.Count == 0)
            {
                throw new ReactorException("Attempted to load a model when Assimp couldn't find any verticies!");
            }

            context.Dispose();
        }