1

I created Lines and when I'm rotate the line. Line will be stretch. How can I stop stretch at rotation time. When I change height in Ortho it will be not displaying properly. When Line is going left or right it will be start strtching but when it will be reach in main point it will come in real position.

#include<fstream>
#include<iostream>
#include<stdlib.h>
#include<glut.h>
using namespace std;
float yr = 0;
void introscreen();
void screen();
void screen1();
void PitchLadder();

int width = 1268;
int height = 720;

float translate =  0.0f;
GLfloat angle = 0.0f;

void display(void) {
    glClearColor(0, 0, 0, 0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-300, 300, -10, 25, 0, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    static int center_x = 0;
    static int center_y = 0;
}

void specialKey(int key, int x, int y) {
    switch (key) {
    case GLUT_KEY_UP:
        translate += 1.0f;
        break;
    case GLUT_KEY_DOWN:
        translate -= 1.0f;
        break;
    case GLUT_KEY_LEFT:
        angle += 1.0f;
        break;
    case GLUT_KEY_RIGHT:
        angle -= 1.0f;
        break;
    }

    glutPostRedisplay();
}

void Rolling(void) {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0, 1, 0);
    glPushMatrix();
    glRotatef(-angle, 0, 0, 1);
    glTranslatef(-10, translate,0);
    PitchLadder();
    glPopMatrix();
    glFlush();
}

void PitchLadder() {
    GLfloat y;
    GLfloat y2;
    GLfloat fSize[5];
    GLfloat fCurrSize;
    fCurrSize = fSize[2];
    for (y2 = -90.0f ; y2 <= 90.0f ; y2 += 10.0f) {
        glLineWidth(fCurrSize);
        glBegin(GL_LINES);
        glVertex3f(-50.0f , y2 , 0);
        glVertex3f(50.0f , y2 , 0);
        glEnd();
        fCurrSize += 1.0f;
        screen();
        screen1();
    }
}

void renderbitmap1(float x3, float y3, void *font1, char *string1) {
    char *c1;
    glRasterPos2f(x3, y3);
    for (c1=string1; *c1 != '\0'; c1++) {
        glutBitmapCharacter(font1, *c1);
    }
}

void screen(void) {
    glColor3f(0, 1, 0);
    char buf1[20] = { '\0' };
    for (int row1 = -90.0f; row1 <= 90 + yr; row1 +=10.0f) {
        sprintf_s(buf1,"%i", row1);
        renderbitmap1(70 , (yr+row1), GLUT_BITMAP_TIMES_ROMAN_24, buf1);
    }
 }

void renderbitmap2(float x4, float y4, void *font2, char *string2) {
    char *c1;
    glRasterPos2f(x4, y4);
    for (c1=string2; *c1 != '\0'; c1++) {
        glutBitmapCharacter(font2, *c1);
    }
}

void screen1(void) {
    glColor3f(0, 1, 0);
    char buf1[20] = { '\0' };
    for (int row1 = -90.0f; row1 <= 90 + yr; row1 +=10.0f) {
        sprintf_s(buf1,"%i", row1);
        renderbitmap2(-70 , (yr+row1), GLUT_BITMAP_TIMES_ROMAN_24, buf1);
    }
 }

int main(int arg, char** argv) {
    glutInit(&arg, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(width, height);
    glutInitWindowPosition(50, 100);
    glutCreateWindow("HUD Lines");
    display();
    glutDisplayFunc(Rolling);
    glutSpecialFunc(specialKey);
    glutMainLoop();
    return 0;
}

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Unknown
  • 15
  • 7
  • 1
    Just showing that one line of code isn't enough for us to help you. Please post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) of your problem. For now, I can only guess that your viewport size and `glOrtho` arguments do not represent the same aspect ratio. – Romen Sep 25 '19 at 13:13
  • You should propably take a look at [How to use `glOrtho()` in OpenGL?](https://stackoverflow.com/questions/2571402/how-to-use-glortho-in-opengl) – molbdnilo Sep 25 '19 at 13:18
  • @Romen I add more code. – Unknown Sep 25 '19 at 13:33

2 Answers2

0

Your window size and orthographic "view" do not have the same aspect ratio:

// This creates a window that's 1268 x 720 (a wide rectangle)
int width = 1268;
int height = 720;
glutInitWindowSize(width, height);    

// This creates a "view" that's 300 x 300 (a square)
glOrtho(-300, 300, -10, 25, 0, 1);    

The "view" will be stretched to fill the viewport (window). You are seeing a 300 x 300 image being stretched to 1268x720, which definitely makes horizontal lines appear longer than vertical lines even though they're the same length in the code.

You should call glOrtho using the width and height variables of your window:

glOrtho(0, width, 0, height, 0, 1);

Notice that I have changed the arguments to (left = 0, right = width, bottom = 0, top = height, ...). This allows you to work with a screen coordinate space that is similar to 2D rendering but the bottom-left corner is (0,0) and the top-right is (width,height).

Romen
  • 1,617
  • 10
  • 26
  • I try this one but It will be not showing me in center. – Unknown Sep 25 '19 at 14:00
  • @Unknown, That is expected if you change the arguments to `glOrtho`, since that moves your view into the 3D world. You can move the object you're drawing back to the center by drawing it at `x = width/2, y = height/2`. – Romen Sep 25 '19 at 14:03
0

At Orthographic Projection, the view space coordinates are linearly mapped to the clip space coordinates respectively normalized device coordinates. The normlaized device space is a cube with a minimum of (-1, -1, -1) and a maximum of (1, 1, 1).
Finally the coordinates in normalized device space are mapped to the rectangular viewport.

If the viewport is rectangular then the aspect ratio has to be considered, when the view space coordinates are transformed to clip space.
The mapping of the normalized device coordinates to the viewport distorted the geometry by the reciprocal aspect ration of the viewport. This distortion has to be compensated by the orthographic projection.

When the orthographic projection is set by glOrtho(left, right, bottom, top, near, far), then the cuboid volume is defined, which maps (left, bottom, near) to (-1, -1, -1) and (right, top, far) to (1, 1, 1).
It is not necessary that the x and y range of the orthographic projection is equal the view port rectangle, bit the ration (left-right)/(top-bottom)hast to be equal the ration of the viewport rectangle else the geometry will be distored.

double size = 200.0f;
double aspect = (double)width / (double)height;
glOrtho(-aspect*size/2.0, aspect*size/2.0, -size/2.0, size/2.0, -1.0, 1.0);
Rabbid76
  • 202,892
  • 27
  • 131
  • 174