opengl zachowanie proporcji figur w oknie

0

witam, mam problem z poprawnym napisaniem programu który zachowuje proporcje figur w oknie, chciałem prosić o wskazanie błędu.
pozdrawiam

 #include <GL/freeglut.h> 

/*
 * program wyswietla szescian z zachowaniem proporcji
 */


const int width = 640;
const int height = 480;
const float a = 1.0;
float fovy = 45.0, aspect = 1.0, near_ = 0.1, far_ = 10.0;//parametry dla gluPerspective(); 

void uklad(){
    glBegin(GL_LINES);
        glColor3f(0.0, 1.0, 0.0); //zielona oś X
        glVertex3f(-8.0, 0.0, 0.0);
        glVertex3f( 8.0, 0.0, 0.0); 

        glColor3f(0.0, 0.0, 1.0); // niebieska oś Y
        glVertex3f(0.0, -8.0, 0.0);
        glVertex3f(0.0,  8.0, 0.0);
	
        glColor3f(1.0,  0.0, 0.0); // czerwona oś Z
        glVertex3f(0.0, 0.0, -8.0);
        glVertex3f(0.0, 0.0,  8.0);
    glEnd();
}


void sciana(){
    glBegin(GL_QUADS);
        glVertex3f(-a / 2, 0.0,  a / 2);
        glVertex3f(-a / 2, 0.0, -a / 2);
        glVertex3f( a / 2, 0.0, -a / 2);
        glVertex3f( a / 2, 0.0,  a / 2);
    glEnd();
}

void szescian(){
    //dolna sciana
    glPushMatrix();
        glLoadIdentity();
        glTranslatef(0.0, -a /2, 0.0);
        glColor3f(0.0, 0.0, 1.0);
        sciana();
    glPopMatrix();
    
    //gorna sciana
    glPushMatrix();
        glLoadIdentity();
        glTranslatef(0.0, a /2, 0.0);
        glColor3f(0.0, 1.0, 1.0);
        sciana();
    glPopMatrix();
    
    //lewa sciana
    glPushMatrix();
        glLoadIdentity();
        glRotatef(90.0, 0.0, 0.0, 1.0);
        glTranslatef(0.0, a /2, 0.0);
        glColor3f(0.0, 1.0, 0.0);
        sciana();
    glPopMatrix();

    //prawa sciana
    glPushMatrix();
        glLoadIdentity();
        glRotatef(90.0, 0.0, 0.0, 1.0);
        glTranslatef(0.0, -a /2, 0.0);
        glColor3f(1.0, 1.0, 0.0);
        sciana();
    glPopMatrix();
    
    //tylna sciana
    glPushMatrix();
        glLoadIdentity();
        glRotatef(90.0, -1.0, 0.0, 0.0);
        glTranslatef(0.0, a /2, 0.0);
        glColor3f(1.0, 0.0, 0.0);
        sciana();
    glPopMatrix();
    
    //przednia sciana
    glPushMatrix();
        glLoadIdentity();
        glRotatef(90.0, -1.0, 0.0, 0.0);
        glTranslatef(0.0, -a /2, 0.0);
        glColor3f(0.4, 1.0, 0.0);
        sciana();
    glPopMatrix();
}

void display() 
{
    glClearColor(0.0,0.0,0.0,1.0); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//"czyszczenie" tła okan i bufora głębokosci
    glEnable(GL_DEPTH_TEST);//włącznie algorytmu zasłaniania
    //glMatrixMode(GL_PROJECTION);
    //glLoadIdentity();
    //gluPerspective(60.0, 1.0, 0.1, 10.0); //bryła widzenia perspektywicznego
    //gluLookAt(2.0,2.0,2.0, 0.0,0.0,0.0, 0.0,1.0,0.0);//obserwator 
    //glMatrixMode(GL_MODELVIEW);
    //glLoadIdentity();

        uklad();
        szescian();
    glFlush();
} 

void odrysuj(int width, int height){
    float  h = float(height), w = float(width);
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(fovy, w/h, near_, far_);
    glMatrixMode(GL_MODELVIEW);
}


int main(int argc, char** argv)
{
    glutInit(&argc, argv);  
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(width, height);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Scena testowa");
    glutDisplayFunc(display);
    glutReshapeFunc(odrysuj);
    glutMainLoop();
    return 0;
}
0

Nie wyjaśniłeś za bardzo na czy problem polega (może jakiś obrazek?), ale

glMatrixMode(GL_MODELVIEW);
tu brakuje jakiegoś gluLookAt

    glPushMatrix();
        glLoadIdentity();
        glTranslatef(0.0, -a /2, 0.0);
        glColor3f(0.0, 0.0, 1.0);
        sciana();
    glPopMatrix();

