OpenGL Koordinaten abspeichern

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • OpenGL Koordinaten abspeichern

    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:

    Quellcode

    1. #include <vector> // STL-vector
    2. #include <algorithm>
    3. #include <GL/glut.h>
    4. #include <glu.h>
    5. #include <iostream>
    6. GLsizei winWidth = 700;
    7. GLsizei winHeight = 400;
    8. static float array[10][10];
    9. float erste[2];
    10. bool flag = false;
    11. bool blnAction = false;
    12. class polygon
    13. {
    14. public:
    15. GLint x,y;
    16. float array[10][10];
    17. };
    18. struct point
    19. {
    20. int x,y;
    21. point();
    22. point(int x_, int y_) : x(x_), y(y_) {}
    23. };
    24. // punktliste.push_back(punkt);
    25. //punktliste.push_back(punkt);
    26. using namespace std;
    27. vector<point> punktliste;
    28. void speichern(int x, int q)
    29. {
    30. cout<<"hier "<<x<<"\n";
    31. cout<<"hier "<<q<<"\n";
    32. //point punkt = {x,q};
    33. punktliste.push_back(point(x,q));
    34. }
    35. void init()
    36. {
    37. glClearColor(0.0, 0.0, 1.0, 1.0); // set Window Color to blue Hintergrundfarbe
    38. glMatrixMode(GL_PROJECTION); //Art des Koordinatensystems
    39. gluOrtho2D(0.0, 200.0, 0.0, 150.0);////Koordinatenverteilung festlegen
    40. //x-> 0.0 ... 200.0 //y- > 0.0 ... 150.0
    41. }
    42. void displayFcn(void)
    43. {
    44. glClear(GL_COLOR_BUFFER_BIT); //Fenster löschen
    45. glPointSize(5.0);
    46. glLineWidth(1.75);
    47. }
    48. void winReshapeFcn(GLint newWidth, GLint newHeight)
    49. {
    50. glViewport(0,0,newWidth, newHeight);
    51. glMatrixMode(GL_PROJECTION);
    52. glLoadIdentity();
    53. gluOrtho2D(0.0,GLdouble(newWidth),0.0,GLdouble(newHeight));
    54. winWidth = newWidth;
    55. winHeight= newHeight;
    56. }
    57. void drawPolygon(polygon endPt1, polygon endPt2)
    58. {
    59. glBegin(GL_POLYGON);
    60. glVertex2i(endPt1.x,endPt2.y);
    61. }
    62. void draw_polygons()
    63. {
    64. std::vector<point>::iterator i = punktliste.begin();
    65. while(i != punktliste.end())
    66. {
    67. //glBegin(GL_LINES);
    68. //glVertex2i(nummer.x, nummer.y);
    69. }
    70. }
    71. void draw()
    72. {
    73. std::vector<point>::iterator i = punktliste.begin();
    74. for ( i=punktliste.begin(); i != punktliste.end(); ++i ){
    75. cout<<(int)i<<"\n";
    76. }
    77. }
    78. /*
    79. void draw_polygons()
    80. {
    81. std::vector<point>::iterator i = polygons.begin();
    82. while(i != poligons.end())
    83. {
    84. glBegin(GL_POLYGON);
    85. std::vector<line_t>::iterator j = (*i).lines;
    86. while(j != (*i).lines.end())
    87. {
    88. glVertex2i( (*j).from.x, (*j).from.y);
    89. glVertex2i( (*j).to.x, (*j).to.y);
    90. ++j;
    91. }
    92. glEnd();
    93. ++i;
    94. }
    95. }
    96. */
    97. void drawLineSegment(point endPt1, point endPt2)
    98. {
    99. glBegin(GL_POINTS); //Zeichenmodus setzten
    100. glVertex2i(endPt1.x, endPt1.y);
    101. glEnd();
    102. }
    103. void drawLine(point endPt1, point endPt2)
    104. {
    105. drawLineSegment(endPt1,endPt2);
    106. glBegin(GL_LINES);
    107. glFlush();
    108. glVertex2i(endPt1.x, endPt1.y);
    109. glFlush();
    110. }
    111. void buttonBoxFcn(GLint button, GLint action)
    112. {
    113. if(button==action)
    114. exit(0);
    115. else
    116. exit(1);
    117. }
    118. void polyline(GLint button, GLint action, GLint xMouse, GLint yMouse) //Mouse callback prozedur
    119. {
    120. static point endPt1, endPt2;
    121. static float x,y;
    122. struct point a;
    123. if(button == GLUT_RIGHT_BUTTON && action == GLUT_DOWN)
    124. {
    125. endPt1.x = xMouse;
    126. endPt1.y = winHeight - yMouse;
    127. cout<<endPt1.x<<"\n" ;
    128. cout<<endPt1.y<<"\n";
    129. a.x = endPt1.x ;
    130. a.y = endPt1.y;
    131. draw();
    132. drawLineSegment(endPt1, endPt2);
    133. speichern(a.x, a.y);
    134. glFlush();
    135. drawLine(endPt1, endPt2);
    136. glFlush();
    137. //drawLineLoop(endPt1,endPt2);
    138. glFlush();
    139. }
    140. else
    141. if(button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
    142. //draw();
    143. //draw_polygons();
    144. //draw_polygons();
    145. //float *g;
    146. //float *t;
    147. //g = &erste[0];
    148. //t = &erste[1];
    149. //glVertex2i(g,t);
    150. //glVertex2iv(erste);
    151. // int point[] = {10,20};
    152. // glVertex2iv(point);
    153. glEnd();
    154. //bool bInAction = false;
    155. glFlush(); //zeichen
    156. }
    157. void main(int argc, char**argv)
    158. {
    159. glutInit(&argc, argv); //initialisierung der GLUT Bibliothek
    160. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); //Initialisierung des Bildschirms(Art des Zwischenspeichers, Farben)
    161. glutInitWindowPosition(100,100); //Positionierung des Fensters
    162. glutInitWindowSize(winWidth,winHeight); //Fenstergröße
    163. glutCreateWindow("Draw Interactive Polyline"); //Fenstertitel
    164. init();
    165. glutButtonBoxFunc(buttonBoxFcn);
    166. glutDisplayFunc(displayFcn); //Festlegen der Funktion die zum zeichen auf dem Bildschurm verwendet wird
    167. glutReshapeFunc(winReshapeFcn);
    168. glutMouseFunc(polyline); //Funktion die die Funktion "polyline" aufruft, wenn der Maus betätigt wurde
    169. glutMainLoop();
    170. }
    Alles anzeigen