glutMouseWheelFunc doesnt trigger callback - opengl

I am trying to implement zoom in or zoom out operations using mouse scroll button
by glutMouseWheelFunc in opengl . I have implemted the code as below :
#include<GL/freeglut.h>
void mouseWheel(int button, int dir, int x, int y)
{
printf("in mouse wheel \n");
if (dir > 0)
{
// Zoom in
ztrans = ztrans - 1.0;
printf("scroll in = %0.3f\n ",ztrans);
}
else
{
// Zoom out
ztrans = ztrans + 1.0;
printf("scroll out = %0.3f\n ",ztrans);
}
glutPostRedisplay();
}
int main(int argc, char **argv)
{
// general initializations
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(800, 400);
glutCreateWindow("Rotation");
// register callbacks
glutReshapeFunc(changeSize);
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutIgnoreKeyRepeat(1);
glutMouseFunc(mouseButton);
glutMotionFunc(mouseMove);
glutMouseWheelFunc(mouseWheel); // Register mouse wheel function
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}
On executing, it is not calling the registered callback function(mouseWheel) . My system has freeglut3 installed.

try using
a static int inside void mouseWheelmethod, and then use it in renderScene
like this
static int k;
static int ztrans
void mouseWheel(int button, int dir, int x, int y)
{
k = dir; // int dir is +1 of -1 based on the direction of the wheel motion
ztrans = ztrans + k;
}
This worked for me,
Try this and feedback, GoodLuck .

Related

FLTK flickering animation

I'm trying to make a simple animation using FLTK(a circle with increasing and decreasing radius). I've managed to write a simple program that seems to work, but the animation flickers. The circle disappears for a couple of milliseconds and then gets back. I've changed Fl_Window class to Fl_Double_Window, but that didn't fix this problem.
class Painting : public Fl_Widget {
public:
Painting(int x, int y, int w, int h) : Fl_Widget(x, y, w, h, 0) {}
private:
void draw()
{
static double inc = 0;
inc += 0.2;
double radius = 50 + 10*sin(inc);
fl_begin_polygon();
fl_arc(100, 100, radius, 0, 360);
fl_end_polygon();
}
};
void redraw_cb(void *data)
{
Fl_Widget *w = (Fl_Widget*)data;
w->redraw();
Fl::repeat_timeout(0.01, redraw_cb, data);
}
int main(int argc, char **argv)
{
Fl_Double_Window *win = new Fl_Double_Window(1000, 500, "hello");
Painting *painting = new Painting(0, 0, 1000, 500);
Fl::add_timeout(1, redraw_cb, painting);
Fl::visual(FL_DOUBLE|FL_INDEX);
win->resizable(painting);
win->end();
win->show();
return Fl::run();
}

CPU intesive OpenGL functions

