3d Object Dragging with mouse click OpenGL
I'm trying to left click and drag any 3d Object and if i let go of it, it should stay in its new position, how would i achieve this? The 3d object is loaded from the draw function that i have in a header file.
Someone said i should be using glutMouseFunc or glutMotionFunc.
void MouseClickCallbackFunction(int button, int state, int x, int y)
{
if (state == GLUT_DOWN) {
if (button == GLUT_LEFT)
{
std::cout << "Left " << x << " " << y <<std::endl;
leftClick.x = x;
leftClick.y = y;
}
else if (button == GLUT_RIGHT_BUTTON) {
std::cout << "Right " << x << " " << y << std::endl;
rightClick.x = x;
rightClick.y = y;
}
}
theGame->mouseClicked(button, state, x, y);
glutPostRedisplay();
}
/* function MouseMotionCallbackFunction()
* Description:
* - this is called when the mouse is clicked and moves
*/
void MouseMotionCallbackFunction(int x, int y)
{
theGame->mouseMoved(x, y);
glutPostRedisplay();
}
int main(int argc, char **argv)
{
/* initialize the window and OpenGL properly */
glutInit(&argc, argv);
glutInitContextVersion(4, 2);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow("OpenGL Framework");
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
std::cout << "GLEW could not be initialized. n";
system("pause");
return 0;
}
//glewInit();
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
/* set up our function callbacks */
glutDisplayFunc(DisplayCallbackFunction);
glutKeyboardFunc(KeyboardCallbackFunction);
glutKeyboardUpFunc(KeyboardUpCallbackFunction);
glutMouseFunc(MouseClickCallbackFunction);
glutMotionFunc(MouseMotionCallbackFunction);
glutTimerFunc(1, TimerCallbackFunction, 0);
/* init the game */
theGame = new Game();
theGame->initializeGame();
/* start the game */
glutMainLoop();
return 0;
}
Here is the Draw fucntion with the object
void Game::draw()
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
PassThrough.Bind();
PassThrough.SendUniformMat4("uModel", MonkeyTransform.data, false);
PassThrough.SendUniformMat4("uView", CameraTransform.GetInverse().data, false);
PassThrough.SendUniformMat4("uProj", CameraProjection.data, false);
PassThrough.SendUniform("uTex", 0);
PassThrough.SendUniform("LightPosition", CameraTransform.GetInverse() * vec4(4.0f,0.0f,0.0f,1.0f));
PassThrough.SendUniform("LightAmbient", vec3(0.15f, 0.15f, 0.15f));
PassThrough.SendUniform("LightDiffuse", vec3(0.7f,0.1f,0.2f));
PassThrough.SendUniform("LightSpecular", vec3(0.8f,0.1f,0.1f));
PassThrough.SendUniform("LightSpecularExponent", 50.0f);
PassThrough.SendUniform("Attenuation_Constant", 1.0f);
PassThrough.SendUniform("Attenuation_Linear", 0.1f);
PassThrough.SendUniform("Attenuation_Quadratic", 0.01f);
glBindVertexArray(Monkey.VAO);
glDrawArrays(GL_TRIANGLES, 0, Monkey.GetNumVerticies());
glBindVertexArray(0);
PassThrough.unBind();
glutSwapBuffers();
}
c++ opengl glut
add a comment |
I'm trying to left click and drag any 3d Object and if i let go of it, it should stay in its new position, how would i achieve this? The 3d object is loaded from the draw function that i have in a header file.
Someone said i should be using glutMouseFunc or glutMotionFunc.
void MouseClickCallbackFunction(int button, int state, int x, int y)
{
if (state == GLUT_DOWN) {
if (button == GLUT_LEFT)
{
std::cout << "Left " << x << " " << y <<std::endl;
leftClick.x = x;
leftClick.y = y;
}
else if (button == GLUT_RIGHT_BUTTON) {
std::cout << "Right " << x << " " << y << std::endl;
rightClick.x = x;
rightClick.y = y;
}
}
theGame->mouseClicked(button, state, x, y);
glutPostRedisplay();
}
/* function MouseMotionCallbackFunction()
* Description:
* - this is called when the mouse is clicked and moves
*/
void MouseMotionCallbackFunction(int x, int y)
{
theGame->mouseMoved(x, y);
glutPostRedisplay();
}
int main(int argc, char **argv)
{
/* initialize the window and OpenGL properly */
glutInit(&argc, argv);
glutInitContextVersion(4, 2);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow("OpenGL Framework");
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
std::cout << "GLEW could not be initialized. n";
system("pause");
return 0;
}
//glewInit();
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
/* set up our function callbacks */
glutDisplayFunc(DisplayCallbackFunction);
glutKeyboardFunc(KeyboardCallbackFunction);
glutKeyboardUpFunc(KeyboardUpCallbackFunction);
glutMouseFunc(MouseClickCallbackFunction);
glutMotionFunc(MouseMotionCallbackFunction);
glutTimerFunc(1, TimerCallbackFunction, 0);
/* init the game */
theGame = new Game();
theGame->initializeGame();
/* start the game */
glutMainLoop();
return 0;
}
Here is the Draw fucntion with the object
void Game::draw()
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
PassThrough.Bind();
PassThrough.SendUniformMat4("uModel", MonkeyTransform.data, false);
PassThrough.SendUniformMat4("uView", CameraTransform.GetInverse().data, false);
PassThrough.SendUniformMat4("uProj", CameraProjection.data, false);
PassThrough.SendUniform("uTex", 0);
PassThrough.SendUniform("LightPosition", CameraTransform.GetInverse() * vec4(4.0f,0.0f,0.0f,1.0f));
PassThrough.SendUniform("LightAmbient", vec3(0.15f, 0.15f, 0.15f));
PassThrough.SendUniform("LightDiffuse", vec3(0.7f,0.1f,0.2f));
PassThrough.SendUniform("LightSpecular", vec3(0.8f,0.1f,0.1f));
PassThrough.SendUniform("LightSpecularExponent", 50.0f);
PassThrough.SendUniform("Attenuation_Constant", 1.0f);
PassThrough.SendUniform("Attenuation_Linear", 0.1f);
PassThrough.SendUniform("Attenuation_Quadratic", 0.01f);
glBindVertexArray(Monkey.VAO);
glDrawArrays(GL_TRIANGLES, 0, Monkey.GetNumVerticies());
glBindVertexArray(0);
PassThrough.unBind();
glutSwapBuffers();
}
c++ opengl glut
take a look at this Compute objects moving with arrows and mouse
– Spektre
Nov 15 '18 at 6:11
add a comment |
I'm trying to left click and drag any 3d Object and if i let go of it, it should stay in its new position, how would i achieve this? The 3d object is loaded from the draw function that i have in a header file.
Someone said i should be using glutMouseFunc or glutMotionFunc.
void MouseClickCallbackFunction(int button, int state, int x, int y)
{
if (state == GLUT_DOWN) {
if (button == GLUT_LEFT)
{
std::cout << "Left " << x << " " << y <<std::endl;
leftClick.x = x;
leftClick.y = y;
}
else if (button == GLUT_RIGHT_BUTTON) {
std::cout << "Right " << x << " " << y << std::endl;
rightClick.x = x;
rightClick.y = y;
}
}
theGame->mouseClicked(button, state, x, y);
glutPostRedisplay();
}
/* function MouseMotionCallbackFunction()
* Description:
* - this is called when the mouse is clicked and moves
*/
void MouseMotionCallbackFunction(int x, int y)
{
theGame->mouseMoved(x, y);
glutPostRedisplay();
}
int main(int argc, char **argv)
{
/* initialize the window and OpenGL properly */
glutInit(&argc, argv);
glutInitContextVersion(4, 2);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow("OpenGL Framework");
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
std::cout << "GLEW could not be initialized. n";
system("pause");
return 0;
}
//glewInit();
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
/* set up our function callbacks */
glutDisplayFunc(DisplayCallbackFunction);
glutKeyboardFunc(KeyboardCallbackFunction);
glutKeyboardUpFunc(KeyboardUpCallbackFunction);
glutMouseFunc(MouseClickCallbackFunction);
glutMotionFunc(MouseMotionCallbackFunction);
glutTimerFunc(1, TimerCallbackFunction, 0);
/* init the game */
theGame = new Game();
theGame->initializeGame();
/* start the game */
glutMainLoop();
return 0;
}
Here is the Draw fucntion with the object
void Game::draw()
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
PassThrough.Bind();
PassThrough.SendUniformMat4("uModel", MonkeyTransform.data, false);
PassThrough.SendUniformMat4("uView", CameraTransform.GetInverse().data, false);
PassThrough.SendUniformMat4("uProj", CameraProjection.data, false);
PassThrough.SendUniform("uTex", 0);
PassThrough.SendUniform("LightPosition", CameraTransform.GetInverse() * vec4(4.0f,0.0f,0.0f,1.0f));
PassThrough.SendUniform("LightAmbient", vec3(0.15f, 0.15f, 0.15f));
PassThrough.SendUniform("LightDiffuse", vec3(0.7f,0.1f,0.2f));
PassThrough.SendUniform("LightSpecular", vec3(0.8f,0.1f,0.1f));
PassThrough.SendUniform("LightSpecularExponent", 50.0f);
PassThrough.SendUniform("Attenuation_Constant", 1.0f);
PassThrough.SendUniform("Attenuation_Linear", 0.1f);
PassThrough.SendUniform("Attenuation_Quadratic", 0.01f);
glBindVertexArray(Monkey.VAO);
glDrawArrays(GL_TRIANGLES, 0, Monkey.GetNumVerticies());
glBindVertexArray(0);
PassThrough.unBind();
glutSwapBuffers();
}
c++ opengl glut
I'm trying to left click and drag any 3d Object and if i let go of it, it should stay in its new position, how would i achieve this? The 3d object is loaded from the draw function that i have in a header file.
Someone said i should be using glutMouseFunc or glutMotionFunc.
void MouseClickCallbackFunction(int button, int state, int x, int y)
{
if (state == GLUT_DOWN) {
if (button == GLUT_LEFT)
{
std::cout << "Left " << x << " " << y <<std::endl;
leftClick.x = x;
leftClick.y = y;
}
else if (button == GLUT_RIGHT_BUTTON) {
std::cout << "Right " << x << " " << y << std::endl;
rightClick.x = x;
rightClick.y = y;
}
}
theGame->mouseClicked(button, state, x, y);
glutPostRedisplay();
}
/* function MouseMotionCallbackFunction()
* Description:
* - this is called when the mouse is clicked and moves
*/
void MouseMotionCallbackFunction(int x, int y)
{
theGame->mouseMoved(x, y);
glutPostRedisplay();
}
int main(int argc, char **argv)
{
/* initialize the window and OpenGL properly */
glutInit(&argc, argv);
glutInitContextVersion(4, 2);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow("OpenGL Framework");
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
std::cout << "GLEW could not be initialized. n";
system("pause");
return 0;
}
//glewInit();
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
/* set up our function callbacks */
glutDisplayFunc(DisplayCallbackFunction);
glutKeyboardFunc(KeyboardCallbackFunction);
glutKeyboardUpFunc(KeyboardUpCallbackFunction);
glutMouseFunc(MouseClickCallbackFunction);
glutMotionFunc(MouseMotionCallbackFunction);
glutTimerFunc(1, TimerCallbackFunction, 0);
/* init the game */
theGame = new Game();
theGame->initializeGame();
/* start the game */
glutMainLoop();
return 0;
}
Here is the Draw fucntion with the object
void Game::draw()
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
PassThrough.Bind();
PassThrough.SendUniformMat4("uModel", MonkeyTransform.data, false);
PassThrough.SendUniformMat4("uView", CameraTransform.GetInverse().data, false);
PassThrough.SendUniformMat4("uProj", CameraProjection.data, false);
PassThrough.SendUniform("uTex", 0);
PassThrough.SendUniform("LightPosition", CameraTransform.GetInverse() * vec4(4.0f,0.0f,0.0f,1.0f));
PassThrough.SendUniform("LightAmbient", vec3(0.15f, 0.15f, 0.15f));
PassThrough.SendUniform("LightDiffuse", vec3(0.7f,0.1f,0.2f));
PassThrough.SendUniform("LightSpecular", vec3(0.8f,0.1f,0.1f));
PassThrough.SendUniform("LightSpecularExponent", 50.0f);
PassThrough.SendUniform("Attenuation_Constant", 1.0f);
PassThrough.SendUniform("Attenuation_Linear", 0.1f);
PassThrough.SendUniform("Attenuation_Quadratic", 0.01f);
glBindVertexArray(Monkey.VAO);
glDrawArrays(GL_TRIANGLES, 0, Monkey.GetNumVerticies());
glBindVertexArray(0);
PassThrough.unBind();
glutSwapBuffers();
}
c++ opengl glut
c++ opengl glut
edited Nov 15 '18 at 15:05
genpfault
42.3k95399
42.3k95399
asked Nov 15 '18 at 3:26
Allz.24Allz.24
15
15
take a look at this Compute objects moving with arrows and mouse
– Spektre
Nov 15 '18 at 6:11
add a comment |
take a look at this Compute objects moving with arrows and mouse
– Spektre
Nov 15 '18 at 6:11
take a look at this Compute objects moving with arrows and mouse
– Spektre
Nov 15 '18 at 6:11
take a look at this Compute objects moving with arrows and mouse
– Spektre
Nov 15 '18 at 6:11
add a comment |
1 Answer
1
active
oldest
votes
The concept is called mouse picking. A method to achieve this is ray casting. Essentially what you want to do is map the mouse coordinates to a region in your scene. In your case you want to check if the position is within the monkey. Then it is as simple as handling the mouse up, down, and move events.
Mouse down: check if object is being picked. If it is, great- maybe highlight it or something. Store ref to object picked; store position of mouse at beginning of pick (pos)
Mouse move: is mouse down? Is picked object ref? Update object position based on delta between pos and coord in mouse move event
Mouse up: clear picked obj ref, position
This link may be of interest http://antongerdelan.net/opengl/raycasting.html
How would i implement this with code? any example?
– Allz.24
Nov 15 '18 at 4:04
can someone show me how to implement this please?
– Allz.24
Nov 15 '18 at 4:22
I found another answer that may help you: stackoverflow.com/questions/25861374/…
– Klathzazt
Nov 15 '18 at 18:24
add a comment |
Your Answer
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: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});
}
});
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%2fstackoverflow.com%2fquestions%2f53311973%2f3d-object-dragging-with-mouse-click-opengl%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The concept is called mouse picking. A method to achieve this is ray casting. Essentially what you want to do is map the mouse coordinates to a region in your scene. In your case you want to check if the position is within the monkey. Then it is as simple as handling the mouse up, down, and move events.
Mouse down: check if object is being picked. If it is, great- maybe highlight it or something. Store ref to object picked; store position of mouse at beginning of pick (pos)
Mouse move: is mouse down? Is picked object ref? Update object position based on delta between pos and coord in mouse move event
Mouse up: clear picked obj ref, position
This link may be of interest http://antongerdelan.net/opengl/raycasting.html
How would i implement this with code? any example?
– Allz.24
Nov 15 '18 at 4:04
can someone show me how to implement this please?
– Allz.24
Nov 15 '18 at 4:22
I found another answer that may help you: stackoverflow.com/questions/25861374/…
– Klathzazt
Nov 15 '18 at 18:24
add a comment |
The concept is called mouse picking. A method to achieve this is ray casting. Essentially what you want to do is map the mouse coordinates to a region in your scene. In your case you want to check if the position is within the monkey. Then it is as simple as handling the mouse up, down, and move events.
Mouse down: check if object is being picked. If it is, great- maybe highlight it or something. Store ref to object picked; store position of mouse at beginning of pick (pos)
Mouse move: is mouse down? Is picked object ref? Update object position based on delta between pos and coord in mouse move event
Mouse up: clear picked obj ref, position
This link may be of interest http://antongerdelan.net/opengl/raycasting.html
How would i implement this with code? any example?
– Allz.24
Nov 15 '18 at 4:04
can someone show me how to implement this please?
– Allz.24
Nov 15 '18 at 4:22
I found another answer that may help you: stackoverflow.com/questions/25861374/…
– Klathzazt
Nov 15 '18 at 18:24
add a comment |
The concept is called mouse picking. A method to achieve this is ray casting. Essentially what you want to do is map the mouse coordinates to a region in your scene. In your case you want to check if the position is within the monkey. Then it is as simple as handling the mouse up, down, and move events.
Mouse down: check if object is being picked. If it is, great- maybe highlight it or something. Store ref to object picked; store position of mouse at beginning of pick (pos)
Mouse move: is mouse down? Is picked object ref? Update object position based on delta between pos and coord in mouse move event
Mouse up: clear picked obj ref, position
This link may be of interest http://antongerdelan.net/opengl/raycasting.html
The concept is called mouse picking. A method to achieve this is ray casting. Essentially what you want to do is map the mouse coordinates to a region in your scene. In your case you want to check if the position is within the monkey. Then it is as simple as handling the mouse up, down, and move events.
Mouse down: check if object is being picked. If it is, great- maybe highlight it or something. Store ref to object picked; store position of mouse at beginning of pick (pos)
Mouse move: is mouse down? Is picked object ref? Update object position based on delta between pos and coord in mouse move event
Mouse up: clear picked obj ref, position
This link may be of interest http://antongerdelan.net/opengl/raycasting.html
answered Nov 15 '18 at 4:01
KlathzaztKlathzazt
2,3091727
2,3091727
How would i implement this with code? any example?
– Allz.24
Nov 15 '18 at 4:04
can someone show me how to implement this please?
– Allz.24
Nov 15 '18 at 4:22
I found another answer that may help you: stackoverflow.com/questions/25861374/…
– Klathzazt
Nov 15 '18 at 18:24
add a comment |
How would i implement this with code? any example?
– Allz.24
Nov 15 '18 at 4:04
can someone show me how to implement this please?
– Allz.24
Nov 15 '18 at 4:22
I found another answer that may help you: stackoverflow.com/questions/25861374/…
– Klathzazt
Nov 15 '18 at 18:24
How would i implement this with code? any example?
– Allz.24
Nov 15 '18 at 4:04
How would i implement this with code? any example?
– Allz.24
Nov 15 '18 at 4:04
can someone show me how to implement this please?
– Allz.24
Nov 15 '18 at 4:22
can someone show me how to implement this please?
– Allz.24
Nov 15 '18 at 4:22
I found another answer that may help you: stackoverflow.com/questions/25861374/…
– Klathzazt
Nov 15 '18 at 18:24
I found another answer that may help you: stackoverflow.com/questions/25861374/…
– Klathzazt
Nov 15 '18 at 18:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53311973%2f3d-object-dragging-with-mouse-click-opengl%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
take a look at this Compute objects moving with arrows and mouse
– Spektre
Nov 15 '18 at 6:11