Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Equilateral triangle

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2009
    Posts
    5

    Equilateral triangle

    Hi all,

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

    Code :
    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 .

  2. #2
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,294

    Re: Equilateral triangle

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

  3. #3
    Senior Member Regular Contributor
    Join Date
    Dec 2008
    Location
    USA
    Posts
    134

    Re: Equilateral triangle

    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.

  4. #4
    Junior Member Newbie
    Join Date
    Aug 2009
    Posts
    5

    Re: Equilateral triangle

    Thanks for your fast answers.

    Quote Originally Posted by Ilian Dinev
    Try having the viewport with size 480x480, instead of 640x480; notice the dependency.
    Quote Originally Posted by todayman
    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.
    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?

  5. #5
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,294

    Re: Equilateral triangle

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

  6. #6
    Senior Member OpenGL Pro dletozeun's Avatar
    Join Date
    Jan 2006
    Location
    FRANCE
    Posts
    1,367

    Re: Equilateral triangle

    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:

    Code :
    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.

  7. #7
    Senior Member Frequent Contributor
    Join Date
    Feb 2006
    Location
    Sweden
    Posts
    744

    Re: Equilateral triangle

    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 );

  8. #8
    Junior Member Newbie
    Join Date
    Aug 2009
    Posts
    5

    Re: Equilateral triangle

    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 .

    Quote Originally Posted by Ilian Dinev
    Are you measuring your monitor? You definitely have set-up the wrong native resolution. Just draw a square in MS Paint, and see .
    Because the triangle on that second image has sides of precisely 240px.
    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?

    Quote Originally Posted by dletozeun
    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:

    Code :
    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.
    Quote Originally Posted by zeoverlord
    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 );
    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:

    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.




  9. #9
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,294

    Re: Equilateral triangle

    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

  10. #10
    Junior Member Newbie
    Join Date
    Aug 2009
    Posts
    5

    Re: Equilateral triangle

    Quote Originally Posted by Ilian Dinev
    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. Triangle Strip - Per Triangle color
    By Aliii in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 09-27-2013, 10:14 AM
  2. An alternative to triangle strips and triangle fans
    By vincoof in forum OpenGL: General
    Replies: 8
    Last Post: 10-26-2012, 04:04 AM
  3. Replies: 3
    Last Post: 10-09-2012, 09:39 PM
  4. triangle strip with a LINE triangle display error
    By Samme in forum OpenGL: Windows
    Replies: 22
    Last Post: 11-20-2008, 07:50 PM
  5. OT: fastest ray-triangle line -triangle intersection algo
    By V-man in forum OpenGL: Advanced Coding
    Replies: 23
    Last Post: 08-28-2002, 02:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean