Computer Graphics Lee ByungGook 2252021 Computer Graphics Lee
Computer Graphics Lee Byung-Gook 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 1
Open. GL • • Most Widely Adopted Graphics Standard High Visual Quality and Performance Industry standard, Reliable and portable Stable, Easy to use and Well-documented 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 2
Open. GL software libraries 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 3
Software libraries • Open. GL : provides most of the graphics functionality • GLU : provides support for some additional operations and primitive types, and is implemented using Open. GL function calls • glut : designed specifically to be used with Open. GL and it takes care of things like opening windows, redraw events, and keyboard and mouse input. It effectively hides all the windowing system dependencies for Open. GL 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 4
Installing Open. GL for Windows • bundled with Win 98, Win 95, Win. NT or download** it • look for dynamically-linked libraries (dlls): – C: Winntsystem 32Opengl 32. dll on Windows NT – C: WindowssystemOpengl 32. dll on Windows 95 • look for header files, e. g. , for Visual C++: – C: Program FilesMicrosoft Visual StudioVC 98includegl*. h 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 5
Installing glut for Windows • http: //www. opengl. org/resources/libraries/glutdlls 37 beta. zip • glut. h – C: Program FilesMicrosoft Visual StudioVC 98includeglglut. h • glut 32. lib – C: Program FilesMicrosoft Visual StudioVC 98libglut 32. lib • glut 32. dll – C: Windowssystem 32glut 32. dll on Windows 95, 98 – C: WINNTsystem 32glut 32. dll on Windows 2000, NT 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 6
Compiling with Visual C++ • Start Visual C++ • Choose File new, then from the Projects tab select Win 32 Console Application • Assign the project a name (`lab 00' for example), and choose a directory where the project files will be stored • Choose File new, then from the Files tab select C++ Source File • Assign the file a name, ‘lab 00' for example. The. cpp extension will be added for you. • Fill in the file lab 00. cpp 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 7
lab 00. cpp // lab 00. cpp, Computer Graphics, lbg@dongseo. ac. kr #include <GL/glut. h> void my. Display(void) { gl. Clear. Color (. 75, 0. 0); gl. Matrix. Mode(GL_PROJECTION); gl. Ortho(0, 500, -1, 1); gl. Matrix. Mode(GL_MODELVIEW); gl. Clear(GL_COLOR_BUFFER_BIT); gl. Color 3 f(0. 60, . 40, . 70); gl. Begin(GL_POLYGON); gl. Vertex 2 i(450, 250); gl. Vertex 2 i(412, 367); gl. Vertex 2 i(312, 440); gl. Vertex 2 i(188, 440); gl. Vertex 2 i(88, 368); gl. Vertex 2 i(50, 250); gl. Vertex 2 i(88, 132); gl. Vertex 2 i(188, 60); gl. Vertex 2 i(312, 60); gl. Vertex 2 i(412, 132); gl. End(); gl. Flush(); } 2/25/2021 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("lab 00"); glut. Display. Func(my. Display); glut. Main. Loop(); } Computer Graphics, Lee Byung-Gook, Dongseo Univ. 8
Compiling with Visual C++ • Choose Project Settings and select the C/C++ panel. Enable Generate browse info. • Select the Link panel. At the beginning (or anywhere) of the Object/library modules entry, add the libraries opengl 32. lib and glut 32. lib. Accept all the project setting changes by choosing OK. 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 9
Compiling with Visual C++ • Choose Build lab 00. exe, or simply hit the F 7 key in order to compile and link. • A successful compilation would have produced the following output in your 'build' window: • ------Configuration: lab 00 - Win 32 Debug------ Compiling. . . lab 00. cpp Linking. . . lab 00. exe - 0 error(s), 0 warning(s) 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 10
Compiling with Visual C++ • If there are errors, hitting F 4 will take you to the location of the first error message in the code, and thereafter always take you to the next error message. Or, one can double click on any error message to jump to the relevant source code. • Once the code has successfully compiled and linked, Build-Execute or Ctrl-F 5 will run the code, as will double-clicking on the test. exe icon (found in the Debug directory) from a file browsing window. 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 11
Basic drawing • Open. GL is a "state" machine : Once it is in a state, it stays in that state until the state is changed. • Basic commands gl. Clear. Color(red, green, blue, 0. 0); // sets background color gl. Clear(GL_COLOR_BUFFER_BIT); // clears the window to the background color gl. Color 3 f(red, green, blue); // values between 0. 0 (no color) & 1. 0 (full color) gl. Begin(*); // determines how vertices are interpreted 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 12
Basic drawing gl. Vertex 2 i(412, 367); gl. Vertex 2 i(312, 440); gl. Vertex 2 i(188, 440); gl. Vertex 2 i(88, 368); gl. Vertex 2 i(50, 250); gl. Vertex 2 i(88, 132); gl. Vertex 2 i(188, 60); gl. Vertex 2 i(312, 60); gl. Vertex 2 i(412, 132); gl. End(); // clears the state initiated by 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 13
Colors {1. 0 f, 0. 0 f} {0. 0 f, 1. 0 f} {1. 0 f, 0. 0 f, 1. 0 f} {0. 0 f, 1. 0 f} {1. 0 f, 1. 0 f} {. 25 f, . 25 f} {. 75 f, . 75 f} {. 60 f, . 40 f, . 12 f} {. 60 f, . 40 f, . 70 f} {. 98 f, . 625 f, . 12 f} {. 98 f, . 04 f, . 70 f} 2/25/2021 //red //green //blue //yellow //magenta //cyan //white //dark gray //light gray //brown //barney purple //pumpkin orange //pastel pink Computer Graphics, Lee Byung-Gook, Dongseo Univ. 14
Drawing primitives • Open. GL supports several basic primitive types, including points, lines, quadrilaterals, and general polygons. All of these primitives are specified using a sequence of vertices. • gl. Begin and gl. End delimit the vertices that define a primitive or a group of like primitives. • GL_POINTS Treats each vertex as a single point. Vertex n defines point n. N points are drawn. 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 15
Drawing primitives • GL_LINES Treats each pair of vertices as an independent line segment. Vertices 2 n-1 and 2 n define line n. N/2 lines are drawn. • GL_LINE_STRIP Draws a connected group of line segments from the first vertex to the last. Vertices n and n+1 define line n. N-1 lines are drawn. • GL_LINE_LOOP Draws a connected group of line segments from the first vertex to the last, then back to the first. Vertices n and n+1 define line n. The last line, however, is defined by vertices N and 1. N lines are drawn. 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 16
Drawing primitives • GL_TRIANGLES Treats each triplet of vertices as an independent triangle. Vertices 3 n-2, 3 n-1, and 3 n define triangle n. N/3 triangles are drawn. • GL_TRIANGLE_STRIP Draws a connected group of triangles. One triangle is defined for each vertex presented after the first two vertices. For odd n, vertices n, n+1, and n+2 define triangle n. For even n, vertices n+1, n, and n+2 define triangle n. N-2 triangles are drawn. • GL_TRIANGLE_FAN Draws a connected group of triangles. One triangle is defined for each vertex presented after the first two vertices. Vertices 1, n+1, and n+2 define triangle n. N-2 triangles are drawn. 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 17
Triangle 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 18
Drawing primitives • GL_QUADS Treats each group of four vertices as an independent quadrilateral. Vertices 4 n-3, 4 n-2, 4 n-1, and 4 n define quadrilateral n. N/4 quadrilaterals are drawn. • GL_QUAD_STRIP Draws a connected group of quadrilaterals. One quadrilateral is defined for each pair of vertices presented after the first pair. Vertices 2 n-1, 2 n+2, and 2 n+1 define quadrilateral n. N/2 -1 quadrilaterals are drawn. Note that the order in which vertices are used to construct a quadrilateral from strip data is different from that used with independent data. • GL_POLYGON Draws a single, convex polygon. Vertices 1 through N define this polygon. 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 19
Quad 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 20
Basic primitives • GL_LINES, GL_LINE_STRIP 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 21
Basic primitives • GL_LINE_LOOP, GL_TRIANGLES 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 22
Basic primitives • GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 23
Basic primitives • GL_QUADS, GL_POLYGON 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 24
Open. GL Naming Convension • • gl : gl library Vertex : command 2 f : number and type of parameters Open. GL Data Type Presentation C-language GLbyte 8 -bit integer signed char GLshort 16 -bit integer short GLint, GLsizei 32 -bit integer long GLfloat, GLclampf 32 -bit float GLdouble, GLclampd 64 -bit float double GLubyte, GLboolean 8 -bit unsigned char GLushort 16 -bit unsigned short GLuint, GLenum 32 -bit unsigned long 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. b s l f d ub us ui 25
gl. Vertex[234][b s i f d ub us ui]{v}(*) // allows 2 (x, y), 3 (x, y, z), or 4 (x, y, z, w) values // allows the following data types // b signed char; 8 bit integer // s short; 16 bit integer // i integer; 32 bit integer // f float; 32 bits // d double; 64 bits // ub unsigned byte; 8 bits // us unsigned short; 16 bits // ui unsigned int; 32 bits // optional v defined in an array (vector) 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 26
lab 01. cpp // lab 01. cpp, Computer Graphics, lbg@dongseo. ac. kr #include <stdio. h> #include <stdlib. h> #include <GL/glut. h> GLint window. Width, window. Height, N=4; void my. Draw(void) { gl. Color 3 f(0. 60, . 40, . 70); gl. Begin(GL_POLYGON); gl. Vertex 2 i(window. Width/N, window. Height/N); gl. Vertex 2 i(3*window. Width/N, 3*window. Height/N); gl. Vertex 2 i(window. Width/N, 3*window. Height/N); gl. End(); } void my. Axis(void) { int i; void my. Display(void) { gl. Color 3 f(0. 98, . 04, . 70); gl. Clear(GL_COLOR_BUFFER_BIT); gl. Begin(GL_LINES); my. Axis(); for(i=1; i<N; i++) { my. Draw(); gl. Vertex 2 i(i*window. Width/N, gl. Flush(); 0); } gl. Vertex 2 i(i*window. Width/N, window. Height); gl. Vertex 2 i(0, i*window. Height/N); gl. Vertex 2 i(window. Width, i*window. Height/N); } gl. End(); }2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 27
lab 01. cpp void my. Reshape(int width, int height) { gl. Clear. Color (. 75, 0. 0); gl. Viewport(0, 0, width, height); gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); gl. Ortho(0, width, 0, height, -1, 1); window. Width=width; window. Height=height; gl. Matrix. Mode(GL_MODELVIEW); } 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("lab 01"); glut. Reshape. Func(my. Reshape); glut. Display. Func(my. Display); glut. Main. Loop(); } 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 28
Lab 01 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 29
Lab 0101, 0102 • Homework 1 : in two weeks 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 30
Lab 0103, 0104 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 31
Lab 0105, 0106 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 32
Lab 0107, 0108 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 33
Lab 0109 2/25/2021 Computer Graphics, Lee Byung-Gook, Dongseo Univ. 34
- Slides: 34