Graphics Programming kucg korea ac kr Graphics Lab
Graphics Programming 고려대학교 컴퓨터 그래픽스 연구실 kucg. korea. ac. kr Graphics Lab @ Korea University
Contents n Our Goal in This Chapter n n KUCG Programming of the Sierpinski gasket How To? n Programming with Open. GL and C/C++ Open. GL API (Application Programmer’s Interface) o Primitives o Attributes o o Color Viewing o Control Functions o kucg. korea. ac. kr Graphics Lab @ Korea University
The Sierpinski Gasket n KUCG What is? Interesting shape in area such as fractal geometry n Object that can be defined recursively and randomly n n How to? n 1. 2. 3. 4. 5. 6. Start with three vertices in the plane Pick an initial point at random inside the triangle Select one of the three vertices at random Find the point halfway between the initial point and the randomly selected vertex Display this new point by putting some sort of marker, such as a small circle, at its location Replace the initial point with this new point Return to step 2 kucg. korea. ac. kr Graphics Lab @ Korea University
Generation of the Sierpinski Gasket KUCG p 0 p 1 v 1 kucg. korea. ac. kr p 2 v 3 Graphics Lab @ Korea University
Algorithm of the Sierpinski Gasket KUCG main( ) { initialize_the_system( ); for( some_number_of_points ) { pt = generate_a_point( ); display_the_point(pt); } cleanup( ); } kucg. korea. ac. kr Graphics Lab @ Korea University
The Pen-Plotter Model (1/3) n Conceptual Model Referencing the Output Device n n Produce images by moving a pen held < Pen plotter > by a gantry 2 Drawing Functions moveto(x, y); n lineto(x, y); n n KUCG (0, 1) (1, 1) (0, 0) (1, 0) Example : moveto(0, 0); lineto(1, 1); lineto(0, 0); kucg. korea. ac. kr Graphics Lab @ Korea University
The Pen-Plotter Model (2/3) n KUCG Vertex Location in space (2 D, 3 D, 4 D) n Define the atomic geometric objects n 1 : point o 2 : line segment o 3 : triangle, circle o 4 : quadrilateral o n Open. GL : gl. Vertex*( ) gl. Vertex 2 i(GLint xi, GLint yi); n gl. Vertex 3 f(GLfloat x, GLfloat y, GLfloat z); n gl. Vertex 3 fv(vertex); n #define GLfloat o GLfloat vertex[3]; o kucg. korea. ac. kr Graphics Lab @ Korea University
The Pen-Plotter Model (3/3) n KUCG Open. GL: gl. Begin( ), gl. End( ) Specify the geometric type n Line segment n (x 1, y 1) gl. Begin(GL_LINES); gl. Vertex 2 f(x 1, y 1); gl. Vertex 2 f(x 2, y 2); gl. End( ); (x 2, y 2) n A pair of points gl. Begin(GL_POINTS); gl. Vertex 2 f(x 1, y 1); gl. Vertex 2 f(x 2, y 2); gl. End( ); kucg. korea. ac. kr (x 1, y 1) (x 2, y 2) Graphics Lab @ Korea University
Function of “Display” of the Sierpinski Gasket KUCG void display(void) { typedef Glfloat point 2[2]; /* arbitrary triangle */ point 2 vertices[3] = { {0. 0, 0. 0}, {250. 0, 500. 0}, {500. 0, 0. 0} }; /* any desired initial point */ point 2 p = { 75. 0, 75. 0 }; for(int i=0; i<5000; i++) { /* pick a random vertex from 0, 1, 2 */ int j=rand()%3; /* compute new point */ p[0] = (p[0]+vertices[j][0])/2. 0; p[1] = (p[1]+vertices[j][1])/2. 0; /* display new point */ gl. Begin(GL_POINTS); gl. Vertex 2 fv(p); gl. End(); } gl. Flush(); } kucg. korea. ac. kr Graphics Lab @ Korea University
The Sierpinski Gasket Generated with 5000 Points kucg. korea. ac. kr KUCG Graphics Lab @ Korea University
Open. GL API (1/3) n API (Application Programmer’s Interface) n Interface between an application program and a graphics system Graphics Library (API) Application Program n KUCG Hardware Input Device Output Device Open. GL API Easy to learn, compared with other APIs n Nevertheless powerful n n Graphics Functions : Black Box Described by only its inputs and outputs n Nothing known about its internal working n User Program kucg. korea. ac. kr Function Calls Data Graphics System Output Input/Output Devices Graphics Lab @ Korea University
Open. GL API (2/3) n KUCG 6 Groups of Graphics Functions n Primitive functions o n Attributes functions o n rotation, translation, scaling Input functions o n describe camera - position, orientation Transformation functions o n how of an API : color, filling pattern Viewing functions o n what of an API - that object that can be displayed interactive application : keyboard, mice, data tablet Control functions o multiprocessing multiwindow environment - communicate the window system kucg. korea. ac. kr Graphics Lab @ Korea University
Open. GL API (3/3) n KUCG Open. GL Interface GLU Open. GL Application Program GL GLUT Frame Buffer Xlib, Xtk GLX Open. GL functions(names : gl~) : GL n Graphics utility library : GLU n Use only GL functions o Contain code for common objects such as spheres o n Graphics utility toolkit : GLUT o interface with the window system kucg. korea. ac. kr Graphics Lab @ Korea University
Primitives in Open. GL KUCG Points n Line segments n Polylines n n Polygons n Triangles and quadrilaterals n Strips and fans n Text n Curved objects n kucg. korea. ac. kr Graphics Lab @ Korea University
Points in Open. GL gl. Begin(GL_POINTS); gl. Vertex 2 fv(p 0); gl. Vertex 2 fv(p 1); gl. Vertex 2 fv(p 2); gl. Vertex 2 fv(p 3); gl. Vertex 2 fv(p 4); gl. Vertex 2 fv(p 5); gl. Vertex 2 fv(p 6); gl. Vertex 2 fv(p 7); gl. End(); kucg. korea. ac. kr KUCG p 7 p 0 p 1 p 6 p 2 p 5 p 3 p 4 Graphics Lab @ Korea University
Lines in Open. GL (1/3) n KUCG Line Segments gl. Begin(GL_LINES); gl. Vertex 2 fv(p 0); gl. Vertex 2 fv(p 1); gl. Vertex 2 fv(p 2); gl. Vertex 2 fv(p 3); gl. Vertex 2 fv(p 4); gl. Vertex 2 fv(p 5); gl. Vertex 2 fv(p 6); gl. Vertex 2 fv(p 7); gl. End(); kucg. korea. ac. kr p 0 p 7 p 1 p 6 p 2 p 5 p 3 p 4 Graphics Lab @ Korea University
Lines in Open. GL (2/3) n KUCG Polylines – Line Strip gl. Begin(GL_LINE_STRIP); gl. Vertex 2 fv(p 0); gl. Vertex 2 fv(p 1); gl. Vertex 2 fv(p 2); gl. Vertex 2 fv(p 3); gl. Vertex 2 fv(p 4); gl. Vertex 2 fv(p 5); gl. Vertex 2 fv(p 6); gl. Vertex 2 fv(p 7); gl. End(); kucg. korea. ac. kr p 0 p 7 p 1 p 6 p 2 p 5 p 3 p 4 Graphics Lab @ Korea University
Lines in Open. GL (3/3) n KUCG Polylines – Line Loop gl. Begin(GL_LINE_LOOP); gl. Vertex 2 fv(p 0); gl. Vertex 2 fv(p 1); gl. Vertex 2 fv(p 2); gl. Vertex 2 fv(p 3); gl. Vertex 2 fv(p 4); gl. Vertex 2 fv(p 5); gl. Vertex 2 fv(p 6); gl. Vertex 2 fv(p 7); gl. End(); kucg. korea. ac. kr p 0 p 7 p 1 p 6 p 2 p 5 p 3 p 4 Graphics Lab @ Korea University
Polygons (1/2) n Definition n n KUCG Object that is closed as a line loop, but that has an interior Simple Polygon n No pair of edges of a polygon cross each other Simple kucg. korea. ac. kr Nonsimple Graphics Lab @ Korea University
Polygons (2/2) n KUCG Convexity n If all points on the line segment between any two points inside the object, or on its boundary, are inside the object p 1 p 2 Convex Objects kucg. korea. ac. kr Graphics Lab @ Korea University
Polygons in Open. GL (1/6) n KUCG Polygon gl. Begin(GL_POLYGON); gl. Vertex 2 fv(p 0); gl. Vertex 2 fv(p 1); gl. Vertex 2 fv(p 2); gl. Vertex 2 fv(p 3); gl. Vertex 2 fv(p 4); gl. Vertex 2 fv(p 5); gl. Vertex 2 fv(p 6); gl. Vertex 2 fv(p 7); gl. End(); kucg. korea. ac. kr p 0 p 7 p 1 p 6 p 2 p 5 p 3 p 4 Graphics Lab @ Korea University
Polygons in Open. GL (2/6) n KUCG Quadrilaterals gl. Begin(GL_QUADS); gl. Vertex 2 fv(p 0); gl. Vertex 2 fv(p 1); gl. Vertex 2 fv(p 2); gl. Vertex 2 fv(p 3); gl. Vertex 2 fv(p 4); gl. Vertex 2 fv(p 5); gl. Vertex 2 fv(p 6); gl. Vertex 2 fv(p 7); gl. End(); kucg. korea. ac. kr p 0 p 7 p 1 p 6 p 2 p 5 p 3 p 4 Graphics Lab @ Korea University
Polygons in Open. GL (3/6) n KUCG Quadstrip gl. Begin(GL_QUAD_STRIP); gl. Vertex 2 fv(p 1); gl. Vertex 2 fv(p 2); gl. Vertex 2 fv(p 3); gl. Vertex 2 fv(p 0); gl. Vertex 2 fv(p 4); gl. Vertex 2 fv(p 7); gl. Vertex 2 fv(p 5); gl. Vertex 2 fv(p 6); gl. End(); kucg. korea. ac. kr p 0 p 7 p 1 p 6 p 2 p 5 p 3 p 4 Graphics Lab @ Korea University
Polygons in Open. GL (4/6) n KUCG Triangles gl. Begin(GL_TRIANGLES); gl. Vertex 2 fv(p 0); gl. Vertex 2 fv(p 1); gl. Vertex 2 fv(p 2); gl. Vertex 2 fv(p 3); gl. Vertex 2 fv(p 4); gl. Vertex 2 fv(p 5); gl. Vertex 2 fv(p 6); gl. Vertex 2 fv(p 7); gl. End(); kucg. korea. ac. kr p 0 p 7 p 1 p 6 p 2 p 5 p 3 p 4 Graphics Lab @ Korea University
Polygons in Open. GL (5/6) n KUCG Triangle Strip gl. Begin(GL_TRIANGLE_STRIP); gl. Vertex 2 fv(p 0); gl. Vertex 2 fv(p 7); gl. Vertex 2 fv(p 1); gl. Vertex 2 fv(p 6); gl. Vertex 2 fv(p 2); gl. Vertex 2 fv(p 5); gl. Vertex 2 fv(p 3); gl. Vertex 2 fv(p 4); gl. End(); kucg. korea. ac. kr p 0 p 7 p 1 p 6 p 2 p 5 p 3 p 4 Graphics Lab @ Korea University
Polygons in Open. GL (6/6) n KUCG Triangle Fan gl. Begin(GL_TRIANGLE_FAN); gl. Vertex 2 fv(p 0); gl. Vertex 2 fv(p 1); gl. Vertex 2 fv(p 2); gl. Vertex 2 fv(p 3); gl. Vertex 2 fv(p 4); gl. Vertex 2 fv(p 5); gl. Vertex 2 fv(p 6); gl. Vertex 2 fv(p 7); gl. End(); kucg. korea. ac. kr p 0 p 7 p 1 p 6 p 2 p 5 p 3 p 4 Graphics Lab @ Korea University
Attributes n Properties that Determines How to Render a Geometric Primitive n n KUCG Color, thickness, pattern of filling, etc. Color n Three color theory Blue Yellow M G C Red Cyan Y Green Additive Color kucg. korea. ac. kr R B Magenta Subtractive Color Solid Graphics Lab @ Korea University
Color in Open. GL n KUCG Set the Clear Color n gl. Clear. Color(1. 0, 1. 0) Opaque: opacity is 1. 0 o Window is cleared by white color o n Set the Color State Varible n gl. Color 3 f(1. 0, 0. 0); o n RGB color – red color Set the Size of Points n gl. Point. Size(2. 0); o 2 pixel wide kucg. korea. ac. kr Graphics Lab @ Korea University
Viewing n KUCG Viewing Volume gl. Ortho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far); n glu. Ortho 2 D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top); n n Matrix Mode n 2 types: model-view, projection gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity( ); glu. Ortho 2 D(0. 0, 500. 0); gl. Matrix. Mode(GL_MODELVIEW); kucg. korea. ac. kr Graphics Lab @ Korea University
Control (1/2) n KUCG Interaction with Window Systems glut. Init(int *argc, char *argv); n glut. Init. Display. Mode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); n glut. Init. Window. Size(480, 640); n glut. Inti. Window. Position(0, 0); n n Aspect Ratio and Viewport n gl. Viewprot(GLint x, GLint y, GLsizei w, GLsizei h); <Aspect-Ratio Mismatch> kucg. korea. ac. kr <Mapping to the Viewpot> Graphics Lab @ Korea University
Control (2/2) n KUCG Example – “main” , “display”, and “myinit” Functions #include <GL/glut. h> void main(int argc, char **argv){ glut. Init(&argc, argv); glut. Init. Display. Mode(GLUT_SINGLE|GLUT_RGB); glut. Init. Window. Size(500, 500); glut. Init. Window. Position(0, 0); glut. Create. Window(“simple Open. GL example”); glut. Display. Func(display); myinit(); glut. Main. Loop(); } void myinit(void){ /* attributes */ gl. Clear. Color(1. 0, 1, 0); gl. Color 3 f(1. 0, 0. 0); /* set up viewing */ gl. Matrix. Mode(GL_PROJECTION); glu. Load. Identity(); glu. Ortho 2 D(0. 0, 500. 0); gl. Matrix. Mode(GL_MODELVIEW); } kucg. korea. ac. kr Graphics Lab @ Korea University
- Slides: 31