1 24 (1) OpenGL TA 2012 10 11 1 C OpenGL (3DCG) OS Linux OS 3DCG OpenGL GUI GLUT OpenGL GLUT GLUI 3DCG 3DCG 1.1 1 3DCG 3DCG GUI 2 3DCG 10/10( ) 11/11( ) 3DCG OpenGL OpenGL+GUI(GLUI) 3DCG 3DCG 1.2 TA
2 2 OpenGL ( ) 1.3 http://www.cyber.t.u-tokyo.ac.jp/~tani/class/mech_enshu/ 2 OpenGL ( ) 2.1 2.1.1 glclearcolor(glclampf red, GLclampf green, GLclampf blue, GLclampfalpha); GLglampf 0.0 1.0 alpha glutclearcolor(0.0, 0.0, 0.0, 0.0); 2.1.2 glclearcolor glclear(gl_color_buffer_bit); 2.1.3 glflush(); 1 2.2 glut GLUT(OpenGL Utility Toolkit) GLUT Windows X window system GLUT 3.7 GLU 3 GLUT glutinit void glutinit(int *argcp, char **argv);
2.2 glut 3 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 1 OpenGL glutinitwindow* OpenGL glutinitwindow* (OpenGL ) glutinitdisplaymode void glutinitdisplaymode(unsigned int mode); mode RGBA (GLUT RGBA or GLUT INDEX)
4 2 OpenGL ( ) (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 2.3 OpenGL glcolor3f(glclampf red, GLclampf green, GLclampf blue); 0.0 1.0 3 glcolor3f(1.0f, 1.0f, 0.0f); glbegin(glenum mode);
2.3 OpenGL 5 mode (GL POLYGON) (GL TRIANGLE FAN) ( ) (GL TRIANGLE STRIP) (GL LINES) (GL POINTS) glend(); glvertex3f(glfloat x, GLfloat y, GLfloat z); glvertex* 3 3 (x, y, z) f 32bit (GLfloat)
6 3 ( ) 3 ( ) 3.1 1. 2. 3. 4. OpenGL 1. ( ) 2. ( ) 3. ( ) ( ) 4. ( ) 2 OpenGL OpenGL 4x4 M v v 4 (x, y, z, w) w 1 v = Mv
3.2 OpenGL 7 w 3 OpenGL 3.2 OpenGL glmatrixmode() void glmatrixmode(glenum mode); mode GL MODELVIEW GL PROJECTION GL TEXTURE. glloadidentity() void glloadidentity(void); 4x4 3 OpenGL gltranslate*() glrotate*() glscale*() ( ) 3.
8 3 ( ) void gltranslate{fd(type x, TYPE y, TYPE z); x, y, z ( ) void glrotate{fd(type angle, TYPE x, TYPE y, TYPE z); (x, y, z) ( ) angle glrotatef(45.0, 0.0, 0.0, 1.0) z 45 void glscale{fd(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() gluperspective() glortho() gluortho2d()
3.2 OpenGL 9. glmatrixmode(gl_projection); glloadidentity(); void gluperspective(gldouble fovy, GLdouble aspect, GLdouble near, GLdouble far); fovy x-z [0.0, 180.0] aspect near far z ( ) 4 glperspective() glviewport() 2 void glviewport(glint x, GLint y, GLsizei width, GLsizei height); (x, y) width height (0, 0, winwidth, winheight) winwidth, winheight
10 4 ( ) 4 ( ) 4.1 OpenGL (geometric primitive) (Vertex) 3 2 (x, y) z 0 Vertex OpenGL (convex). GLU 4.2 OpenGL OpenGL glvertex*() void glvertex{234{sifd[v](type coords) : glvertex2s(2, 3); glvertex3f(2.3, 1.1, -2.2); GLdouble dvect[3]={5.0, 9.0, 1992.0; glvertex3dv(dvect);
4.2 OpenGL 11 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(); 5 OpenGL glbegin() glend() glvertex*()
12 4 ( ) 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(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();
4.3 13 4.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( )
14 4 ( ) void glfrontface(glenum mode); void glcullface(glenum mode); ( ) glfrontface() glcullface(gl BACK) glenable(gl CULL FACE). 4.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
4.4 GLUT 15 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
16 4 ( ) Z stacks base height slices Z stacks Z void glutwireteapot(gldouble size); void glutsolidteapot(gldouble size); glutsolidteapot glutwireteapot OpenGL size
17 5 5.1 (glloadmatrix(), glmultmatrix(), glloadidentity() ) glpushmatrix() glpopmatrix() ( ) glpushmatrix() glpopmatrix() 6 void glpushmatrix(void); 1 2 glmatrixmode() void glpopmatrix(void); 2
18 5 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(); 5.2 4 5 ( ) 4 5
5.2 19 7 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();
20 5 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 5.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*()
5.3 21 glrotate*() ( ) 2 glrotate*() ( ) 8 #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();
22 5 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;
5.4 23 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; 5.4 2 9 gltranslate*() glrotate*() glpushmatrix() glpopmatrix() glscale*()
24 5 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);
5.4 25 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);
26 5 /* 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;
27 6 6.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));
28 6 func x y ( ) void glutpostredisplay(void); glutdisplayfunc() glutidlefunc() void glutidlefunc(void (*func)(void)); func NULL func GLUT glutmainloop() void glutmainloop(void); GLUT 6.2 OpenGL ( ) 1 OpenGL 2 (double-buffer) 2 GLUT void glutswapbuffers(void); (viewpoint)
6.2 29 #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();
30 6 /* 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
6.2 31 1 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() 10
32 7 GLUI 7 GLUI 7.1 GLUI GLUI OpenGL OpenGL glut OpenGL glut C++ Linux, Windows (Visual C++, gcc), Macintosh glut glut GUI glut GUI CG glmultmatrix() glut glui glui 11 GLUI
7.1 GLUI 33 7.1.1 GLUI GLUI (Buttons) (Checkboxes) ON OFF (Radio Buttons) (Editable Text Boxes) (Spinners) (Listboxes) (Rotation Controls) (Translation Controls) 12 GLUI (Static Text) (Separators) Static Text (Columns)
34 7 GLUI (Panels) (Rollouts) 12 13 14 glui 13 14 GLUI *glui = GLUI_Master.create_glui( "GLUI" ); GLUI *glui = GLUI_Master.create_glui_subwindow(main_window, glui->set_main_gfx_window( main_window ); GLUI_SUBWINDOW_BOTTOM);
7.2 glut GLUI 35 GLUI SUBWINDOW BOTTOM GLUI SUBWINDOW TOP GLUI SUBWINDOW LEFT 7.2 glut GLUI teapot UI #include <stdio.h> #include <math.h> #include <GL/glut.h> void display(void) { glclear(gl_color_buffer_bit); glutwireteapot(1.5); glutswapbuffers(); glutpostredisplay(); int main(int argc, char *argv[]) { glutinit(&argc, argv); glutinitdisplaymode(glut_rgba GLUT_DOUBLE); glutcreatewindow(argv[0]); glutdisplayfunc(display); glclearcolor(0.0, 0.0, 1.0, 0.0); glutmainloop(); return 0; GLUI #include <stdio.h> #include <math.h> #include <GL/glut.h> #include <glui.h>
36 7 GLUI /*--- ---*/ float rotate[16] = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 ; void display(void) { glclear(gl_color_buffer_bit); /*--- ---*/ glpushmatrix(); glmultmatrixf( rotate ); glutwireteapot(1.5); glpopmatrix(); /*--- ---*/ glutswapbuffers(); glutpostredisplay(); /*=== ===*/ void gluicallback(int num) { exit(0); /*=== ===*/ int main(int argc, char *argv[]) { glutinit(&argc, argv); glutinitdisplaymode(glut_rgba GLUT_DOUBLE); glutcreatewindow(argv[0]); glutdisplayfunc(display); glclearcolor(0.0, 0.0, 1.0, 0.0);
7.3 GLUI 37 /*---GLUI initialize---*/ GLUI *glui = GLUI_Master.create_glui("control"); /*---GLUI ---*/ GLUI_Rotation *view_rot= new GLUI_Rotation(glui, "Rotation",rotate); /*===GLUI ===*/ new GLUI_Button(glui, "Exit", 0, gluicallback); glutmainloop(); return 0; 15 GLUI teapot UI 7.3 GLUI GLUI example1, example2, example3, example4, example5 bin example /usr/local/src/glui-2.35/src/example/ /usr/local/src/glui-2.35/src/bin/
38 7 GLUI 2 1. sample glui.cpp sample glui teapot.cpp Makefile Teapot glutwireteapot glutsolidteapot 2. exmple5 2 Teapot Teapot 3. 1 UI ( ) 16 example5
7.3 GLUI 39 3( ) GLUI solar system ex.tar.gz 1. plant view main.cpp 2. orbit view main.cpp a Day rate b 17 3.1 18 3.1 19 3.1 20 3.2
40 7 GLUI 21 3.2 ( ) 22 3.2 ( ) 23 3.2 ( )