1 20 (1) OpenGL TA 2008 10 20 1 C OpenGL (3DCG) OS Linux (Open SUSE 10.3) 3DCG OpenGL GUI GLUT OpenGL GLUT GLUI USB EyeToy 1.1 1 3DCG 2 3DCG GUI 2 USB EyeToy) 10/20( ) 10/27( ) 3DCG OpenGL OpenGL+GUI(GLUI) 3DCG 1.2 TA
2 2 OpenGL 1.3 http://www.cyber.t.u-tokyo.ac.jp/~kuni/enshu2008/ 2 OpenGL OpenGL(Open Graphics Library) Silicon Graphics (SGI) 3D OpenGL SGI HP, SUN, IBM UNIX Linux, FreeBSD PC UNIX Windows, Mac OS API PDA OpenGL OpenGL ES 3D 2.1 OpenGL OpenGL OpenGL OpenGL GLUT OpenGL GL OpenGL OpenGL gl GLU OpenGL Utility Library OpenGL GLU glu Window OpenGL (Window ) X Window System GLX, Windows NT/98/95 WGL, OS/2 PGL, Power Mac AGL OS, Window GLUT OpenGL Utility Toolkit(Window ) OpenGL
2.1 OpenGL 3 GLUT glut OpenGL 1 OpenGL
4 3 OpenGL 3 OpenGL 3.1 OpenGL OpenGL OpenGL 2 OpenGL ( ) ( ) 4x4 ( ) ( ) OpenGL OpenGL ( )
3.2 OpenGL 5 3.2 OpenGL main() /* Window */ InitializeAWindowPlease(); /* */ glclearcolor (0.0, 0.0, 0.0, 0.0); glclear (GL_COLOR_BUFFER_BIT); /* */ glcolor3f (1.0, 1.0, 1.0); /* */ glortho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); /* */ glbegin(gl_polygon); glvertex3f (0.25, 0.25, 0.0); glvertex3f (0.75, 0.25, 0.0); glvertex3f (0.75, 0.75, 0.0); glvertex3f (0.25, 0.75, 0.0); glend(); /* */ glflush (); /* Window */ UpdateTheWindowAndCheckForEvents(); 3.3 OpenGL GLUT OpenGL (sample clr.c) Web 3.4 3.4.1 glclearcolor(glclampf red, GLclampf green, GLclampf blue, GLclampfalpha); GLglampf 0.0 1.0
6 3 OpenGL 3 OpenGL (sample-clr.c) alpha glutclearcolor(0.0, 0.0, 0.0, 0.0); 3.4.2 glclearcolor glclear(gl_color_buffer_bit); 3.4.3 glflush(); 1 3.5 glut GLUT(OpenGL Utility Toolkit) GLUT Windows X window system GLUT 3.7 GLU 3
3.5 glut 7 GLUT glutinit void glutinit(int *argcp, char **argv); argcp main argc GLUT argv main argv GLUT X Window System GLUT -display, -geometry man X void glutinitwindowposition(int x, int y); void glutinitwindowsize(int width, int height); x, y width, height 4 OpenGL glutinitwindow* OpenGL glutinitwindow* (OpenGL )
8 4 2 glutinitdisplaymode void glutinitdisplaymode(unsigned int mode); mode RGBA (GLUT RGBA or GLUT INDEX) (GLUT DEPTH) (GLUT SINGLE or GLUT DOUBLE) RGBA glutinitdisplaymode(glut_rgba GLUT_DOUBLE GLUT_DEPTH); OpenGL int glutcreatewindow(char *titlestring); titlestring OpenGL OpenGL void glutpostredisplay(void); display 1 1 void glutmainloop(void); glutdisplayfunc 4 2 2 ( ) (sample-hello.c)
4.1 OpenGL 9 5 OpenGL (sample-hello.c) 4.1 OpenGL glcolor3f(glclampf red, GLclampf green, GLclampf blue); 0.0 1.0 3 glcolor3f(1.0f, 1.0f, 0.0f); glbegin(glenum mode); mode (GL POLYGON) (GL TRIANGLE FAN) ( ) (GL TRIANGLE STRIP) (GL LINES) (GL POINTS)
10 4 2 glend(); glvertex3f(glfloat x, GLfloat y, GLfloat z); glvertex* 3 3 (x, y, z) f 32bit (GLfloat) 4.2 OpenGL gluortho2d 6 gluortho2d(x1,x2,y1,y2); OpenGL 3 3 1 void glmatrixmode(glenum mode); mode
4.3 11 (GL PROJECTION) (GL MODELVIEW) (GL TEXTURE) void glloadidentity(void); 2 ( ) void gluortho2d(gldouble left, GLdouble right, GLdouble bottom, GLdouble top); gluortho2d 3 glortho near, far -1.0, 1.0 OpenGL 2 3-1.0 1.0 gluortho2d(-1.0, 1.0, -1.0, 1.0); glortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); 1. glmatrixmode(gl_projection); 2. glloadidentity(void); 3. gluortho2d(left, right, bottom, top); 4. glmatrixmode(gl_modelview); 4.3 OpenGL OpenGL ( ) void glviewport(glint x, GLint y, GLsizei width, GLsizei height);
12 4 2 7 (x,y) width height glutreshapefunc void glutreshapefunc(void (*func)(int width, int height)); OpenGL 2
13 5 OpenGL 5.1 OpenGL OpenGL 3 glut #include <GL/gl.h> /* */ #include <GL/glu.h> /* glu */ #include <GL/glx.h> /* X Window System */ GLUT #include <GL/glut.h> gl.h glu.h glut.h glut.h window.h UNIX WIndows GLUT -lglut -lglu -lgl simple.c. simple % gcc simple.c -o simple -lglut -lglu -lgl
14 5 OpenGL 5.2 Makefile #MAKEFILE CC = gcc RM = rm -f CFLAGS = -Wall LDFLAGS = -lglut -lglu -lgl -lm TARGET OBJS = sample-clr = sample-clr.o.c.o: $CC -c $CFLAGS $< TARGET: $OBJS $CC -o $TARGET $OBJS $LDFLAGS clean: $RM $TARGET *.o *~ 5.3 1 1. sample-clr.c 2. 3. sample-hello.c 4. glortho() glvertex3f()
15 6 6.1 1. 2. 3. 4. OpenGL 1. ( ) 2. ( ) 3. ( ) ( ) 4. ( ) 8 OpenGL OpenGL 4x4 M v v = Mv v 4 (x, y, z, w) w 1
16 6 w 9 OpenGL 6.2 (sample-cube.c) void init(void) glclearcolor (0.0, 0.0, 0.0, 0.0); glshademodel (GL_FLAT); void display(void) glclear (GL_COLOR_BUFFER_BIT); glcolor3f (1.0, 1.0, 1.0); glloadidentity (); /* clear the matrix */ /* viewing transformation */ glulookat (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glscalef (1.0, 2.0, 1.0); /* modeling transformation */ glutwirecube (1.0); glflush ();
6.2 17 void reshape (int w, int h) glviewport (0, 0, (GLsizei) w, (GLsizei) h); glmatrixmode (GL_PROJECTION); glloadidentity (); glfrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); glmatrixmode (GL_MODELVIEW); /* ARGSUSED1 */ void keyboard(unsigned char key, int x, int y) switch (key) case 27: exit(0); break; int main(int argc, char** argv) glutinit(&argc, argv); glutinitdisplaymode (GLUT_SINGLE GLUT_RGB); glutinitwindowsize (500, 500); glutinitwindowposition (100, 100); glutcreatewindow (argv[0]); init (); glutdisplayfunc(display); glutreshapefunc(reshape); glutkeyboardfunc(keyboard); glutmainloop(); return 0; glulookat() glfrustum() glviewport()
18 6 6.3 glloadidentity() (current matrix) glulookat() (0, 0, 5) (0, 0, 0) (0, 1, 0) glulookat() z (0, 1, 0) glulookat() z 5. glscalef() 3 y 2 ( ) ( ) glulookat() gltranslatef(0.0, 0.0, -5.0) OpenGL (modelview matrix) glutwirecube() display() glutdisplayfunc display() OpenGL 2 (perspective) glfrustum() (orthographic) CAD reshape() glmatrixmode(gl PROJECTION) glloadidentity() glfrustum()
6.4 OpenGL 19 glviewport() ( (0, 0)) ( ) reshape() ( ) reshape() 6.4 OpenGL glmatrixmode() void glmatrixmode(glenum mode); mode GL MODELVIEW GL PROJECTION GL TEXTURE. glloadidentity() void glloadidentity(void); 4x4 3 OpenGL gltranslate*() glrotate*() glscale*() ( ) 3. void gltranslatefd(type x, TYPE y, TYPE z);
20 6 x, y, z ( ) void glrotatefd(type angle, TYPE x, TYPE y, TYPE z); (x, y, z) ( ) angle glrotatef(45.0, 0.0, 0.0, 1.0) z 45 void glscalefd(type x, TYPE y, TYPE z); x, y, z x, y, z x, y, z (gltranslate*() glrotate*()) OpenGL Utility Library(GLU) glulookat(). void glulookat(gldouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy, GLdouble upz); eyex, eyey, eyez centerx, centery, centerz upx, upy, upz z y glfrustum() glperspective() glortho() glortho2d().
6.4 OpenGL 21 glmatrixmode(gl_projection); glloadidentity(); void glfrustum(gldouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far); (left, bottom, -near) (right, top, -near) near far ( ). 10 glfrustum() void glperspective(gldouble fovy, GLdouble aspect, GLdouble near, GLdouble far); fovy x-z [0.0, 180.0] aspect near far z ( ) 11 glperspective()
22 6 void glortho(gldouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far); (left, bottom, -near) (right, top, -near) (left, bottom, -far) (right, top, -far) near far 12 glortho() void glortho2d(gldouble left, GLdouble right, GLdouble bottom, GLdouble top); 2 (left, bottom) (right, top) z -1.0 1.0 glortho() 2 z 0.0 glviewport() 2 void glviewport(glint x, GLint y, GLsizei width, GLsizei height);
6.4 OpenGL 23 (x, y) width height (0, 0, winwidth, winheight) winwidth, winheight
24 7 7 7.1 OpenGL (geometric primitive) (Vertex) 3 2 (x, y) z 0 Vertex OpenGL (convex). GLU 7.2 OpenGL OpenGL glvertex*() void glvertex234sifd[v](type coords) : glvertex2s(2, 3); glvertex3f(2.3, 1.1, -2.2); GLdouble dvect[3]=5.0, 9.0, 1992.0; glvertex3dv(dvect);
7.2 OpenGL 25 glvertex*() glbegin() glend() glbegin() : glbegin(gl_polygon); glvertex2f(0.0, 0.0); glvertex2f(0.0, 3.0); glvertex2f(4.0, 3.0); glvertex2f(6.0, 1.5); glvertex2f(4.0, 0.0); glend(); glbegin(gl_polygon); glend(); glbegin(gl_triangle_fan); glend(); glbegin(gl_triangle_strip); glend(); glbegin(gl_lines); glend(); GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, and GL_POLYGON. glbegin() glend() glvertex*() :
26 7 13 OpenGL glbegin(gl_points); glcolor3f(0.0, 1.0, 0.0); glvertex(...); glcolor3f(1.0, 1.0, 0.0); glvertex(...); glvertex(...); glend();. : #define PI 3.1415926535898 GLint circle_points=100; glbegin(gl_line_loop); for(i=0; i<circle_points; i++) angle = 2*PI*i/circle_points; glvirtex2f(cos(angle), sin(angle)); glend();
7.3 27 7.3 glpointsize() void glpointsize(glfloat size); 1.0 void gllinewidth(glfloat width); void gllinestipple(glint factor, GLushort pattern); (width) pattern 16 0 1 1 0 factor pattern gllinestipple(1, 0x3F07); glenable(gl_line_stapple); 0x3f07 (2 0011111100000111 ) 3 ON 5 OFF 6 ON 2 OFF factor 2 6 ON 10 OFF 12 ON 4 OFF gllinestipple() glenable() glpolygonmode() void glpolygonmode(glenum face, GLenum mode); face GL FRONT AND BACK, GL FRONT, GL BACK mode GL POINT( ), GL LINE( ), GL FILL( )
28 7 void glfrontface(glenum mode); void glcullface(glenum mode); ( ) glfrontface() glcullface(gl BACK) glenable(gl CULL FACE). 7.4 GLUT GLUT 9 2 OpenGL void glutwiresphere(gldouble radius, GLint slices, GLint stacks); void glutsolidsphere(gldouble radius, GLint slices, GLint stacks); glutsolidsphere glutwiresphere radius radius slices Z ( ) stacks Z ( ) void glutwirecube(gldouble size); void glutsolidcube(gldouble size); glutwirecube glutsolidcube size size
7.4 GLUT 29 void glutwiretorus(gldouble innerradius, GLdouble outerradius, GLint nsides, GLint rings); void glutsolidtorus(gldouble innerradius, GLdouble outerradius, GLint nsides, GLint rings); glutsolidtorus glutwiretorus Z innerradius outerradius nsides rings void glutwireicosahedron(void); void glutsolidicosahedron(void); void glutwireoctahedron(void); void glutsolidoctahedron(void); void glutwiretetrahedron(void); void glutsolidtetrahedron(void); void glutwiredodecahedron(void); void glutsoliddodecahedron(void); glutsolidisosahedron glutwireisosahedron 1.0 20 glutsolidoctahedron glutwireoctahedron 1.0 8 glutsolidtetrahedron glutwiretetrahedron 3 4 glutsoliddodecahedron glutwiredodecahedron 3 12 void glutwirecone(gldouble radius, GLdouble height, GLint slices, GLint stacks); void glutsolidcone(gldouble radius, GLdouble height, GLint slices, GLint stacks); glutsolidcone glutwirecone Z Z=0 Z=height Z slices
30 7 Z stacks base height slices Z stacks Z void glutwireteapot(gldouble size); void glutsolidteapot(gldouble size); glutsolidteapot glutwireteapot OpenGL size 2 1. sample-cube.c make 2. cube 3. 4. glutlookat() gltranslatef(0.0, 0.0, -5.0) 5. glfrustum() gluperspective(60.0. 1.0, 1.5, 20.0) fovy aspect 6. cube
31 8 8.1 (glloadmatrix(), glmultmatrix(), glloadidentity() ) glpushmatrix() glpopmatrix() ( ) glpushmatrix() glpopmatrix() 14 void glpushmatrix(void); 1 2 glmatrixmode() void glpopmatrix(void); 2
32 8 glmatrixmode() glpopmatrix() 32 4x4 glloadidentity() 2 2 3 glmatrixmode(gl_projection); glpushmatrix(); /* save the current projection */ glloadidentity(); glortho(...); /* set up for displaying help */ display_the_help(); glpopmatrix(); 8.2 4 5 ( ) 4 5
8.2 33 15 draw_wheel_and_bolts() long i; draw_wheel(); for(i=0;i<5;i++) glpushmatrix(); glrotatef(72.0*i,0.0,0.0,1.0); gltranslatef(3.0,0.0,0.0); draw_bolt(); glpopmatrix();
34 8 draw_body_and_wheel_and_bolts() draw_car_body(); glpushmatrix(); gltranslatef(40,0,30); /*move to first wheel position*/ draw_wheel_and_bolts(); glpopmatrix(); glpushmatrix(); gltranslatef(40,0,-30); /*move to 2nd wheel position*/ draw_wheel_and_bolts(); glpopmatrix();... /*draw last two wheel similarly*/ 72 3 40 30 8.3 glrotate*() gltranslate*() glutwiresphere() 2 glpushmatrix(); glutwiresphere(1.0, 20, 16); /* draw sun */ glrotatef ((GLfloat) year, 0.0, 1.0, 0.0); gltranslatef (2.0, 0.0, 0.0); glrotatef ((GLfloat) day, 0.0, 1.0, 0.0); glutwiresphere(0.2, 10, 8); /* draw smaller planet */ glpopmatrix(); glrotate*() gltranslate*()
8.3 35 glrotate*() ( ) 2 glrotate*() ( ) 16 #include <GL/glut.h> #include <stdlib.h> static int year = 0, day = 0; void init(void) glclearcolor (0.0, 0.0, 0.0, 0.0); glshademodel (GL_FLAT); void display(void) glclear (GL_COLOR_BUFFER_BIT); glcolor3f (1.0, 1.0, 1.0); glpushmatrix(); glutwiresphere(1.0, 20, 16); /* draw sun */ glrotatef ((GLfloat) year, 0.0, 1.0, 0.0); gltranslatef (2.0, 0.0, 0.0); glrotatef ((GLfloat) day, 0.0, 1.0, 0.0); glutwiresphere(0.2, 10, 8); /* draw smaller planet */ glpopmatrix(); glutswapbuffers();
36 8 void reshape (int w, int h) glviewport (0, 0, (GLsizei) w, (GLsizei) h); glmatrixmode (GL_PROJECTION); glloadidentity (); gluperspective(60.0, (GLfloat) w/(glfloat) h, 1.0, 20.0); glmatrixmode(gl_modelview); glloadidentity(); glulookat (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); /* ARGSUSED1 */ void keyboard (unsigned char key, int x, int y) switch (key) case d : day = (day + 10) % 360; glutpostredisplay(); break; case D : day = (day - 10) % 360; glutpostredisplay(); break; case y : year = (year + 5) % 360; glutpostredisplay(); break; case Y : year = (year - 5) % 360; glutpostredisplay(); break; case 27: exit(0); break; default: break;
8.4 37 int main(int argc, char** argv) glutinit(&argc, argv); glutinitdisplaymode (GLUT_DOUBLE GLUT_RGB); glutinitwindowsize (500, 500); glutinitwindowposition (100, 100); glutcreatewindow (argv[0]); init (); glutdisplayfunc(display); glutreshapefunc(reshape); glutkeyboardfunc(keyboard); glutmainloop(); return 0; 8.4 2 17 gltranslate*() glrotate*() glpushmatrix() glpopmatrix() glscale*()
38 8 gltranslatef (-1.0, 0.0, 0.0); glrotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0); gltranslatef (1.0, 0.0, 0.0); glpushmatrix(); glscalef (2.0, 0.4, 1.0); glutwirecube (1.0); glpopmatrix(); 2 x x 2 gltranslatef (1.0, 0.0, 0.0); glrotatef ((GLfloat) elbow, 0.0, 0.0, 1.0); gltranslatef (1.0, 0.0, 0.0); glpushmatrix(); glscalef (2.0, 0.4, 1.0); glutwirecube (1.0); glpopmatrix(); ( ) #include <GL/glut.h> #include <stdlib.h> static int shoulder = 0, elbow = 0; void init(void) glclearcolor (0.0, 0.0, 0.0, 0.0); glshademodel (GL_FLAT);
8.4 39 void display(void) glclear (GL_COLOR_BUFFER_BIT); glpushmatrix(); gltranslatef (-1.0, 0.0, 0.0); glrotatef ((GLfloat) shoulder, 0.0, 0.0, 1.0); gltranslatef (1.0, 0.0, 0.0); glpushmatrix(); glscalef (2.0, 0.4, 1.0); glutwirecube (1.0); glpopmatrix(); gltranslatef (1.0, 0.0, 0.0); glrotatef ((GLfloat) elbow, 0.0, 0.0, 1.0); gltranslatef (1.0, 0.0, 0.0); glpushmatrix(); glscalef (2.0, 0.4, 1.0); glutwirecube (1.0); glpopmatrix(); glpopmatrix(); glutswapbuffers(); void reshape (int w, int h) glviewport (0, 0, (GLsizei) w, (GLsizei) h); glmatrixmode (GL_PROJECTION); glloadidentity (); gluperspective(65.0, (GLfloat) w/(glfloat) h, 1.0, 20.0); glmatrixmode(gl_modelview); glloadidentity(); gltranslatef (0.0, 0.0, -5.0);
40 8 /* ARGSUSED1 */ void keyboard (unsigned char key, int x, int y) switch (key) case s : shoulder = (shoulder + 5) % 360; glutpostredisplay(); break; case S : shoulder = (shoulder - 5) % 360; glutpostredisplay(); break; case e : elbow = (elbow + 5) % 360; glutpostredisplay(); break; case E : elbow = (elbow - 5) % 360; glutpostredisplay(); break; case 27: exit(0); break; default: break; int main(int argc, char** argv) glutinit(&argc, argv); glutinitdisplaymode (GLUT_DOUBLE GLUT_RGB); glutinitwindowsize (500, 500); glutinitwindowposition (100, 100); glutcreatewindow (argv[0]); init (); glutdisplayfunc(display); glutreshapefunc(reshape); glutkeyboardfunc(keyboard); glutmainloop(); return 0;
41 9 9.1 GLUT GLUT void glutdisplayfunc(void (*func)(void)); glutpostredisplay() void glutreshapefunc(void (*func)(void)); func 2 func glviewport() glutreshapefunc() NULL glviewport(0, 0, width, height) void glutkeyboadfunc(void (*func)(unsigned int key, int x, int y)); ASCII func key ASCII x, y ( ) void glutmousefunc(void (*func)(int button, int state, int x, int y)); func button GLUT LEFT BUTTON, GLUT MIDDLE BUTTON, GLUT RIGHT BUTTON state GLUT UP GLUT DOWN x y ( ) void glutmotionfunc(void (*func)(int x, int y));
42 9 func x y ( ) void glutpostredisplay(void); glutdisplayfunc() glutidlefunc() void glutidlefunc(void (*func)(void)); func NULL func GLUT glutmainloop() void glutmainloop(void); GLUT 9.2 OpenGL ( ) 1 OpenGL 2 (double-buffer) 2 GLUT void glutswapbuffers(void); (viewpoint)
9.2 43 #include <GL/glut.h> static GLfloat spin = 0.0; void display(void) glclear(gl_color_buffer_bit); glpushmatrix(); glrotatef(spin, 0.0, 0.0, 1.0); glcolor3f(1.0, 1.0, 1.0); glrectf(-25.0, -25.0, 25.0, 25.0); glpopmatrix(); glutswapbuffers(); void spindisplay(void) spin = spin + 2.0; if (spin > 360.0) spin = spin - 360.0; glutpostredisplay(); void init(void) glclearcolor (0.0, 0.0, 0.0, 0.0); glshademodel (GL_FLAT); void reshape(int w, int h) glviewport (0, 0, (GLsizei) w, (GLsizei) h); glmatrixmode(gl_projection); glloadidentity(); glortho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0); glmatrixmode(gl_modelview); glloadidentity();
44 9 /* ARGSUSED2 */ void mouse(int button, int state, int x, int y) switch (button) case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) glutidlefunc(spindisplay); break; case GLUT_MIDDLE_BUTTON: if (state == GLUT_DOWN) glutidlefunc(null); break; default: break; /* * Request double buffer display mode. * Register mouse input callback functions */ int main(int argc, char** argv) glutinit(&argc, argv); glutinitdisplaymode (GLUT_DOUBLE GLUT_RGB); glutinitwindowsize (250, 250); glutinitwindowposition (100, 100); glutcreatewindow (argv[0]); init (); glutdisplayfunc(display); glutreshapefunc(reshape); glutmousefunc(mouse); glutmainloop(); return 0; glutswapbuffers() GLUT
9.2 45 3 1. sample-teapot.c 2. planet.tar.gz planet ex.c glpushmatrix() glpopmatrix() 3. planet ex.c 4. planet ex.c 5. sample-robot.c ( glpushmatrix() glpopmatrix() ( ) 18