Jonas
Jonas

Reputation: 1069

Resize GLUT window without resizing content?

I couldn't achieve the following behavior. I want to resize my window from the bottom without having the rendered scene distorted or changing its position.

enter image description here

I'm using C++ OpenGL GLUT

void resize(int w, int h)
{
    float ratio = G_WIDTH / G_HEIGHT;
    glViewport(0, 0, (h-(w/ratio))>0? w:(int)(h*ratio), (w-(h*ratio))>0? h:(int)(w/ratio));
}

Upvotes: 2

Views: 7508

Answers (4)

VinceFior
VinceFior

Reputation: 1279

Here's the solution I found, which I think works quite well.

I wrote my reshape method as follows, which is registered with glutReshapeFunc(reshape) in my main method:

void reshape(int width, int height) {
    glViewport(0, 0, width, height);
    aspect = ((double) width) / height;

And whenever I redraw the scene, I use this call to set the camera:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, aspect, 1.0, 100.0); // 45 degrees fovY, correct aspect ratio
// then remember to switch back to GL_MODELVIEW and call glLoadIdentity

This lets me treat the window as, well, a window into my scene, without scaling or moving the scene. I hope this is what you're looking for!

Upvotes: 1

Majid Max
Majid Max

Reputation: 517

if you are using orthogonal perspective use this:

#include <GL/glut.h>

int x0, y0 = 0;
int ww, hh = 0;

void updateCamera()
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(x0, x0+ww, y0, y0+hh, -1, 1);
  glScalef(1, -1, 1);
  glTranslatef(0, -hh, 0);
}

void reshape(int w, int h)
{
    ww = w;
    hh = h;
    glViewport(0, 0, w, h);  
    updateCamera();
}

void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  glBegin(GL_TRIANGLES);
    glColor3f(0.0, 0.0, 1.0); glVertex2i(0, 0);
    glColor3f(0.0, 1.0, 0.0); glVertex2i(200, 200);
    glColor3f(1.0, 0.0, 0.0); glVertex2i(20, 200);
  glEnd();
  glFlush();
  glutPostRedisplay(); 
}

int mx = 0;
int my = 0;
int dragContent = 0;

void press(int button, int state, int x, int y)
{
    mx = x;
    my = y;
    dragContent = button == GLUT_LEFT_BUTTON && state == GLUT_DOWN;
}

void move(int x, int y)
{
    if(dragContent)
    {
        x0 -= x - mx;
        y0 += y - my;
        mx = x;
        my = y;
        updateCamera();
    }
}

int main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutCreateWindow("Resize window without resizing content + drag content");
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutMouseFunc(press);
  glutMotionFunc(move);
  glutMainLoop();
  return 0;
}

you can use left mouse to drag content (triangle).

Upvotes: 2

SinisterMJ
SinisterMJ

Reputation: 3509

ratio = wantedWidth / wantedHeight
glViewport(0, 0, 
      (height-(width/ratio))>0? width:(int)(height*ratio),
  (width-(height*ratio))>0? height:(int)(width/ratio);

would do that, it keeps the ratio always the same between height / width of the window, while also trying to use the maximum size of the window available.

Edit: put it to always upper left corner.

Upvotes: 1

Mark Stevens
Mark Stevens

Reputation: 2366

In the GLUT resize scene function, the viewport is probably set to the size of the window by default. I would try just changing that to be the (fixed) size you want:

Instead of:

void windowReshapeFunc( GLint newWidth, GLint newHeight ) 
{
  glViewport( 0, 0, newWidth, newHeight );
  .. etc...
}

Do this:

void windowReshapeFunc( GLint newWidth, GLint newHeight ) 
{
  glViewport( 0, 0, 400, 600 );
  .. etc...
}

Or whatever size you want. The window will still resize, but these scene will always render to the (now fixed) viewport.

Upvotes: 3

Related Questions