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

View file

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