enablerbr's picture

Problem Rectangle instead of square.

converting NeHe tutorial 2 i have encountered a small problem. instead of getting a square quad to appear. i have a rectangle quad appearing.

            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();	                                                                                                                                        // Keep Going

heres the full code.

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 NeHeLesson2
{
    public partial class Form1 : Form
    {
        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, glControl1.ClientSize.Width / glControl1.ClientSize.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)
        {
            glControl1.MakeCurrent();
 
            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.Begin(OpenTK.Graphics.BeginMode.Triangles);		    // Drawing Using Triangles
            GL.Vertex3(0.0f, 1.0f, 0.0f);					        // Top
            GL.Vertex3(-1.0f, -1.0f, 0.0f);					        // Bottom Left
            GL.Vertex3(1.0f, -1.0f, 0.0f);					        // Bottom Right
            GL.End();											    // Finished Drawing The Triangle
            GL.Translate(3.0f, 0.0f, 0.0f);						    // Move Right 3 Units
            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();									            // Keep Going
 
            glControl1.SwapBuffers();
        }
 
        private void glControl1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.Escape:
                    this.Close();
                    break;
            }
        }
    }
}

Comments

Comment viewing options

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

Solved the problem. changed Glu.Perspective(45.0f, glControl1.ClientSize.Width / glControl1.ClientSize.Height, 0.1f, 100.0f); to this Glu.Perspective(45.0f, glControl1.AspectRatio, 0.1f, 100.0f);

the Fiddler's picture

The issue is that you are doing integer division when calculating the aspect ratio, i.e.:

int aspect_ratio = glControl1.ClientSize.Width / glControl1.ClientSize.Height;

vs

float aspec_ratio = glControl1.ClientSize.Width / (float)glControl1.ClientSize.Height;

Obviously, only the second will give correct results. GLControl.AspectRatio is just a shortcut to this calculation.

enablerbr's picture

ahh forgot about command wanting double but i was giving it int. thanks.