Equilateral triangle

Hi all,

when trying to generate a equilateral triangle OpenGL doesn’t display it for me.


h:=SQRT(0.75);

glVIEWPORT(0,0,640,480);
glMATRIXMODE(GL_PROJECTION);
glLOADIDENTITY;
glMATRIXMODE(GL_MODELVIEW);
glLOADIDENTITY;

glCOLOR3f(1.0, 1.0, 0.0);
glVERTEX3f(0.0, 0.0, 0.0);
glVERTEX3f(1.0, 0.0, 0.0);
glVERTEX3f(0.5, h, 0.0);

The code should display an equilateral triangle with length a=1 for each side. The height h of an equilateral triangle as described is the square root of 0.75 as defined. What I get on screen is shown here:

Clearly the bottom side is larger than the other two sides and so I didn’t get an equilateral triangle.

Probably easy to solve but since I work on this for long time now I think I could need some hints :).

Try having the viewport with size 480x480, instead of 640x480; notice the dependency.

You should know the difference between the viewing volume and the viewport. The viewing volume represents the chunk of space that gets rendered. In your case, this volume is cubical, so the equilateral triangle remains unscaled. All the geometry is mapped from the viewing volume onto the viewport. OpenGL is scaling your cubical viewing volume to fit onto a rectangular viewport (640x480), so the triangle is scaled appropriately.

Thanks for your fast answers.

Still before starting this thread I tried to change to a quadratic viewport intuitivly (for some reason I also chose 480x480) since I actually wasn’t aware of the difference between viewing volume and viewport. I unfortunately got this:

So actually this was not solving the problem for me.

Not sure if I got you two wrong though?

Are you measuring your monitor? You definitely have set-up the wrong native resolution. Just draw a square in MS Paint, and see :slight_smile: .
Because the triangle on that second image has sides of precisely 240px.

Don’t let the projection matrix to identity, it should depend on the viewport width/height ratio in order to keep shapes proportions.

In you case if the window is a square, the rendered triangle will be equilateral.

So for orthographic projection use something like this:


float a = width / height;
glOrtho( -a, a, -1, 1, -1, 1 );

This way you are sure that every polygon in the view volume [-a a]x[1 1]x[-1 1] will be visible, and the width of the view volume will be automatically adjusted to keep object proportions.

you have to set your projection matrix with the correct aspect ratio,
like gluPerspective (45.0f, (GLfloat)(width)/(GLfloat)(height),10.0f, 100.0f);
or glOrtho( 0, 133 , 100 , 0, -1, 1 );

Thanks for all your hints. Okay, after much of reading (seems as if 99% of all resources copied the text from opengl.org doc) I see why the object got distorted. The problem still exists :(.

Actually yes, I am measuring my monitor :(. Oh, about MS Paint, you know, I never trust MS software much so I didn’t ever wonder when it swore me to draw a square making indeed a rectangle ;D.

How did you measure the pixel length?

Okay, since I need a perspective projection (the equilateral triangles will form a 3d object finally) I have to use glFrustum or the mentioned gluPerspective if I understood correctly. That I did now. Here is the corresponding code:


h:=SQRT(0.75);
.
.
.
glVIEWPORT(0,0,640,480);
glMATRIXMODE(GL_PROJECTION);
glLOADIDENTITY;
gluPERSPECTIVE(45.0, 640/480, 1.0, 3.0);
glMATRIXMODE(GL_MODELVIEW);
glLOADIDENTITY;
.
.
.
glCOLOR3f(1.0, 1.0, 0.0);
glVERTEX3f(-0.5, -0.5, -2.0);
glVERTEX3f(0.5, -0.5, -2.0);
glVERTEX3f(0.0, h-0.5, -2.0);

Don’t wonder about the slight change in the vertex coordinates but I had to fit them to the new defined viewing frustum. The dimensions of the triangle itself didn’t change. Unfortunately I get this though:

The bottom side is still larger than the other two sides even though I was expecting the gluPerspective function to correct this so that no distortion could be recognized. Seems I still do something wrong. Any further hints are very welcome.

Fix your monitor’s resolution! Make that MS Paint square look like a square onscreen.
I measured the sides with GIMP.

“640/480” could tell the compiler to put “1.0”. Make it 640.0/480.0

640/480 as used in the code is recognized correctly by the compiler.

Let me summarize my progress so far (you probably noticed I am not too familiar with OpenGL) because your last reply confused me a little:

  1. when just projecting an equilateral triangle to a rectangle viewport, you receive a clearly distorted rectangle because 1 equals a different number of pixels in each direction (x,y); this is also measuarable in pixels in the first picture

  2. when projecting an equilateral triangle to a quadratical viewport, you receive an undistorted triangle at the viewport because 1 equals the same number of pixels in each direction; also measurable in pixels in the second picture; the triangle appears still distorted because of the rectangle desktop resolution

  3. when projecting an quilateral triangle to a rectangle viewport, you receive an undistorted triangle at the viewport because of magic gluPERSPECTIVE function; also measurable in pixels in the third picture; the triangle appears still distorted because of the rectangle desktop resolution

Are these three statements correct?

I assume your statement to fix my monitor resolution means to change it to a quadratical resolution which I really would like to avoid. Furthermore when I share my program I can’t force any user to change his/her resolution. There must be a solution so that any user gets a real equilateral triangle shown at the screen independent of his/her desktop resolution.

When thinking about this I assumed to switch to fullscreen mode would solve this because then the resolution would be 640x480 as defined by the viewport (independent of the desktop resolution) - however, again the three sides weren’t equilateral.

Any further hints are welcome.

No, I’ve been telling you this: your LCD monitor’s native resolution is 1680x1050, while you’re running it at 1024x768.

Users, that do this, don’t care about aspect-ratio even if it hits them in the face. 150kg Angelina Jolie onscreen >_<

At a resolution of 1680x1050 the triangle is still distorted.

:slight_smile: 1680x1050 then is not that monitor’s native one.
What’s the brand and model of your monitor, we might be able to tell you the resolution.