Esempio n. 1
0
        public override void Draw(float Dt)
        {
            ShaderUniforms.Camera = Engine.Camera3D;
            ShaderUniforms.Model  = Matrix4x4.CreateRotationZ((float)-Math.PI);
            Gfx.EnableDepthDest(false);
            Gfx.EnableCullFace(false);

            lock (VertsMeshLock) {
                VertsMesh.SetVertices(ProcessedVerts, ProcessedCount);
                // VertsMesh.SetVertices(ProcessedCount, ProcessedVerts);
            }

            LegShader.Bind();
            VertsMesh.Draw();
            LegShader.Unbind();

            ////////

            Vector2 MousePos = (Engine.Window.WindowSize.GetHeight() - Engine.Window.MousePos) * new Vector2(-1, 1);

            CursorWorldPos = Engine.Camera3D.ScreenToWorldDirection(MousePos, ShaderUniforms.Model) * PickDistance.Value;

            ShaderUniforms.Camera = Engine.Camera2D;
            ShaderUniforms.Model  = Matrix4x4.Identity;
            Gfx.Point(new Vertex2(MousePos, (CollisionSamples > PickSampleNum.Value) ? Color.Red : Color.Green), 5);
        }
        static void Draw(float Dt)
        {
            WorldSurface.Draw(Default);

            if (Program.RenderVoxels)
            {
                ShaderUniforms.Model = Matrix4x4.CreateTranslation(-Program.WorldOrigin);
                DefaultFlatColor.Bind();
                Voxels.VoxMesh.Draw();
                DefaultFlatColor.Unbind();
            }

            Matrix4x4 TransRot = OptotrakClient.GetRotation() * OptotrakClient.GetTranslation(); // This is the transformation matrix for rendering of the plane

            ShaderUniforms.Model = Matrix4x4.CreateScale(10) * TransRot;                         // Translates, rotates and scales the plane to appropriate size.
            Default.Bind();
            Plane.Draw();
            Default.Unbind();

            if (Program.RenderPoints)
            {
                RealSenseClient.GetVerts(ref Points);
                ShaderUniforms.Model = Matrix4x4.Identity;
                DefaultFlatColor.Bind();
                Points.Draw();
                DefaultFlatColor.Unbind();
            }

            DrawPin(PinMat1, LegClient.R_Start - Program.WorldOrigin);
            DrawPin(PinMat2, LegClient.R_End - Program.WorldOrigin);

            DrawPin(PinMat1, LegClient.L_Start - Program.WorldOrigin);
            DrawPin(PinMat2, LegClient.L_End - Program.WorldOrigin);
        }
Esempio n. 3
0
 public static void DrawScreenTriangle(Shader shader, Mesh3D screenTriangle)
 {
     screenTriangle.Draw(shader);
 }
Esempio n. 4
0
 public virtual void DrawMesh(Mesh3D Mesh)
 {
     BeginDraw(0);
     Mesh.Draw();
     EndDraw(0);
 }
