예제 #1
0
 public ShaderMaterialTranslator(string Type)
 {
     this.TexturesNumber          = 0;
     this._TextureBitmaps         = new List <Bitmap>();
     this._MaterialTranslatorType = Type;
     this._Entries = new List <ShaderMaterialTranslatorEntry>();
     LoadEntries();
     Translator = this;
 }
예제 #2
0
        private byte[] PackTextures(List <Bitmap> TextureBitmaps, Vertex MaxResolution)
        {
            List <byte> Textures = new List <byte>();

            for (int i = 0; i < TextureBitmaps.Count; i++)
            {
                TextureBitmaps[i] = new Bitmap(TextureBitmaps[i], new Size((int)MaxResolution.X, (int)MaxResolution.Y));
                Textures.AddRange(ShaderMaterialTranslator.ImageToByte(TextureBitmaps[i]));
            }
            return(Textures.ToArray());
        }
예제 #3
0
        public void SetDefaults()
        {
            ShaderMaterialTranslator SMT = _CurrentTranslator as ShaderMaterialTranslator;

            if (this.CurrentTranslator.TranslateMaterial(Material.Default))
            {
                _CurrentRenderer.SetMaterial(new object[3] {
                    new string[6] {
                        "Default", SMT.VertexShaderOutput, SMT.FragmentShaderOutput, null, null, null
                    }, null, null
                }, true);
            }
        }
예제 #4
0
        public override bool TranslateMaterial(Material AppliedMaterial)
        {
            MaterialNode Output = null;

            this._TextureBitmaps = new List <Bitmap>();
            this._TexturesNumber = 0;
            this._Textures       = null;
            for (int i = 0; i < AppliedMaterial.Nodes.Count; i++)
            {
                if (AppliedMaterial.Nodes[i].ID == "Output")
                {
                    Output = AppliedMaterial.Nodes[i];
                }
            }
            if (Output == null)
            {
                return(false);
            }
            string Fragment = GenerateFragment(Output);

            if (Fragment == "")
            {
                return(false);
            }
            this._FragmentShaderOutput = Fragment;
            String LibPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/Engineer/";

            this._VertexShaderOutput = File.ReadAllText(LibPath + this._MaterialTranslatorType + "\\Generator\\Vertex.shader");
            if (_TexturesNumber > 0)
            {
                List <byte> Textures = new List <byte>();
                for (int i = 0; i < TexturesNumber; i++)
                {
                    _TextureBitmaps[i] = new Bitmap(_TextureBitmaps[i], new Size(256, 256));
                    Textures.AddRange(ShaderMaterialTranslator.ImageToByte(_TextureBitmaps[i]));
                }
                this._Textures = Textures.ToArray();
            }
            return(true);
        }
