Hallo,
ich bin dabei ein kleines Programm in OpenGL zu schreiben, indem es möglich ist, mit der Maus Punkte auf der Oberfläche zu bestimmen und diese dann zu verbinden. Es sollen Polygone gezeichnet werden. Ich habe probleme mit den Koordinaten, ich kann sie einfach nicht abapeichern. Bekomme dazu immer eine Fehlermeldung. Im moment, werden die Linien direkt mir dem vorherigen Punkt verbunden. Ich möchte das aber so haben, dass ich erstmal die Punkte alle zeichne und mit der linken Maustaste sich die Linien alle verbinden.
Für jede Hilfe bin ich euch sehr dankbar..
Hier ist mein Code:
Alles anzeigen
ich bin dabei ein kleines Programm in OpenGL zu schreiben, indem es möglich ist, mit der Maus Punkte auf der Oberfläche zu bestimmen und diese dann zu verbinden. Es sollen Polygone gezeichnet werden. Ich habe probleme mit den Koordinaten, ich kann sie einfach nicht abapeichern. Bekomme dazu immer eine Fehlermeldung. Im moment, werden die Linien direkt mir dem vorherigen Punkt verbunden. Ich möchte das aber so haben, dass ich erstmal die Punkte alle zeichne und mit der linken Maustaste sich die Linien alle verbinden.
Für jede Hilfe bin ich euch sehr dankbar..
Hier ist mein Code:
Quellcode
- #include <vector> // STL-vector
- #include <algorithm>
- #include <GL/glut.h>
- #include <glu.h>
- #include <iostream>
- GLsizei winWidth = 700;
- GLsizei winHeight = 400;
- static float array[10][10];
- float erste[2];
- bool flag = false;
- bool blnAction = false;
- class polygon
- {
- public:
- GLint x,y;
- float array[10][10];
- };
- struct point
- {
- int x,y;
- point();
- point(int x_, int y_) : x(x_), y(y_) {}
- };
- // punktliste.push_back(punkt);
- //punktliste.push_back(punkt);
- using namespace std;
- vector<point> punktliste;
- void speichern(int x, int q)
- {
- cout<<"hier "<<x<<"\n";
- cout<<"hier "<<q<<"\n";
- //point punkt = {x,q};
- punktliste.push_back(point(x,q));
- }
- void init()
- {
- glClearColor(0.0, 0.0, 1.0, 1.0); // set Window Color to blue Hintergrundfarbe
- glMatrixMode(GL_PROJECTION); //Art des Koordinatensystems
- gluOrtho2D(0.0, 200.0, 0.0, 150.0);////Koordinatenverteilung festlegen
- //x-> 0.0 ... 200.0 //y- > 0.0 ... 150.0
- }
- void displayFcn(void)
- {
- glClear(GL_COLOR_BUFFER_BIT); //Fenster löschen
- glPointSize(5.0);
- glLineWidth(1.75);
- }
- void winReshapeFcn(GLint newWidth, GLint newHeight)
- {
- glViewport(0,0,newWidth, newHeight);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0.0,GLdouble(newWidth),0.0,GLdouble(newHeight));
- winWidth = newWidth;
- winHeight= newHeight;
- }
- void drawPolygon(polygon endPt1, polygon endPt2)
- {
- glBegin(GL_POLYGON);
- glVertex2i(endPt1.x,endPt2.y);
- }
- void draw_polygons()
- {
- std::vector<point>::iterator i = punktliste.begin();
- while(i != punktliste.end())
- {
- //glBegin(GL_LINES);
- //glVertex2i(nummer.x, nummer.y);
- }
- }
- void draw()
- {
- std::vector<point>::iterator i = punktliste.begin();
- for ( i=punktliste.begin(); i != punktliste.end(); ++i ){
- cout<<(int)i<<"\n";
- }
- }
- /*
- void draw_polygons()
- {
- std::vector<point>::iterator i = polygons.begin();
- while(i != poligons.end())
- {
- glBegin(GL_POLYGON);
- std::vector<line_t>::iterator j = (*i).lines;
- while(j != (*i).lines.end())
- {
- glVertex2i( (*j).from.x, (*j).from.y);
- glVertex2i( (*j).to.x, (*j).to.y);
- ++j;
- }
- glEnd();
- ++i;
- }
- }
- */
- void drawLineSegment(point endPt1, point endPt2)
- {
- glBegin(GL_POINTS); //Zeichenmodus setzten
- glVertex2i(endPt1.x, endPt1.y);
- glEnd();
- }
- void drawLine(point endPt1, point endPt2)
- {
- drawLineSegment(endPt1,endPt2);
- glBegin(GL_LINES);
- glFlush();
- glVertex2i(endPt1.x, endPt1.y);
- glFlush();
- }
- void buttonBoxFcn(GLint button, GLint action)
- {
- if(button==action)
- exit(0);
- else
- exit(1);
- }
- void polyline(GLint button, GLint action, GLint xMouse, GLint yMouse) //Mouse callback prozedur
- {
- static point endPt1, endPt2;
- static float x,y;
- struct point a;
- if(button == GLUT_RIGHT_BUTTON && action == GLUT_DOWN)
- {
- endPt1.x = xMouse;
- endPt1.y = winHeight - yMouse;
- cout<<endPt1.x<<"\n" ;
- cout<<endPt1.y<<"\n";
- a.x = endPt1.x ;
- a.y = endPt1.y;
- draw();
- drawLineSegment(endPt1, endPt2);
- speichern(a.x, a.y);
- glFlush();
- drawLine(endPt1, endPt2);
- glFlush();
- //drawLineLoop(endPt1,endPt2);
- glFlush();
- }
- else
- if(button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
- //draw();
- //draw_polygons();
- //draw_polygons();
- //float *g;
- //float *t;
- //g = &erste[0];
- //t = &erste[1];
- //glVertex2i(g,t);
- //glVertex2iv(erste);
- // int point[] = {10,20};
- // glVertex2iv(point);
- glEnd();
- //bool bInAction = false;
- glFlush(); //zeichen
- }
- void main(int argc, char**argv)
- {
- glutInit(&argc, argv); //initialisierung der GLUT Bibliothek
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //Initialisierung des Bildschirms(Art des Zwischenspeichers, Farben)
- glutInitWindowPosition(100,100); //Positionierung des Fensters
- glutInitWindowSize(winWidth,winHeight); //Fenstergröße
- glutCreateWindow("Draw Interactive Polyline"); //Fenstertitel
- init();
- glutButtonBoxFunc(buttonBoxFcn);
- glutDisplayFunc(displayFcn); //Festlegen der Funktion die zum zeichen auf dem Bildschurm verwendet wird
- glutReshapeFunc(winReshapeFcn);
- glutMouseFunc(polyline); //Funktion die die Funktion "polyline" aufruft, wenn der Maus betätigt wurde
- glutMainLoop();
- }