I'm using Visual Studio 2013 and OpenGL to create a simulation.
And I use the keboard inputs to make certain changes in the variables and the update is seen on the window that was created.
However there is a small delay between after I press the key and the changed output on the window.
I tried using the Visual Studio Diagnostic tools and saw that there were 2 key functions that was CPU intensive.
One was a user function that I created and another was 'display/main/__tmainCRTStartup/mainCRTStartup
I'm assuming this a GLUT function. So is this normal or am I doing something wrong?
Any help would be appreciated.
void keyboard (unsigned char key, int x, int y)
{
switch (key)
{
case 'r': case 'R':
if (filling==0)
{
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
filling=1;
}
else
{
glPolygonMode (GL_FRONT_AND_BACK, GL_POINT);
filling=0;
}
break;
case 27:
exit(0);
break;
}
}
.
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(-90,0.0,1.0,0.0);
glRotatef(-90,1.0,0.0,0.0);
rotation_x = rotation_x + (rotation_x_increment - rotation_x)/50;
rotation_y = rotation_y + (rotation_y_increment - rotation_y)/50;
rotation_z = rotation_z + rotation_z_increment;
if (rotation_x > 359) rotation_x = 0;
if (rotation_y > 359) rotation_y = 0;
if (rotation_z > 359) rotation_z = 0;
if(rotation_x_increment > 359) rotation_x_increment = 0;
if(rotation_y_increment > 359) rotation_y_increment = 0;
if(rotation_z_increment > 359) rotation_z_increment = 0;
glRotatef(rotation_x,1.0,0.0,0.0);
glRotatef(rotation_y,0.0,1.0,0.0);
glRotatef(rotation_z,0.0,0.0,1.0);
glTranslatef(x_translate,0.0,0.0);
glTranslatef(0.0,y_translate,0.0);
glTranslatef(0,0,z_translate);
glEnd();
glutSwapBuffers();
glFlush(); // This force the execution of OpenGL commands
}
.
int main(int argc, char **argv)
{
IntroDisplay();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(screen_width,screen_height);
glutInitWindowPosition(0,0);
glutCreateWindow("Ultrasonic Testing");
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc (resize);
glutKeyboardFunc (keyboard);
glutSpecialFunc (keyboard_s);
glutMouseFunc(mouse);
glutMotionFunc(mouseMove);
init();
glutMainLoop();
return 0;
}
I've copied the code to a new project, i've changed the display function to draw a triangle and it works very fast.
(I know it should be a comment, not an answer, but I don't have enough reputation to add comments).
PD: You can change
glTranslatef(x_translate,0.0,0.0);
glTranslatef(0.0,y_translate,0.0);
glTranslatef(0,0,z_translate);
to
glTranslatef(x_translate,y_translate,z_translate);

Ifstream doesn't work with OpenGL/freeglut

A program crashes if I try to use ifstream while having OpenGL/freeglut. My code:
#include <fstream>
#include <windows.h>
#include <GL/freeglut.h>
double x, y;
std::ifstream read("coordinates.txt");
void display() {
glBegin(GL_LINE_STRIP);
while (read >> x) //Crashes here
{
read >> y;
glVertex2d(x, y);
}
glEnd();
glFlush();
}
void key(unsigned char mychar, int x, int y) {
if (mychar == 27) {
exit(0);
}
}
void initialize()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-27, 27, -27, 27);
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(1920, 1080);
glutInitWindowPosition(0, 0);
glutCreateWindow("Lorenz Attractor");
initialize();
glutDisplayFunc(display);
glutKeyboardFunc(key);
glColor3d(0, 0, 1);
glutFullScreen();
glutMainLoopEvent();
Sleep(60000);
}
coordinates.txt:
1.1 1.03
2.5 2
3 5.3
I don't even need to include freeglut, I checked out an older project that was working perfectly before and now it crashes as well. Using Code::Blocks with MinGW. Why would this happen? Thanks!
display will be called more than one time. It's called whenever the display needs to be redrawn, such as when the window comes into view, another window is moved over top of it, the window is resized, etc.
display reads a file. Well, after the first time it reads the file, the file will be empty. After all, you opened the file in a global variable (FYI: never do that), and you kept reading until the file was empty.
Don't read files while you're drawing. Read the file into a data structure (say, a vector<float>). Do that before the rendering loop. Then, use the data structure to draw from.

using a complex display function prevents glutKeyboardFunc from working

Using glutKeyboardFunc in a very simple exemple, i could get it to work very easily :
void special(int key, int x, int y) {
printf("key %d\n", key);
}
void keyboard(unsigned char key, int x, int y) {
printf("key %d\n", key);
}
void display() {}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutCreateWindow("GLUT Program");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
glutMainLoop();
return EXIT_SUCCESS;
}
but using it with a very complex display function, the keyboard routine is never called, I am not sure about the part that prevents this call from working so i am just asking here for ideas about what could be the cause, i can't really post the code because that would be the entire project... i am using freeglut with an opengl context and glm for math computation.
Nevertheless here is the new call :
static void loop_function() {
glutSetWindow(win);
Scene::unique_scene->mainloop();
}
I am a bit lost about what to do to find the error, if someone could enlighten me i would be grateful.
Having an infinite loop in the glutDisplayFunc is not the way to go, it is called automatically at the end of the glutKeyboardFunc if you added the call
glutPostRedisplay();

