Fixed framerate rendering.

This commit is contained in:
the_fiddler 2008-04-13 18:43:15 +00:00
parent 4eca16d79f
commit 805f2373e6
2 changed files with 42 additions and 42 deletions

View file

@ -34,7 +34,8 @@ namespace Examples.Tutorial
{
}
#region private Fields
#region Private Fields
// GLSL Objects
int VertexShaderObject, FragmentShaderObject, ProgramObject;
int TextureObject;
@ -51,16 +52,21 @@ namespace Examples.Tutorial
float UniformScaleFactorY; // fractal vertical scaling is only affected by window resize
float UniformOffsetX = 1.8f; // fractal horizontal offset
float UniformOffsetY = 1.8f; // fractal vertical offset
// Text drawing (for fps)
TextPrinter printer = new TextPrinter();
TextureFont font = new TextureFont(new Font(FontFamily.GenericSansSerif, 14.0f));
#endregion private Fields
#region OnLoad
/// <summary>
/// Setup OpenGL and load resources here.
/// </summary>
/// <param name="e">Not used.</param>
public override void OnLoad(EventArgs e)
{
// Check for necessary capabilities:
if (!GL.SupportsExtension("VERSION_2_0"))
{
@ -69,7 +75,7 @@ namespace Examples.Tutorial
this.Exit();
}
this.VSync = VSyncMode.Off;
this.VSync = VSyncMode.On;
GL.Disable(EnableCap.Dither);
GL.ClearColor(0.2f, 0f, 0.4f, 0f);
@ -164,13 +170,12 @@ namespace Examples.Tutorial
{
GL.DeleteTextures(1, ref TextureObject);
GL.DeleteProgram(ProgramObject); // implies deleting the previously flagged ShaderObjects
base.OnUnload(e);
}
#endregion
#region OnResize
/// <summary>
/// Respond to resize events here.
/// </summary>
@ -178,7 +183,7 @@ namespace Examples.Tutorial
/// <remarks>There is no need to call the base implementation.</remarks>
protected override void OnResize(OpenTK.Platform.ResizeEventArgs e)
{
// magic numbers so the fractal almost fits inside the window.
// Magic numbers so the fractal almost fits inside the window.
// If changing this, also change the -1.6f offset in the fragment shader accordingly.
UniformScaleFactorX = Width / (UniformOffsetX * 2f);
UniformScaleFactorY = Height / (UniformOffsetY * 2f);
@ -187,12 +192,10 @@ namespace Examples.Tutorial
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(-1.0, 1.0, -1.0, 1.0, 0.0, 0.1); // 2D setup
GL.Ortho(-1.0, 1.0, -1.0, 1.0, 0.0, 1.0); // 2D setup
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
base.OnResize(e);
}
#endregion
@ -225,9 +228,12 @@ namespace Examples.Tutorial
/// <remarks>There is no need to call the base implementation.</remarks>
public override void OnRenderFrame(RenderFrameEventArgs e)
{
this.Title = "FPS: " + 1 / e.Time;
//this.Title = "FPS: " + 1 / e.Time;
GL.Clear(ClearBufferMask.ColorBufferBit);
// First, render the next frame of the Julia fractal.
GL.UseProgram(ProgramObject);
// advance the animation by elapsed time, scaling is solely used to make the anim more interesting
AnimOffsetX += (float)(e.Time * AnimSpeedX);
AnimOffsetY += (float)(e.Time * AnimSpeedY);
@ -246,14 +252,21 @@ namespace Examples.Tutorial
// Fullscreen quad. Using immediate mode, since this app is fragment shader limited anyways.
GL.Begin(BeginMode.Quads);
{
GL.Vertex3(-1.0f, -1.0f, 0f);
GL.Vertex3(1.0f, -1.0f, 0f);
GL.Vertex3(1.0f, 1.0f, 0f);
GL.Vertex3(-1.0f, 1.0f, 0f);
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();
this.SwapBuffers();
// Then, render the fps:
GL.UseProgram(0);
printer.Begin();
GL.Color3(Color.PaleGoldenrod);
printer.Draw((1 / e.Time).ToString("F2"), font);
printer.End();
SwapBuffers();
}
#endregion
@ -266,13 +279,10 @@ namespace Examples.Tutorial
[STAThread]
public static void Main()
{
using (T11_Julia_Set example = new T11_Julia_Set())
{
// Get the title and category of this example using reflection.
ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]);
example.Title = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title);
example.Run(30.0, 60.0);
Utilities.SetWindowTitle(example);
example.Run(30.0);
}
}

View file

@ -9,13 +9,11 @@ using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Diagnostics;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using OpenTK.Graphics.OpenGL.Enums;
using System.Diagnostics;
namespace Examples.Tutorial
{
@ -26,12 +24,10 @@ namespace Examples.Tutorial
public class Text : GameWindow
{
TextureFont serif = new TextureFont(new Font(FontFamily.GenericSerif, 24.0f));
TextureFont sans = new TextureFont(new Font(FontFamily.GenericSansSerif, 14.0f));
TextHandle poem_handle;
ITextPrinter text = new TextPrinter();
public Text() : base(800, 600)
{ }
string poem = new StreamReader("Data/Poem.txt").ReadToEnd();
int lines; // How many lines the poem contains.
@ -40,6 +36,10 @@ namespace Examples.Tutorial
float warparound_position;
float current_position;
public Text()
: base(800, 600)
{ }
#region OnLoad
public override void OnLoad(EventArgs e)
@ -48,7 +48,6 @@ namespace Examples.Tutorial
current_position = initial_position;
scroll_speed = -1.0f;
text.Prepare(poem, serif, out poem_handle);
// Count the amount of lines in the text, to find out the correct
@ -69,8 +68,8 @@ namespace Examples.Tutorial
public override void OnUnload(EventArgs e)
{
poem_handle.Dispose();
serif.Dispose();
if (poem_handle != null) poem_handle.Dispose();
if (serif != null) serif.Dispose();
}
#endregion
@ -120,28 +119,19 @@ namespace Examples.Tutorial
else if (scroll_speed < 0.0f && current_position < warparound_position)
current_position = initial_position;
TextHandle t = null;
text.Prepare((1.0 / e.Time).ToString(), serif, out t);
// TextPrinter.Begin() sets up a 2d orthographic projection, with the x axis
// moving from 0 to viewport.Width (left to right) and the y axis from
// 0 to viewport.Height (top to bottom). This is the typical coordinate system
// used in 2d graphics, and is necessary for achieving pixel-perfect glyph rendering.
// TextPrinter.End() restores your previous projection/modelview matrices.
text.Begin();
using (t)
{
//text.Begin();
text.Draw(t);
//text.End();
}
GL.Color3(Color.PaleGoldenrod);
text.Draw((1.0 / e.Time).ToString("F2"), sans);
GL.Translate(0.0f, current_position, 0.0f);
GL.Color3(Color.White);
text.Draw(poem_handle);
text.End();
SwapBuffers();
}