1 22 (2) OpenGL+GUI(GLUI) 3DCG TA 2010 10 18 1 OpenGL OpenGL GUI GLUI 1.1 http://www.cyber.t.u-tokyo.ac.jp/~kuni/enshu2010/ 1.2 TA 1.3 2010/12/6( ) USB
2 2 2 OpenGL (R,G,B,A) 2.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
2.1 OpenGL 3 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 glrasterpos{234{sifd(type x, TYPE y, TYPE z, TYPE w); void glrasterpos{234{sifdv(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 fromat 2 3 glpixelstore*()
4 2 void glpixelstore{if(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 æ œ 1 void glpixelzoom(glfloat zoom_x GLfloat zoom_y); (gldrawpixels(),glcopypixels()) x y zoom x zoom y 1.0 2.0 4
2.2 5 2.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);
6 2 void display(void) { glclear(gl_color_buffer_bit); glrasterpos2i(0, 0); gldrawpixels(checkimagewidth, checkimageheight, GL_RGB, GL_UNSIGNED_BYTE, checkimage); glflush(); 2.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)
7 3 OpenGL 6 A N J K H A 2 H E E J E L A @ A 2 5? A A I K H B =? A B > K E @ E C I K H B =? A B M = J H A A 3.1 3 0.0...1.0 gltexcoord() void gltexcoord{1,2,3,4{s,i,d,f(type coords); void gltexcoord{1,2,3,4{s,i,d,fv(type *coords); 3.2 (sample-checker.c) glteximage2d()
8 3 3 #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);
3.2 9 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);
10 3 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
3.2 11 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)
12 3 3.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()
3.3 13 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
14 3 1 1. sampel-checker.c sample-texbind.c 2. JPEG viewjpeg.c, readjpeg.c USB USB USB 3. sample-image.c JPEG 4. glut- SolidTeapot 5. USB
15 4 GLUI 4.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 4 GLUI
16 4 GLUI 4.1.1 GLUI GLUI (Buttons) (Checkboxes) ON OFF (Radio Buttons) (Editable Text Boxes) (Spinners) (Listboxes) (Rotation Controls) (Translation Controls) 5 GLUI (Static Text) (Separators) Static Text (Columns)
4.1 GLUI 17 (Panels) (Rollouts) 5 6 7 glui 6 7 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);
18 4 GLUI GLUI SUBWINDOW BOTTOM GLUI SUBWINDOW TOP GLUI SUBWINDOW LEFT 4.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>
4.2 glut GLUI 19 /*--- ---*/ 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);
20 4 GLUI /*---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; 8 GLUI teapot UI 4.3 GLUI GLUI glui-2.36.tgz example example1.cpp, example2.cpp, example3.cpp, example4.cpp, example5.cpp example6.cpp bin tar xvzf glui-2.36.tgz cd glui-2.36 make
4.3 GLUI 21 2 1. sample glui.cpp sample glui teapot.cpp Makefile Teapot glutwireteapot glutsolidteapot 2. exmple5 2 Teapot Teapot 3. UI ( )
22 4 GLUI 3 GLUI solar system ex.tar.gz C++ ( 1. plant view main.cpp 2. orbit view main.cpp a Day rate b 9 3.1 10 3.1 11 3.1 12 3.2
4.3 GLUI 23 13 3.2 ( ) 14 3.2 ( ) 15 3.2 ( )