Advertisement Jump to content
Sign in to follow this  

OpenGL My rectangle is not repositioning itself.

This topic is 1261 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is my code:

I took it from an example and added a rectangle.

When I resize the window the rectangle is not repositioning itself.

//Include GLEW
#include <GL/glew.h>

//Include GLFW
#include <GLFW/glfw3.h>

//Include the standard C++ headers
#include <stdio.h>
#include <stdlib.h>

//Define an error callback
static void error_callback(int error, const char* description)
	fputs(description, stderr);

//Define the key input callback
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
	if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
		glfwSetWindowShouldClose(window, GL_TRUE);

int main(void)
	//Set the error callback

	//Initialize GLFW
	if (!glfwInit())

	//Set the GLFW window creation hints - these are optional
	//glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //Request a specific OpenGL version
	//glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); //Request a specific OpenGL version
	//glfwWindowHint(GLFW_SAMPLES, 4); //Request 4x antialiasing

	//Declare a window object
	GLFWwindow* window;

	//Create a window and create its OpenGL context
	window = glfwCreateWindow(640, 480, "Test Window", NULL, NULL);

	//If the window couldn't be created
	if (!window)
		fprintf(stderr, "Failed to open GLFW window.\n");

	//This function makes the context of the specified window current on the calling thread. 

	//Sets the key callback
	glfwSetKeyCallback(window, key_callback);

	//Initialize GLEW
	GLenum err = glewInit();

	//If GLEW hasn't initialized
	if (err != GLEW_OK)
		fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
		return -1;

	//Set a background color
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

	//Main Loop
		//Clear color buffer

		glVertex2f(-0.5f, 0.5f);
		glVertex2f(0.5f, 0.5f);
		glVertex2f(0.5f, -0.5f);
		glVertex2f(-0.5f, -0.5f);

		//Swap buffers
		//Get and organize events, like keyboard and mouse input, window resizing, etc...

	} //Check if the ESC key had been pressed or if the window had been closed
	while (!glfwWindowShouldClose(window));

	//Close OpenGL window and terminate GLFW
	//Finalize and clean up GLFW


Share this post

Link to post
Share on other sites
According to this, you need to set a callback for the framebuffer and call glViewport in that callback.

I guess I should mention why this is happening. GLFW provides a windowing framework around OpenGL, however, it has to be told your intent. When you resize the window, do you want to move the viewport and keep the size or resize it to the window? OpenGL doesn't have a concept of a "window" and if GLFW sends window size and position data to OpenGL without your express knowledge, strange things can and will happen. Edited by MarkS

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. 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!