/// <summary>
 /// Destroys the RS4P.
 /// </summary>
 public void Destroy()
 {
     GL.DeleteFramebuffer(fbo);
     GraphicsUtil.CheckError("RS4P - Destroy - 0.1");
     GL.DeleteTexture(DiffuseTexture);
     GraphicsUtil.CheckError("RS4P - Destroy - 0.2");
     GL.DeleteTexture(PositionTexture);
     GraphicsUtil.CheckError("RS4P - Destroy - 0.3");
     GL.DeleteTexture(NormalsTexture);
     GraphicsUtil.CheckError("RS4P - Destroy - 0.5");
     GL.DeleteTexture(DepthTexture);
     GraphicsUtil.CheckError("RS4P - Destroy - 0.6");
     GL.DeleteTexture(RenderhintTexture);
     GraphicsUtil.CheckError("RS4P - Destroy - 0.7");
     GL.DeleteTexture(Rh2Texture);
     GraphicsUtil.CheckError("RS4P - Destroy");
 }
        /// <summary>
        /// Compiles a compute shader by name to a shader.
        /// </summary>
        /// <param name="fname">The file name.</param>
        /// <param name="specialadder">Special additions (EG defines)</param>
        /// <returns>The shader program.</returns>
        public int CompileCompute(string fname, string specialadder = "")
        {
            fname = FileEngine.CleanFileName(fname.Trim());
            string ftxt  = GetShaderFileText(fname + ".comp");
            int    index = ftxt.IndexOf(FILE_START);

            if (index < 0)
            {
                index = 0;
            }
            else
            {
                index += FILE_START.Length;
            }
            ftxt = ftxt.Insert(index, specialadder);
            int shd = GL.CreateShader(ShaderType.ComputeShader);

            GL.ShaderSource(shd, ftxt);
            GL.CompileShader(shd);
            string SHD_Info = GL.GetShaderInfoLog(shd);

            GL.GetShader(shd, ShaderParameter.CompileStatus, out int SHD_Status);
            if (SHD_Status != 1)
            {
                throw new Exception("Error creating ComputeShader. Error status: " + SHD_Status + ", info: " + SHD_Info);
            }
            int program = GL.CreateProgram();

            GL.AttachShader(program, shd);
            GL.LinkProgram(program);
            string str = GL.GetProgramInfoLog(program);

            if (str.Length != 0)
            {
                SysConsole.Output(OutputType.INFO, "Linked shader with message: '" + str + "'" + " -- FOR -- " + ftxt);
            }
            GL.DeleteShader(shd);
            GraphicsUtil.CheckError("Shader - Compute - Compile");
            return(program);
        }
