Opengl stretching

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

Related Posts