niepotrzebne te LoadIdentity wewnątrz PushMatrix/PopMatrix.

0

chodzi mi o to żeby po zmianie rozmiaru okna dana figura np kwadrat zachowywywała proporcje a nie zmieniała się w prostokąt

0

o to poprawiony kod:

 #include <GL/freeglut.h> 

/*
 * program wyswietla szescian z zachowaniem proporcji
 */


const int width = 640;
const int height = 480;
const float a = 1.0;
float fovy = 45.0, aspect = 1.0, near_ = 0.1, far_ = 10.0;//parametry dla gluPerspective(); 

void uklad(){
    glBegin(GL_LINES);
        glColor3f(0.0, 1.0, 0.0); //zielona oś X
        glVertex3f(-8.0, 0.0, 0.0);
        glVertex3f( 8.0, 0.0, 0.0); 

        glColor3f(0.0, 0.0, 1.0); // niebieska oś Y
        glVertex3f(0.0, -8.0, 0.0);
        glVertex3f(0.0,  8.0, 0.0);
	
        glColor3f(1.0,  0.0, 0.0); // czerwona oś Z
        glVertex3f(0.0, 0.0, -8.0);
        glVertex3f(0.0, 0.0,  8.0);
    glEnd();
}


void sciana(){
    glBegin(GL_QUADS);
        glVertex3f(-a / 2, 0.0,  a / 2);
        glVertex3f(-a / 2, 0.0, -a / 2);
        glVertex3f( a / 2, 0.0, -a / 2);
        glVertex3f( a / 2, 0.0,  a / 2);
    glEnd();
}

void szescian(){
    //dolna sciana
    glPushMatrix();
        glTranslatef(0.0, -a /2, 0.0);
        glColor3f(0.0, 0.0, 1.0);
        sciana();
    glPopMatrix();
    
    //gorna sciana
    glPushMatrix();
        glTranslatef(0.0, a /2, 0.0);
        glColor3f(0.0, 1.0, 1.0);
        sciana();
    glPopMatrix();
    
    //lewa sciana
    glPushMatrix();
        glRotatef(90.0, 0.0, 0.0, 1.0);
        glTranslatef(0.0, a /2, 0.0);
        glColor3f(0.0, 1.0, 0.0);
        sciana();
    glPopMatrix();

    //prawa sciana
    glPushMatrix();
        glRotatef(90.0, 0.0, 0.0, 1.0);
        glTranslatef(0.0, -a /2, 0.0);
        glColor3f(1.0, 1.0, 0.0);
        sciana();
    glPopMatrix();
    
    //tylna sciana
    glPushMatrix();
        glRotatef(90.0, -1.0, 0.0, 0.0);
        glTranslatef(0.0, a /2, 0.0);
        glColor3f(1.0, 0.0, 0.0);
        sciana();
    glPopMatrix();
    
    //przednia sciana
    glPushMatrix();
        glRotatef(90.0, -1.0, 0.0, 0.0);
        glTranslatef(0.0, -a /2, 0.0);
        glColor3f(0.4, 1.0, 0.0);
        sciana();
    glPopMatrix();
}

void display() 
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//"czyszczenie" tła okan i bufora głębokosci
    glLoadIdentity();
    gluLookAt(2.0,2.0,2.0, 0.0,0.0,0.0, 0.0,1.0,0.0);//obserwator 
        uklad();
        szescian();
    glFlush();
} 

void odrysuj(int width, int height){
    float  h = float(height), w = float(width);
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(fovy, w/h, near_, far_);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void init(){
    glClearColor(0.0,0.0,0.0,1.0);
    glEnable(GL_DEPTH_TEST);//włącznie algorytmu zasłaniania
}


int main(int argc, char** argv)
{
    glutInit(&argc, argv);  
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(width, height);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Scena testowa");
        init();
    glutDisplayFunc(display);
    glutReshapeFunc(odrysuj);
    glutMainLoop();
    return 0;
}
0
void display() 
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//"czyszczenie" tła okan i bufora głębokosci
        uklad();
        szescian();
    glFlush();
} 
 
void odrysuj(int width, int height){
    float  h = float(height), w = float(width);
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(fovy, w/h, near_, far_);
    glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(2.0,2.0,2.0, 0.0,0.0,0.0, 0.0,1.0,0.0);//obserwator 
}

tak powinno być.

0

możesz mi to wyjaśnić??

0

gluPerspective w trybie glMatrixMode(GL_PROJECTION).
gluLookAt w glMatrixMode(GL_MODELVIEW).

Jedno i drugie wystarczy w reshape func, nie trzeba w każdym display.

1 użytkowników online, w tym zalogowanych: 0, gości: 1