示例#3
0
        /// <summary>
        /// Renders a line box.
        /// </summary>
        /// <param name="min">The minimum coordinate.</param>
        /// <param name="max">The maximmum coordinate.</param>
        /// <param name="view">The relevant view.</param>
        /// <param name="rot">Any rotation.</param>
        public void RenderLineBox(Location min, Location max, View3D view, Matrix4d?rot = null)
        {
            if (min.IsNaN() || min.IsInfinite() || max.IsNaN() || max.IsInfinite())
            {
                SysConsole.Output(OutputType.WARNING, "Invalid line box from " + min + " to " + max);
                SysConsole.Output(OutputType.DEBUG, Environment.StackTrace);
                return;
            }
            GL.ActiveTexture(TextureUnit.Texture0);
            TEngine.White.Bind();
            GraphicsUtil.CheckError("RenderLineBox: BindTexture");
            Location halfsize = (max - min) * 0.5;

            if ((min + halfsize) == Location.Zero)
            {
                return; // ???
            }
            if (Math.Abs(min.X + halfsize.X) < 1 || Math.Abs(min.Y + halfsize.Y) < 1 || Math.Abs(min.Z + halfsize.Z) < 1)
            {
                return; // ???
            }
            if (Math.Abs(min.X) < 1 || Math.Abs(min.Y) < 1 || Math.Abs(min.Z) < 1)
            {
                return; // ???
            }
            Matrix4d mat = Matrix4d.Scale(halfsize.ToOpenTK3D())
                           * (rot != null && rot.HasValue ? rot.Value : Matrix4d.Identity)
                           * Matrix4d.CreateTranslation((min + halfsize).ToOpenTK3D());

            view.SetMatrix(2, mat); // TODO: Client reference!
            GraphicsUtil.CheckError("RenderLineBox: SetMatrix");
            GL.BindVertexArray(Box.Internal.VAO);
            GraphicsUtil.CheckError("RenderLineBox: Bind VAO");
            GL.DrawElements(PrimitiveType.Lines, 24, DrawElementsType.UnsignedInt, IntPtr.Zero);
            GraphicsUtil.CheckError("RenderLineBox: Pass");
        }
        /// <summary>
        /// Compiles a VertexShader and FragmentShader to a usable shader program.
        /// </summary>
        /// <param name="VS">The input VertexShader code.</param>
        /// <param name="FS">The input FragmentShader code.</param>
        /// <param name="vars">All variables to include.</param>
        /// <param name="geom">The input GeometryShader code, if any.</param>
        /// <returns>The internal OpenGL program ID.</returns>
        public int CompileToProgram(string VS, string FS, string[] vars, string geom)
        {
            if (vars.Length > 0)
            {
                reusableDefValues.Clear();
                for (int i = 0; i < vars.Length; i++)
                {
                    if (vars[i].Length > 0)
                    {
                        reusableDefValues.Add(vars[i], "1");
                    }
                }
                VS = PatchDefs(VS, reusableDefValues);
                FS = PatchDefs(FS, reusableDefValues);
                if (geom != null)
                {
                    geom = PatchDefs(geom, reusableDefValues);
                }
            }
            int gObj = -1;

            if (geom != null)
            {
                gObj = GL.CreateShader(ShaderType.GeometryShader);
                GL.ShaderSource(gObj, geom);
                GL.CompileShader(gObj);
                string GS_Info = GL.GetShaderInfoLog(gObj);
                GL.GetShader(gObj, ShaderParameter.CompileStatus, out int GS_Status);
                if (GS_Status != 1)
                {
                    throw new Exception("Error creating GeometryShader. Error status: " + GS_Status + ", info: " + GS_Info);
                }
            }
            int VertexObject = GL.CreateShader(ShaderType.VertexShader);

            GL.ShaderSource(VertexObject, VS);
            GL.CompileShader(VertexObject);
            string VS_Info = GL.GetShaderInfoLog(VertexObject);

            GL.GetShader(VertexObject, ShaderParameter.CompileStatus, out int VS_Status);
            if (VS_Status != 1)
            {
                throw new Exception("Error creating VertexShader. Error status: " + VS_Status + ", info: " + VS_Info);
            }
            int FragmentObject = GL.CreateShader(ShaderType.FragmentShader);

            GL.ShaderSource(FragmentObject, FS);
            GL.CompileShader(FragmentObject);
            string FS_Info = GL.GetShaderInfoLog(FragmentObject);

            GL.GetShader(FragmentObject, ShaderParameter.CompileStatus, out int FS_Status);
            if (FS_Status != 1)
            {
                throw new Exception("Error creating FragmentShader. Error status: " + FS_Status + ", info: " + FS_Info);
            }
            int Program = GL.CreateProgram();

            GL.AttachShader(Program, FragmentObject);
            GL.AttachShader(Program, VertexObject);
            if (geom != null)
            {
                GL.AttachShader(Program, gObj);
            }
            GL.LinkProgram(Program);
            string str = GL.GetProgramInfoLog(Program);

            if (str.Length != 0)
            {
                SysConsole.Output(OutputType.INFO, "Linked shader with message: '" + str + "'" + " -- FOR: variables: " + string.Join(",", vars));
            }
            GL.DeleteShader(FragmentObject);
            GL.DeleteShader(VertexObject);
            if (geom != null)
            {
                GL.DeleteShader(gObj);
            }
            GraphicsUtil.CheckError("Shader - Compile");
            return(Program);
        }