Reshape Funktion bei OpenGL



  • Hallo,
    kann mir vielleciht jemand sagen wie ich bei meinem Programm es hinbekomme, dass wenn sich das Fenster öffnet in dem man Punkte malen kann und ich dann die Fenstergröße verändere u wieder Punkte male, die Punkte an der richtigen Stelle gemalt werden??
    Dass ich die Reshape Funktion brauche, weiß ich allerdings nicht genau, wo die Aufrufe hinmüssen und wie genau die Funktion aussehen muss.
    Hier is mein Programm mal. Wär schön wenn mir jemand helfen kann.

    #include <iostream>
    #include <vector>
    #include <stdlib.h>
    #include <GL/glut.h>
    #include <fstream>
    #include <gl/glaux.h>
    using namespace std;

    int width = 400;
    int height = 400;

    //stores coordinates
    vector<int>clickCoordinates;
    int x,y;

    typedef struct
    {
    int x;
    int y;
    }xyCoordinates ;

    void reshape(int width, int height)
    {
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,width,0,height,1,-1);
    cout<<"new window size "<<height<<endl;
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }

    //display function
    void display (void)
    {
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POINTS);
    glColor3f(1.0,1.0,1.0);
    glViewport(0,0,width,height);

    for (int i=0; i<clickCoordinates.size();i=i+2){

    glVertex2f(clickCoordinates[i], clickCoordinates[i+1]);
    }
    glEnd();
    glFlush();
    glutSwapBuffers();
    }

    //mouse function
    void mouse (int button, int state, int x, int y)
    {
    glutReshapeFunc(reshape);
    if (button == GLUT_LEFT_BUTTON &&state == GLUT_DOWN) {
    glVertex2i(x,y);

    xyCoordinates temp;
    temp.x;
    temp.y;

    clickCoordinates.push_back(x);
    clickCoordinates.push_back(y);

    glutReshapeFunc(reshape);
    for (int i=0; i<clickCoordinates.size(); i++)
    cout<<"\n";

    }
    glutPostRedisplay();
    }

    void init()
    {
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,width,0,height,-1,1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClearColor(0, 0, 0, 0); //background color
    glPointSize(4.0); //point size = Pixel size
    }

    int main (int argc, char **argv)
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(400, 400);
    glutCreateWindow("Admintool");
    glutMouseFunc(mouse);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    init();
    glutMainLoop();
    return 0;
    }



  • ah, ich habs.
    Brauchte noch eine Variable newheight und musste newheight = height setzen.


Anmelden zum Antworten