Jump to content

Recommended Posts

Posted (edited)

Boas, como se redimensiona uma janela em openGL de forma a nunca deformar o seu conteudo ?

deixo aqui a função que tinha pra reshape :

void myReshape(GLsizei w, GLsizei h) {
   glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(-2.0f, 2.0f, -2.0f, 2.0f, -2.0f, 2.0f);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}
Edited by thoga31
Colocado GeSHi + removidas maiúsculas do título
Posted (edited)

- saber o rácio largura altura

- converter os dados conforme esse racio

void myReshape(GLsizei w, GLsizei h) {
   double ratio = 0.0;

   glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();

   if (w > h)
   {
       ratio = w / h;
       glOrtho(-2.0f * ratio,
                2.0f * ratio,
               -2.0f,
                2.0f,
               -2.0f,  2.0f);
   }
   else
   {
       ratio = h / w;
       glOrtho(-2.0f,
                2.0f,
               -2.0f * ratio,
                2.0f * ratio,
               -2.0f,  2.0f);
   }

   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}

ps : não testado

Edited by HappyHippyHippo
IRC : sim, é algo que ainda existe >> #p@p
Posted (edited)

Com esse código sempre que tento mudar o tamanho da janela , ela desaparece.. O que posso fazer para isso nao acontecer?

E quando recupero a execução a janela já esta maior e a imagem do viewport toda deformada.

Edited by SM
Posted

#include "main.h"

#include <GLUT/glut.h>

void myReshape(GLsizei w, GLsizei h) {

double ratio = 0.0;

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

if (w > h)

{

ratio = w / h;

glOrtho(-2.0f * ratio,

2.0f * ratio,

-2.0f,

2.0f,

-2.0f, 2.0f);

}

else

{

ratio = h / w;

glOrtho(-2.0f,

2.0f,

-2.0f * ratio,

2.0f * ratio,

-2.0f, 2.0f);

}

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

}

void myDisplay(void) {

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

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(0.0f, 1.0f, 0.0f);

glBegin(GL_POLYGON);

glVertex3f(-1.0f, -1.0f, 0.0f);

glVertex3f(0.0f, -1.0f, 0.0f);

glVertex3f(0.0f, 1.0f, 0.0f);

glVertex3f(-1.0f, 1.0f, 0.0f);

glEnd();

glColor3f(1.0f, 0.0f, 0.0f);

glBegin(GL_POLYGON);

glVertex3f(1.0f, 1.0f, 0.0f);

glVertex3f(1.0f, -1.0f, 0.0f);

glVertex3f(0.0f, -1.0f, 0.0f);

glVertex3f(0.0f, 1.0f, 0.0f);

glEnd();

glFlush();

}

int main(int argc, char** argv) {

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize (400, 400);

glutInitWindowPosition (-1, -1);

glutCreateWindow(" Teste");

glutDisplayFunc(myDisplay);

glutReshapeFunc(myReshape);

glutMainLoop();

}

Posted
void myReshape(GLsizei w, GLsizei h) {
   double ratio = (GLdouble)w / (GLdouble)h;

   glViewport(0, 0, w, h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   if (ratio >= 1.0)
   {
       glOrtho(-2.0f * ratio,
               2.0f * ratio,
               -2.0f,
               2.0f,
               -2.0f,  2.0f);
   }
   else
   {
       glOrtho(-2.0f,
               2.0f,
               -2.0f * (1.0 / ratio),
               2.0f * (1.0 / ratio),
               -2.0f,  2.0f);
   }
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}
IRC : sim, é algo que ainda existe >> #p@p

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.