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; } 
3

2 Answers

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

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

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy