180 likes | 432 Views
Chapter 2. Graphics Programming. Sierpinski Gasket OpenGL API Primitives and Attributes Color Viewing Control Functions The Gasket program Polygons and recursion The 3-D gasket. Sierpinski Gasket (1). Recursive and Random Construction Process [ F0201 ] p.36 Code.
E N D
Chapter 2. Graphics Programming • Sierpinski Gasket • OpenGL API • Primitives and Attributes • Color • Viewing • Control Functions • The Gasket program • Polygons and recursion • The 3-D gasket
Sierpinski Gasket (1) • Recursive and Random Construction Process [F0201] • p.36 Code 1. Pick an initial point at random inside the triangle 2. Select one of the three vertices at random 3. Find the point halfway between the initial point and the randomly selected vertex 4. Display the new point by putting some sort of marker, such as a small circle, at its location 5. Replace the initial point with this new point 6. Return to step 2.
Sierpinski Gasket (2) • Pen-Plotter Model [F0202] • used in LOGO, GKS(Graphics Kernel System), PostScript • functions (p.37 codes) • moveto(x,y) • lineto(x,y) • altering the color or thickness • extension to 3D graphics system • the projection of points in 3D space • user: direct work in 3D space • computer: automatically process • two-dimensional applications: special case in 3D world • p = (x, y, 0) <= p = (x, y) • vertex (‘glVertex*’ where * are nt or ntv) • a location in space in computer graphics glVertex2i(GLint xi, GLint yi) glVertex3f(GLfloat x, GLfloat y, GLfloat y) GLfloat vertex[3] glVertex3fv(vertex)
Sierpinski Gasket (3) • A variety of geometric object by grouping vertics • glBegin(GL_LINES); • glVertex2f(x1,y1); • glVertex2f(x2,y2); • glEnd(); • glBegin(GL_POINTS); • glVertex2f(x1,y1); • glVertex2f(x2,y2); • glEnd(); void display(void) { point2 vertices[3] = {{0.0, 0.0}, {250.0, 500.0}, {500.0, 0.0}}; int j, k; int rand(); static point2 p = {75.0, 50.0}; for (k = 0; k < 50000; k++) { j = rand() % 3; p[0] = (p[0]+vertices[j][0])/2.0; p[1] = (p[1]+vertices[j][1])/2.0; glBegin(GL_POINTS); glVertex2fv(p); glEnd(); } glFlush(); } • Several issues (p.41) for • a complete program
Sierpinski Gasket (4) • Coordinate Systems • world coordinate (problem coordinate) • user’s coordinate • use any units • so, device-independent graphics • devicecoordinate(physical device coordinate, raster coordinate, screen coordinate) • use integer units corresponding to pixel • mapping of graphics system [F0205]
OpenGL API (1) • Graphics Functions • primitive functions: what • points(vertices), line segments, polygons, texts, curves, surfaces, etc. • attribute functions: how • color, intensity, line style, text style, area-filling patterns, etc. • viewing functions • view, window • transformation functions • translation, rotation, scaling, etc. • input functions • three different input modes with a variety of input devices (Chapter 3) • control functions • communication with the window system, parameter initialization, exception handling
GLU GL Frame Buffer OpenGL Application Program GLUT Xlib, Xtk GLX OpenGL API (2) • OpenGL Interface
OpenGL API (3) • How to use OpenGL? • OpenGL library • header file <gl\gl.h> <gl\glut.h> <gl\glu.h> • library file opengl32.lib glut.lib glu32.lib • programming in Visual C++ • Win32 Console application • Win32 application : windows programming • MFC AppWizard • file setup • .lib files opengl32.lib glut.lib glu32.lib --> vc/lib/ • .dll file opengl32.dll glu32.dll glut.dll --> /windows
Primitives and Attributes (1) • OpenGL • basic library: a small set of primitives • GLU: a richer set of objects derived from the basic library • Definition of Primitives glBegin(type); glVertex*( . . . ); : glVertex*( . . . ); glEnd( ); • Line Segments and PolyLines [F0208] glBegin(GL_POINTS); glVertex3f(10.0f, 15.0f, 18.0f); glVertex3f(20.0f, 12.0f, 17.0f); glEnd( ); glBegin(GL_LINES); glVertex3f(10.0f, 15.0f, 18.0f); glVertex3f(20.0f, 12.0f, 17.0f); glVertex3f(15.0f, 15.0f, 18.0f); glVertex3f(25.0f, 12.0f, 17.0f); glEnd( );
Primitives and Attributes (2) • Polygon • filling: hollow, solid, patterned, textured [F0209, F0210] • simple polygon : no pair of edges cross each other [F0211]
Primitives and Attributes (3) • Convex Polygon • convexity: all points on the line segment between any two points inside the object are inside the object [F0212] • convex objects [F0213] • Polygon Types in OpenGL [F0214, F0215]
Primitives and Attributes (4) • Text Font • outline font (stroke font, postscript font) [F0216] • define line segments or curves that outline each character • bitmap font (raster character, bitmapped font) [F0217] • rapid bit-block-transfer operation in frame buffer • raster-character replication [F0218] • Curved Object • primitives to approximate curves and surfaces • a circle with a regular polygon with n sides • mathematical definitions of curved objects (Chapter 10) • supported by GLU library • quadric surfaces, parametric polynomial curves and surfaces • Attributes • how a geometric primitive is to be rendered • point (color, size), line (color,thickness, type), polygon (pattern types, etc) [F0219] • immediate mode vs. use of display list • text (height, width, font, style (bold, italic, underlined)) [F0220]
Color (1) • Color Matching • light • the part of electromagnetic spectrum • color [F0221] • combinations of wavelengths of electromagnetic spectrum • color matching • a color characterized by a distribution C(l) Û a color on the screen of CRT • additive color matching [F0222] C = T1R + T2G + T3B C ® (T1, T2, T3) : tristimulus values • Color Model • color solid [F0223, PLATE21] • primaries : Red, Green, Blue Cyan(green+blue; 청록), Magenta(red+blue; 자홍), Yellow(red+green) • additive color (RGB) model vs. subtract color (CMY) model [F0224] • black/white background
Color (2) • RGB Color • conceptually separate frame buffers • for red, green, and blue images [F0225] • color cube • specify a color independently of # of bits in the frame buffer • hardware matches the specified color as closely as possible to the available display • RGBA system of OpenGL A (alpha channel) : opacity / transparency value ex. fog effect, image combination // define a clearing color glClearColor(1.0, 1.0, 1.0, 1.0) // set the present drawing color glColor3f(1.0, 0.0, 0.0) • Indexed Color • color-lookup table [F0226, F0227] • pixel value: index for color-lookup table • an anology with a painter • OpenGL: glindexi(element);
Viewing • Viewing Volume and Clipping • viewing(clipping) rectangle [F0228] • the area of the world that we image • viewing volume [F0229] • orthographic projection [F0230] • OpenGL • default: 2*2*2 • glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble near, GLdouble far) • gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom) • near = -1.0, far = 1.0 • Matrix Modes (Chapter 4) • model-view matrix : transformation operation • projection matrix : projection operation glMatrixMode(GL_PROJECTION); glLoadIdentity( ); glOrtho(left, right, bottom, top, near, far); glMatrixMode(GL_MODELVIEW);
Control Functions (1) • Interaction with the Window System • glutInit(int *argcp, char **argv) • initialization of the interaction between the windowing system and OpenGL • glutCreateWindow(char *name) • open an OpenGL window • glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE) • CLUT_RGB: RGB color mode (vs GLUT_INDEX) • GLUT_DEPTH: depth buffer for hidden surface removal • GLUT_DOUBLE: (double buffer) (vs GLUT_SINGLE) • default: GLUT_RGB | GLUT_SINGLE • glutInitWindowSize(480, 640) • glutInitWindowPosition(0, 0)
Control Functions (2) • Aspect Ratio and Viewport • aspect ratio of a rectangle • the ratio of the rectangle’s width to its height • aspect ratio mismatch [F0232] • viewport [F0233] • a rectangular area of the display window • default: the entire window • can adjust the aspect ratio mismatch • glViewport(GLint x, GLint y, GLsizei w, GLsizei h) • Main Functions • glutMainLoop(void) • cause the program to begin an event processing loop • glutDisplayFunc(void (*func) (void)) • func: the name of the function that will be called whenever the windowing system determines that the OpenGL window needs to be redisplayed. • myinit() • set the OpenGL state variables dealing with viewing and attributes - parameters that we prefer to set independently of the display function
Sierpinski Gasket • Program [PR1] #include <gl\glut.h> void myinit(void) { glClearColor(1.0, 1.0, 1.0, 1.0); glColor3f(1.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity( ); glOrtho(0.0f, 500.0f, 0.0f, 500.0f, 1.0f, -1.0f); glMatrixMode(GL_MODELVIEW); } void display(void) { typedef GLfloat point2[2]; point2 vertices[3] = {{0.0, 0.0}, {250.0, 500.0}, {500.0, 0.0}}; int j, k; long random(); point2 p = {75.0, 50.0}; glClear(GL_COLOR_BUFFER_BIT); for (k = 0; k < 50000; k++) { j = rand() % 3; p[0] = (p[0]+vertices[j][0])/2.0; p[1] = (p[1]+vertices[j][1])/2.0; glBegin(GL_POINTS); glVertex2fv(p); glEnd(); } glFlush(); } void main(int argc, char** argv) { void myinit(), display(); glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(0,0); glutCreateWindow("Simple OpenGL example"); glutDisplayFunc(display); myinit(); glutMainLoop(); }