Mouse control of a camera in an OpenGL program
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.
c comparative-review event-handling opengl
New contributor
add a comment |
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.
c comparative-review event-handling opengl
New contributor
Why is "the function one is good but is poorly written?" They seem identical save for the call toglfwGetCursorPos()
.
– user1118321
1 hour ago
add a comment |
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.
c comparative-review event-handling opengl
New contributor
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.
c comparative-review event-handling opengl
c comparative-review event-handling opengl
New contributor
New contributor
edited 9 mins ago
200_success
128k15150412
128k15150412
New contributor
asked 4 hours ago
Cewein
161
161
New contributor
New contributor
Why is "the function one is good but is poorly written?" They seem identical save for the call toglfwGetCursorPos()
.
– user1118321
1 hour ago
add a comment |
Why is "the function one is good but is poorly written?" They seem identical save for the call toglfwGetCursorPos()
.
– user1118321
1 hour ago
Why is "the function one is good but is poorly written?" They seem identical save for the call to
glfwGetCursorPos()
.– user1118321
1 hour ago
Why is "the function one is good but is poorly written?" They seem identical save for the call to
glfwGetCursorPos()
.– user1118321
1 hour ago
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Cewein is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210291%2fmouse-control-of-a-camera-in-an-opengl-program%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Cewein is a new contributor. Be nice, and check out our Code of Conduct.
Cewein is a new contributor. Be nice, and check out our Code of Conduct.
Cewein is a new contributor. Be nice, and check out our Code of Conduct.
Cewein is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f210291%2fmouse-control-of-a-camera-in-an-opengl-program%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why is "the function one is good but is poorly written?" They seem identical save for the call to
glfwGetCursorPos()
.– user1118321
1 hour ago