Graphische Primitive Punkte Geradenstücke (Liniensegmente) Polygone
Punkte glvertex3f (50.0, 50.0, 0.0) glvertex2f (50.0, 50.0) glvertex4f (50.0, 50.0, 0.0,1.0) GLfloat vect[3] = {50.0, 50.0, 0.0} glvertex3fv (vect)
Darstellung von Punkten I glbegin(gl_points); /* Liste von Punkten */ glvertex2f (50.0, 50.0) ; /* Punkt-Definition */ glvertex4f (-25.0, -25.0, 0.0, 1.0); /* */ glvertex4f (0.0, 0.0, 0.0,1.0); /* */ glvertex3f (319.0, -14.0, 0.0); /* */ glvertex3f (7.0, 3.0, 0.0); /* */ glvertex2f (-30.0, 12.0); /* */ glend(); /* Ende der Liste */
Darstellung von Punkten II
Darstellung von Punkten III glpointsize(7.0); /* Punktgröße definieren */ glbegin(gl_points); /* Liste von Punkten */ glvertex2f (50.0, 50.0) ; /* Punkt-Definition */ glvertex4f (-25.0, -25.0, 0.0, 1.0); /* */ glvertex4f (0.0, 0.0, 0.0,1.0); /* */ glvertex3f (319.0, -14.0, 0.0); /* */ glvertex3f (7.0, 3.0, 0.0); /* */ glvertex2f (-30.0, 12.0); /* */ glend(); /* Ende der Liste */
Darstellung von Punkten IV Inquiry-Funktion: GLfloat size[2]; /* min/max Größe */ GLfloat step; /* Schrittweite */ glgetfloatv (GL_POINT_RANGE, size); glgetfloatv (GL_POINT_SIZE_GRANULARITY, &step);
Darstellung von Punkten IV
Darstellung von Punkten V void display(void) { float angle, x, y; GLclampf b = 0.0; GLclampf g = 0.0; GLfloat size = 10.0; glclear(gl_color_buffer_bit); for (angle=0.0; angle <= PI; angle+= PI/20.0) //#define P I 3.1415 { x = 30*cos(angle); y = 30.0*sin(angle); glpointsize(size); //!!! glbegin(gl_points); glcolor3f (1.0, b, g); glvertex2f (x, y); glend(); size += 0.5; b += 0.05; g += 0.05; } /* end_for */ } glflush();
Geradenstücke I GLfloat vect[2] = {-30.0, 12.0} glbegin(gl_lines); /* Liste von Punkten */ glvertex2f (50.0, 50.0) ; /* Punkt-Definition */ glvertex4f (-25.0, -25.0, 0.0, 1.0); /* */ glvertex4f (0.0, 0.0, 0.0,1.0); /* */ glvertex3f (319.0, -14.0, 0.0); /* */ glvertex3f (7.0, 3.0, 0.0); /* */ glvertex2fv (vect); /* */ glend(); /* Ende der Liste */
Geradenstücke II
Geradenstücke III gllinewidth(5.0); glbegin(gl_lines); /* Liste von Punkten */ glvertex2f (50.0, 50.0) ; /* Punkt-Definition */ glvertex4f (-25.0, -25.0, 0.0, 1.0); /* */ glvertex4f (0.0, 0.0, 0.0,1.0); /* */ glvertex3f (19.0, -14.0, 0.0); /* */ glvertex3f (7.0, 3.0, 0.0); /* */ glvertex2f (-30.0, 12.0); /* */ glend(); /* Ende der Liste */
Geradenstücke IV Inquiries: GLfloat width_range[2]; GLfloat step; glgetfloatv (GL_LINE_WIDTH_RANGE, width_range); glgetfloatv (GL_LINE_WIDTH_GRANULARITY, &step);
Geradenstücke V
Geradenstücke VI void display(void) { float angle, x, y; GLclampf b = 0.0; GLclampf g = 0.0; glclear(gl_color_buffer_bit); gllinewidth(3.0); glbegin(gl_line_strip); for (angle=0.0; angle <= PI; angle+= PI/11.0) { x = 30*cos(angle); y = 30.0*sin(angle); glcolor3f (1.0, b, g); glvertex2f (x, y); b += 0.1; g += 0.1; } /* end_for */ glend(); /* Ende LINE_STRIP */ glflush(); }
Geradenstücke VII GL_LINES GL_LINE_STRIP GL_LINE_LOOP
Geradenstücke VIII Line Stippling
Geradenstücke IX Line Stippling void gllinestipple(glint factor, GLushort pattern); // 1 <=factor <= 255 Beispiel: gllinestipple (1, 0xAAAA); glenable(gl_line_stipple);... gldisable(gl_line_stipple);
void display(void) { GLfloat y; GLint factor = 1; GLushort pattern = 0x5555; Geradenstücke X Line Stippling // Varying Y coordinate // Stippling factor // Simple stipple pattern glclear(gl_color_buffer_bit); // Clear the window with current clearing color glenable(gl_line_stipple); // Enable stippling for(y = -40.0f; y <= 40.0f; y += 10.0f) { gllinestipple(factor,pattern); // Step up Y axis 10 units at a time // Reset the repetition factor and pattern glbegin(gl_lines); glvertex2f(-40.0f, y); glvertex2f(40.0f, y); glend(); factor++; } glflush(); // Flush drawing commands }
Geradenstücke XI Line Stippling
Polygone I Voraussetzungen eben Polygon ist Teilmenge einer Ebene einfach keine Schnittpunkte der Kanten konvex jede Strecke zwischen zwei Punkten des Polygon liegt vollständig innerhalb des Polygons. zweiseitig (front face, back face)
Polygone II Darstellung nur Eckpunkte (vertices) Umriß (outline) gefüllt mit oder ohne Muster (solid, stipple pattern) Voreinstellung: gleiche Darstellung für Vorder- und Rückseite
Polygone III Darstellung Einstellungen: void glpolygonmode(glenum face, Glenum mode) face: GL_FRONT_AND_BACK, GL_FRONT, GL_BACK) mode: GL_POINT, GL_LINE, GL_FILL Beispiele: glpolygonmode(gl_front, GL_FILL); glpolygonmode(gl_back, GL_LINE);
Polygone IV Definition von Vorder- und Rückseite Definition der Vorderseite durch geordnete Aufzählung der Eckpunkte: mathematisch positiv / in Gegenuhrzeigerrichtung (counterclockwise): mathematisch negativ / in Uhrzeigerrichtung (clockwise)
Polygone V Bestimmung von Vorder-/Rückseite in OpenGL Berechnung der Polygonfläche in geeigneten Koordinaten:
Polygone VI Definition der Vorderseite Definition durch Festlegung der Drehrichtung: void glfrontface( GLenum mode) mode: GL_CCW, GL_CW default: GL_CCW Beispiel: glfrontface (GL_CCW);
Polygone VIIa Darstellung glbegin(gl_triangle_fan); //Defined in counterclockwise order glvertex3f(0.0, 0.0, 0.0); for (i=0; i<=8; i++) { } if (i%2 == 0) glcolor3f (1.0, 0.0, 0.0); else glcolor3f (0.0, 1.0, 0.0); glvertex3f (cos(i*2*pi/8), sin(i*2*pi/8), 0.0); glend(); glbegin(gl_triangle_fan); //Defined in clockwise order glcolor3f (1.0, 0, 1.0); for (i=0; i>= -7; i--) glvertex3f (cos(i*2*pi/8)+1, sin(i*2*pi/8)+1, -1); glend();
Polygone VIIb Darstellung
Polygone VIII Polygon Stippling void glpolygonstipple(glubyte *mask) mask: pointer to 32x32 stipple pattern Beispiel: GLubyte pattern[128]; glpolygonstipple (pattern); glenable(gl_polygon_stipple). gldisable(gl_polygon_stipple)
Polygone VIIIa Polygon Stippling
Polygone IX GL_TRIANGLES GL_TRIANGLE_FAN GL_TRIANGLE_STRIP GL_QUADS GL_QUAD_STRIP GL_POLYGON void glrectw(glww x1, GLww y1, GLww x2, GLww y2) mit (w,ww){(d, double),( f, float), (i, int), (s,short)}
void draw_triangles (void) { int i; Polygone IXa glbegin(gl_triangles); //Defined in counterclockwise order for (i=0; i<8; i++) { if (i%2 == 0) glcolor3f (1.0, 1.0, 0.0); else glcolor3f (0.0, 1.0, 1.0); glvertex3f(0.0, 0.0, 0.0); glvertex3f (cos(i*2*pi/8), sin(i*2*pi/8), 0.0); glvertex3f (cos((i+1)*2*pi/8), sin((i+1)*2*pi/8), 0.0); } glend(); }
Polygone IXai
Polygone IXb void draw_triangle_fan (void) { int i; glbegin(gl_triangle_fan); //Defined in counterclockwise order glvertex3f(0.0, 0.0, 0.0); for (i=0; i<=8; i++) { if (i%2 == 0) glcolor3f (1.0, 0.0, 0.0); else glcolor3f (0.0, 1.0, 0.0); glvertex3f (cos(i*2*pi/8), sin(i*2*pi/8), 0.0); } glend(); }
Polygone IXbi
Polygone IXc void draw_triangle_strip (void) { int i; float phi = -1.0*PI; float r = 1.5; glbegin(gl_triangle_strip); //First triangle defined in counterclockwise order glvertex3f (r*sin(pi/4.0)*cos(phi), r*cos(pi/4.0), r*sin(pi/8.0)*sin(phi)); glvertex3f (r*sin(pi/2.0)*cos(phi), r*cos(pi/2.0), r*sin(pi/2.0)*sin(phi)); for (i=0; i<8; i++) { if (i%2 == 0) { glcolor3f (1.0, 1.0, 0.0); glvertex3f (r*sin(pi/4.0)*cos(phi-(i+1)*pi/8.0), r*cos(pi/4.0), r*sin(pi/4.0)*sin(phi-(i+1)*pi/8.0)); }
Polygone IXci else { glcolor3f (0, 1.0, 1.0); glvertex3f (r*sin(pi/2.0)*cos(phi-(i+1)*pi/8.0), r*cos(pi/2.0), r*sin(pi/2.0)*sin(phi-(i+1)*pi/8.0)); } } glcolor3f (1.0, 1.0, 0.0); glvertex3f (r*sin(pi/4.0)*cos(0.0), r*cos(pi/4.0), r*sin(pi/2.0)*sin(0.0)); glend(); }
Polygone IXcii
Polygone IXd void draw_quads (void) { int i; glbegin (GL_QUADS); for (i=0; i>=-8; i--) { if (i%2 == 0) glcolor3f (0.0, 0.0, 1.0); else glcolor3f (0.0, 1.0, 0.0); glvertex3f (cos(i*pi/4.0), sin(i*pi/4.0), 0); glvertex3f (0.5*cos(i*PI/4.0), 0.5*sin(i*PI/4.0), 1.5); glvertex3f (0.5*cos((i-1)*PI/4.0), 0.5*sin((i-1)*PI/4.0), 1.5); glvertex3f (cos((i-1)*pi/4.0), sin((i-1)*pi/4.0), 0); } glend(); }
Polygone IXdi
Polygone IXe void draw_quad_strip(void) { int i; glbegin (GL_QUAD_STRIP); for (i=0; i>=-8; i--) { if (i%2 == 0) glcolor3f (1.0, 1.0, 0.0); else glcolor3f (1.0, 1.0, 1.0); glvertex3f (cos(i*pi/4.0), sin(i*pi/4.0), 0); glvertex3f (0.5*cos(i*PI/4.0), 0.5*sin(i*PI/4.0), 1.5); } glend(); }
Polygone IXei
Rendering von Kanten: Polygone X Boundary Edges void gledgeflag (GLboolean flag) flag: GL_TRUE, GL_FALSE default: GL_TRUE Beispiel: gledgeflag (GL_TRUE); glvertex3f (.); gledgeflag (GL_FALSE) glvertex3f (.);
Rendering von Rückseiten: Polygone XI Back Face Culling glcullface(glenum mode) mode: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK default: GL_BACK Beispiel: glcullface (GL_BACK) glenable (GL_CULL_FACE); gldisable(gl_cull_face);
Polygone XII Verdeckungsrechnung glutinitdisplaymode (GLUT_DEPTH ); glclear (GL_DEPTH_BUFFER_BIT ); glenable(gl_depth_test); gldisable(gl_depth_test);
Übungsaufgaben Aufgabe 2: Orientierung von Polygonen Geben Sie eine Herleitung für die Gleichung zur Bestimmung der Fläche eines Polygons aus der Vorlesung. Wie/warum hängt die so berechnete Fläche mit der Orientierung des Polygons im Raum zusammen? Aufgabe 3: Modellierung Entwickeln Sie Algorithmen, um die gebräuchlichen, drei-dimensionalen Grundkörper, wie Würfel, Kugel, Zylinder, Tetraeder, Torus mit den zwei-dimensionalen Primitiven von OpenGL zu approximieren. Schreiben Sie zu jedem Körper eine Routine draw_xxx() mit xxx {cube,.., torus).