1 27 (1) OpenGL TA 2015 9 29 1 C OpenGL (3DCG) OS Linux OS 3DCG OpenGL 3DCG 3DCG 1.1 1 3DCG 3DCG 2 3DCG GUI AR 9/29( ) 10/1( ) 3DCG OpenGL 3DCG 3DCG 1.2 TA 1.3
2 2 OpenGL ( ) 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); argcp main argc GLUT argv main argv GLUT X Window System GLUT -display, -geometry
2.2 glut 3 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) (GLUT DEPTH) (GLUT SINGLE or GLUT DOUBLE) RGBA
4 2 OpenGL ( ) 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); mode (GL POLYGON) (GL TRIANGLE FAN)
2.3 OpenGL 5 ( ) (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 gltranslatefd(type x, TYPE y, TYPE z); 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() 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 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);
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 7 OpenGL (R,G,B,A) 7.1 OpenGL (x y) width height pixels void glreadpixels(glint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels); format (RGBA ) type GL COLOR INDEX GL RGB GL RED GL GREEN GL BLUE GL ALPHA GL LUMINANCE GL LUMINANCE ALPHA GL STENCIL INDEX GL DEPTH COMPONENT 1 glreadpixels(),gldrawpixels() glrasterpos*() width height
7.1 OpenGL 33 GL UNSIGNED BYTE GL BYTE GL UNSIGNED SHORT GL SHORT GL UNSIGNED INT GL INT GL UNSIGNED BYTE 8 8 16 16 32 32 2 glreadpixels(),gldrawpixels() void gldrawpixels(glsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels); void glrasterpos234sifd(type x, TYPE y, TYPE z, TYPE w); void glrasterpos234sifdv(type *coords); format type glreadpixels() glreadpixels() gldrawpixels() void glcopypixels(glint x, GLint y, GLsizei width, GLsizei height, GLenum buffer); buffer GL COLOR, GL STENCIL, GL DEPTH 1 4 format 2 3 glpixelstore*()
34 7 void glpixelstoreif(glenum pname TYPE param); GL UNPACK* gldrawpixels(),glteximage1d(),glteximage2d(),gltexsubimage1d(), gltexsubimage2d() GL PACK* glreadpixels(),glgetteximage() GL UNPACK SWAP BYTES, GL PACK SWAP BYTES GLboolean FALSE TRUEFALSE GL UNPACK LSB BYTES, GL PACK LSB BYTES GLboolean FALSE TRUEFALSE GL UNPACK ROW LENGTH, GL PACK ROW LENGTH GLint 0 GL UNPACK SKIP ROWS, GL PACK SKIP ROWS GLint 0 GL UNPACK SKIP PIXELS, GL PACK SKIP PIXELS GLint 0 GL UNPACK ALIGNMENT, GL PACK ALIGNMENT GLint 4 1,2,3,4 *ROW_LENGTH *SKIP_PIXELS ª æ œ *SKIP_ROWS æ œ 11 void glpixelzoom(glfloat zoom_x GLfloat zoom_y); (gldrawpixels(),glcopypixels()) x y zoom x zoom y 1.0 2.0 4
7.2 35 7.2 gldrawpixels() sample-image.c #include <GL/glut.h> #include <stdlib.h> #define checkimagewidth 64 #define checkimageheight 64 GLubyte checkimage[checkimageheight][checkimagewidth][3]; static GLdouble zoomfactor = 1.0; static GLint height; void makecheckimage(void) int i, j, c; for (i = 0; i < checkimageheight; i++) for (j = 0; j < checkimagewidth; j++) c = ((((i&0x8)==0)^((j&0x8)==0)))*255; checkimage[i][j][0] = (GLubyte) c; checkimage[i][j][1] = (GLubyte) c; checkimage[i][j][2] = (GLubyte) c; void init(void) glclearcolor (0.0, 0.0, 0.0, 0.0); glshademodel(gl_flat); makecheckimage(); glpixelstorei(gl_unpack_alignment, 1);
36 7 void display(void) glclear(gl_color_buffer_bit); glrasterpos2i(0, 0); gldrawpixels(checkimagewidth, checkimageheight, GL_RGB, GL_UNSIGNED_BYTE, checkimage); glflush(); 7.3 Independent JPEG Group s software JPEG Linux (http://www.ijg.org/) JPEG JPEG OpenGL viewjpeg.c: OpenGL readjpeg.h, readjpeg.c: JPEG testimg.jpg: JPEG Makefile TARGET OBJS = viewjpeg = viewjpeg.o readjpeg.o CC = gcc CFLAGS = -g -Wall -O2 LIBS = -lglut -lglu -lgl -ljpeg TARGET:$(OBJS) $(CC) -o $(TARGET) $(OBJS) $(LIBS).c.o: $(CC) $(FLAGS) -c $< clean: rm -f *.o *~ $(TARGET)
37 8 OpenGL 6 A N J K H A 2 H E E J E L A @ A 12 5? A A I K H B =? A B > K E @ E C I K H B =? A B M = J H A A 8.1 3 0.0...1.0 gltexcoord() void gltexcoord1,2,3,4s,i,d,f(type coords); void gltexcoord1,2,3,4s,i,d,fv(type *coords); 8.2 (sample-checker.c) glteximage2d()
38 8 13 #include <GL/glut.h> #include <stdlib.h> /* Create checkerboard texture */ #define checkimagewidth 64 #define checkimageheight 64 GLubyte checkimage[checkimagewidth][checkimageheight][3]; void makecheckimage(void) int i, j, c; for (i = 0; i < checkimagewidth; i++) for (j = 0; j < checkimageheight; j++) c = ((((i&0x8)==0)^((j&0x8)==0)))*255; checkimage[i][j][0] = (GLubyte) c; checkimage[i][j][1] = (GLubyte) c; checkimage[i][j][2] = (GLubyte) c; void myinit(void) glclearcolor (0.0, 0.0, 0.0, 0.0); glenable(gl_depth_test); gldepthfunc(gl_less); makecheckimage(); glpixelstorei(gl_unpack_alignment, 1);
8.2 39 glteximage2d(gl_texture_2d, 0, 3, checkimagewidth, checkimageheight, 0, GL_RGB, GL_UNSIGNED_BYTE, &checkimage[0][0][0]); gltexparameterf(gl_texture_2d, GL_TEXTURE_WRAP_S, GL_CLAMP); gltexparameterf(gl_texture_2d, GL_TEXTURE_WRAP_T, GL_CLAMP); gltexparameterf(gl_texture_2d, GL_TEXTURE_MAG_FILTER, GL_LINEAR); gltexparameterf(gl_texture_2d, GL_TEXTURE_MIN_FILTER, GL_LINEAR); gltexenvf(gl_texture_env, GL_TEXTURE_ENV_MODE, GL_DECAL); glenable(gl_texture_2d); glshademodel(gl_flat); void display(void) glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT); glbegin(gl_quads); gltexcoord2f(0.0, 0.0); glvertex3f(-2.0, -1.0, 0.0); gltexcoord2f(0.0, 1.0); glvertex3f(-2.0, 1.0, 0.0); gltexcoord2f(1.0, 1.0); glvertex3f(0.0, 1.0, 0.0); gltexcoord2f(1.0, 0.0); glvertex3f(0.0, -1.0, 0.0); gltexcoord2f(0.0, 0.0); glvertex3f(1.0, -1.0, 0.0); gltexcoord2f(0.0, 1.0); glvertex3f(1.0, 1.0, 0.0); gltexcoord2f(1.0, 1.0); glvertex3f(2.41421, 1.0, -1.41421); gltexcoord2f(1.0, 0.0); glvertex3f(2.41421, -1.0, -1.41421); glend(); glutswapbuffers(); void myreshape(int w, int h) glviewport(0, 0, w, h); glmatrixmode(gl_projection); glloadidentity(); gluperspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 30.0); glmatrixmode(gl_modelview); glloadidentity(); gltranslatef(0.0, 0.0, -3.6);
40 8 int main(int argc, char** argv) glutinit(&argc, argv); glutinitdisplaymode(glut_double GLUT_RGB GLUT_DEPTH); glutcreatewindow("checker"); myinit(); glutreshapefunc (myreshape); glutdisplayfunc(display); glutmainloop(); return 0; /* ANSI C requires main to return int. */ makecheckimage GLubyte (unsigned char) WIDTH HEIGHT 3 1 RGB 3 2 64 64 makecheckimage myinit glpixelstorei() image[ ] gltexparameteri, gltexparameterf GL TEXTURE MAG FILTER GL TEXTURE MIN FILTER (MAG) (MIN) GL LINEAR GL NEAREST GL TEXTURE WRAP S(T) s, t GL REPAET (GL CLAMP) 3 0.0..3.0 gltexenvf GL DECAL GL MODULATE
8.2 41 glteximage2d void glteximage2d(glenum target, GLint level, GLint format, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels); target GL TEXTURE 2D GL PROXY TEXTURE 2D level 0 format (RGBA ) GL RGB GL RGBA width height OpenGL 2 1 2 4 8 16 32 64 128 256 64x64 1024x1024 border 0 format pixels RGB GL RGB PNG GL RGBA type pixels GL UNSIGNED BYTE width,height,pixel width,height 2 pixels ( image) display() glenable(gl TEXTURE 2D) gldisable(gl TEXTURE 2D)
42 8 8.3 OpenGL ( ) 1. 2. 3. 4. OpenGL void glgentextures(glsizei n, GLuint *texturenames); glgentextures() n texturenames void glbindtexture(glenum target, GLuint texturename); target GL TEXTURE 1D GL TEXTURE 2D texturename glteximage2d() 2 gltexsubimage2d()
8.3 43 void gltexsubimage2d( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels ); target GL TEXTURE 2D level 0 xoffset x yoffset y (0, 0) width height format type gldrawpixels() format type pixels gltexsubimage2d() glteximage2d() 2 2 n GLU gluscaleimage() int gluscaleimage( GLenum format, GLint widthin, GLint heightin, GLenum typein, const void * datain, GLint widthout, GLint heightout, GLenum typeout, void * dataout ); format widthin heightin typein datain widthout heightout typeout dataout
44 8 2 1. sampel-checker.c sample-texbind.c 2. JPEG viewjpeg.c, readjpeg.c 3. sample-image.c JPEG 4. 1 glut- SolidTeapot 5. 2 6. GIMP