Jump to content

Janela OpenGL


SM

Recommended Posts

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
Link to comment
Share on other sites

- 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
Link to comment
Share on other sites

foi confirmar (não testei, vi na net) é parece que eu estava correcto ...

se continuas com problemas então não será no código apresentado e terás de dar um exemplo do que está a acontecer (e provavelmente mais código relacionado com o problema)

IRC : sim, é algo que ainda existe >> #p@p
Link to comment
Share on other sites

#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();

}

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.