Esempio n. 5
0
        //static OpenGLDevice NuklearDev;

        static void Main(string[] args)
        {
            /*IBWFile F = IBWLoader.Load("dataset/ibw/Image0018.ibw");
             *
             * for (int i = 0; i < F.Depth; i++) {
             *      Bitmap Bmp = new Bitmap(F.Width, F.Height);
             *
             *      int D_Dim = i;
             *
             *      float Min = float.MaxValue;
             *      float Max = float.MinValue;
             *
             *      for (int y = 0; y < F.Height; y++) {
             *              for (int x = 0; x < F.Width; x++) {
             *                      float Flt = (float)F.GetData(x, y, D_Dim) * 1000000000;
             *
             *                      Min = Math.Min(Min, Flt);
             *                      Max = Math.Max(Max, Flt);
             *              }
             *      }
             *
             *      float Offset = -Min;
             *      float OffsetMax = Max + Offset;
             *      float ScaleVal = 255.0f / OffsetMax;
             *
             *      Min = float.MaxValue;
             *      Max = float.MinValue;
             *
             *      for (int y = 0; y < F.Height; y++) {
             *              for (int x = 0; x < F.Width; x++) {
             *                      float Flt = ((float)F.GetData(x, y, D_Dim) * 1000000000 + Offset) * ScaleVal;
             *
             *                      int Clr = (int)Flt;
             *                      Bmp.SetPixel(x, F.Height - y - 1, System.Drawing.Color.FromArgb(255, Clr, Clr, Clr));
             *              }
             *      }
             *
             *      Bmp.Save("test_" + i + ".png");
             * }
             *
             *
             * File.WriteAllText("text_data.txt", F.NoteData);
             *
             *
             * Console.WriteLine("Done!");
             * Console.ReadLine();
             * return;//*/



            const float Scale = 0.9f;

            ClearColor = new FishGfx.Color(60, 80, 100);

            RenderAPI.GetDesktopResolution(out int W, out int H);
            RWind = new RenderWindow((int)(W * Scale), (int)(H * Scale), "Vector PFM");

            Console.WriteLine("OpenGL {0}", RenderAPI.Version);
            Console.WriteLine("Running on {0}", RenderAPI.Renderer);

            // Load shader programs
            Shader_DrawRayCast = new ShaderProgram(new ShaderStage(ShaderType.VertexShader, "data/default3d.vert"),
                                                   new ShaderStage(ShaderType.FragmentShader, "data/defaultRayCast.frag"));
            Shader_DrawRayCast.Uniforms.Camera.SetPerspective(RWind.GetWindowSizeVec());
            Shader_DrawRayCast.Uniforms.Camera.Position   = new Vector3(0, 300, 0);
            Shader_DrawRayCast.Uniforms.Camera.PitchClamp = new Vector2(-80, 80);

            Shader_DrawFlat = new ShaderProgram(new ShaderStage(ShaderType.VertexShader, "data/default3d.vert"),
                                                new ShaderStage(ShaderType.FragmentShader, "data/defaultFlatColor.frag"));
            Shader_DrawFlat.Uniforms.Camera = Shader_DrawRayCast.Uniforms.Camera;

            Shader_Screen = new ShaderProgram(new ShaderStage(ShaderType.VertexShader, "data/default.vert"),
                                              new ShaderStage(ShaderType.FragmentShader, "data/default.frag"));
            Shader_Screen.Uniforms.Camera.SetOrthogonal(0, 0, 1, 1);

            Shader_Textured = new ShaderProgram(new ShaderStage(ShaderType.VertexShader, "data/default3d.vert"),
                                                new ShaderStage(ShaderType.FragmentShader, "data/default.frag"));
            Shader_Textured.Uniforms.Camera = Shader_DrawRayCast.Uniforms.Camera;

            //NuklearDev = new OpenGLDevice(RWind.GetWindowSizeVec());
            //NuklearAPI.Init(NuklearDev);

            RWind.OnMouseMoveDelta += (Wnd, X, Y) => Shader_DrawRayCast.Uniforms.Camera.Update(new Vector2(-X, -Y));
            RWind.OnKey            += OnKey;
            //RWind.OnMouseMove += (Wnd, X, Y) => NuklearDev.OnMouseMove((int)X, (int)Y);

            RWind.OnMouseMoveDelta += (Wnd, X, Y) => {
                if (LeftMouse)
                {
                    const float MoveSpeed = 1.0f;

                    if (X != 0 || Y != 0)
                    {
                        Camera Cam = Shader_DrawRayCast.Uniforms.Camera;

                        if (X != 0)
                        {
                            Cam.Position -= Cam.WorldRightNormal * MoveSpeed * -X;
                        }

                        if (Y != 0)
                        {
                            Cam.Position += Cam.WorldUpNormal * MoveSpeed * -Y;
                        }

                        RecalcCamera();
                    }
                }
                else if (RightMouse)
                {
                    DesiredPivotDistance += Y;
                    RecalcCamera();
                }
            };

            HMap = new Terrain();
            HMap.LoadFromImage(Image.FromFile("dataset/data2/heightmap.png"), 100);
            //HMap.LoadFromImage(Image.FromFile("dataset/height_test.png"), 10);
            //HMap.LoadFromImage(Image.FromFile("dataset/owl.png"), 100);


            DesiredPivotDistance = float.PositiveInfinity;
            CameraPivot          = new Vector3(HMap.Width / 2, HMap.GetHeight(HMap.Width / 2, HMap.Height / 2), HMap.Height / 2);
            RecalcCamera();

            PinMesh = new Mesh3D {
                PrimitiveType = PrimitiveType.Triangles
            };
            PinMesh.SetVertices(GfxUtils.LoadObj("data/models/pin/pin.obj"));

            PinTexture = Texture.FromFile("data/models/pin/pin_mat.png");

            Mesh3D Vectors = new Mesh3D {
                PrimitiveType = PrimitiveType.Lines
            };

            {
                //Vertex3[] Verts = new Vertex3[HMap.Width * HMap.Height * 2];
                List <Vertex3> Verts = new List <Vertex3>();

                for (int i = 0; i < HMap.Width * HMap.Height * 2; i += 2)
                {
                    int X = (i / 2) % HMap.Width;
                    int Y = (i / 2) / HMap.Width;

                    const int Density = 2;
                    if (X % Density != 0 || Y % Density != 0)
                    {
                        continue;
                    }

                    float Height = HMap.GetHeight(X, Y);

                    Verts.Add(new Vertex3(new Vector3(X, Height - 0.5f, Y), FishGfx.Color.Black));
                    Verts.Add(new Vertex3(new Vector3(X, Height + 20, Y), FishGfx.Color.White));
                }

                Vectors.SetVertices(Verts.ToArray());
            }

            RWind.GetWindowSize(out int WindowWidth, out int WindowHeight);
            RenderTexture Screen = new RenderTexture(WindowWidth, WindowHeight);

            RayCastingTexture = Screen.CreateNewColorAttachment(1);

            Background = Texture.FromFile("data/background.png");

            Mesh2D ScreenQuad = new Mesh2D();

            ScreenQuad.SetVertices(new Vertex2[] {
                new Vertex2(new Vector2(0, 0), new Vector2(0, 0)),
                new Vertex2(new Vector2(0, 1), new Vector2(0, 1)),
                new Vertex2(new Vector2(1, 1), new Vector2(1, 1)),
                new Vertex2(new Vector2(1, 0), new Vector2(1, 0))
            });
            ScreenQuad.SetElements(new uint[] { 0, 1, 2, 0, 2, 3 }.Reverse().ToArray());

            Stopwatch SWatch = Stopwatch.StartNew();
            float     Dt     = 0;

            while (!RWind.ShouldClose)
            {
                Update(Dt);

                // Draw the world onto a render texture including the ray casting buffer
                Screen.Bind(0, 1);
                {
                    Shader_DrawRayCast.Bind();
                    Gfx.Clear(FishGfx.Color.Transparent);

                    /*Gfx.EnableCullFace(false);
                     * HMap.Draw();
                     * Gfx.EnableCullFace(true);*/

                    // Draw back face
                    Gfx.CullFront();
                    Texture Orig = HMap.OverlayTexture;
                    HMap.OverlayTexture = Background;
                    HMap.Draw();

                    // Draw front face
                    Gfx.CullBack();
                    HMap.OverlayTexture = Orig;
                    HMap.Draw();

                    Shader_DrawRayCast.Unbind();
                }
                Screen.Unbind();

                // Draw other stuff
                Screen.Bind(0);
                {
                    Shader_DrawFlat.Bind();
                    Vectors.Draw();
                    Shader_DrawFlat.Unbind();

                    Shader_Textured.Bind();
                    Shader_Textured.SetModelMatrix(Matrix4x4.CreateScale(2) * Matrix4x4.CreateTranslation(CameraPivot));
                    PinTexture.BindTextureUnit();
                    PinMesh.Draw();
                    PinTexture.UnbindTextureUnit();
                    Shader_Textured.Unbind();
                }
                Screen.Unbind();

                // Draw render texture and GUI to screen
                Shader_Screen.Bind();
                {
                    Gfx.Clear(ClearColor);
                    Gfx.EnableDepthDest(false);

                    if (FunctionMode == 1)
                    {
                        Screen.Color.BindTextureUnit();
                    }
                    else if (FunctionMode == 2)
                    {
                        RayCastingTexture.BindTextureUnit();
                    }

                    ScreenQuad.Draw();
                    //NuklearAPI.Frame(DrawGUI);
                    Gfx.EnableDepthDest(true);
                }
                Shader_Screen.Unbind();

                // Swap buffers, do magic
                RWind.SwapBuffers();
                Events.Poll();

                while (SWatch.ElapsedMilliseconds / 1000.0f < 1.0f / 60.0f)
                {
                    ;
                }
                Dt = SWatch.ElapsedMilliseconds / 1000.0f;
                SWatch.Restart();
            }
        }
