3 OpenGL April 11, 2017 1 / 28
3.1 ( ) OpenGL OpenGL 2 / 28
3.2 OpenGL OpenGL OpenGL (Open Graphics Library) Silicon Graphics, Inc. 2 3 API (Application Program Interface) [4] UNIX OS Windows Macintosh CAD CG 3 / 28
3.3 OpenGL GLUT (OpenGL Utility Toolkit) OpenGL 4 / 28
GLUT GUI Toolkit 5 / 28
3.4 program3 1.c #include <GL/glut.h> static void display(void); static void display(void) { } int main(int argc, char *argv[]) { glutinit(&argc, argv); glutcreatewindow(argv[0]); glutdisplayfunc(display); glutmainloop(); return 0; } 6 / 28
glutinit() #include <GL/glut.h> void glutinit(int *argc, char **argv); GLUT main. glutcreatewindow() #include <GL/glut.h> int glutcreatewindow(char *name);. name 7 / 28
glutdisplayfunc() #include <GL/glut.h> void glutdisplayfunc(void (*func)(void)); func. glutmainloop() #include <GL/glut.h> void glutmainloop(void);. 8 / 28
3.5 program3 2.c int main(int argc, char *argv[]) {... /* Window */ glutinitdisplaymode(glut_rgba); glutinitwindowsize(250, 250); glutinitwindowposition(100, 200); glutcreatewindow(argv[0]); /* */ glclearcolor(0.0, 0.0, 0.0, 0.0); } /* */ glutdisplayfunc(display);... 9 / 28
static void display(void) { glclear(gl_color_buffer_bit); glcolor3f(1.0, 1.0, 1.0); glbegin(gl_polygon); glvertex2f(0.5, 0.5); glvertex2f(-0.5, 0.5); glvertex2f(-0.5, -0.5); glvertex2f(0.5, -0.5); glend(); glflush(); } 10 / 28
glutinitdisplaymode() void glutinitdisplaymode(unsigned int mode); mode GLUT RGBA RGBA (,,, ) GLUT INDEX glutinitwindowsize() void glutinitwindowsize(int w, int h); ( 1, 1) 2 11 / 28
glutinitwindowposition() void glutinitwindowposition(int x, int y); glclearcolor() void glclearcolor(glclampf R, GLclampf G, GLclampf B, GLclampf A); R G B A,,, α GLclampf (float ) 0 1 (0, 0, 0) (1, 1, 1) 12 / 28
glclear() void glclear(glbitfield mask); mask ( ) GL COLOR BUFFER BIT GL DEPTH BUFFER BIT, glflush() void glflush(void); OpenGL 13 / 28
glbegin()... glend void glbegin(glnum mode);... void glend(void); mode 14 / 28
mode GL POINTS: GL LINES: GL LINE STRIP: GL LINE LOOP: v3 v0 v1 v2 v3 v0 v1 GL LINES GL LINE STRIP GL LINE LOOP v2 v3 v0 v1 v2 15 / 28
GL TRIANGLES / GL QUADS: 3 4 GL TRIANGLE STRIP / GL QUAD STRIP: GL TRIANGLE FAN: GL POLYGON: v5 v0 v3 v4 v2 v1 GL TRIANGLES v4 v3 v2 v0 v1 v4 v3 GL TRIANGLE STRIP v4 v3 v0 v2 v0 v2 v1 GL TRIANGLE FAN v1 GL POLYGON 16 / 28
void glvertex2f(glfloat x, GLfloat y) GLfloat (float ) 3, 4 (x,y,z), (x,y,z,w) glvertex3f (), glvertex4f () double glvertex?d(), int glvertex?i() void glvertex??v() void glcolor3f(glfloat r, GLfloat g, GLfloat b) r, g, b,, 0 1 r, g, b, a void glcolor4f( ) double glcolor?d( ), int glcolor?i( ) 17 / 28
3.6 program3 3.c void display(void) {... for(i=0; i<star_num; i++){ /* */ x = 2.0*random_num()-1.0; y = 2.0*random_num()-1.0; brightness = random_num(); size = STAR_SIZE*random_num(); /* 0.0 STAR_SIZE */ } /* */ draw_point(x, y, brightness, size); } glflush(); 18 / 28
rand int rand(void); 0 RAND MAX srand() rand() RAND MAX int 1.0 0 19 / 28
(anti-aliasing) /* */ glblendfunc(gl_src_alpha, GL_ONE_MINUS_SRC_ALPHA); glenable(gl_blend); /* ON */ /* */ glenable(gl_point_smooth); glhint(gl_point_smooth_hint, GL_NICEST); 20 / 28
glenable(gl POINT SMOOTH) glenable(gl LINE SMOOTH) glenable(gl POLYGON SMOOTH) glhint(gl POINT SMOOTH HINT, GL FASTEST); glhint(gl LINE SMOOTH HINT, GL FASTEST); glhint(gl POLYGON SMOOTH HINT, GL FASTEST); glhint(gl POINT SMOOTH HINT, GL NICEST); glhint(gl LINE SMOOTH HINT, GL NICEST); glhint(gl POLYGON SMOOTH HINT, GL NICEST); 21 / 28
3.7 program3 4.c length /= 2.0; /* */ if(length > 0.01){ draw_gasket(x0, y0, length); draw_gasket(mid_point(x0, x1), mid_point(y0, y1), length); draw_gasket(mid_point(x2, x0), mid_point(y2, y0), length); } 22 / 28
3.8 program3 5.c glutmousefunc(mouse); void mouse(int button, int state, int x, int y) { switch (button) { case GLUT_LEFT_BUTTON: printf("the left"); break; case GLUT_MIDDLE_BUTTON: printf("the middle"); break;... 23 / 28
program3 6.c case GLUT_LEFT_BUTTON: point[pointnum][0] = x; point[pointnum][1] = y; if (state == GLUT_UP) { /* */ glcolor3d(0.0, 0.0, 0.0); glbegin(gl_lines); glvertex2iv(point[pointnum - 1]); /* */ glvertex2iv(point[pointnum]); /* */ glend(); glflush(); void resize(int w, int h) 5 24 / 28
3.9.1 ( ) program3 2.c program3 3.c 3.1 25 / 28
3.1: 26 / 28
3.10 OpenGL OpenGL 1 OpenGL 2 3 API 2 OpenGL GLUT OpenGL GLUT 3 2 27 / 28
[1] Mark J. Kilgard. The OpenGL Utility Toolkit (GLUT) programming interface API version 3( ). http://opengl.jp/glut/index.html. [2],. OpenGL 3 CG., 2003. [3]. GLUT OpenGL. http://www.wakayama-u.ac.jp/ tokoi/opengl/libglut.html. [4] OpenGL. http://www.opengl.org/. 28 / 28