3

See my code (in openGL, C++) the blue sphere is more wide [stretched], how can I fix it, I need translation without stretching: can you please fix my code to avoid stretching? my code:

#include <GL/glut.h>

static void resize(int width, int height)
{
    const float ar = (float) width / (float) height;
    glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
}

static void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3d(0,0,1);
    glPushMatrix();
        glTranslatef(3.0, 0.0, -6.0);
        glutSolidSphere(1,50,50);
    glPopMatrix();

    glColor3d(1,0,0);
    glPushMatrix();
        glTranslatef(0.0, 0.0, -6.0);
        glutSolidSphere(1,50,50);
    glPopMatrix();
    glutSwapBuffers();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(1200,1200);
    glutCreateWindow("gggggggggggg");
    glutReshapeFunc(resize);
    glClearColor(1,1,1,1);
    glutDisplayFunc(display);
    glutMainLoop();
}
gy ab
  • 33
  • 5
  • I feel compelled to ask this: Is there any reason you are using OpenGL code that has been deprecated for over a decade? – GraphicsMuncher Jan 16 '17 at 04:26
  • No. I don't know about other options. What do you recommend to use? I am trying to make a 3D collision simulation of 2 ellipsoids that rotate about their center of mass (about specific axis) and traslate toward each other. – gy ab Jan 16 '17 at 09:07
  • I think it was targeted at OpenGL 4.0 or newer and probably core profile which needs GLSL. OpenGL 1.0 is still valid if you want to have working apps... OpenGL 4.0 and above is still not well supported among the gfx vendors. You can easily end up with unforseen HW incompatibility gfx problems. If you are coding games it is OK as it is expected to have a valid GPU but appart of that (for example in manufactory) there are still tons of useless Intel and Toshiba stuff where newer GL stuff is not operating as should. – Spektre Jan 16 '17 at 09:15
  • But using at least VBO is a good idea for speed take a look at this: [simple complete GL+VAO/VBO+GLSL+shaders example in C++](http://stackoverflow.com/a/31913542/2521214) so you have a glance of what we are talking about – Spektre Jan 16 '17 at 09:17

1 Answers1

1

The distortion comes from glFrustum. This is an example of projection that does what you're asking.

glOrtho(-5, 5, -5 / ar, 5 / ar, -20, 20);
neclepsio
  • 453
  • 3
  • 15