コンピュータグラフィックス S 演習資料 OpenGL & GLUT の基本関数の説明 1. OpenGL & GLUT 2. GLUT 2.1. GLUT void glutinit( int argc, char ** argv ); glut void glutinitdysplaymode( unsigned int mode ); mode void glutinitwindowsize( int width, int height ); GLUT void glutwindowposition( int x, int y ); GLUT int glutcreatewindow ( char * title ); GLUT title ID ID 1
void glutmainloop(); GLUT 2.2. void glutdisyplayfunc( void (*func)( void ) ); void glutreshapefunc( void (*func)( int width, int height ) ); void (*func)( int width, int height ); void glutidlefunc( void (*func)( void ) ); void glutmousefunc( void (*func)( int button, int state, int x, int y ) ); void (*func)( int button, int state, int x, int y ); 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 ) ); void glutpassivemotionfunc( void (*func)( int x, int y ) ); glutmotionfunc() glutpassivemotionfunc() 2
void glutkeyboardfunc( void (*func)( unsigned char key, int x, int y ) ); void (*func)( unsigned char key, int x, int y ); key GLUT_KEY_ LEFT, GLUT_KEY_RIGHT, GLUT_KEY_UP, GLUT_KEY_DOWN 2.3. void glutpostredisplay(); GLUT GLUT 2.4. GLUT void glutwirecube( GLdouble size ); void glutsolidcube( GLdouble size ); size glutwirecube() glutsolidcube() void glutwiresphere( GLdouble radius, GLdouble slices, GLdouble stacks ); void glutsolidsphere( GLdouble radius, GLdouble slices, GLdouble stacks ); size slices, stacks slices, stacks void glutwirecone( GLdouble radius, GLdouble height, GLdouble slices, GLdouble stacks ); void glutsolidcone( GLdouble radius, GLdouble height, GLdouble slices, GLdouble stacks ); radius, height slices, stacks glutsolidicosahedron(), glutsolidcone(), glutsolidcone(), glutsolidcone(), 3
glbegin() glend() 2.5. GLUT void glutbitmapcharacter( void * font, int character ); font GLUT_BITMAP_8_BY_13, GLUT_BIT MAP_9_BY_15, GLUT_BITMAP_TIMES_ROMAN_10, GLUT_BITMAP_TI MES_ROMAN_24, GLUT_BITMAP_HELVETICA_10, GLUT_BITMAP_HELVETICA_12, GLUT_BITM AP_HELVETICA_18 font void glrasterpos2i( GLint x, GLint y ); void glrasterpos2f( GLfloat x, GLfloat y ); void glrasterpos3i( GLint x, GLint y, GLint z ); void glrasterpos3i(glfloat x, GLfloat y, GLfloat z ); glvertex () int glutbitmapwidth ( void * font, int character ); glutbitmapcharacter() glutbitmapcharacter() 4
3. OpenGL OpenGL OpenGL gl glu OpenGL OpenGL Utility Library OpenGL glut GLUT OpenGL Utility Toolkit GLUT OpenGL GLUT PC GLUT OpenGL glvertex3f( x, y, z ), glvertex3d( x, y, z ) ( x, y, z ) float double double float 3.1. OpenGL OpenGL void glmatrixmode( GLenum mode ); mode GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE GL_MODELVIEW GL_PRO JECTION GL_TEXTURE 31 82 GL_MODELVIEW, GL_PROJECTION 3.1.1. void glloadidentity( void ); 5
void gltranslatef( GLfloat x, GLfloat y, GLfloat z ); void gltranslated( GLdouble x, GLdouble y, GLdouble z ); (x, y, z) void glrotatef( GLfloat angle, GLfloat x, GLfloat y, GLfloat z ); void glrotated(gldouble angle, GLdouble x, GLdouble y, GLdouble z ); (x, y, z) angle x x y y A = z z 1 1 A i A A 1,, An A A= A A A A 1 2 3 n void glulookat( GLdouble eye_x, GLdouble eye_y, GLdouble eye_z, GLdouble center_x, GLdouble center_y, GLdouble center_z, GLdouble up_x, GLdouble up_y, GLdouble up_z ); (eye_x, eye_y, eye_z) ( center_x, center_y, center_z ) ( up_x, up_y, up_z ) void glloadmatrixf( const GLfloat * m ); void glloadmatrixd( const GLdouble * m ); 16 m 16 m GLdouble m[4][4]; m[i][j] j 6
i i j void glmultmatrixf( const GLfloat * m ); void glmultmatrixd( const GLdouble * m ); 16 m glloadmatrixf(),glloadmatrixd() OpenGL glpushmatrix() glpopmatrix() void glpushmatrix ( void ); void glpopmatrix ( void ); glpushmatrix() glpopmatrix() glpopmatrix() glpushmatrix() glpopmatrix() glpushmatrix() 3.1.2. void glfrustum( GLdouble left, GLdouble right, GLdouble bottm, GLdouble top, GLdouble near, GLdouble far ); glfrustum() near 1 glfrustum() 1 glperspective() 7
glfrustum() gluperspective() void gluperspective( GLdouble fovy, GLdouble aspect, GLdouble near, GLdouble far ); glviewport() void glviewport( GLint x, GLint y, GLsizei width, GLsizei height ); reshape() glviewport() gluperspective() 45 glfrustum() gluperspective () glloadidentity() 1 glortho() void glortho( GLdouble left, GLdouble right, GLdouble bottm, GLdouble top, GLdouble near, GLdouble far ); 8
3.2. 3.2.1. void glclear( GLbitfield mask ); GL_COLOR_BUFF ER_BIT, GL_DEPTH_BUFFER_BIT, GL_ACCUM_BUFFER_BIT, GL_STENCIL_BUFFER_BIT GL_COLOR_BUFFER GL_DEPTH_BUFFER void glclearcolor( CLclampf red, GLclampf green, GLclampf blue, GLclamp alpha ); red, green, blue alpha 1.0 0 1 [0,1] 3.2.2. glbegin() glend() void glbegin( GLenum type ); type type GL_POINTS GL_LINES GL_TRIANGLES GL_POLYGON void glend(); GL_POINTS, GL_LINES, GL_TRIANGLES glbegin() glend() GL_TRIANGLES 3n n GL_POLYGON glbegin() glend() glbegin() glend() 9
void glcolor3f( GLfloat r, GLfloat g, GLfloat b ); void glcolor3d( GLdouble r, GLdouble g, GLdouble b ); ( r, g, b ) void glnormal3f( GLfloat nx, GLfloat ny, GLfloat zn ); void glnormal3d( GLdouble nx, GLdouble ny, GLdouble zn ); ( nx, ny, zn ) void glvertex3f( GLfloat x, GLfloat y, GLfloat z ); void glvertex3d( GLdouble x, GLdouble y, GLdouble z ); (x, y, z) 3f 3d float double glvertex2f(), glvertex4f() 3.3. OpenGL void glenable( GLenum capability ); capability GL_LIGHTING, GL_COLOR_MATERIAL, GL_DEPTH_TEST, GL_CULL_FACE 40 void gldisable( GLenum capability ); GL_LIGHTING, GL_COLOR_MA TERIAL, GL_DEPTH_TEST, GL_CULL_FACE 10
void glcullface( GLenum mode ); mode GL_FRONT, GL_BACK, GL_FRONT_AND_BACK 3.4. OpenGL void gllightf( GLenum light_no, GLenum pname, GLfloat param ); void gllighti( GLenum light_no, GLenum pname, GLint param ); void gllightfv( GLenum light_no, GLenum pname, GLfloat * param ); void gllightiv( GLenum light_no, GLenum pname, Glint * param ); light_no GL_LIGHT0, GL_LIGHT1,, GL_LIGHT7 pname GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, GL_ POSITION, GL_SPOT_DIRECTION, GL_SPOT_DIRECTION, GL_SPOT_EXPONENT, GL_SPOT_C UTOFF, GL_CONSTANT_ATTENUATION, GL_LINEAR_ATTENUATION, GL_QUADRATIC_ATTENU ATION param GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR GL_POSITION x,y,z,w w 1.0 x,y,z w 0.0 x,y,z GL_SPOT_DIRECTION, GL_SPOT_EXPONENT, GL_SPOT_CUTOFF GL_CONSTANT_ATTENUATION, GL_LINEAR_ATTENUATION, GL_QUADRATIC_ATTENUATION RGB M { ln } { sn } s pecular_factor Color = L M + max, 0 L M + max, 0 L M ambient ambient diffuse diffuse specular specular n l s l L ambient, L diffuse, L specular M ambient, M diffuse, M specular, M specular_factor 11
Mambient, Mdiffuse glcolor3f() M specular, M specular_factor (1.0,1.0,1.0), 1.0 glmaterial() glenable() GL_LIGHTING GL_LIGHT0, GL_LIGHT1, gldisable() OpenGL 3.5. OpenGL glbegin() glend() glvertex() glvertex() void glvertexpointer( Glint size, GLenum type, GLsizei stride, const GLvoid * pointer ); size type GL_FLOAT, GL_DOUBLE, GL_INT stride pointer glvertexpointer() void glnormalpointer( GLenum type, GLsizei stride, const GLvoid * pointer ); void glcolor( Glint size, GLenum type, GLsizei stride, const GLvoid * pointer ); void gltexcoordpointer( Glint size, GLenum type, GLsizei stride, const GLvoid * pointer ); glvertexpointer() glnormalpointer() size void glenableclientstate( GLenum array_type ); void gldisableclientstate( GLenum array_type ); 12
or array_type GL_VERTEX_ARRAY, GL_NORMAL_ ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY void gldrawarrays( GLenum mode, GLint first, GLsizei count ); mode GL_POINTS GL_LINES GL_TRIANGLES GL_POLYGON glbegin() first, count void gldrawelements( GLenum mode, GLint count, GLenum type, void * indices ); mode gldrawarrays(), glbegin() count type GL_UNSIGNED_INT, GL_UNSIGNED_SHORT, GL_UNSIGNED_ BYTE indices glarrayelement( Glint no ); 3.6. glteximage2d() void glteximage2d( GLenum target, GLint level, GLenum int_format, GLsizei width, GLsizei height, GLsizei border, GLenum format, GLenum type, const GLvoid * pixels ); target GL_TEXTURE_2D level 0 13
int_format format width height border 0 format pixels RGB GL_RGB RGB GL_RGBA type pixels RGB 3 =24 GL_UNSIGNED_BYTE pixels format type OpenGL BMP, GIF, JPEG 24 BMP gltexparameteri() void gltexparameteri( GLenum target, GLenum pname, GLint param ); target pname param target param target GL_TEXTURE_MAG_FILTER GL_TEXTURE_MIN_FILTER pname GL_NEAREST GL_LINEAR target GL_TEXTURE_WRAP_S GL_TEXTURE_WRAP_T pname GL_REPEATE 1 GL_CLAMP glenable( GL_TEXTURE_2D ) gldisable() void gltexenv( GLenum target, GLenum pname ); void gltexenvf( GLenum target, GLenum pname, GLfloat param ); void gltexenvfv( GLenum target, GLenum pname, GLfloat * param ); target GL_TEXTURE_ENV pname GL_DECAL, GL_REPLACE, GL_MODULATE, GL_BLEND GL_REPLACE GL_DECAL GL_REPLACE 14
GL_MODULATE GL_BLEND GL_MODULATE GL_BLEND void gltexcoord2f( GLfloat u, GLfloat v ); void gltexcoord2d( GLdouble u, GLdouble v ); (u, v) 0.0< u <1.0, 0.0< v <1.0 (u, v) glteximage2d () glbindtexture() glbindtexture() glbindtexture() 15