Beispiel #1
0
        void timer_Tick(object sender, EventArgs e)
        {
            GL.Clear(0, 0, 0);
            GL.ClearDepth();

            GL.SelectBuffer(vertexBuffer);
            GL.SelectShader(cubeShader);

            GL.Draw();

            GL.Blit();
            objectRotation = new Vector3(0, 0, rotAngle++);

            oSin             = GetSin(objectRotation);
            oCos             = GetCos(objectRotation);
            cameraPosition.y = 10f * (float)Math.Sin(rotAngle / 25.1f);
            GL.ForceCameraPosition(cameraPosition);
        }
Beispiel #2
0
        void RT_RenderFrame()
        {
            #region LightDataTimingsAndMouse
            CalculateDeltaTime();
            lightPosition = new Vector3(1000f * (float)Math.Cos(tick), 50, 1000f * (float)Math.Sin(tick));

            tick += 0.01f * deltaTimeAdjusted;
            int MouseX = 0;
            int MouseY = 0;

            #endregion


            //   GL.Line3D(new Vector3(0, 0, 0), new Vector3(100, 100, 100), 30, 30, 30);

            #region CursorPosition
            if (CursorHook)
            {
                int cursorX = Cursor.Position.X;
                int cursorY = Cursor.Position.Y;

                int sourceX = 0;
                int sourceY = 0;

                this.Invoke((Action) delegate()
                {
                    sourceX = PointToScreen(Point.Empty).X + this.ClientSize.Width / 2;
                    sourceY = PointToScreen(Point.Empty).Y + this.ClientSize.Height / 2;
                });

                MouseX = cursorX - sourceX;
                MouseY = cursorY - sourceY;

                Cursor.Position = new Point(sourceX, sourceY);
                if (!requestHome)
                {
                    cameraRotation += new Vector3(0, MouseY / 8f, MouseX / 8f);
                }
            }
            else if (mmbdown & !requestHome)
            {
                int cursorX = Cursor.Position.X;
                int cursorY = Cursor.Position.Y;

                MouseX    = cursorX - MMBDeltaX;
                MouseY    = cursorY - MMBDeltaY;
                MMBDeltaX = cursorX; MMBDeltaY = cursorY;

                cameraPosition = renderX.Pan3D(cameraPosition, cameraRotation, MouseX / 8f, MouseY / 8f);
            }
            #endregion

            #region KeyboardDeltas
            if (rdown | ldown)
            {
                if (rdown)
                {
                    if (KeyDelta.x > 0)
                    {
                        KeyDelta.x = 0;
                    }
                    KeyDelta.x--;
                }
                else if (ldown)
                {
                    if (KeyDelta.x < 0)
                    {
                        KeyDelta.x = 0;
                    }
                    KeyDelta.x++;
                }
            }
            else
            {
                KeyDelta.x = 0;
            }

            if (udown | bdown)
            {
                if (udown)
                {
                    if (KeyDelta.y > 0)
                    {
                        KeyDelta.y = 0;
                    }
                    KeyDelta.y--;
                }
                else if (bdown)
                {
                    if (KeyDelta.y < 0)
                    {
                        KeyDelta.y = 0;
                    }
                    KeyDelta.y++;
                }
            }
            else
            {
                KeyDelta.y = 0;
            }
            #endregion

            #region CameraLerpingAndPosition
            if (!requestHome)
            {
                cameraPosition = renderX.Pan3D(cameraPosition, cameraRotation, (KeyDelta.x / 32f) * deltaTimeAdjusted, 0, (KeyDelta.y / 32f) * deltaTimeAdjusted);
            }
            else
            {
                cameraPosition = Vector3.Lerp(cameraPosition, TargetPosition, 0.1f * deltaTimeAdjusted);
                cameraRotation = Vector3.LerpAngle(cameraRotation, TargetRotation, 0.1f * deltaTimeAdjusted);

                if ((cameraPosition - TargetPosition).Abs() < 0.01f && (cameraRotation - TargetRotation).Abs().Repeat(360) < 0.01f)
                {
                    requestHome = false;
                }
            }

            GL.ForceCameraRotation(cameraRotation);
            GL.ForceCameraPosition(cameraPosition);
            #endregion

            #region MatrixInterpolation
            if (Math.Abs(CMatrix - TMatrix) > 0.001f)
            {
                CMatrix = renderX.Lerp(CMatrix, TMatrix, 0.1f * deltaTimeAdjusted);
            }
            else
            {
                CMatrix = TMatrix;
            }

            GL.SetMatrixData(90, 160, CMatrix);
            MiniGL.SetMatrixData(90, 350, CMatrix);

            #endregion

            #region BufferCaching
            bool y = cameraPosition.Equals(lcP) && cameraRotation.Equals(lcR) && Math.Abs(CurrentFOV - TargetFOV) < 0.01f && CMatrix == TMatrix;
            if (!y)
            {
                readyCache = false;
            }
            #endregion

            #region Indicator, Buffer Selection, Clearing and LightData
            PrepareLightningData();
            ProcessCameraIndicator();
            GL.Clear();
            //   GL.Clear(55, 155, 255);
            GL.ClearDepth();

            GL.SelectShader(DisplayTexture);
            GL.SelectBuffer(VertexBuffer);
            #endregion

            //GL.BlitInto

            if (y & !readyCache & FBCaching)
            {
                GL.CreateCopyOnDraw(cachedBuffer);
                GL.Draw();
                readyCache = true;
                y          = false;
            }

            sw.Start();
            if (readyCache)
            {
                GL.CopyFromCache(cachedBuffer, CopyMethod.SplitLoop);
            }
            else
            {
                GL.Draw();
            }
            sw.Stop();

            //   GL.Draw(LineBuffer, LineShader);
            //   GL.Draw(CubeVBO, cubeShader);

            //  GL.Clear(255);

            #region AxesIndicator
            GL.Line3D(new Vector3(0, 0, 0), new Vector3(1000000, 0, 0), 255, 0, 0);
            GL.Line3D(new Vector3(0, 0, 0), new Vector3(0, 1000000, 0), 0, 255, 0);
            GL.Line3D(new Vector3(0, 0, 0), new Vector3(0, 0, 1000000), 0, 0, 255);
            #endregion

            //  sw.Restart();
            //    GL.VignettePass();
            //  sw.Stop();

            MiniGL.BlitInto(GL, GL.RenderWidth - 130, GL.RenderHeight - 128, Color.FromArgb(255, 0, 0, 0));

            //   GL.BlitInto(infoBitmap, new Rectangle(0, 0, 200, 200));
            //   GL.BlitFrom(infoBitmap, new Rectangle(0, 0, 40, 40), 0, 0);
            DrawText();

            GL.Blit();

            //   this.Invoke((Action)delegate() { this.Text = (sw.Elapsed.TotalMilliseconds) + " ms"; });
            this.Invoke((Action) delegate() { this.Text = (1000f / deltaTime) + " FPS, DrawTime: " + sw.Elapsed.TotalMilliseconds + "ms"; });
            //   this.Invoke((Action)delegate() { this.Text = "pos: " + cameraPosition.ToString() + ", rot: " + cameraRotation.ToString(); });


            lcR = cameraRotation;
            lcP = cameraPosition;

            sw.Reset();
        }
