Exemplo n.º 1
0
 /// <summary>
 /// a call invoke the <see cref="Field.Update"/> of every var.
 /// </summary>
 public virtual void UpDateAllVars()
 {
     foreach (var item in Vars.Values)
     {
         (item as Field).Update();
         OpenGlDevice.CheckError();
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// sets the value of the field. It works only when the shader is <see cref="OGLShader.Using"/> true is.
        /// </summary>
        /// <param name="value">the new value</param>
        /// <returns>true when the shader is using and the field has a handle >=0.</returns>

        public bool SetValue(Drawing3d.Matrix value)
        {
            if (!(Owner.Using) || (handle < 0))
            {
                return(false);
            }
            float[] M = value.ToFloat();

            GL.UniformMatrix4(handle, 1, false, ref M[0]);
            OpenGlDevice.CheckError();
            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// is called from <see cref="OpenGlDevice.setTexture(Texture)"/> to set the values to the current <see cref="OpenGlDevice.Shader"/>.
        /// </summary>
        /// <param name="Shader">the current <see cref="OpenGlDevice.Shader"/>.</param>
        public void SetAttributes(GLShader Shader)
        {
            Matrix M             = Matrix.Scale(new xyz(1 / WorldWidth, -1 / WorldHeight, 1 / WorldWidth));
            Matrix TextureMatrix = Matrix.identity;

            if (SoBigAsPossible != Texture.BigAsPosible.None)
            {
                TextureMatrix = Matrix.Scale(new xyz(1, -1, 1));
            }
            else
            {
                TextureMatrix = M * Matrix.Translation(new xyz(-Translation.x, -Translation.y, 0)) * Matrix.Rotation(new xyz(0, 0, 1), -Rotation);
            }

            GL.ActiveTexture(TextureUnit.Texture0 + Index);
            GL.BindTexture(TextureTarget.Texture2D, Handle);
            OpenGlDevice.CheckError();
            Field T = Shader.getvar("Texture" + Index.ToString() + "Enabled");

            if (T != null)
            {
                T.SetValue(1);
            }

            T = Shader.getvar("Texture" + Index.ToString() + "_");
            if (T != null)
            {
                T.SetValue(Index);
            }
            T = Shader.getvar("Texture" + Index.ToString() + "Matrix");
            if (T != null)
            {
                T.SetMatrix(TextureMatrix);
            }
            if (NormalMap)
            {
                T = Shader.getvar("NormalMap");
                if (T != null)
                {
                    T.SetValue(true);
                }
            }
            if (Tesselation)
            {
                T = Shader.getvar("Tesselation");
                if (T != null)
                {
                    T.SetValue(true);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// internal.
        /// </summary>
        internal static void MeshdrawTriangles(OpenGlDevice Device, IndexType[] Indices, xyzf[] Points, xyzf[] Normals, xyf[] Texture)
        {
            if ((Device.RenderKind != RenderKind.SnapBuffer) && (!Entity.Compiling))
            {
                return;
            }
            if ((Device.RenderKind == RenderKind.SnapBuffer)) // nur dann....
            {
                Selector.StoredSnapItems[Selector.StoredSnapItems.Count - 1].TriangleInfo = new TriangleInfo(Indices, Points, Normals, Texture);
            }
            // die einzige Zuweisung der Triangleinfo
            int ID = MeshVertices.Count;

            for (int i = 0; i < Indices.Length; i++)
            {
                MeshIndices.Add((IndexType)(Indices[i] + ID));
            }
            if (MeshVertices.Count + Points.Length > IndexType.MaxValue)
            {
                int CT = (MeshVertices.Count + Points.Length);
                MeshIndices = new List <IndexType>();
                Renew();
                throw new Exception("Points count to large " + CT.ToString());
            }
            for (int i = 0; i < Points.Length; i++)
            {
                MeshVertices.Add(Device.ModelMatrix * Points[i]);
            }
            xyzf NP = Device.ModelMatrix * new xyzf(0, 0, 0);

            if (Normals != null)
            {
                for (int i = 0; i < Normals.Length; i++)
                {
                    MeshNormals.Add(Device.ModelMatrix * Normals[i] - NP);
                }
            }
            if (Texture != null)
            {
                for (int i = 0; i < Texture.Length; i++)
                {
                    MeshTextureCoords.Add(Texture[i]);
                }
            }
            MeshMode = Device.PolygonMode;
            OpenGlDevice.CheckError();
        }
Exemplo n.º 5
0
        private void ClippEnable(int Id, bool On)
        {
            if (On)
            {
                ClippEnabled[Id] = 1;
            }
            else
            {
                ClippEnabled[Id] = 0;
            }
            if (Shader != null)
            {
                Field CE = Shader.getvar("ClippingPlaneEnabled");
                if (CE != null)
                {
                    CE.Update();
                }
            }

            OpenGlDevice.CheckError();
        }
Exemplo n.º 6
0
        internal void _Ondraw(OpenGlDevice Device)
        {
            OnDraw(Device);

            OpenGlDevice.CheckError();
        }
Exemplo n.º 7
0
        /// <summary>
        /// overrides the draw method .
        /// </summary>
        /// <param name="Device"></param>
        protected override void OnDraw(OpenGlDevice Device)
        {
            int StoredObject = -1;

            if (Selector.WriteToSnap)
            {
                CompiledMesh C = this as CompiledMesh;
                if (C != null)
                {
                    StoredObject = C.SnapObject;
                }
                Device.Selector.SetObjectNumber(StoredObject);
            }
            Entity.CurrentEntity = this;


            if (Compiling)
            {
                MeshCreator.AddProg(this);
                return;
            }
            float     PW   = Device.PenWidth;
            PenStyles PS   = Device.PenStyle;
            Material  Save = Device.Material;

            if (Mode == PolygonMode.Line)
            {
                Device.PenWidth = PenWidth;
                Device.PenStyle = PenStyle;
                Device.Emission = PenColor;
            }
            else
            {
                Device.Material = Material;
            }
            PolygonMode SavePolygonMode = Device.PolygonMode;

            Device.PolygonMode = Mode;
            if ((VAO >= 0) && (Device.RenderKind == RenderKind.Render))
            {
                OpenGlDevice.CheckError();
                Field C = null;
                if (Colors != null)
                {
                    C = Device.Shader.getvar("ColorEnabled");
                    if (C != null)
                    {
                        C.SetValue(1);
                    }
                }
                GL.BindVertexArray(VAO);
                if (!TesselateOn)
                {
                    GL.DrawElements(Primitives, VBOIndices.IndexArray.Length, DrawElementsType.UnsignedInt, 0);
                    OpenGlDevice.CheckError();
                }
                else
                {
                    GLShader SaveShader = Device.Shader;
                    Device.Shader = Device.TesselationtShader;
                    if (FieldResolutionFactor == null)
                    {
                        FieldResolutionFactor = Device.Shader.getvar("ResolutionFactor");
                    }
                    if (FieldDispFactor == null)
                    {
                        FieldDispFactor = Device.Shader.getvar("gDispFactor");
                    }
                    if (FieldEyePos == null)
                    {
                        FieldEyePos = Device.Shader.getvar("gEyeWorldPos");
                    }
                    if (FieldDispFactor != null)
                    {
                        FieldDispFactor.SetValue((float)TessDispFactor);
                    }
                    if (FieldEyePos != null)
                    {
                        FieldEyePos.SetValue(Device.Camera.Position);
                    }
                    Device.texture = TessDisplacementMap;
                    //GL.ActiveTexture(TextureUnit.Texture0 + 1);
                    //GL.BindTexture(TextureTarget.Texture2D, TessDisplacementMap.Handle);
                    if (Texture != null)
                    {
                        Device.texture = Texture;
                    }

                    if (FieldResolutionFactor != null)
                    {
                        FieldResolutionFactor.SetValue((float)TessResolutionFactor);
                    }

                    GL.DrawElements(BeginMode.Patches, VBOIndices.IndexArray.Length, DrawElementsType.UnsignedInt, 0);
                    TessDisplacementMap.Hide();
                    Device.Shader  = SaveShader;
                    Device.texture = null;
                }

                GL.BindVertexArray(0);
                if (Colors != null)
                {
                    if (C != null)
                    {
                        C.SetValue(0);
                    }
                }
            }
            else
            {
                if (Primitives == BeginMode.Quads)
                {
                    DrawQuads(Device);
                }
                else
                {
                    Texture SaveTexture = Device.texture;
                    Device.texture = Texture;
                    Device.drawMesh(Indices, VBOPosition.xyzPoints, VBONormals.xyzPoints, VBOTexture.xyPoints, VBOColors.xyzPoints);
                    Device.texture = SaveTexture;
                }
            }
            Device.PolygonMode = SavePolygonMode;
            if (Mode == PolygonMode.Line)
            {
                Device.PenWidth = PW;
                Device.PenStyle = PS;
                Device.Emission = Color.Black;
            }
            Device.Material = Save;
        }
Exemplo n.º 8
0
        void DrawQuads(OpenGlDevice Device)
        {
            object SnapHandle = null;

            if (Device.RenderKind == RenderKind.SnapBuffer)
            {
                MeshSnappItem SI = new MeshSnappItem(this);
                SnapHandle = Device.Selector.RegisterSnapItem(SI);
                MeshCreator.MeshdrawTriangles(Device, QuadsToTriangle(Indices), Position, Normals, TextureCoords);
            }
            int MPosition = Device.Shader.Position.handle;

            if (MPosition >= 0)
            {
                GL.EnableVertexAttribArray(MPosition);
            }
            GL.VertexAttribPointer(MPosition, 3, VertexAttribPointerType.Float, false, 0, ref Position[0]);
            int THandle = Device.Shader.Texture.handle;

            if ((THandle >= 0) && (Texture != null))
            {
                GL.EnableVertexAttribArray(THandle);
            }
            int NHandle = Device.Shader.Normal.handle;

            if ((NHandle >= 0) && (Normals != null))
            {
                GL.EnableVertexAttribArray(NHandle);
            }
            Field C = Device.Shader.getvar("ColorEnabled");

            if (C != null)
            {
                C.SetValue(0);
            }
            int CHandle = Device.Shader.Color.handle;

            if ((CHandle >= 0) && (Colors != null))
            {
                if (C != null)
                {
                    C.SetValue(1);
                }
                GL.EnableVertexAttribArray(CHandle);
            }
            if ((MPosition >= 0) && (Position.Length > 0))
            {
                GL.VertexAttribPointer(MPosition, 3, VertexAttribPointerType.Float, false, 0, ref Position[0].x);
                if ((Colors != null) && (Colors.Length == Position.Length) && (CHandle >= 0))
                {
                    GL.VertexAttribPointer(CHandle, 3, VertexAttribPointerType.Float, false, 0, ref Colors[0].x);
                }

                if ((Texture != null) && (TextureCoords.Length > 0) && (THandle >= 0))
                {
                    GL.VertexAttribPointer(THandle, 2, VertexAttribPointerType.Float, false, 0, ref TextureCoords[0].x);
                }
                if ((NHandle >= 0) && (Normals != null) && (Normals.Length == Position.Length))
                {
                    GL.VertexAttribPointer(NHandle, 3, VertexAttribPointerType.Float, false, 0, ref Normals[0].x);
                }
            }

            // Draw:

            GL.DrawElements(PrimitiveType.Quads, Indices.Length, DrawElementsType.UnsignedInt, ref Indices[0]);
            OpenGlDevice.CheckError();

            // Attribute freigeben
            if (MPosition >= 0)
            {
                GL.DisableVertexAttribArray(MPosition);
            }
            if (NHandle >= 0)
            {
                GL.DisableVertexAttribArray(NHandle);
            }
            if ((THandle >= 0))
            {
                GL.DisableVertexAttribArray(THandle);
            }
            if ((CHandle >= 0))
            {
                GL.DisableVertexAttribArray(CHandle);
            }
            if (C != null)
            {
                C.SetValue(0);
            }
            if (Device.RenderKind == RenderKind.SnapBuffer)
            {
                Device.Selector.UnRegisterSnapItem(SnapHandle);
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// calls recursively the <see cref="CustomEntity.OnDraw(OpenGlDevice)"/>method of the members in the <see cref="Children"/>list.
        /// </summary>
        /// <param name="Device"></param>
        public void Paint(OpenGlDevice Device)

        {
            if ((!Visible) && (Device.RenderKind != RenderKind.SnapBuffer))
            {
                return;
            }
            double SaveOpacity = Device.Translucent;

            OpenGlDevice.CheckError();
            if (Device.RenderKind == RenderKind.Render)
            {
                if ((Pattern != null) && (Texture == null))
                {
                    Texture = GetPatternTexture(Device);// ActivatePattern(Device);
                }
            }
            OpenGlDevice.CheckError();


            Entity SaveCurrent = CurrentEntity;


            if (HasTexture)
            {
                Device.texture = Texture;
            }
            if (HasMaterial)
            {
                Device.Material = Material;
            }
            if (HasOpacity)
            {
                Device.Translucent = System.Math.Min(Opacity, Device.Translucent);
            }

            CurrentEntity = this;
            if (Device.OutFitChanged)
            {
                setMesh(null);
            }
            if (Device.RenderKind == RenderKind.Render)
            {
                if (Invalid)
                {
                    Device.invalid = true;
                }
            }
            if (((Mesh == null) || (Invalid)) && (CompileEnable) && (!Compiling) && (Device.RenderKind == RenderKind.Render))
            {
                Compile(Device); // sollte nicht bei refreshSnapBuffer auf gerufen werden
            }
            Invalid = false;


            if (Device.Selector.CreateSnap) // kommt vonRefreshMeshBuffer
            {
                if (SnappEnable)
                {
                    MeshCreator.CreateMeshes(Device, CompileMeshDraw); // Entity wird gedrawt im snap items mode
                }
                return;                                                // eventuell Children interndraw braucht nicht aufgerufen werden!!!
            }

            if (HasTransformation)
            {
                Device.PushMatrix();
                Device.MulMatrix(Transformation);
            }
            if ((Mesh != null))// && (Device.RenderKind == RenderKind.Render))
            {
                Mesh.internDraw(Device); base.OnDraw(Device);
            }
            else
            {
                if (Mesh != null)
                {
                    Mesh.Paint(Device);
                }
                else
                {
                    internDraw(Device);   // bei snap wird im createsnap registriert
                }
            }

            if (Mesh == null)
            {
                for (int i = 0; i < Children.Count; i++)
                {
                    if (Children[i].Visible)
                    {
                        Children[i].Paint(Device);
                    }
                }
            }
            CurrentEntity = SaveCurrent;
            if (HasTransformation)
            {
                Device.PopMatrix();
            }
            if (HasOpacity)
            {
                Device.Translucent = SaveOpacity;
            }

            if (Entity.Compiling)
            {
                MeshCreator.CheckMeshCreator();
            }
            OpenGlDevice.CheckError();
            if (Device.RenderKind == RenderKind.Render)
            {
                Invalid = false;
            }
        }