How to change screen resolution using QT, OpenGL, C++, and Linux?

I just want to make a full screen game. I know how to change resolution on Windows but how do I change the resolution under Linux? Is there a cross platform QT solution for this? Also I've got borders around my GLWidget. How do I make the widget cover the entire window?
I'm just going to post the code:
#include <QtOpenGL>
class GLWidget : public QGLWidget
{
public:
void initializeGL()
{
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
glClearDepth(1.0f);
}
void paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
}
void resizeGL(int width, int height)
{
int side = qMin(width, height);
glViewport((width - side) / 2, (height - side) / 2, side, side);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-0.5f, +0.5f, -0.5f, +0.5f, 4.0f, 15.0f);
glMatrixMode(GL_MODELVIEW);
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QDesktopWidget *desktop = app.desktop();
QWidget window;
GLWidget *glWidget = new GLWidget;
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(glWidget);
window.setLayout(mainLayout);
window.setWindowTitle("Hello GL");
window.resize(QSize(640, 480));
window.show();
window.showFullScreen();
return app.exec();
}
You can use xrrsetscreenconfigandrate, as explained here:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<X11/Xlib.h>
#include<X11/extensions/Xrandr.h>
Display *dpy;
Window root;
int num_sizes;
XRRScreenSize *xrrs;
XRRScreenConfiguration *conf;
short possible_frequencies[64][64];
short original_rate;
Rotation original_rotation;
SizeID original_size_id;
int main(int argc, char *argv[]){
//
// CONNECT TO X-SERVER, GET ROOT WINDOW ID
//
dpy = XOpenDisplay(NULL);
root = RootWindow(dpy, 0);
//
// GET POSSIBLE SCREEN RESOLUTIONS
//
xrrs = XRRSizes(dpy, 0, &num_sizes);
//
// LOOP THROUGH ALL POSSIBLE RESOLUTIONS,
// GETTING THE SELECTABLE DISPLAY FREQUENCIES
//
for(int i = 0; i < num_sizes; i ++) {
short *rates;
int num_rates;
printf("\n\t%2i : %4i x %4i (%4imm x%4imm ) ", i, xrrs[i].width, xrrs[i].height, xrrs[i].mwidth, xrrs[i].mheight);
rates = XRRRates(dpy, 0, i, &num_rates);
for(int j = 0; j < num_rates; j ++) {
possible_frequencies[i][j] = rates[j];
printf("%4i ", rates[j]); } }
printf("\n");
//
// GET CURRENT RESOLUTION AND FREQUENCY
//
conf = XRRGetScreenInfo(dpy, root);
original_rate = XRRConfigCurrentRate(conf);
original_size_id = XRRConfigCurrentConfiguration(conf, &original_rotation);
printf("\n\tCURRENT SIZE ID : %i\n", original_size_id);
printf("\tCURRENT ROTATION : %i \n", original_rotation);
printf("\tCURRENT RATE : %i Hz\n\n", original_rate);
//
// CHANGE RESOLUTION
//
printf("\tCHANGED TO %i x %i PIXELS, %i Hz\n\n", xrrs[1].width, xrrs[1].height, possible_frequencies[1][0]);
XRRSetScreenConfigAndRate(dpy, conf, root, 1, RR_Rotate_0, possible_frequencies[1][0], CurrentTime);
//
// SLEEP A WHILE
//
usleep(6000000);
//
// RESTORE ORIGINAL CONFIGURATION
//
printf("\tRESTORING %i x %i PIXELS, %i Hz\n\n", xrrs[original_size_id].width, xrrs[original_size_id].height, original_rate);
XRRSetScreenConfigAndRate(dpy, conf, root, original_size_id, original_rotation, original_rate, CurrentTime);
//
// EXIT
//
XCloseDisplay(dpy); }
//
// gcc -o Xrandr Xrandr.cc -lX11 -lXrandr -lstdc++
//