예제 #5
0
        public virtual void Draw3DScene(Scene3D CurrentScene, int Width, int Height)
        {
            bool GlobalUpdate = false;

            this._CurrentRenderer.Toggle(RenderEnableCap.Depth, true);
            List <Light> Lights = CurrentScene.Lights;
            List <Actor> Actors = CurrentScene.Actors;

            this._CurrentRenderer.SetViewport(Width, Height);
            this._CurrentRenderer.ClearColor(new float[4] {
                (CurrentScene.BackColor.R * 1.0f + 1) / 256,
                (CurrentScene.BackColor.G * 1.0f + 1) / 256,
                (CurrentScene.BackColor.B * 1.0f + 1) / 256,
                (CurrentScene.BackColor.A * 1.0f + 1) / 256
            });
            this._CurrentRenderer.SetCameraPosition(CurrentScene.EditorCamera.Translation);
            this._CurrentRenderer.ResetLights();
            for (int k = 0; k < CurrentScene.Lights.Count; k++)
            {
                Vertex[] LightVertices = new Vertex[4];
                LightVertices[0] = VertexBuilder.FromRGB(CurrentScene.Lights[k].Color.R,
                                                         CurrentScene.Lights[k].Color.G,
                                                         CurrentScene.Lights[k].Color.B);
                LightVertices[1] = CurrentScene.Lights[k].Translation;
                LightVertices[2] = CurrentScene.Lights[k].Attenuation;
                if (CurrentScene.Lights[k].Active)
                {
                    LightVertices[3] = new Vertex(CurrentScene.Lights[k].Intensity, 0, 0);
                }
                else
                {
                    LightVertices[3] = new Vertex(0, 0, 0);
                }
                GlobalUpdate = GlobalUpdate || this._CurrentRenderer.SetViewLight(k, LightVertices);
            }
            this._CurrentRenderer.Clear();
            this._Matrix.MatrixMode("Projection");
            this._Matrix.LoadIdentity();
            this._Matrix.DefaultPerspective(Width, Height);
            this._CurrentRenderer.SetProjectionMatrix(_Matrix.ProjectionMatrix);
            this._Matrix.MatrixMode("ModelView");
            this._Matrix.LoadIdentity();
            Vertex Eye = new Vertex();

            this._Matrix.DefaultView(Eye, new Vertex(Eye.X, Eye.Y, Eye.Z - 1));
            this._Matrix.Rotate(CurrentScene.EditorCamera.Rotation.X, 1, 0, 0);
            this._Matrix.Rotate(CurrentScene.EditorCamera.Rotation.Y, 0, 1, 0);
            this._Matrix.Rotate(CurrentScene.EditorCamera.Rotation.Z, 0, 0, 1);
            this._Matrix.Translate(-CurrentScene.EditorCamera.Translation.X,
                                   -CurrentScene.EditorCamera.Translation.Y,
                                   -CurrentScene.EditorCamera.Translation.Z);
            this._Matrix.PushMatrix();
            this._CurrentRenderer.SetModelViewMatrix(_Matrix.ModelViewMatrix);

            for (int i = 0; i < CurrentScene.Actors.Count; i++)
            {
                if (CurrentScene.Actors[i].Active)
                {
                    this._Matrix.Scale(CurrentScene.Actors[i].Scale.X, CurrentScene.Actors[i].Scale.Y, CurrentScene.Actors[i].Scale.Z);
                    this._Matrix.Translate(CurrentScene.Actors[i].Translation.X,
                                           CurrentScene.Actors[i].Translation.Y,
                                           CurrentScene.Actors[i].Translation.Z);
                    this._Matrix.Rotate(CurrentScene.Actors[i].Rotation.X, 1, 0, 0);
                    this._Matrix.Rotate(CurrentScene.Actors[i].Rotation.Y, 0, 1, 0);
                    this._Matrix.Rotate(CurrentScene.Actors[i].Rotation.Z, 0, 0, 1);

                    this._CurrentRenderer.SetModelViewMatrix(_Matrix.ModelViewMatrix);
                    for (int j = 0; j < CurrentScene.Actors[i].Geometries.Count; j++)
                    {
                        if (!this._CurrentRenderer.IsMaterialReady(CurrentScene.Actors[i].Materials[CurrentScene.Actors[i].GeometryMaterialIndices[j]].ID) || CurrentScene.Actors[i].Materials[CurrentScene.Actors[i].GeometryMaterialIndices[j]].Modified || GlobalUpdate)
                        {
                            this._CurrentRenderer.UpdateMaterial();
                            ShaderMaterialTranslator SMT = _CurrentTranslator as ShaderMaterialTranslator;
                            if (this.CurrentTranslator.TranslateMaterial(CurrentScene.Actors[i].Materials[CurrentScene.Actors[i].GeometryMaterialIndices[j]]))
                            {
                                _CurrentRenderer.SetMaterial(new object[3] {
                                    new string[6] {
                                        CurrentScene.Actors[i].Materials[CurrentScene.Actors[i].GeometryMaterialIndices[j]].ID, SMT.VertexShaderOutput, SMT.FragmentShaderOutput, null, null, null
                                    }, SMT.TexturesNumber, SMT.Textures
                                }, true);
                            }
                            else
                            {
                                _CurrentRenderer.SetMaterial(new object[3] {
                                    new string[6] {
                                        "Default", null, null, null, null, null
                                    }, null, null
                                }, false);
                            }
                            CurrentScene.Actors[i].Materials[CurrentScene.Actors[i].GeometryMaterialIndices[j]].Modified = false;
                        }
                        else
                        {
                            _CurrentRenderer.SetMaterial(new object[3] {
                                new string[6] {
                                    CurrentScene.Actors[i].Materials[CurrentScene.Actors[i].GeometryMaterialIndices[j]].ID, null, null, null, null, null
                                }, null, null
                            }, false);
                        }

                        this._CurrentRenderer.UpdateMaterial();
                        this._CurrentRenderer.RenderGeometry(CurrentScene.Actors[i].Geometries[j].Vertices,
                                                             CurrentScene.Actors[i].Geometries[j].Normals,
                                                             CurrentScene.Actors[i].Geometries[j].TexCoords,
                                                             CurrentScene.Actors[i].Geometries[j].Faces,
                                                             CurrentScene.Actors[i].Modified);
                    }
                    this._Matrix.PopMatrix();
                }
            }
        }