mcb_'s picture

Render speed increasing linearly from start up but no error

Hello,
When i run my code its render time decreasing constantly from start up , i could not find any reason.

Please run my code and see result. Also when you resize window same thing happens.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenTK;
using OpenTK.Platform;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using System.Threading;
 
namespace AwsTest
{
    public partial class MainForm : Form
    {
        IntPtr windowHandle;
        volatile bool KillRenderThread;
        Thread oThread=null;
        IGraphicsContext context;
        IWindowInfo winfo;
        int Render_width, Render_height;
 
        public MainForm()
        {
            InitializeComponent();
            //oThread = new Thread(new ThreadStart(RenderToPanel));
        }
 
        private void MainForm_Load(object sender, EventArgs e)
        {
 
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
 
        }
 
        private void splitContainer1_Panel2_HandleCreated(object sender, EventArgs e)
        {
            splitContainer1.Panel2.Invoke(new MethodInvoker(delegate { windowHandle = splitContainer1.Panel2.Handle; }));
            Render_width = splitContainer1.Panel2.Width;
            Render_height = splitContainer1.Panel2.Height;
        }
 
        public void RenderToPanel()
        {
            //splitContainer1.Panel2.Invoke(new MethodInvoker(delegate { windowHandle = splitContainer1.Panel2.Handle; }));
            winfo = Utilities.CreateWindowsWindowInfo(windowHandle); // Similar methods exist for X11 and Carbon
            context = new GraphicsContext(GraphicsMode.Default, winfo);
            context.MakeCurrent(winfo);
            context.LoadAll();
            float alpha = 0;
            GL.MatrixMode(MatrixMode.Projection);
            GL.Ortho(0, Render_width,0, Render_height, -100.0f, 10.0f);
            GL.LoadIdentity();
            while (!KillRenderThread)
            {
 
                GL.Clear(ClearBufferMask.ColorBufferBit);
                //GL.LoadIdentity();
 
               /* GL.Ortho(-1.0, 1.0, -1.0, 1.0, 0.0, 4.0);
                GL.Begin(PrimitiveType.Triangles);
                GL.Color3(Color.MidnightBlue);
                GL.Vertex2(-1.0f, 1.0f);
                GL.Color3(Color.SpringGreen);
                GL.Vertex2(0.0f, -1.0f);
                GL.Color3(Color.Ivory);
                GL.Vertex2(1.0f, 1.0f);*/
 
                GL.Translate(0.0f, 0.0f, 0.0f);
                GL.Rotate(0.0, 0.0, 0.0, 30.0);
                GL.Rotate(alpha, 0.0, 1.0, 1.0);
                GL.Begin(PrimitiveType.Quads);
 
                GL.Color3(0.0f, 0.5f, 0.1f);
                GL.Vertex3(0.2f, 0.2f, -0.2f);
                GL.Vertex3(-0.2f, 0.2f, -0.2f);
                GL.Vertex3(-0.2f, 0.2f, 0.2f);
                GL.Vertex3(0.2f, 0.2f, 0.2f);
 
 
                GL.Color3(0.0f, 0.4f, 0.0f);
                GL.Vertex3(0.2f, -0.2f, 0.2f);
                GL.Vertex3(-0.2f, -0.2f, 0.2f);
                GL.Vertex3(-0.2f, -0.2f, -0.2f);
                GL.Vertex3(0.2f, -0.2f, -0.2f);
 
                GL.Color3(0.0f, 0.2f, 0.7f);
                GL.Vertex3(0.2f, 0.2f, 0.2f);
                GL.Vertex3(-0.2f, 0.2f, 0.2f);
                GL.Vertex3(-0.2f, -0.2f, 0.2f);
                GL.Vertex3(0.2f, -0.2f, 0.2f);
 
                GL.Color3(0.0f, 0.0f, 0.5f);
                GL.Vertex3(0.2f, -0.2f, -0.2f);
                GL.Vertex3(-0.2f, -0.2f, -0.2f);
                GL.Vertex3(-0.2f, 0.2f, -0.2f);
                GL.Vertex3(0.2f, 0.2f, -0.2f);
 
                GL.Color3(0.8f, 0.1f, 0.2f);
                GL.Vertex3(-0.2f, 0.2f, 0.2f);
                GL.Vertex3(-0.2f, 0.2f, -0.2f);
                GL.Vertex3(-0.2f, -0.2f, -0.2f);
                GL.Vertex3(-0.2f, -0.2f, 0.2f);
 
 
                GL.Color3(0.0f, 0.8f, 0.0f);
                GL.Vertex3(0.2f, 0.2f, -0.2f);
                GL.Vertex3(0.2f, 0.2f, 0.2f);
                GL.Vertex3(0.2f, -0.2f, 0.2f);
                GL.Vertex3(0.2f, -0.2f, -0.2f);
 
                alpha = alpha + (float)0.1;
                GL.End();
                GL.Flush();
 
                context.SwapBuffers();
                //context.Update(winfo);
                int iX = 100;
                Thread.Sleep(iX);
            }
            context.Dispose();
        }
 
        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {
            if (oThread != null)
            {
                if (oThread.IsAlive)
                {
                    KillRenderThread = true;
                    oThread.Join();
                    oThread = null;
                }
            }
            KillRenderThread = false;
            oThread = new Thread(new ThreadStart(RenderToPanel));
            oThread.IsBackground = true;
            oThread.Start();          
        }
 
 
        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {
            var p = sender as Panel;
            var g = e.Graphics;
 
            g.FillRectangle(new SolidBrush(Color.FromArgb(0, Color.Black)), p.DisplayRectangle);
 
            Point[] points = new Point[4];
 
            points[0] = new Point(0, 0);
            points[1] = new Point(0, p.Height);
            points[2] = new Point(p.Width, p.Height);
            points[3] = new Point(p.Width, 0);
 
            Brush brush = new SolidBrush(Color.DarkBlue);
 
            g.FillPolygon(brush, points);
        }
 
 
 
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            KillRenderThread = true;
            oThread.Join();
            oThread = null;
            base.OnFormClosing(e);
        }
    }
}

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mcb_'s picture

I solved my problem, when i take GL.LoadIdentity() inside while loop or when i set alpha variable to static value\ problem solved.
Reason was related with GL.LoadIdentity(). i was not resetting identity matrices, then i was rotating scene with increasing amount of angle.
:)