PDA

View Full Version : Do not resize the DrawArea



tkausl
02-24-2015, 10:34 PM
Hello there,

this is probably a stupid question, simple math, or something else, i am not sure.

What i have is a window with a small 3D scene (thats just 2 models rendered) in opengl. It starts as a window in 1024x768, thats cool. Then i resize the window (maximize it), and my handler does on resizing the following:


void onResize(GLFWwindow* window, int width, int height){
//This is my projection-matrix which gets multiplicated with the model and viewmatrix, the usual stuff.
Projection = glm::perspective(45.0f, (float)width/(float)height, 0.1f, 100.0f);
glViewport(0, 0, width, height);
} and it works great. Nothing stretched, everything in the correct resolution. But: It just draws everything bigger. So if i would resize my window from, lets say, 500x500 to 1000x1000 it would just draw it twice as big (in both dimensions), but what i would like to have is, that it does not draws it bigger, but just draws "more" of the scene around. You understand what i mean? So if you look through a hole in a piece of paper, and then just remove the paper, you don't see the exact same things bigger, but you see actually more around you.
What i actually tried is, remove one of the both lines in the onResize function, but this does more harm then help.

My guess is, that its just a matrix i have to set up correctly or a gl-call i have to do but i'm clueless since the glm::perspective does not take any size-arguments (just the aspect,ratio) and glViewport seems to just set up the screen-area i can draw in but has nothing to do with the actual drawing.

GClements
02-25-2015, 10:45 AM
what i would like to have is, that it does not draws it bigger, but just draws "more" of the scene around.

IOW, you want a larger view angle.



the glm::perspective does not take any size-arguments (just the aspect,ratio)

It takes an aspect ratio and a view angle.



and glViewport seems to just set up the screen-area i can draw in but has nothing to do with the actual drawing.
glViewport() sets the portion of the window which is mapped to X and Y coordinates of the clip space. Enlarging the viewport while keeping all other factors (e.g. the projection matrix) constant will enlarge the rendered image).

If you want a larger window to result in a larger view angle, you could just calculate the appropriate view angle and aspect ratio based upon the window dimensions, but it's probably easier to use glm::frustum() instead. glm::perspective() is effectively just a convenience wrapper; any glm::perspective() call can be translated to an equivalent glm::frustum() call, i.e.


double rf = tan(fovy/2);
double dy = zNear * rf;
double dx = aspect * dy;
return glm::frustum(-dx,dx,-dy,dy,zNear,zFar);

So rather than calculating dx and dy based upon fovy and aspect, you can just calculate them as e.g.


double dy = zNear * window_width / size;
double dx = zNear * window_height / size;
return glm::frustum(-dx,dx,-dy,dy,zNear,zFar);

where size is the window size corresponding to a 90 view angle (45).

EmJayJay
02-28-2015, 05:15 AM
double rf = tan(fovy/2);
double dy = zNear * rf;
double dx = aspect * dy;
return glm::frustum(-dx,dx,-dy,dy,zNear,zFar);

So rather than calculating dx and dy based upon fovy and aspect, you can just calculate them as e.g.


double dy = zNear * window_width / size;
double dx = zNear * window_height / size;
return glm::frustum(-dx,dx,-dy,dy,zNear,zFar);

where size is the window size corresponding to a 90 view angle (45).
Can you clarify that the fovy means field of view on Y axis/plane and not the usually implied X axis/plane which is usually the case.

GClements
02-28-2015, 06:56 PM
Can you clarify that the fovy means field of view on Y axis/plane
Yes. fovy is the angle between the top and bottom edges of the view frustum (the angle between either edge and the centre is fovy/2).