1 Ope ngl OpenGL OpenGL OpenGL OpenGL OpenGL
Chapter 1 Ope ngl 31 OpenGL,. Ope ngl? OpenGL, OpenGL,. Ope ngl OpenGL,. Ope ngl OpenGL. Ope ngl OpenGL,. OpenGL. OpenGL OpenGL, GLUT(Graphi cs Li brary Ut i l i t y Tool ki t ).. Ope ngl? OpenGL, 3 250 (200 OpenGL, 50 OpenGL Utility Library ). OpenGL., OpenGL.., OpenGL 3. OpenGL,,,,, (geometric primitives ). GLU(OpenGL Utility Library), OpenGL. GLU NURBS. OpenGL GLU.
32 Ope ngl 4 - OpenGL 1.4 OpenGL, OpenGL.,. OpenGL. 1 9 OpenGL.. 1 (wireframe).,. (, ).,.,.,.,,.. 2 (depth-cue).. OpenGL ( (fog) ). 3 (antialiasing). (pi xel, pi ct ur e el ement ),.. 4 (lighting) (flat-shading).... 5, (smooth-shading). 3. 6 5 (s hadow) (texture). OpenGL, 9 14. 3 2..,,.
Chapter 1 Ope ngl 33 7 (motion-blur).,. 8. 3. 9 2. 2. 10 DOF(depth-of-field)... OpenGL. OpenGL ( OpenGL ). 1.., (OpenGL,,,, ). 2. 3,. 3..,,,. 4.. (rasterization)., OpenGL.,,. X Window System OpenGL.,, OpenGL (client), (server). OpenGL ( ), OpenGL
34 Ope ngl 4 - OpenGL 1.4. OpenGL,. Ope ngl OpenGL.., OpenGL (state),. OpenGL,. (rende ring). (mode l) (vertice) (,, ).. (pixel),.. (bitplane), (bit)..,., OpenGL. [ 1-1] [ 1-1]. [ 1-1] Ope ngl #i ncl ude <what eve ryouneed.h> mai n( ) { I ni t i al i zeawi ndowpl ease ( ) ; gl Cl ea rcol or (0.0, 0.0, 0.0, 0.0) ; gl Cl ea r (GL_COLOR_BUFFER_BIT) ; gl Col or3f (1.0, 1.0, 1.0) ;
Chapter 1 Ope ngl 35 } gl Ort ho (0.0, 1.0, 0.0, 1.0, - 1.0, 1.0) ; gl Begi n (GL_POLYGON) ; gl Ve rt ex3f (0.25, 0.25, 0.0) ; gl Ve rt ex3f (0.75, 0.25, 0.0) ; gl Ve rt ex3f (0.75, 0.75, 0.0) ; gl Ve rt ex3f (0.25, 0.75, 0.0) ; gl End () ; gl Fl ush () ; Updat ethewi ndowandcheckfor Event s ( ) ; [ 1-1] mai n( ) (window). I ni t i al i zeawi ndowpl eas e ( ),, OpenGL. OpenGL. gl Cl earcol or ( ), gl Cl ear ( )., gl Cl ear ( )., gl Col or3f ( ) ( ).,.
36 Ope ngl 4 - OpenGL 1.4 gl Ort ho ( ) OpenGL, (coordinate system)., gl Begi n( ) gl End( ).. gl Ve rt ex3f ( ). (x, y, z), z 0., (buffer) gl Fl us h( ). Updat ethewi ndowandchec kforeve nt s ( ),.,, OpenGL., I ni t i al i zeawi ndow Pl eas e ( ) Updat ethewi ndowandchec kforeve nt s ( ), [ 1-1]. Ope ngl gl Cl earcol or ( ) OpenGL gl (prefix). GL_,, _ (, GL_COLOR_BUFFER_BIT)., gl Col or3f ( ) gl Ve rt ex3f ( ) 3f (suffix). gl Col or3f ( ) Col or OpenGL, OpenGL., 3., Col or. f (floating-point). OpenGL. OpenGL 8. [ 1-1] ISO C OpenGL
Chapter 1 Ope ngl 37 OpenGL. C++ Ada OpenGL. [ 1-1] C OpenGL b 8 i nt eger si gned char GLbyt e s 16 i nt eger short GLshor t i 32 i nt eger i nt l ong GLi nt, GLsi zei f 32 f l oat i ng-poi nt f l oat GLf l oat, GLcl ampf d 64 f l oat i ng-poi nt doubl e GLdoubl e, GLcl ampd ub 8 unsi gned i nt eger unsi gned cha r GLubyt e, GLbool ean us 16 uns i gned i nt ege r unsi gned short GLushort ui 32 uns i gned i nt ege r unsi gned i nt unsi gned l ong GLui nt, GLenum, GLbi t f i el d,, gl Ve rt ex2i (1, 3) ; gl Ve rt ex2f (1.0, 3.0) ; 32 integer, (single-precision floating-point). OpenGL C OpenGL. OpenGL,. OpenGL v, ( ).,,..
38 Ope ngl 4 - OpenGL 1.4 gl Col or 3f (1.0, 0.0, 0.0) ; GLf l oat col or_a r r ay [ ] = {1.0, 0.0, 0.0} ; gl Col or 3f v (col or_ar ray) ;, OpenGL GLvoid. OpenGL. OpenGL, (*)., gl Col or* ( ).., gl Ve rt ex*v ( ). Ope ngl OpenGL (state machine).,..,. OpenGL,,,,,,,,. gl Enabl e ( ) / gl Di s abl e ( ) /.,. gl Get Bool eanv ( ), gl Get Doubl ev ( ), gl Get Fl oat v ( ), gl Get I nt ege rv ( ), gl Get Poi nt e rv ( ), gl Is Enabl e d( ).. (, gl Get Li ght * ( ), gl Get Error ( ), gl Get Pol ygonst i ppl e ( ))., gl Pus hat t ri b( ), gl Pus hcl i e nt At t ri b( ) (attribute stack), gl PopAt t ri b( ) gl PopCl i ent At t ri b( )..
Chapter 1 Ope ngl 39 OpenGL B. gl Get * ( ). Ope ngl OpenGL OpenGL (OpenGL Rendering Pipeline). OpenGL [ 1-2], OpenGL. 3,. [ 1-2] [ 1-2] OpenGL.,, (evaluator) (pre-vertex),,,.
40 Ope ngl 4 - OpenGL 1.4 ( ),. OpenGL., (dis play list) (, (immediate mode) ). ( 7 ).. (basis function) (control point). (evaluator),. (polynomial mapping), (surface normal),,, ( 12 )., (Per-Vertex Operation). 4 4. 3D (projection) ( 3 ).,.,.,,,,.
Chapter 1 Ope ngl 41 (Primitive Assembly). (clipping),. (point clipping),, (line clipping) (polygon clipping),., (perspective division)., (viewport) (depth, z- ). (culling),. (2 ).,,,,. OpenGL.., (pixel map) (scale), (bias). (clamp) (8 ).,,, (mapping), (clamping) (pixel-transfer operation),...
42 Ope ngl 4 - OpenGL 1.4 OpenGL (Texture Assembly).,. OpenGL. (9 ). (rasterization) (fragment).,. (filled polygon),,,,, (coverage, ).., (per-fragment operation).., (texel, texture element) (texturing). fog scissor,,, ( ).., (blending), (dithering),, (bitmask) (6 10 ).,,. Ope ngl OpenGL,., OpenGL
Chapter 1 Ope ngl 43.. OpenGL (GLU, OpenGL Ut i l i t y Li bra r y), (t essel l at i on), OpenGL. OpenGL. GLU OpenGL Ref e r ence Manual. GLU, 11 12 GLU NURBS, GLU. GLU gl u. OpenGL. X Wi ndow Syst em, GLX(OpenGL Ext ens i on t o t he X Wi ndow Syst em) OpenGL. GLX gl x. MS Wi ndows 95/ 98/ NT, OpenGL WGL. WGL wgl., IBMOS/ 2 PGL(Pr esent at i on Manager t o OpenGL i nt e r f ace ), pgl. Appl e, AGL, agl. C. GLX OpenGL Ref er ence Manual. OpenGL (GLUT, OpenGL Ut i l i t y Tool ki t ) Ma r k Ki l gar d, API. GLUT, Mar k Ki l ga rd OpenGL Pr ogr ammi ng f or t he X Wi ndow Syst em (Addi son-wesl ey, 1996 ). GLUT gl ut. GLUT p.27. include OpenGL gl.h., OpenGL GLU glu.h. OpenGL. #i ncl ude <GL/ gl. h> #i ncl ude <GL/ gl u.h>
44 Ope ngl 4 - OpenGL 1.4 MS Wi ndows gl. h gl u.h wi ndows.h, MS Wi ndows gl.h gl u. h wi ndows. h. OpenGL. gl.h. OpenGL (OpenGL Extensions), glext.h., OpenGL (http://www.opengl.org).. #i ncl ude "gl ext.h " <> ( ). glext.h, gl.h., (Unix, /usr/include). GLX, AGL, PGL, WGL,., GLX. #i ncl ude <X11/Xl i b. h> #i ncl ude <GL/ gl x. h> MS Windows WGL. #i ncl ude <wi ndows.h> GLUT. #i ncl ude <GL/ gl ut.h>
Chapter 1 Ope ngl 45 gl ut. h gl. h gl u.h, i ncl ude. gl ut.h MS Wi ndows,. GLUT gl ut. h, gl.h gl u. h., OpenGL C,. #i ncl ude <st dl i b.h> #i ncl ude <st di o. h>. GLUT OpenGL OpenGL. OpenGL.,,, Windows., GLUT. OpenGL GLUT., OpenGL,,, GLUT, 3. (OpenGL (GLU) GLUT, 3 ). GLUT OpenGL, OpenGL. GLUT ( GLUT
46 Ope ngl 4 - OpenGL 1.4 D Kilgard OpenGL Programming for the X Window System 4 5 ).. gl ut I ni t (i nt *argc, cha r **argv ) GLUT, (X Wi ndow Syst em -di spl ay -geomet ry ). GLUT gl ut Ini t ( ). gl ut I ni t Di s pl aymode (uns i gned i nt mode ) RGBA,.,, ( gl ut Set Col or ( ) ).,,, (accumul at i on buf f e r )., RGBA,, gl ut I ni t Di s pl aymode (GLUT_ DOUBLE GLUT_ RGB GLUT_ DEPTH). gl ut I ni t Wi ndowpos i t i on(i nt x, i nt y ). gl ut I ni t Wi ndowsi ze (i nt wi dt h, i nt si z e ). i nt gl ut Creat ewi ndow(cha r *s t ri ng ) OpenGL.., gl ut Mai nloop( ). gl ut Di s pl ayfunc (voi d (*f unc ) (voi d ) ). GLUT.., gl ut Pos t Re di s pl ay ( ) gl ut Mai nloop( ).
Chapter 1 Ope ngl 47 gl ut Mai nloop(voi d)..,,.. [ 1-2] [ 1-1] GLUT.., i ni t ( ). di s pl ay ( ), GLUT. [ 1-2] GLUT Ope ngl : he l l o. c voi d di s pl ay (voi d) { / *. */ gl Cl ear (GL_COLOR_BUFFER_BIT) ; / * (0.25, 0.25, 0.0) (0 75, 0.75, 0.0) * ( ). */ gl Col or3f (1.0, 1.0, 1.0) ; gl Begi n(gl_polygon) ; gl Ve rt ex3f (0.25, 0.25, 0.0) ; gl Ve rt ex3f (0 75, 0.25, 0.0) ; gl Ve rt ex3f (0.75, 0.75, 0.0) ; gl ve rt ex3f (0.25, 0.75, 0.0) ; gl End ( ) ; / * OpenGL. */ } gl Fl us h( ) ; voi d i ni t (voi d) { / * ( ). */ gl Cl earcol or (0.0, 0.0, 0.0, 0.0) ; / *. */
48 Ope ngl 4 - OpenGL 1.4 } gl Mat r i xmode (GL_PROJ ECTION) ; gl LoadIdent i t y () ; gl Or t ho (0.0, 1.0, 0.0, 1.0, -1.0, 1.0) ; / * ( RGBA ),. * "hel l o ", * (i ni t ), * (di s pl ay). *,. */ } i nt mai n (i nt a rgc, cha r ** ar gv) { gl ut I ni t (&ar gc, ar gv) ; gl ut I ni t Di spl aymode (GLUT_SINGLE GLUT_RGB) ; gl ut I ni t Wi ndowsi ze (250, 250) ; gl ut I ni t Wi ndowposi t i on (100, 100) ; gl ut Cr eat ewi ndow( "hel l o ") ; i ni t () ; gl ut Di spl ayfunc (di spl ay) ; gl ut Mai nloop () ; ret ur n 0 ; / * ISO C mai n i nt. */. gl ut Res hape Func (voi d (*f unc ) (i nt w, i nt h ) ). gl ut KeyboardFunc (voi d (*f unc ) (unsi gned char key, i nt x, i nt y ) ),. gl ut Mous efunc (voi d (*f unc ) (i nt but t on, i nt s t at e, i nt x, i nt y ) ),. gl ut Mot i onfunc (voi d (*f unc ) (i nt x, i nt y ) ).
Chapter 1 Ope ngl 49, (idle) gl ut I dl e Func (voi d (*f unc ) (voi d ) ).. NULL. 3 GLUT 3. (cone ) (i cosahedron) (t eapot ) (cube ) (oct ahedron) (t et rahedron) (dodecahedron) (sphere ) (t or us ),.,. voi d gl utwi recube (GLdoubl e s i z e ) ; voi d gl ut Sol i dcube (GLdoubl e si ze ) ; voi d gl utwi resphe re (GLdoubl e radi us, GLi nt sl i ces, GLi nt s t acks ) ; voi d gl ut Sol i dsphe re (GLdoubl e radi us, GLi nt sl i ces, GLi nt s t acks ) ; ( D )..,,. 24... 1
50 Ope ngl 4 - OpenGL 1.4 24, ( 16 ). 60 76 (refresh), 120. 30 60 60 120. 120.,.. open_wi ndow() ; f or (i = 0 ; i < 1000000 ; i ++) { cl ea r_t he_wi ndow() ; } dr aw_f rame (i ) ; wai t _unt i l _a_24t h_of _a_second_i s_ove r ( ) ;,, 1/24,. 1/24,,. 1/24 (?).,. OpenGL (double-buffering)..,,..,.,..
Chapter 1 Ope ngl 51. open_wi ndow_i n_doubl e_buf f er_mode ( ) ; f or (i = 0 ; i < 1000000 ; i ++) { cl ear_t he_wi ndow( ) ; draw_f r ame (i ) ; swap_t he_buf f er s ( ) ; } OpenGL, swap_t he_ buf f e rs ( ),.,. 60 60fps(frames per second), 1/60. 1/60,., 1/45, 30fps 1/30 1/45 1/90.,., 1/60, 60fps, 30fps, 20fps, 15fps, 12fps (60/1, 60/2, 60/3, 60/4, 60/5,...).,, 60fps, 1/60 60fps 30fps. 1/30 30 20fps. (1/60, 2/60, 3/60 ),,.,..
52 Ope ngl 4 - OpenGL 1.4 = +.,,. 3.,., (viewpoint),,.. swap_t he_ buf f e rs ( ). swap_t he_ buf f e rs ( ),, OpenGL., X Window System, GLX. voi d gl xswapbuffers (Di spl ay *dpy, Wi ndow wi ndow) ; ( C.) GLUT,. voi d gl ut SwapBuf f er s (voi d) ; [ 1-3] [ 1-3], gl ut SwapBuf f e rs ( )., GLUT idle.. [ 1-3]
Chapter 1 Ope ngl 53 [ 1-3] : doubl e. c st at i c GLf l oat s pi n = 0.0 ; voi d i ni t (voi d) { } gl Cl earcol or (0.0, 0.0, 0.0, 0.0) ; gl ShadeModel (GL_FLAT) ; voi d di s pl ay (voi d) { } gl Cl ear (GL_COLOR_BUFFER_BIT) ; gl Pus hmat r i x ( ) ; gl Rot at ef (s pi n, 0.0, 0.0, 1.0) ; gl Col or 3f (1.0, 1.0, 1.0) ; gl Rect f ( -25.0, -25.0, 25.0, 25.0) ; gl PopMat r i x () ; gl ut SwapBuf f e r s () ; voi d s pi ndi s pl ay (voi d) { } s pi n = spi n + 2.0 ; i f (spi n > 360.0) spi n = s pi n - 360.0 ; gl ut Post Redi s pl ay () ; voi d r es hape (i nt w, i nt h) { } gl Vi ewpor t (0, 0, (GLs i zei ) w, (GLsi zei ) h) ; gl Mat r i xmode (GL_PROJ ECTION) ; gl LoadI dent i t y ( ) ; gl Ort ho ( -50.0, 50.0, -50.0, 50.0, - 1.0, 1.0) ; gl Mat r i xmode (GL_MODELVI EW) ; gl LoadI dent i t y ( ) ; voi d mouse (i nt but t on, i nt st at e, i nt x, i nt y) { swi t ch(but t on) { case GLUT_LEFT_BUTTON:
54 Ope ngl 4 - OpenGL 1.4 } } i f (st at e == GLUT_DOWN) gl ut Idl efunc (s pi ndi s pl ay) ; break ; case GLUT_MI DDLE_BUTTON: i f (st at e == GLUT_DOWN) break ; def aul t : break ; gl ut Idl efunc (NULL) ; / * *, *. */ i nt mai n(i nt ar gc, char ** a rgv) { } gl ut I ni t (&ar gc, ar gv) ; gl ut I ni t Di spl aymode (GLUT_DOUBLE GLUT_RGB) ; gl ut I ni t Wi ndowsi ze (250, 250) ; gl ut I ni t Wi ndowposi t i on (100, 100) ; gl ut Cr eat ewi ndow(a rgv [0] ) ; i ni t () ; gl ut Di spl ayfunc (di spl ay) ; gl ut ReshapeFunc (reshape ) ; gl ut MouseFunc (mouse ) ; gl ut Mai nloop () ; ret ur n 0 ;