Introduction
I'm doing an OpenGL program in C. As of now I'm working on the 3D camera system and got the control right. Now I'm working on the mouse control. It works, but I have used two different ways to do it.
The problem
The two different ways are using a global variable and callback, and just using a local variable and function.
I don't know which one is better. One seems to loop over itself every time and the other only when the mouse moves, but uses a global variable.
For all the following code you will see that the Camera
struct is defined as:
Camera camera = {
.view = GLM_MAT4_IDENTITY_INIT,
.pos = { 0.f,0.f,3.f },
.target = { 0.f, 0.f, 0.f },
.upAxe = { 0.0f, 1.0f, 0.0f },
.front = { 0.0f, 0.0f, -1.0 },
.yaw = -90.f,
.pitch = 0.f,
.lastX = SCR_HEIGHT / 2,
.lastY = SCR_WIDTH / 2,
.lastFrame = 0.0f,
.deltaTime = 0.0f
};
The code
First the "global" way:
I have only shown the intended part of the code; other code is not pertinent.
main.c
:
void mouseCallBack(GLFWwindow * window, double xpos, double ypos);
Camera camera = {
.view = GLM_MAT4_IDENTITY_INIT,
.pos = { 0.f,0.f,3.f },
.target = { 0.f, 0.f, 0.f },
.upAxe = { 0.0f, 1.0f, 0.0f },
.front = { 0.0f, 0.0f, -1.0 },
.yaw = -90.f,
.pitch = 0.f,
.lastX = SCR_HEIGHT / 2,
.lastY = SCR_WIDTH / 2,
.lastFrame = 0.0f,
.deltaTime = 0.0f
};
int main()
{
glfwSetCursorPosCallback(window, mouseCallBack);
while (!glfwWindowShouldClose(window))
{
}
}
void mouseCallBack(GLFWwindow * window, double xpos, double ypos)
{
float xoffset = xpos - camera.lastX;
float yoffset = camera.lastY - ypos;
camera.lastX = xpos;
camera.lastY = ypos;
float sensivity = 0.05f;
xoffset *= sensivity;
yoffset *= sensivity;
camera.yaw += xoffset;
camera.pitch += yoffset;
if (camera.pitch > 89.f) camera.pitch = 89.f;
if (camera.pitch < -89.f) camera.pitch = -89.f;
vec3 front;
front[0] = cos(glm_rad(camera.yaw)) * cos(glm_rad(camera.pitch));
front[1] = sin(glm_rad(camera.pitch));
front[2] = sin(glm_rad(camera.yaw)) * cos(glm_rad(camera.pitch));
glm_normalize_to(front, camera.front);
}
The function and local way:
int main()
{
Camera camera = {
.view = GLM_MAT4_IDENTITY_INIT,
.pos = { 0.f,0.f,3.f },
.target = { 0.f, 0.f, 0.f },
.upAxe = { 0.0f, 1.0f, 0.0f },
.front = { 0.0f, 0.0f, -1.0 }
};
camera.yaw = 0.f;
camera.pitch = 0.f;
camera.lastX = SCR_HEIGHT / 2;
camera.lastY = SCR_WIDTH / 2;
camera.lastFrame = 0.0f;
camera.deltaTime = 0.0f;
while (!glfwWindowShouldClose(window))
{
//input
processMouse(window, &camera);
}
}
and in GLFWfunction.c
:
void processMouse(GLFWwindow * window, Camera * camera)
{
double xpos;
double ypos;
glfwGetCursorPos(window, &xpos, &ypos);
if (xpos != camera->lastX || ypos != camera->lastY)
{
float xoffset = xpos - camera->lastX;
float yoffset = camera->lastY - ypos;
float sensivity = 0.1f;
xoffset *= sensivity;
yoffset *= sensivity;
camera->yaw += xoffset;
camera->pitch += yoffset;
if (camera->pitch > 89.0f) camera->pitch = 89.f;
if (camera->pitch < -89.0f) camera->pitch = -89.f;
vec3 front;
front[0] = cos(glm_rad(camera->pitch)) * cos(glm_rad(camera->yaw));
front[1] = sin(glm_rad(camera->pitch));
front[2] = sin(glm_rad(camera->yaw)) * cos(glm_rad(camera->pitch));
glm_normalize_to(front, camera->front);
camera->lastX = xpos;
camera->lastY = ypos;
}
}
What would you choose? My eyes tell me that the global alternative is good and clean, but contradict a bit of the C logic whereas the function one is good but is poorly written.
1 Answer 1
Is the camera a global entity? Will the only ever be just one?
Perhaps, but perhaps not. You can have two or more viewpoints shown in multiple viewports. You might use shadow mapping, which positions a camera at a light source for rendering a shadow map.
If you are thinking of a generic "mouse controller" for a camera, which you can reuse in multiple applications, I’d shy away from the global camera.
If you are working on a one of a kind application, have no intention of reusing the code, will only ever use one point-of-view, and needs the minuscule speed gain and reduction of complexity of not passing a pointer to the camera around to functions as required, a global camera is fine.
Explore related questions
See similar questions with these tags.
glfwGetCursorPos()
. \$\endgroup\$