protected override void OnRenderFrame(FrameEventArgs e) { // called once per frame; render GL.ClearColor(Color.Black); GL.Enable(EnableCap.Texture2D); GL.Disable(EnableCap.DepthTest); GL.Color3(1.0f, 1.0f, 1.0f); sw.Stop(); if (sw.ElapsedMilliseconds != 0) { Console.WriteLine(1 / (sw.ElapsedMilliseconds / 1000f)); } sw.Reset(); game.Tick(); sw.Start(); if (terminated) { Exit(); return; } // convert Game.screen to OpenGL texture GL.BindTexture(TextureTarget.Texture2D, screenID); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, game.screen.width, game.screen.height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, game.screen.pixels ); // clear window contents GL.Clear(ClearBufferMask.ColorBufferBit); // setup camera GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); // draw screen filling quad GL.Begin(PrimitiveType.Quads); GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(-1.0f, -1.0f); GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(1.0f, -1.0f); GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(1.0f, 1.0f); GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(-1.0f, 1.0f); GL.End(); // prepare for generic OpenGL rendering GL.Enable(EnableCap.DepthTest); GL.Disable(EnableCap.Texture2D); GL.Clear(ClearBufferMask.DepthBufferBit); game.RenderGL(); // tell OpenTK we're done rendering SwapBuffers(); }
protected override void OnRenderFrame(FrameEventArgs e) { // prepare for generic OpenGL rendering GL.ClearColor(Color.Black); GL.Enable(EnableCap.DepthTest); GL.Disable(EnableCap.Texture2D); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.LoadIdentity(); GL.Color3(1.0f, 1.0f, 1.0f); // called once per frame; render game.Tick(); if (terminated) { Exit(); return; } // convert Game.screen to OpenGL texture //GL.BindTexture( TextureTarget.Texture2D, screenID ); //GL.TexImage2D( TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, //game.screen.width, game.screen.height, 0, //OpenTK.Graphics.OpenGL.PixelFormat.Bgra, // PixelType.UnsignedByte, game.screen.pixels // ); // clear window contents GL.Clear(ClearBufferMask.ColorBufferBit); // setup camera GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); // draw screen filling quad /* * GL.Color3(0f, 0f, 0f); * GL.Begin( PrimitiveType.Quads ); * GL.Vertex2( -1.0f, -1.0f ); * GL.Vertex2( 1.0f, -1.0f ); * GL.Vertex2( 1.0f, 1.0f ); * GL.Vertex2( -1.0f, 1.0f ); * GL.End(); */ //Run the game GL game.RenderGL(); // tell OpenTK we're done rendering SwapBuffers(); }
protected override void OnRenderFrame(FrameEventArgs e) { game.RenderGL(); if (Game.Recording) { SaveImage(); } Vector3[] colours = new Vector3[game.vertexBuffer.Length]; for (int i = 0; i < game.vertexBuffer.Length; i++) { if (i < game.boundsVertices.Length + game.vertices.Length) { if (Game.displayMode == Game.Mode.SHAPES) { if (game.sceneNumber == 2) { colours[i] = new Vector3(MathHelper.Clamp(game.vertexBuffer[i].Y * 4, 0, 1), MathHelper.Clamp(game.vertexBuffer[i].Y * 4, 0, 1), 1.0f); } else if (game.sceneNumber == 4) { colours[i] = new Vector3(MathHelper.Clamp(game.vertexBuffer[i].Y * 6, 0, 1), MathHelper.Clamp(game.vertexBuffer[i].Y * 6, 0, 1), 1.0f); } else { colours[i] = new Vector3(0, 0, 1.0f); } } else { colours[i] = new Vector3(1.0f, 1.0f, 1.0f); } } else { colours[i] = new Vector3(0.9f, 0.9f, 0.9f); } } int positionVboHandle = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle); GL.BufferData <Vector3>(BufferTarget.ArrayBuffer, new IntPtr(game.vertexBuffer.Length * Vector3.SizeInBytes), game.vertexBuffer, BufferUsageHint.StaticDraw); int normVboHandle = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, normVboHandle); GL.BufferData <Vector3>(BufferTarget.ArrayBuffer, new IntPtr(game.normalsBuffer.Length * Vector3.SizeInBytes), game.normalsBuffer, BufferUsageHint.StaticDraw); int colVboHandle = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, colVboHandle); GL.BufferData <Vector3>(BufferTarget.ArrayBuffer, new IntPtr(colours.Length * Vector3.SizeInBytes), colours, BufferUsageHint.StaticDraw); int attribute_vpos = GL.GetAttribLocation(shaderProgramHandle, "in_position"); int attribute_norm = GL.GetAttribLocation(shaderProgramHandle, "in_normal"); int attribute_col = GL.GetAttribLocation(shaderProgramHandle, "in_colour"); GL.GenVertexArrays(2, out vaoHandle2); GL.BindVertexArray(vaoHandle2); GL.EnableVertexAttribArray(attribute_norm); GL.BindBuffer(BufferTarget.ArrayBuffer, normVboHandle); GL.VertexAttribPointer(attribute_norm, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0); GL.EnableVertexAttribArray(attribute_vpos); GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle); GL.VertexAttribPointer(attribute_vpos, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0); GL.EnableVertexAttribArray(attribute_col); GL.BindBuffer(BufferTarget.ArrayBuffer, colVboHandle); GL.VertexAttribPointer(attribute_col, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0); GL.Viewport(0, 0, Width, Height); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.BindVertexArray(vaoHandle2); GL.UniformMatrix4(projectionMatrixLocation, false, ref projectionMatrix); GL.UniformMatrix4(modelviewMatrixLocation, false, ref modelviewMatrix); GL.DrawArrays(PrimitiveType.Lines, 0, game.boundsVertices.Length); if (Game.displayMode == Game.Mode.PARTICLES) { GL.DrawArrays(PrimitiveType.Points, game.boundsVertices.Length, game.vertices.Length); GL.DrawArrays(PrimitiveType.Triangles, game.boundsVertices.Length + game.vertices.Length, game.cubeVertices.Length); } else if (Game.displayMode == Game.Mode.SHAPES) { GL.DrawArrays(PrimitiveType.Triangles, game.boundsVertices.Length, game.vertices.Length); GL.DrawArrays(PrimitiveType.Triangles, game.boundsVertices.Length + game.vertices.Length, game.cubeVertices.Length); } SwapBuffers(); }