Is there a way to fix viewport distortion(stretch) when my glutWindow is not a square

Well i got into a problem when i coded my code completely this day.
My project(Fractal Tree)stretch when my window is not a square.
When i tried glutReshapeFunc my project would not be visible but only an black screen.
Is there any work-around about this.

My full code:

kamp=angle.
sakos=branches.

main.cpp

#include <GL/Glut.h>
#include <iomanip>
#include <windows.h>
#include "color.h"
#include "sakos.h"
#include <time.h>

int initial_time = time(NULL), final_time,frame_count;


using namespace std;
// coords.
float px1=0.0;
float px2=0.0;
float py1=-1.0;
float py2=-0.7;

int kampas=10;
int n=13;
double mult=0.7;


void keyboard(unsigned char key,int x,int y){
	switch(key){
	case 'q':
	exit(0);
	break;
	case '.':
	kampas++;
	break;
	case ',':
	kampas--;
	break;
	case '[':
		n--;
		if(n<0){
			n=0;
		}
		break;
	case ']':
		n++;
		break;
	case 'w':
		py2+=0.001;
		break;
	case 's':
		py2-=0.001;
		break;
	case 'd':
		mult+=0.001;
		break;
	case 'a':
		mult-=0.001;
		if(mult<0){
			mult=0.0;
		}
		break;
	}

}


void initgl()
{
glClearColor(0.0,0.0,0.0,1.0);
	glutPostRedisplay();
}
void stiebas(){
glBegin(GL_LINES);
	glVertex2f(px1,py1);
	glVertex2f(px2,py2);
	glEnd();

}

void display()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	stiebas();
	sakosr(px1,py1,px2,py2,kampas,n,mult);
	rainbow(0.1);
	glFlush();
	Sleep(10);
	glutPostRedisplay();
	frame_count++;
		final_time =time(NULL);
		if(final_time - initial_time >= 1)
		{
			cout<<"FPS :"<<frame_count/(final_time-initial_time) << endl;
			frame_count=0;
			initial_time=final_time;
		}

}

int main(int argc,char** argv){
glutInit(&argc,argv);
glutInitWindowSize(600,600);
glutInitWindowPosition(100,100);
glutCreateWindow("Fraktal Tree");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
initgl();
glutMainLoop();
}




color.h

#include <cstdlib>
#include <GL/GLUT.h>


#define COLOR




void anarchy(int r,int g,int b){
	r=rand()%255;
	g=rand()%255;
	b=rand()%255;
}

void rainbow(float pl){
	static int r = 255, g = 0, b = 0;

		if(r==255&&g<255&&b==0){
			g++;
		}
		if(r>0&&g==255&&b==0){
			r--;
		}
		if(r==0&&g==255&&b<255){
			b++;
		}
		if(r==0&&g>0&&b==255){
			g--;
		}
		if(r<255&&g==0&&b==255){
			r++;
		}
		if(r==255&&g==0&&b>0){
			b--;
		}
		glLineWidth(pl);
		glColor3ub(r,g,b);
}

sakos.h

void sakosr(float px1,float py1,float px2,float py2,int kamp,int n,double mult){
	glBegin(GL_LINE_STRIP);
	glVertex2f(px2,py2);
	float px3=(px2-px1)*mult;
	float py3=(py2-py1)*mult;
	GLfloat px3r=px3*cos(kamp*rtd)+py3*sin(kamp*rtd)+px2;
	GLfloat py3r=-px3*sin(kamp*rtd)+py3*cos(kamp*rtd)+py2;
	glVertex2f(px3r,py3r);
	glVertex2f(px2,py2);
	GLfloat px3l=px3*cos(-kamp*rtd)+py3*sin(-kamp*rtd)+px2;
	GLfloat py3l=-px3*sin(-kamp*rtd)+py3*cos(-kamp*rtd)+py2;
	glVertex2f(px3l,py3l);
	px1=px2;
	py1=py2;
	px2=px3r;
	py2=py3r;
	glEnd();
	if(n>0){
		sakosr(px1,py1,px2,py2,kamp,n-1,mult);
		px2=px3l;
		py2=py3l;
		sakosr(px1,py1,px2,py2,kamp,n-1,mult);
	}
}

Hey,

maybe this helps:

glViewport      (0, 0, (GLint)w, (GLint)h);

You need to update your projection matrix. OpenGL uses fixed coordinate ranges from -1 to 1 for all its axes. If your have a window height of 400px and a width of 800px a square would look like a rectangle. To fix this you divide the width of the rectangle by 2, because: w/h = 400/800 = 1/2. Now your square looks again like a square.

EDIT: Just have a look at the post before mine

Unlikely. The first time a context is bound to a window, the viewport is automatically set to the extent of the window.

What the OP needs is to set the projection matrix using e.g. glOrtho() to compensate for the window’s aspect ratio. The default is to use an identity matrix, which maps a square in eye space to the viewport (i.e. it will distort the image if the window isn’t a square and nothing else is done to compensate for that).

For an orthographic projection, the transformation can be included in the model-view matrix. That won’t work for a perspective projection, and even for an orthographic projection there are sometimes reasons for keeping non-uniform scaling out of the model-view matrix.

For me the main problem is that i tried glViewport and glOrtho2D method of showing my project which would result in black screen without fractal tree.
Maybe it has something to do with color transition which i implemented with color.h ??

[QUOTE=StabberKnight906;1291147]For me the main problem is that i tried glViewport and glOrtho2D method of showing my project which would result in black screen without fractal tree.
Maybe it has something to do with color transition which i implemented with color.h ??[/QUOTE]

No. It just means that you aren’t setting the matrix correctly.

Well now i wonder how should i set my matrix correctly in opengl ?

GLU will generate them for you.

Pro tip: check out the math in GLM

Source for math help:
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/

In the reshape callback, call glViewport() with the window dimensions and glOrtho() (or gluOrtho2D) with either the window dimensions or something derived from them, e.g.


glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-1.0*w/h, 1.0*w/h, -1.0, 1.0);

[QUOTE=GClements;1291162]In the reshape callback, call glViewport() with the window dimensions and glOrtho() (or gluOrtho2D) with either the window dimensions or something derived from them, e.g.


glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-1.0*w/h, 1.0*w/h, -1.0, 1.0);

[/QUOTE]

Thank you very much it worked like a charm.And you forgot to add glLoadIdentity(); between glMatrixMode and GluOrtho2D