2010-07-16 11 views
9

Estoy dibujando varias formas (como círculos) que están desviadas del ancho de la ventana &. Dado que la ventana siempre comienza en un tamaño determinado, se dibujan correctamente, pero cuando se cambia el tamaño de la ventana, se ensucia la relación de aspecto.En OpenGL, ¿cómo puedo ajustar la ventana que está cambiando de tamaño?

¿Cómo puedo dibujar las formas correctamente, independientemente del tamaño de la ventana?

Respuesta

13

Definitivamente no desea que el tamaño de los objetos depende de manera explícita en el tamaño de la ventana.

Como ya lo sugirió genpfault, ajuste su matriz de proyección cada vez que cambie el tamaño de la ventana.

Cosas que hacer en la ventana de cambio de tamaño:

  1. Ajuste de visualización:

    glViewPort(0,0, width, height)

  2. Ajuste matriz de proyección:

    glFrustum(left * ratio, right * ratio, bottom, top, nearClip,farClip)

    o

    glOrtho(left * ratio, right * ratio, bottom, top, nearClip,farClip)

    o

    gluOrtho2D(left * ratio, right * ratio, bottom, top)

    (suponiendo que left, right, bottom y top son todos iguales y ratio=width/height)

+0

Scissoring también influye en la zona vuelve a dibujar, si una vez configurado el clip de rect llamando 'glScissor()'. Ver [esto] (https://www.opengl.org/discussion_boards/showthread.php/176760-glClearColor-WM-SIZE-only-part-is-cleared). – zwcloud

4

Si está usando algo como gluPerspective() sólo tiene que utilizar la ventana de la relación de anchura/altura:

gluPerspective(60, (double)width/(double)height, 1, 256); 
1

debe configurar algún tipo de función de controlador de ventana que se llama cada vez que se cambia el tamaño de su ventana OpenGL. Debe tener cuidado del caso cuando aspectRatio> 1 y cuando aspectRatio < = 1 por separado. Si no lo hace, la geometría puede caer fuera de la pantalla después de cambiar el tamaño de la pantalla.

void windowResizeHandler(int windowWidth, int windowHeight){ 
    const float aspectRatio = ((float)windowWidth)/windowHeight; 
    float xSpan = 1; // Feel free to change this to any xSpan you need. 
    float ySpan = 1; // Feel free to change this to any ySpan you need. 

    if (aspectRatio > 1){ 
     // Width > Height, so scale xSpan accordinly. 
     xSpan *= aspectRatio; 
    } 
    else{ 
     // Height >= Width, so scale ySpan accordingly. 
     ySpan = xSpan/aspectRatio; 
    } 

    glOrhto2D(-1*xSpan, xSpan, -1*ySpan, ySpan, -1, 1); 

    // Use the entire window for rendering. 
    glViewport(0, 0, windowWidth, windowHeight); 
} 
0

Estoy aprendiendo opengl too. Me enfrenté a este problema ayer y no encontré la respuesta correcta. Hoy, he resuelto el problema. En mi pequeño programa, el objeto cambia de tamaño al cambiar el tamaño de la ventana al cambiar el ancho o heigt. Este es mi código (por favor, perdone mis errores):

#include <GL/freeglut.h> 

void fnReshape(int ww, int hh) 
{ 
    GLdouble r; 
    if(hh == 0) 
    hh = 1.0; 
    if(hh > ww) 
    r = (double) hh/(double) ww; 
    else 
    r = (double) ww/(double) hh; 
    glViewport(0, 0, ww, hh); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    if(hh > ww) 
    glFrustum(-1.0, 1.0, -1.0 * r, 1.0 * r, 2.0, 200.0); 
    else 
    glFrustum(-1.0 * r, 1.0 * r, -1.0, 1.0, 2.0, 200.0); 
} 
////////////////////////////////////////////////////////////////// 
void fnDisplay(void) 
{ 
    glClear(GL_COLOR_BUFFER_BIT); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glTranslatef(0., 0., -5.); 
    glBegin(GL_QUAD_STRIP); 
    glColor3f(0., 0., 1.0); 
    glVertex3f(-1., -1., 0.); 
    glVertex3f(-1., 1., 0.); 
    glVertex3f(1., -1., 0.); 
    glVertex3f(1., 1., 0.); 
    glEnd(); 
    glutSwapBuffers(); 
} 
//////////////////////////////////////////////////////////// 
int main(int argc, char **argv) { 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_RGBA |GLUT_DOUBLE); 
    glutInitWindowSize(500, 500); 
    glutCreateWindow("Sample window"); 
    glClearColor(1.0, 0, 0, 1.0); 
    glutDisplayFunc(fnDisplay); 
    glutReshapeFunc(fnReshape); 
    glutMainLoop(); 
    return 0; 
} 
0

Existen muchos métodos para hacerlo. El siguiente ejemplo muestra cómo lo estoy haciendo en mis proyectos y su funcionamiento. El ejemplo muestra simplemente un rectángulo que no cambia su tamaño cuando se cambia el tamaño de la ventana. Puede copiar y pegar directamente en su proyecto (considero que soy un principiante OpenGL)

#include<windows.h> 
#include<glut.h> 

GLfloat x1=100.0f; 
GLfloat y1=150.0f; 
GLsizei rsize = 50; 

GLfloat xstep=1.0f; 
GLfloat ystep=1.0f; 

GLfloat windowWidth; 
GLfloat windowHeight; 


void scene(void){ 
    glClear(GL_COLOR_BUFFER_BIT); 
    glColor3f(1.0f,0.0f,0.0f); 

    glRectf(x1,y1,x1+rsize,y1-rsize); 

    glutSwapBuffers(); 
} 

void setupRc(void){ 

    glClearColor(0.0f,0.0f,1.0f,0.0f); 

} 

void changeSize(GLsizei w,GLsizei h){ 
    if(h==0) 
    h=1; 
    glViewport(0,0,w,h); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    if(w>=h){ 
    windowWidth=250.0f*w/h; 
    windowHeight=250.f; 
    } 
    else{ 
    windowWidth=250.0f; 
    windowHeight=250.f*h/w; 
    } 

    glOrtho(0.0f,windowWidth,0.0f,windowHeight,1.0f,-1.0f); 


    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 


} 




void main(void){ 

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); 

    glutCreateWindow("Rectangle"); 

    glutReshapeFunc(changeSize); 
    glutDisplayFunc(scene); 
    setupRc(); 

    glutMainLoop(); 

} 
Cuestiones relacionadas