2017-04-01 4 views
0

Ich möchte das Objekt (6-Punkt-Stern mit 2 Dreiecken) per Mausklick verschieben. Ich habe den folgenden Code geschrieben, aber es gibt keine Antwort.Objekte mit Mausklick bewegen

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; 

Im Fall GLUT_LEFT_BUTTON, lege ich Minimal-/maxmimum x und y-Position, aber nichts funktioniert, wenn ich die linke Maustaste angeklickt. Hier

ist die vollständige 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; 
    } 
} 
+2

Mögliche Duplikat von [(OpenGL) Drehen Objekt] (http://stackoverflow.com/questions/43117080/open-rot-rotate-object) – ITWitch

Antwort

1

Ich möchte das Objekt (6-Punkt-Sterne mit 2 Dreiecke) bewegen, mit der Maus

klicken Wenn Sie auf nach links wollen und Diese Position ist jetzt das Zentrum des Sterns, dann verkomplizieren Sie die Dinge. Sie haben bereits die Position des Sterns als globale Variablen x und y. In mouse() setzen Sie einfach diese auf die Mausposition.

jedoch daran denken, die Höhe des Fensters durch y, zu subtrahieren, da 0x0 in OpenGL oben links auf dem Bildschirm, aber links unten ist.

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

Seit mouse() ‚s x und y Parameter Schatten Ihre globalen Variablen x und y, haben Sie mit :: Präfix.

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

Hier ist ein GIF von Klick zufälligen Stellen auf dem Fenster: Sie können auch mouse() ‚s Parameter zu sagen mx und my umbenennen

+0

Danke. Aber wenn der Stern gestoppt wird, funktioniert das Klicken auf den Bildschirm nicht. Auch die Farbe ändert sich nicht. Ich kann nicht herausfinden, wo das Problem liegt. -.-; –

+0

@BlackJ Sie haben wahrscheinlich gerade etwas verpasst. [Dies ist der Code, den ich getestet habe, und machte das GIF von.] (Https://pastebin.com/A7seMnPG) – Vallentin

+0

Oh, ich habe es gelöst! Ich habe das Hinzufügen von glutPostRedisplay() zu LEFT und MIDDLE Button Fall verpasst. Wie auch immer, vielen Dank. –

Verwandte Themen