Esempio n. 6
0
        public override void DrawMesh(Mesh3D Mesh)
        {
            RenderAPI.DbgPushGroup("Fog DrawMesh");

            RenderAPI.DbgPushGroup("Depth capture");
            DepthCapture.Push();
            {
                Gfx.Clear(Color.Transparent);
                RenderState RS = Gfx.PeekRenderState();
                RS.EnableBlend = true;
                RS.BlendFunc(BlendFactor.One, BlendFactor.One);
                RS.EnableDepthTest = false;


                RS.EnableCullFace = true;
                RS.CullFace       = CullFace.Back;
                RS.SetColorMask(true, true, false, false);

                DepthThicknessShader.Bind(ShaderUniforms.Current);

                // First pass
                RenderAPI.DbgPushGroup("Draw front face");
                Gfx.PushRenderState(RS);
                {
                    DepthThicknessShader.Uniform1f("Scale", 1.0f);
                    Mesh.Draw();
                }
                Gfx.PopRenderState();
                RenderAPI.DbgPopGroup();

                // Second pass
                RS.CullFace = CullFace.Front;
                RS.SetColorMask(true, false, false, false);
                RenderAPI.DbgPushGroup("Draw back face");
                Gfx.PushRenderState(RS);
                {
                    DepthThicknessShader.Uniform1f("Scale", -1.0f);
                    Mesh.Draw();
                }
                Gfx.PopRenderState();
                RenderAPI.DbgPopGroup();

                DepthThicknessShader.Unbind();
            }
            DepthCapture.Pop();
            RenderAPI.DbgPopGroup();

            RenderAPI.DbgPushGroup("Fog render");
            FogShader.Bind(ShaderUniforms.Current);
            ThicknessTex.BindTextureUnit();
            // Final pass
            {
                /*// Depth sort to make sure fragment shader executes only once
                 * RenderState RS = Gfx.PeekRenderState();
                 * RS.SetColorMask(false);
                 * RS.EnableDepthMask = true;
                 * Gfx.PushRenderState(RS);
                 * Mesh.Draw();
                 * Gfx.PopRenderState();
                 *
                 * // Actual shading
                 * RS.SetColorMask(true);
                 * RS.EnableDepthMask = false;
                 * RS.DepthFunc = DepthFunc.Equal;
                 * Gfx.PushRenderState(RS);
                 * Mesh.Draw();
                 * Gfx.PopRenderState();*/

                EngineRenderer.BeginDrawStencilMask(StencilMaskMode.AnyFaceDepthTested);
                Mesh.Draw();
                EngineRenderer.EndDrawStencilMask();

                EngineRenderer.BeginUseStencilMask(StencilFunction.Equal, 0xFF, 0xFF, false);
                Mesh.Draw();
                EngineRenderer.EndUseStencilMask();
            }

            ThicknessTex.UnbindTextureUnit();
            FogShader.Unbind();
            RenderAPI.DbgPopGroup();

            RenderAPI.DbgPopGroup();
        }