Beispiel #3
0
        private void Form1_Load(object sender, EventArgs e)
        {
            #region StartupWindow
            StartupForm StartForm = new StartupForm(Environment.GetCommandLineArgs());
            StartForm.ShowDialog();


            if (StartupForm.ApplicationTerminated)
            {
                this.Close();
                Application.Exit();
                return;
            }
            #endregion

            #region WindowSettings
            this.Size = new Size(StartupForm.W, StartupForm.H);
            int WindowWidth  = StartupForm.W - ClientSize.Width;
            int WindowHeight = StartupForm.H - ClientSize.Height;
            this.Size = new Size(StartupForm.W + WindowWidth, StartupForm.H + WindowHeight);
            this.SetStyle(ControlStyles.StandardDoubleClick, false);
            FBCaching        = StartupForm.NormalsInterpolated;
            this.MouseWheel += Form1_MouseWheel;
            #endregion

            #region FullscreenSettings
            if (StartupForm.FullscreenSelected)
            {
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                this.WindowState     = FormWindowState.Maximized;
                StartupForm.W        = this.Width;
                StartupForm.H        = this.Height;
            }
            #endregion



            #region VertexBufferInitialization
            STLImporter Importer = new STLImporter(StartupForm.FilePath);

            float[] vertexpoints = new float[Importer.AllTriangles.Length * 3 * 3];
            float[] normalBuffer = new float[Importer.AllTriangles.Length * 3];
            for (int i = 0; i < Importer.AllTriangles.Length; i++)
            {
                vertexpoints[i * 9]     = Importer.AllTriangles[i].vertex1.x;
                vertexpoints[i * 9 + 1] = Importer.AllTriangles[i].vertex1.y;
                vertexpoints[i * 9 + 2] = Importer.AllTriangles[i].vertex1.z;
                vertexpoints[i * 9 + 3] = Importer.AllTriangles[i].vertex2.x;
                vertexpoints[i * 9 + 4] = Importer.AllTriangles[i].vertex2.y;
                vertexpoints[i * 9 + 5] = Importer.AllTriangles[i].vertex2.z;
                vertexpoints[i * 9 + 6] = Importer.AllTriangles[i].vertex3.x;
                vertexpoints[i * 9 + 7] = Importer.AllTriangles[i].vertex3.y;
                vertexpoints[i * 9 + 8] = Importer.AllTriangles[i].vertex3.z;
                normalBuffer[i * 3]     = Importer.AllTriangles[i].normals.x;
                normalBuffer[i * 3 + 1] = Importer.AllTriangles[i].normals.y;
                normalBuffer[i * 3 + 2] = Importer.AllTriangles[i].normals.z;
            }
            #endregion

            ModelCenter = CalculateCenterOfModel(ref vertexpoints, out DistanceCenter);
            //   vertexpoints = STLImporter.AverageUpFaceNormalsAndOutputVertexBuffer(Importer.AllTriangles, 45);

            NormalBuffer = new GLBuffer(normalBuffer, 3, MemoryLocation.Heap);
            VertexBuffer = new GLBuffer(vertexpoints, 3, MemoryLocation.Heap);

            NormalBuffer = new GLBuffer(renderX.PrimitiveTypes.CubeNormals(), 3, MemoryLocation.Heap);
            VertexBuffer = new GLBuffer(renderX.PrimitiveTypes.Cube(), 5, MemoryLocation.Heap);


            //GL._DebugBlit
            StandardShader = new Shader(null, BasicShader, GLRenderMode.TriangleFlat);
            //StandardShader.SetOverrideAttributeCopy(true);
            // Skybox = new GLCubemap("skybox_data");


            DisplayTexture = new Shader(CubeShader, TextureShader, GLRenderMode.Triangle);
            //  DisplayTexture.SetOverrideAttributeCopy(false);
            //  DisplayTexture.SetOverrideAttributeCount(3);
            // GL.BlitInto

            texture2d    = new GLTexture("container2.png", MemoryLocation.Heap, DuringLoad.Flip);
            TEXTURE_ADDR = (int *)texture2d.GetAddress();

            textureHeight         = texture2d.Height;
            textureWidthMinusOne  = texture2d.Width - 1;
            textureHeightMinusOne = texture2d.Height - 1;


            #region MemoryAddresses
            nbAddr       = (float *)NormalBuffer.GetAddress();
            vertexpoints = null;

            #endregion

            #region CubeObject
            CubeVBO    = new GLBuffer(renderX.PrimitiveTypes.Cube(), 5, MemoryLocation.Heap);
            cubeShader = new Shader(CubeVS, CubeFS, GLRenderMode.TriangleFlat);
            cubeShader.SetOverrideAttributeCount(0);
            #endregion

            #region 3D_Grid
            float[] vpoints = new float[] {
                0, 0, 0, 9, 0, 0,
                0, 0, 1, 9, 0, 1,
                0, 0, 2, 9, 0, 2,
                0, 0, 3, 9, 0, 3,
                0, 0, 4, 9, 0, 4,
                0, 0, 5, 9, 0, 5,
                0, 0, 6, 9, 0, 6,
                0, 0, 7, 9, 0, 7,
                0, 0, 8, 9, 0, 8,
                0, 0, 9, 9, 0, 9,

                0, 0, 0, 0, 0, 9,
                1, 0, 0, 1, 0, 9,
                2, 0, 0, 2, 0, 9,
                3, 0, 0, 3, 0, 9,
                4, 0, 0, 4, 0, 9,
                5, 0, 0, 5, 0, 9,
                6, 0, 0, 6, 0, 9,
                7, 0, 0, 7, 0, 9,
                8, 0, 0, 8, 0, 9,
                9, 0, 0, 9, 0, 9
            };

            LineBuffer = new GLBuffer(vpoints, 3, MemoryLocation.Heap);
            LineShader = new Shader(GridShaderVS, GridShaderFS, GLRenderMode.Line, GLExtraAttributeData.XYZ_CameraSpace);

            #endregion

            #region CameraIndicator
            CameraIndicator = new Shader(CIVS, null, GLRenderMode.TriangleGouraud);
            CameraIndicator.SetOverrideCameraTransform(true);
            CameraIndicator.SetOverrideAttributeCopy(true);

            //Inner Part
            CICVertex  = new GLBuffer(renderX.PrimitiveTypes.CMI(), 3, MemoryLocation.Heap);
            CICNormals = new GLBuffer(renderX.PrimitiveTypes.CMINormals(), 3, MemoryLocation.Heap);

            //Outer Part
            CIPVertex  = new GLBuffer(renderX.PrimitiveTypes.CMS(), 3, MemoryLocation.Heap);
            CIPNormals = new GLBuffer(renderX.PrimitiveTypes.CMSNormals(), 3, MemoryLocation.Heap);

            CIC_Normals = (float *)CICNormals.GetAddress();
            CIP_Normals = (float *)CIPNormals.GetAddress();
            #endregion

            #region Post-Processing
            SSRShader = new Shader(null, SSR_Fragment, GLRenderMode.Triangle, GLExtraAttributeData.XYZ_XY_Both);

            SSRShaderPost = new Shader(SSR_Pass);
            #endregion

            #region MiniGLIndicator
            MiniGL = new renderX(130, 128);
            MiniGL.SetMatrixData(90, 10);
            MiniGL.SelectShader(CameraIndicator);
            MiniGL.SetFaceCulling(true, false);
            MiniGL.InitializeClickBuffer();
            MiniGL.SetClickBufferWrite(true);
            #endregion

            #region renderX_Initialization
            GL = new renderX(StartupForm.W, StartupForm.H, this.Handle);
            GL.SelectBuffer(VertexBuffer);
            GL.SelectShader(StandardShader);
            GL.SetMatrixData(90, 10);


            #endregion

            #region GLSettings
            cachedBuffer = new GLCachedBuffer(GL);

            GL.SetWireFrameOFFSET(-0.1f);
            GL.SetFaceCulling(true, false);

            #endregion

            //  GL.SetLinkedWireframe(true, 255, 255, 255);
            //  GL.SetViewportScaling(1920, 1080, InterpolationMethod.NearestNeighbour);
            GL.SetDebugWireFrameColor(255, 255, 255);
            // GL.SetLineAntiAliasing(true);
            GL.SetLineAntiAliasing(true);
            GL.SetLineThickness(1);
            // GL.SetLineThickness(3);
            //   GL.SetWireFrameOFFSET(-5000f);

            //    GL.SetLineAntiAliasing(true);

            //  GL.SetLinkedWireframe(true, 0, 0, 255);
            GL.InitializeVignetteBuffer(VignetteShader);

            //  cameraPosition = new Vector3(-1.16f, 18f, -33.5f);
            //   cameraRotation = new Vector3(0,0,90);

            cameraPosition = new Vector3(0, 0, -50);
            cameraRotation = new Vector3(0, 0, 0);


            #region RenderThreadStart
            RT              = new RenderThread(144);
            RT.RenderFrame += RT_RenderFrame;
            RT.Start();
            #endregion
        }