user7791536
user7791536

Reputation:

Moving objects with mouse click

I want to move the object(6-point star with 2 triangles) with mouse clicking. I wrote the following code, but there is no response.

case GLUT_LEFT_BUTTON:
    if (state == GLUT_DOWN) {
        float x_min = (-x + 500) / 500;
        float x_max = (x - 500) / 500;
        float y_min = (-y + 500) / 500;
        float y_max = (y - 500) / 500;
        gluOrtho2D(x_min, x_max, y_min, y_max);
    }
 glutPostRedisplay();
 break;

In the case GLUT_LEFT_BUTTON, I put minimum/maxmimum x and y position but nothing works when I clicked left mouse button.

Here is the full codes:

#include <stdlib.h>
#include <GL/glut.h>

float v1[3] = { -35.0f,  22.5f, 0.0f };
float v2[3] = { -35.0f, -22.5f, 0.0f };
float v3[3] = { 0.0f,  42.5f, 0.0f };
float v4[3] = { 0.0f, -42.5f, 0.0f };
float v5[3] = { 35.0f,  22.5f, 0.0f };
float v6[3] = { 35.0f, -22.5f, 0.0f };

static GLfloat spin = 0.0;

float x = 400.0f, y = 442.5f;
float x_position;
float y_position;

float color1[3] = { 1.0f, 1.0f, 1.0f };
float color2[3] = { 1.0f, 1.0f, 1.0f };

int mode = 1;
int rotate = 1;

void init(void);
void triangle_1(void);
void triangle_2(void);
void display(void);
void spinDisplay_1(void);
void spinDisplay_2(void);
void reshape(int, int);
void changeColor(int);
void mouse(int, int, int, int);

////////////////////////////////////////////////////////////////////

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(300, 300);
    glutCreateWindow("6-Point Star");

    init();

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMouseFunc(mouse);
    glutMainLoop();

    return 0;
}

////////////////////////////////////////////////////////////////////

void init(void) {
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
}

void triangle_1(void) {          //// triangle_1 and triangle_2 make 6-point star ////
    glColor3fv(color1);

    glBegin(GL_TRIANGLE_FAN);
    glVertex3fv(v1);
    glVertex3fv(v4);
    glVertex3fv(v5);

    glEnd();
}

void triangle_2(void) {
    glColor3fv(color2);

    glBegin(GL_TRIANGLE_FAN);
    glVertex3fv(v2);
    glVertex3fv(v3);
    glVertex3fv(v6);

    glEnd();
}

void display(void) {
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();

    glTranslatef(x, y, 0.0f);
    glRotatef(spin, 0.0, 0.0, 1.0);

    triangle_1();
    triangle_2();

    glPopMatrix();

    glutSwapBuffers();
}

void spinDisplay_1(void) {
    spin = spin + 2.0;

    if (spin > 360.0) {
        spin = spin - 360.0;
    }

    glutPostRedisplay();
}

void spinDisplay_2(void) {
    spin = spin - 2.0;

    if (spin < 360.0) {
        spin = spin + 360.0;
    }

    glutPostRedisplay();
}

void reshape(int w, int h) {
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 500.0, 0.0, 500.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void changeColor(int n) {
    if (n == 1) {
        color1[0] = 0.0f, color1[1] = 0.0f, color1[2] = 1.0f;
        color2[0] = 0.0f, color2[1] = 1.0f, color2[2] = 0.0f;
    }
    else if (n == 2) {
        color1[0] = 1.0f, color1[1] = 1.0f, color1[2] = 1.0f;
        color2[0] = 1.0f, color2[1] = 1.0f, color2[2] = 1.0f;
    }
}

void mouse(int button, int state, int x, int y) {         /////// mouse event ////////
    switch (button) {
    case GLUT_LEFT_BUTTON:
        if (state == GLUT_DOWN) {
            float x_min = (-x + 500) / 500;
            float x_max = (x - 500) / 500;
            float y_min = (-y + 500) / 500;
            float y_max = (y - 500) / 500;
            gluOrtho2D(x_min, x_max, y_min, y_max);
        }
        glutPostRedisplay();
        break;
    case GLUT_MIDDLE_BUTTON:
        if (state == GLUT_DOWN) {
            if (mode == 1) {
                changeColor(mode);
                mode = 2;
            }
            else if (mode == 2) {
                changeColor(mode);
                mode = 1;
            }
        }
        break;
    case GLUT_RIGHT_BUTTON:
        if (state == GLUT_DOWN)
            if (rotate == 1) {
                glutIdleFunc(spinDisplay_1);
                rotate = 2;
            }
            else if (rotate == 2) {
                glutIdleFunc(spinDisplay_2);
                rotate = 1;
            }
        break;
    default:
        break;
    }
}

Upvotes: 0

Views: 2057

Answers (1)

vallentin
vallentin

Reputation: 26157

I want to move the object (6-point star with 2 triangles) with mouse clicking

If you want to left click and that position now being the center of the star, then you're overcomplicating things. You already have the star's position as global variables x and y. Thus in mouse() you just set those to the mouse position.

However remember to subtract the height of the window by y, since 0x0 is top left on the screen but bottom left in OpenGL.

if (state == GLUT_DOWN) {
    ::x = x;
    ::y = glutGet(GLUT_WINDOW_HEIGHT) - y;
}

Since mouse()'s x and y parameters shadows your global variables x and y, you have to prefix with ::. You could also rename mouse()'s parameters to say mx and my:

void mouse(int button, int state, int mx, int my) {
    [...]
    if (state == GLUT_DOWN) {
        x = mx;
        y = glutGet(GLUT_WINDOW_HEIGHT) - my;
    }

Here's a GIF of clicking random places on the window:

Upvotes: 1

Related Questions