enablerbr's picture

Strange rotation effect with NeHe lesson 4 conversion.

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.Audio;
using OpenTK.Graphics;
using OpenTK.Input;
using OpenTK.Math;
using OpenTK.Platform;
 
namespace NeHeLesson4
{
    public partial class Form1 : Form
    {
        private float rtri;
        private float rquad;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
 
            glControl1_Resize(this, EventArgs.Empty);   // Ensure the Viewport is set up correctly
        }
 
        private void glControl1_Load(object sender, EventArgs e)
        {
            GL.ShadeModel(OpenTK.Graphics.ShadingModel.Smooth);							// Enable Smooth Shading
            GL.ClearColor(0.0f, 0.0f, 0.0f, 0.5f);				                        // Black Background
            GL.ClearDepth(1.0f);									                    // Depth Buffer Setup
            GL.Enable(OpenTK.Graphics.EnableCap.DepthTest);							    // Enables Depth Testing
            GL.DepthFunc(OpenTK.Graphics.DepthFunction.Lequal);							// The Type Of Depth Testing To Do
            GL.Hint(OpenTK.Graphics.HintTarget.PerspectiveCorrectionHint, OpenTK.Graphics.HintMode.Nicest);
        }
 
        private void glControl1_Resize(object sender, EventArgs e)
        {
            if (glControl1.ClientSize.Height == 0)
                glControl1.ClientSize = new System.Drawing.Size(glControl1.ClientSize.Width, 1);
 
            GL.Viewport(0, 0, glControl1.ClientSize.Width, glControl1.ClientSize.Height);
 
            GL.MatrixMode(OpenTK.Graphics.MatrixMode.Projection);						// Select The Projection Matrix
            GL.LoadIdentity();									                        // Reset The Projection Matrix
 
            // Calculate The Aspect Ratio Of The Window
            Glu.Perspective(45.0f, (double)glControl1.Width / (double)glControl1.Height, 0.1f, 100.0f);
 
            GL.MatrixMode(OpenTK.Graphics.MatrixMode.Modelview);					    // Select The Modelview Matrix
            GL.LoadIdentity();
        }
 
        private void glControl1_Paint(object sender, PaintEventArgs e)
        {
            GL.Clear(OpenTK.Graphics.ClearBufferMask.ColorBufferBit | OpenTK.Graphics.ClearBufferMask.DepthBufferBit);	// Clear Screen And Depth Buffer
            GL.LoadIdentity();									    // Reset The Current Modelview Matrix
            GL.Translate(-1.5f, 0.0f, -6.0f);						// Move Left 1.5 Units And Into The Screen 6.0
            GL.Rotate(rtri, 0.0f, 1.0f, 0.0f);				        // Rotate The Triangle On The Y axis ( NEW )
            GL.Begin(OpenTK.Graphics.BeginMode.Triangles);		    // Drawing Using Triangles
            GL.Color3(1.0f, 0.0f, 0.0f);                            // Set The Color To Red
            GL.Vertex3(0.0f, 1.0f, 0.0f);					        // Top
            GL.Color3(0.0f, 1.0f, 0.0f);						    // Set The Color To Green
            GL.Vertex3(-1.0f, -1.0f, 0.0f);					        // Bottom Left
            GL.Color3(0.0f, 0.0f, 1.0f);						    // Set The Color To Blue
            GL.Vertex3(1.0f, -1.0f, 0.0f);					        // Bottom Right
            GL.End();											    // Finished Drawing The Triangle
            GL.Translate(1.5f, 0.0f, -6.0f);						    // Move Right 1.5 Units And Into The Screen 6.0
            GL.Rotate(rquad, 1.0f, 0.0f, 0.0f);					    // Rotate The Quad On The X axis ( NEW )
            GL.Color3(0.5f, 0.5f, 1.0f);							// Set The Color To Blue One Time Only
            GL.Begin(OpenTK.Graphics.BeginMode.Quads);				// Draw A Quad
            GL.Vertex3(-1.0f, 1.0f, 0.0f);					        // Top Left
            GL.Vertex3(1.0f, 1.0f, 0.0f);					        // Top Right
            GL.Vertex3(1.0f, -1.0f, 0.0f);					        // Bottom Right
            GL.Vertex3(-1.0f, -1.0f, 0.0f);					        // Bottom Left
            GL.End();									            // Done Drawing The Quad
 
            rtri += 0.2f;											// Increase The Rotation Variable For The Triangle ( NEW )
            rquad -= 0.15f;										    // Decrease The Rotation Variable For The Quad ( NEW )
 
            glControl1.SwapBuffers();
 
            glControl1.Invalidate();
        }
 
        private void glControl1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.Escape:
                    this.Close();
                    break;
            }
        }
    }
}

the shapes in the NeHe lesson 4 rotate on 1 axis. however in my code. not only do they rotate on 1 axis but also rotate the whole translation. so what you see is a shape moving around the screen and not just on the 1 spot.


Comments

Comment viewing options

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

You missed the GL.LoadIdentity() before drawing the quad.
That is probably causing the problem.

enablerbr's picture

doh. thanks Mincus. that sorted it.