Jump to content
SM

Janela OpenGL

Recommended Posts

SM

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

Share this post


Link to post
Share on other sites
HappyHippyHippo

- 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

Share this post


Link to post
Share on other sites
HappyHippyHippo

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

Share this post


Link to post
Share on other sites
SM

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

Share this post


Link to post
Share on other sites
SM

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

}

Share this post


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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • 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.