Jump to content
  • Advertisement
calioranged

OpenGL Updating Aspect Ratio When Window Resized

Recommended Posts

Posted (edited)

I would like the scene to resize relative to the window size... if the cube is being drawn in the centre of the screen originally then I would like it to maintain this centralised position if the the window dimensions change. In the image below (on the right), when the screen was resized (manually by the user), the cube didn't maintain a position central to the window, it just stayed in its original position and did not update to the new aspect ratio. I would like to know how to solve this problem.

Original WindowResized Window

This is how I originally create a window:

GLFW::GLFW(int right, int top)
	: m_Window()
{
	if (!glfwInit())
	{
		std::cout << "GLFW initialisation failed!\n";
		__debugbreak();
	}

	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	m_Window = glfwCreateWindow(right, top, "OpenGL Window", NULL, NULL);

	if (!m_Window)
	{
		glfwTerminate();
		std::cout << "Could not create window!\n";
		__debugbreak();
	}

	glfwMakeContextCurrent(m_Window);
	glfwSwapInterval(2);

	if (glewInit() != GLEW_OK)
	{
		std::cout << "GLEW initialisation failed!\n";
		__debugbreak();
	}

	std::cout << glGetString(GL_VERSION) << std::endl;
}

But I am unsure of how to retrieve the new aspect ratio from OpenGL upon resizing of the window? 

From searching around, I can see glViewport is used to set the new aspect ratio, and is usually implemented similarly to this inside the main loop:

while (!glfw.WindowShouldClose())  
{
  shader.ClearBuffers(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glViewport(0, 0, new_width, new_height);
  MVP.projection = glm::perspective(glm::radians(60.0F), (float)new_width / (float)new_height, 0.1F, 10.0F);
  shader.SetMVPUniform("u_MVP", MVP.GetMVP());
  Cube_IBO.DrawElements(CubeIndices.size());
  glfw.SwapBuffers(); 
}

But it is unclear to me how we obtain the new aspect ratio (new_width / new_height) once the window dimensions have been manually changed.

Edited by calioranged

Share this post


Link to post
Share on other sites
Advertisement

Oops, a correction: there is no need to obtain the new values separately, they are already passed in by the callback parameters.

 

Share this post


Link to post
Share on other sites
22 hours ago, Green_Baron said:

Oops, a correction: there is no need to obtain the new values separately, they are already passed in by the callback parameters.

Great. Thanks for your help.

Edited by calioranged

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!