Turtle Painting



  • hi ihr da 🙂
    ich hab folgendes programm bisher geschrieben:

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

    float movex = 0.0;
    float movey = 0.0;

    static GLint dreieck;
    static GLint paint = 0 ; //whether paint or not

    void drawTriangle(void){
    glBegin(GL_TRIANGLES); // Drawing Using Triangles
    glVertex3f(0.0f, 0.2f, 0.0f); // Top
    glVertex3f(-0.2f,-0.2f, 0.0f); // Bottom Left
    glVertex3f(0.2f,-0.2f, 0.0f); // Bottom Right
    glEnd();
    }

    void init(void){
    glClearColor(1.0, 1.0, 1.0, 1.0);
    dreieck = glGenLists(1);
    glNewList(dreieck, GL_COMPILE);
    glEndList();

    }

    static void painting(void)
    {
    //static GLfloat red[4] = {3.0, 0.1, 0.0, 1.0};
    //static GLfloat green[4] = {0.1, 3.0, 0.0, 1.0};
    //static GLfloat blue[4] = {0.0, 0.0, 3.0, 1.0};

    float x,y,z;
    glBegin(GL_LINES);
    glColor3f(3.0,0.0,0.0);
    x = movex;
    y = movey;
    z = 0.0;
    glVertex3f(x,y,z);
    x += movex;
    y += movey;
    z = 0.0;
    glVertex3f(x,y,z);
    glEnd();

    glutPostRedisplay();
    }

    static void onReshape(int width, int height){
    float ar;
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    ar=(float) width / (float) height;
    glFrustum(-ar, ar, -1.0, 1.0, 0.5, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0, 0.0, -1.0);
    glutPostRedisplay();
    }

    static void onDisplay(void){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glColor3f(0.0, 0.0, 0.0);
    drawTriangle();

    glutSwapBuffers();
    }

    static void onIdle(void){
    glutPostRedisplay();
    }

    static void onKeyPress(unsigned char key, int x, int y){
    switch(key){
    case ' ': // to pause/restart painting with the space bar
    paint = !paint ;
    if (paint) glutIdleFunc(painting) ;
    else glutIdleFunc(NULL) ;
    break ;

    case 27: exit(0);
    break;
    }
    }

    static void onSpecialKey( int k, int x, int y)
    {
    switch ( k) {
    case GLUT_KEY_UP:
    glTranslatef(0.0, 0.1, 0.0);
    //movey += 0.1;
    break;
    case GLUT_KEY_DOWN:
    glTranslatef(0.0, -0.1, 0.0);
    break;
    case GLUT_KEY_LEFT:
    glTranslatef(-0.1, 0.0, 0.0);
    break;
    case GLUT_KEY_RIGHT:
    glTranslatef(0.1, 0.0, 0.0);
    break;
    default:
    return;
    }
    glutPostRedisplay();
    }

    int main (int argc, char *argv[]) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowSize(640, 480);
    glutCreateWindow("Test");
    init();
    glutReshapeFunc(onReshape);
    glutDisplayFunc(onDisplay);

    glutSpecialFunc(onSpecialKey);
    glutKeyboardFunc(onKeyPress);
    glutIdleFunc(painting);
    glutMainLoop();
    return 0;
    }

    das ding zeichnet mir ein dreieck, dass ich in meinem canvas auf der x und y achse rumschieben kann
    nun soll, wenn man die leertaste drückt, angefangen werden zu zeichen (ob vom mittelpunkt des dreiecks, oder einem seiner ecken is mir wurscht) - ein einfacher strich.
    dabei soll der strich immer meiner bewegung des dreiecks folgen und solange gezeichnet werden, bis ich wieder die leertaste drücke.

    ich bin schon recht stolz auf mich, soweit gekommen zu sein.
    aber ich hab keine ahnung, wie es ab da weitergehen könnte/sollte.
    jemand ne idee? 🙂



  • Bitte benutz codetags, so kann den Code ja niemand lesen.

    DAs Prinzip ist einfach: speicher dir alle Eckpunkte, und verbinde dann die gespeicherten Eckpunkte 😉



  • wie benutzt man hier code-tags? ^^
    🙂

    @antwort: klingt logisch. soweit war ich auch schon. hab ich aber kein plan von. ^^ 🙂



  • Alles was du zwischen [_cpp_] und [/cpp] postest, wird als C++-Code formatiert (Die Unterstriche musst du rauslassen). Mach das mal, dann schaue ich mir auch deinen Code an.



  • #include <iostream> 
    #include <GL/glut.h> 
    #include <stdlib.h> 
    
    float movex = 0.0; 
    float movey = 0.0; 
    
    static GLint dreieck; 
    static GLint paint = 0 ; //whether paint or not 
    
    void drawTriangle(void){ 
    glBegin(GL_TRIANGLES); // Drawing Using Triangles 
    glVertex3f(0.0f, 0.2f, 0.0f); // Top 
    glVertex3f(-0.2f,-0.2f, 0.0f); // Bottom Left 
    glVertex3f(0.2f,-0.2f, 0.0f); // Bottom Right 
    glEnd(); 
    } 
    
    void init(void){ 
    glClearColor(1.0, 1.0, 1.0, 1.0); 
    dreieck = glGenLists(1); 
    glNewList(dreieck, GL_COMPILE); 
    glEndList(); 
    
    } 
    
    static void painting(void) 
    { 
    //static GLfloat red[4] = {3.0, 0.1, 0.0, 1.0}; 
    //static GLfloat green[4] = {0.1, 3.0, 0.0, 1.0}; 
    //static GLfloat blue[4] = {0.0, 0.0, 3.0, 1.0}; 
    
    float x,y,z; 
    glBegin(GL_LINES); 
    glColor3f(3.0,0.0,0.0); 
    x = movex; 
    y = movey; 
    z = 0.0; 
    glVertex3f(x,y,z); 
    x += movex; 
    y += movey; 
    z = 0.0; 
    glVertex3f(x,y,z); 
    glEnd(); 
    
    glutPostRedisplay(); 
    } 
    
    static void onReshape(int width, int height){ 
    float ar; 
    glViewport(0, 0, width, height); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    ar=(float) width / (float) height; 
    glFrustum(-ar, ar, -1.0, 1.0, 0.5, 1.0); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glTranslatef(0.0, 0.0, -1.0); 
    glutPostRedisplay(); 
    } 
    
    static void onDisplay(void){ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    
    glColor3f(0.0, 0.0, 0.0); 
    drawTriangle(); 
    
    glutSwapBuffers(); 
    } 
    
    static void onIdle(void){ 
    glutPostRedisplay(); 
    } 
    
    static void onKeyPress(unsigned char key, int x, int y){ 
    switch(key){ 
    case ' ': // to pause/restart painting with the space bar 
    paint = !paint ; 
    if (paint) glutIdleFunc(painting) ; 
    else glutIdleFunc(NULL) ; 
    break ; 
    
    case 27: exit(0); 
    break; 
    } 
    } 
    
    static void onSpecialKey( int k, int x, int y) 
    { 
    switch ( k) { 
    case GLUT_KEY_UP: 
    glTranslatef(0.0, 0.1, 0.0); 
    //movey += 0.1; 
    break; 
    case GLUT_KEY_DOWN: 
    glTranslatef(0.0, -0.1, 0.0); 
    break; 
    case GLUT_KEY_LEFT: 
    glTranslatef(-0.1, 0.0, 0.0); 
    break; 
    case GLUT_KEY_RIGHT: 
    glTranslatef(0.1, 0.0, 0.0); 
    break; 
    default: 
    return; 
    } 
    glutPostRedisplay(); 
    } 
    
    int main (int argc, char *argv[]) { 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE); 
    glutInitWindowSize(640, 480); 
    glutCreateWindow("Test"); 
    init(); 
    glutReshapeFunc(onReshape); 
    glutDisplayFunc(onDisplay); 
    
    glutSpecialFunc(onSpecialKey); 
    glutKeyboardFunc(onKeyPress); 
    glutIdleFunc(painting); 
    glutMainLoop(); 
    return 0; 
    }
    


  • hallo?
    hilfe?
    noch jemand da, der sich damit auskennt? :S



  • Willst was vorgekautes oder hast du ein spezielles Problem? Was ist denn an Blue-Steels Vorschlag so schlimm, dass du den nicht mal angehst?

    Mach einen std::vector<std::pair<float, float>>, lies jedes Mal, wenn die Richtung geändert wird, die Koordinaten aus und speicher sie im Vector. Beim Zeichnen fängst du einfach an, vom ersten bis zum letzten Element im Vector einen Linienzug zu zeichnen und fügst noch eine Linie vom letzten Punkt bis zum aktuellen Punkt hinzu. Fertig.



  • aber genau da liegt doch das Problem: wie speicher ich die Koordinaten in dem Vector, um sie danach zu verbinden? Und genau das ist das spezielle Problem.
    😕



  • Du könntest z.B. die relative Verschiebung vom Ursprung (beim ersten Punkt) bzw. vom Vorgänger (bei allen anderen Punkten) speichern. D.h. jeder Tastendruck verschiebt den "Cursor" ja in eine Richtung, du merkst dir, wie weit die Verschiebung geht und rechnest einfach die Differenz zum Vorherigen-/Ursprungspunkt. Die speicherst du im Vector, um beim Zeichnen das ganze wieder vom Ursprung nachzuvollziehen.


Anmelden zum Antworten