I need to implement a rectangular slider for a program I’m working on.
Right now, I draw a rectangle using glRectf()
at the bottom of the screen and I change the bottom-right coordiates depending on the mouse position if the mouse is inside the rectangle.
I have a global variable for the rectangle’s bottom right x position that changes in the mouse function. But, the rectangle always remain the same.
The relevant code is below.
void drawSlider()
{
glColor3f(1.0f, 1.0f, 1.0f);
glOrtho(0.1f, 500, 500, 0.0f, 0.0f, 1.0);
glRectf(0.5f, 490.0f, sliderX, 500.0f);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); // reset the current matrix to identity matrix
// create a viewing matrix defined by an eye point and a reference point
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_DEPTH_TEST);
// Draw all components
glPushMatrix();
glRotatef(rotate_x, 1.0f, 0.0f, 0.0f);
glRotatef(rotate_y, 0.0f, 1.0f, 0.0f);
drawAxis();
glScalef(scale, scale, scale);
drawCube();
glPopMatrix();
drawSlider();
glFlush();
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
printf("Coordinates: (%d, %d)\n", x, y);
if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
{
if (y > 490.0f)
{
// Redraw rectangle
sliderX = (float)x;
}
glutPostRedisplay();
}
I only check for top coordinate because the rectangle’s height is fixed and its always on the bottom of the (500x500)
window.
Only the width should change on click and drag.
I haven’t added the code to the motion function
because the mouse function
itself is not working
Any tips on how to fix it?