Jump to content

  • Log In with Google      Sign In   
  • Create Account

FPS Camera in GLFW has noticable render/input lag


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 JoeyDewd   Members   -  Reputation: 611

Like
1Likes
Like

Posted 08 May 2014 - 04:18 AM

Hey guys!
 
I'm working on a large OpenGL tutorial and decided to use GLFW for my tutorial content. However, when I started to re-implement my camera interface (using Eular angles) in GLFW I noticed large render stutters. If I start moving my camera around with the keyboard (thus moving forward/backward and straving) while moving my mouse at the same time, it looks like it skips/stutters certain frames (it's not a smoothly interpolated scene anymore). See the following video that demonstrates my issue:

 

https://www.youtube.com/watch?v=C0onl-Pftp4

Note that everything runs smooth when just using the wasd keys. Also, when moving just the camera, it runs smooth as well. It is the combination of the two that causes the stutters. 

 

There are several possible causes of the issue, but I've tested them and could not really get to a conclsuion:

  • There is something wrong with how I handle my input via GLFW (maybe pollEvents should not be called here?)
  • By setting my cursor back with glfwSetCursorPos it might move the cursor in such a way that it introdues the aforementioned issues (although I thought I conquered this using the mouseSet boolean variable).
  • There is something wrong with the logic?

I'm having difficulties with that the cause of my errors could be and was hoping any of you could see the flaw in my code:

...
// Camera
bool keys[1024];
GLfloat yaw;
GLfloat pitch;
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
GLboolean mouseSet = false;
....

while(!glfwWindowShouldClose(window))
{
    // Check and call events
    glfwPollEvents();
    Do_Movement();  // Does keyboard movement based on pressed keys

    // Render Code
    ...

    // Swap the buffers
    glfwSwapBuffers(window);
}

// Moves/alters the camera positions based on user input
void Do_Movement()
{
	// Camera controls
	GLfloat cameraSpeed = 0.05f;
	if(keys[GLFW_KEY_W])
		cameraPos += cameraSpeed * cameraFront;
	if(keys[GLFW_KEY_S])
		cameraPos -= cameraSpeed * cameraFront;
	if(keys[GLFW_KEY_A])
		cameraPos -= glm::cross(cameraFront, cameraUp) * cameraSpeed;
	if(keys[GLFW_KEY_D])
		cameraPos +=  glm::cross(cameraFront, cameraUp) * cameraSpeed;
}

// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
	if(action == GLFW_PRESS)
		keys[key] = true;
	else if(action == GLFW_RELEASE)
		keys[key] = false;	
}

// Callback function for mouse movement
void mouse_callback(GLFWwindow* window, double xpos, double ypos)
{
	if(!mouseSet)
	{
		GLfloat centerX, centerY;
		centerX = windowWidth / 2;
		centerY = windowHeight / 2;

		GLfloat xoffset = xpos - centerX;
		GLfloat yoffset = ypos - centerY;
		GLfloat sensitivity = 800.0f;
		xoffset /= sensitivity;
		yoffset /= sensitivity;

		yaw += -xoffset;
		pitch += -yoffset;

		cameraFront = glm::vec3(cos(pitch) * sin(yaw), sin(pitch), cos(pitch) * cos(yaw));

		mouseSet = true;
		glfwSetCursorPos(window, centerX, centerY);
	}
	else
		mouseSet = false;
}

If there is anything missing in the code you'd still like to know, please let me know and I'll upload the required source code.

Thanks in advance.


Edited by JoeyDewd, 08 May 2014 - 06:12 AM.


Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS