Author Topic: Scaling / resizing in OpenGL?  (Read 8515 times)

0 Members and 1 Guest are viewing this topic.

Offline Gore Motel

  • C= 64
  • **
  • Posts: 45
  • Karma: 18
    • View Profile
    • Gore Motel
Scaling / resizing in OpenGL?
« on: December 11, 2013 »
Hey everyone! I hope someone well versed in OpenGL can help me out with a problem I'm having.

I'm drawing 2D stuff with OpenGL in immediate mode. I'm using orthographic projection and setting the viewport to contain the entire window. Here's an example:

Code: [Select]
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glOrtho(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
//
// draw stuff
//

Everything looks good at that size. Now, how do I scale or resize everything that I draw so that it will look good in a window of different dimensions?

If I draw a rectangle like so:

Code: [Select]
glBegin(GL_QUADS);
glVertex2i(20, 100);
glVertex2i(40, 100);
glVertex2i(40, 150);
glVertex2i(20, 150);
glEnd;

It will look just how I want it to look if the window has its original dimensions. If I change the width and height of the window, the rectangle stays the same, but I want it to move and scale proportionally.

How do I figure out where the rectangle should be positioned and what dimensions it should have when I resize the window?

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: Scaling / resizing in OpenGL?
« Reply #1 on: December 11, 2013 »
Hi Gore Motel :)

You're probably best off multiplying your sizes and offsets according to the current window dimensions. I don't think there's any way to have that happen automatically with glOrtho, so you'll need to calculate the values for scaling yourself. You should also remember to take aspect ratio into account if you want to avoid your objects getting distorted. You'll end up with black bars at the top/bottom  (like a widescreen movie)  or left/right.

This question seems to be pretty closely related to what you're trying to do. It's in relation to OpenGL in Java, but the principles should help you.

Cheers,

Raizor
raizor

Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Scaling / resizing in OpenGL?
« Reply #2 on: December 11, 2013 »
With glOrtho(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT, 0, 1); you create a coordinate system which is just as big as your window.
So when the size of your window changes, your coordinate system changes, too. That's not very nice to handle.
You probably just want to pick a constant width and height for your coordinate system - eg. glOrtho(0,1280, 0,720, 0,1).
So no matter how large your window is, the area 0,0..1280,720 always fills the whole window.
Now you just have to figure out how to compensate changing aspect ratios...
Challenge Trophies Won:

Offline Gore Motel

  • C= 64
  • **
  • Posts: 45
  • Karma: 18
    • View Profile
    • Gore Motel
Re: Scaling / resizing in OpenGL?
« Reply #3 on: December 18, 2013 »
Thanks for the tips, guys! I tried some stuff on my own and got all sorts of weird results. Then I stumbled upon this article: OpenGL 2D Independent Resolution Rendering, by David Amador. His solution works perfectly and it's not hard to implement.

Offline Canopy

  • Atari ST
  • ***
  • Posts: 208
  • Karma: 20
    • View Profile
Re: Scaling / resizing in OpenGL?
« Reply #4 on: December 18, 2013 »

just a note: all the matrix functions are deprecated in "modern gl" (i thnk in v3+), this means it still works now but may not in the future

(honestly, its not likely to ever be taken away, but it definitely doesn't exist in OpenGL ES, so if you have ambitions to port to iOS or a linux based O?/S like android you'll have to do things the newer way)

its not really difficult, but can be confusing at first to manage it all manually, there are some nice c++ helpers out there (e.g. GLM) to do it, but you know how some people like to do things themselves.

you basically have two 'lists' of transforms.

firstly the 'world'  or  'scene' one.. which you can apply AR correction (as described in the article). and when doing 3d, apply global transforms ('camera') to the world/scene at this level.

the second list you have for each entity (model/mesh), and that can be rotation/translated(moved)/scaled individually.

when calculating the final transform matrix for a mesh/model the transforms are mutiplied together. you apply all the model ones first, then the world ones. (see how the world applies too all in the scene, the model applies to only the model/mesh)

once you have your head around it and think about where changes are made it can be efficient to cache each entities world/model coords and only update them when changes occur.

For example. if the world level stuff hasn't changed you can only recalculate for models that have moved, and if a second frame is drawn without any movement at all there is no need recalculate any matrixes at all.


if you're interested in rolling your own, there seem to have been quite a few people doing it their own way even with old style GL

https://www.opengl.org/discussion_boards/showthread.php/128764-Using-OpenGL-with-your-own-Matrices
http://stackoverflow.com/questions/9092010/how-to-update-opengl-modelview-matrix-with-my-own-4x4-matrix


Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Scaling / resizing in OpenGL?
« Reply #5 on: December 18, 2013 »
spot on with the advice there canopy! when it comes too coding in gl please for anyone avoid built in matrix functions completely esp when doing glsl. they just lead too problems further down the road, hard too read code/bugs etc.

GLM is an excellent lib does everything really and keeps the code nice and clean.
Challenge Trophies Won:

Offline Gore Motel

  • C= 64
  • **
  • Posts: 45
  • Karma: 18
    • View Profile
    • Gore Motel
Re: Scaling / resizing in OpenGL?
« Reply #6 on: February 11, 2014 »
I have now moved to modern OpenGL, actually WebGL to be exact. Scaling is much easier, since the browser can do all the work. And for my matrix operations, I use glMatrix.