How can I respond to a middle mouse click in C++/OpenGL?
I know it may have to do with the WM_MBUTTONDOWN event however I am completely clueless with using it. I am also unfamiliar with callback functions so if it has to be used, can it be thoroughly explained? Can anyone show me how to implement the code for middle mouse click event?
You can try and implement the oldschool version with the WM_ commands, but I think it'll be a lot easier to use GLUT (since it really makes life with OpenGL easier).
#include <GL/glut.h>
void myMouseHandleFunction(int button, int state, int x, int y){
if(button==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN) std::cout << "Pressed middle mouse button!";
}
int main(int argc, char *argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(300, 200);
glutCreateWindow("Hello World!");
glutMouseFunc(myMouseHandleFunction);
glutMainLoop();
return 0;
}
If you're doing a simple app, GLUT will be sufficient. If you wanna do something more complicated, try freeglut or openglut. The old, basic GLUT doesn't handle the mouse wheel, so if you want to check for that - you'll need one of those two.
Related
I want to create in OpenGL two differents windows but they should appears not together.
I mean: when I execute my code I visualize both of windows contemporarily.
My aim instead is to visualize at the beginning the first window, then the first window should be closed and the second will appear. My pseudocode is the following (this realeses only the two windows together ntemporarily):
int main(int argc, char** argv)
{
glutInit(&argc, argv);
initRendering();
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
//window1=
glutCreateWindow("First window");
glutDisplayFunc(drawScene1);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
cout << "Before inserting 'choice' I want to visualize the first window\n
After the insert of 'choice' I want to visualize the second window
and close the first one !";
cin >> choice;
//create the second window
//window2 =
glutCreateWindow("Second Window");
//define a window position for second window
glutPositionWindow(540, 40);
// register callbacks for second window, which is now current
glutReshapeFunc(handleResize);
glutDisplayFunc(drawScene2);
glutKeyboardFunc(handleKeypress);
glutMainLoop();
return 0;
}
The code is designed to load a .obj file, draw, rotate camera around mesh (this is ongoing) and display rotation value when the mouse button is clicked. I am using eclipse C++ IDE with FLTK. When the mouse button is clicked the window crashes. The error message when debugging is "no source available for gl_draw".
The code is as follows:
void onMouseButton(int button, int state, int x, int y)
{
switch(button){
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN){
// Print value on object
char s[40];
sprintf(s, "ROT=%.2f", g_rotation);
glRasterPos2f(60,40);
gl_draw(s, strlen(s));
}
}
}
The main code:
int main(int argc, char **argv)
{
Fl_Window win(WIDTH,HEIGHT, "Wavefront");
win.resizable(win);
win.show(argc, argv);
// Docs say to add glut subwindow /after/ calling win.show()
win.begin();
// initialize and run program
//glutInit(&argc, argv); // GLUT initialization
glutInitWindowSize(WIDTH-20, HEIGHT-20);
glutInitWindowPosition(10,10); // place inside parent window
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutCreateWindow("Ear");
glutDisplayFunc(display); // register Display Function
glutIdleFunc( display ); // register Idle Function
glutMouseFunc( onMouseButton );
glutKeyboardFunc( keyboard ); // register Keyboard Handler
initialize();
obj.Load("Ear_obj.obj");
//gl_draw(s, strlen(s));
glutMainLoop();
win.end();// run GLUT mainloop
return 0;
}
If gl_draw(s, strlen(s)); is removed code run as expected , therefore loads the mesh, draws it, displays the mesh as the camera is rotated but would do nothing if the mouse is clicked.
Is the a way to display any text when the mouse is clicked or is there a step I am missing that is causing the error?
I've tried some OpenGL C++ training.
But I have a logic problem, how can I update my OpenGL Windows window.
It should draw text one, then delay 1-2sec, then draw text 2, but now it draws same time. Can anyone help or give a hint.
void text () {
wait(1);
Sleep(1000);
std::string text_one;
text_one = "Text 1";
glColor3f(1,01, 0);
drawText(text_one.data(), text_one.size(), 050, 150);
glutPostRedisplay();
wait (1)
std::string text_two;
text_two = "Text 2";
glColor3f(1,0, 0);
drawText(text_two.data(), text_two.size(), 250, 150);
}
and here the main
int main(int argc, char **argv) {
// init GLUT and create Window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,640);
glutCreateWindow("Test 001");
// register callbacks
glutDisplayFunc(renderScene);
glutIdleFunc(text);
// enter GLUT event processing cycle
glutMainLoop();
return 1;
}
You should render in renderScene callback. It will be called automatically in you screen refresh rate. If you want some delay you need to implement it inside this callback (functions called from this callback).
So basically you need to re-render everything every 1/60 second.
If you want to implement easy delay you can do something like this:
void renderScene() {
time += deltaTime;
RenderText1();
if (time > delayTime)
RenderText2();
glutSwapBuffers();
}
I'm using Sumanta Guha's code sample and I'm trying to create two windows. Using the following code:
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
// First top-level window definition.
glutInitWindowSize(250, 500);
glutInitWindowPosition(100, 100);
// Create the first window and return id.
id1 = glutCreateWindow("windows.cpp - window 1");
// Initialization, display, and other routines of the first window.
setup1();
glutDisplayFunc(drawScene1);
glutReshapeFunc(resize1);
glutKeyboardFunc(keyInput); // Routine is shared by both windows.
// Second top-level window definition.
glutInitWindowSize(250, 500);
glutInitWindowPosition(400, 100);
// Create the second window and return id.
id2 = glutCreateWindow("windows.cpp - window 2");
// Initialization, display, and other routines of the second window.
setup2();
glutDisplayFunc(drawScene2);
glutReshapeFunc(resize2);
glutKeyboardFunc(keyInput); // Routine is shared by both windows.
glutMainLoop();
return 0;
}
I'm using Windows 7, and normally it should display two windows. But as you can see, only one Window displays properly and the other one doesn't seem to work quite as well. Are there additional steps that I have to take other than GLUT_DOUBLE and buffer swap?
Are there additional steps that I have to take other than GLUT_DOUBLE and buffer swap?
Since you are creating multiple windows, you have to call glutSetWindow() in your callbacks.
freeglut has an extension (which doesn't work) to create a shared opengl context, but the original glut doesn't support it.
Last week, I tried a few examples I found while reading GLUT tutorials and everything was working great.
Now, when I retry those same examples, I get a weird behaviour: my GLUT window displays the part of the desktop that is at the place the window is (so if the GLUT window starts at (100,100) and is sized 500px by 500px, it displays the part of the desktop that starts at (100,100) and ends at (600,600)).
The following example that I tested last week should show a wire teapot on a black background. I'd like to know if there's something wrong with my code (since I don't recall changing anything in it) or if the problems come from the library not being linked correctly or if it's something else that I'm not aware of.
#include <GL/glut.h>
void displayFunc(void);
int glutWindow;
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
// define the size
glutInitWindowSize(500,500);
// the position where the window will appear
glutInitWindowPosition(100,100);
glutWindow = glutCreateWindow("UITest");
glClearColor(0.0, 0.0, 0.0, 0.0);
glutDisplayFunc(displayFunc);
glutMainLoop();
return EXIT_SUCCESS;
}
void displayFunc(void){
glClear(GL_COLOR_BUFFER_BIT);
glutWireTeapot(0.5);
}
You need to add a call to glFlush() in the end of displayFunc().