smhykcll's picture

OpenTK rotation 3D cube with mouse

im working on 3D object.i could render cube.but it doesnt rotate exactly.and i want to rotate with mouse movement.i heve read something about it.They advice to use Matrix4.But i dont know how i use it.And they advice to change angles.Please give me advice.
Here is my source code....

using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK;
using System.Diagnostics;
 
namespace proje
{
    public partial class Form1 : Form
    {
        bool loaded = false;
        double xrot, yrot, zrot;
        public Form1()
        {
            InitializeComponent();
 
        }
 
        double rotate_y = 0;
        double rotate_x = 0;
        double rotate_z = 0;
        private void display(){
            if (!loaded) // Play nice
                return;
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.LoadIdentity();
            GL.Translate(0.0f, 0.0f, 0.0f);
            GL.Rotate(rotate_x, 1f, 0f, 0f);
            GL.Rotate(rotate_y, 0f, 1f, 0f);
            GL.Rotate(rotate_z, 0f, 0f, 1f);
 
            GL.Begin(BeginMode.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);
 
            GL.End();
            GL.Flush();
            glControl1.SwapBuffers();
 
 
    }
 
         private void glControl1_Resize(object sender, EventArgs e)
    {
        SetupViewport();
        glControl1.Invalidate();
 
    }
 
        private void glControl1_Paint(object sender, PaintEventArgs e)
        {
 
            display();
 
        }
        private void glControl1_Load(object sender, EventArgs e)
        {
            loaded = true;
            GL.ShadeModel(ShadingModel.Smooth);
            GL.ClearColor(Color.Black);
            GL.ClearDepth(1.0f);
            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Lequal);
            GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
            SetupViewport();
            Application.Idle += Application_Idle; // press TAB twice after +=
        }
 
        void Application_Idle(object sender, EventArgs e)
        {
            // no guard needed -- we hooked into the event in Load handler
            while (glControl1.IsIdle)
            {
                display();
                SetupViewport();          
            }
        } 
        private void SetupViewport()
        {
            int w = glControl1.Width;
            int h = glControl1.Height;    
            GL.MatrixMode(MatrixMode.Projection);
           GL.LoadIdentity();
            GL.Ortho(0, w, 0, h, -1, 1); // Bottom-left corner pixel has coordinate (0, 0)
            GL.Viewport(0, 0, w, h); // Use all of the glControl painting area
        }
 
 
        private void Form1_Load(object sender, EventArgs e)
        {
            glControl1.Resize+=new EventHandler(glControl1_Resize);
        }
 
        }
 
        }