OpenGL & GLUT OpenGL & GLUT 321190 2007년봄학기 3/9/2007 박경신 OpenGL http://www.opengl.org/ http://www.sgi.com/software/opengl Windows95 이후 OpenGL 이표준으로들어가있음. ftp://ftp.microsfot.com/softlib/mslfiles/opengl95.exe GLUT for Win32 http://www.xmission.com/~nate/glut.html Glut-3.7.6-bin.zip 내려받기 GLUT for LINUX http://www.opengl.org/resources/libraries/glut/ http://rpmfind.net/linux/rpm2html/search.php?query=glut glut-3.7-9.i386.rpm 내려받기 rpm rebuild glut-3.7-9.i386.rpm (as root) 2 Installing OpenGL & GLUT Libraries 를 Visual C++ 의 lib\ 에설치 Opengl32.lib Glu32.lib Glut32.lib Include files 을 Visual C++ 의 include\gl\ 에설치 Gl.h Glu.h Glut.h Dynamically-linked libraries 를 c:\windows\system32 (Windows XP) 에설치 Opengl32.dll Glu32.dll Glut32.dll C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK 3 Compiling OpenGL/GLUT Programs VC++ 실행 프로젝트새로만들기 메뉴에서 File->New->Projects Win32 Console Application 선택 프로젝트이름지정 Linker 에 library files 을지정 메뉴에서 Project->Settings->link->Object/library modules (Project->Properties->Linker->Input->Additional dependencies) opengl32.lib glu32.lib glut32.lib 를넣는다 프로젝트에파일새로만들기 메뉴에서 Project->Add to Project-> Files 파일이름지정 빌드 (build) (F7) 와실행 (execute) (F5) 4
Compiling OpenGL/GLUT Programs LINUX 사용자는 cc 컴파일하거나 makefile 을이용할것 cc 컴파일 %cc I/usr/X11R6/include program.c -o program - L/usr/X11R6/lib lglut lglu lgl lxm lxi lxext lx11 lm lpthread %./program Makefile 을아래와같이만들고, %make CFLAGS = -I/usr/X11R6/include LIBS = -L/usr/X11R6/lib lglut lglu lgl lxm lxi lxext lx11 lm -lpthread program: program.c $(CC) ($CFLAGS) program.c o program $(LIBS) 5 Windows System 윈도우시스템 Microsoft Windows X Window systems 윈도우시스템과 OpenGL 시스템은모두래스터그래픽스시스템임 OpenGL 프로그래밍을하기위해서는 사용윈도우시스템에서제공하는래스터시스템을기반으로윈도우프로그래밍수행 윈도우프로그래밍문맥에서추상적인래스터시스템인 OpenGL 시스템을윈도우시스템에연결 OpenGL에서제공하는함수들을사용하여 3차원그래픽스프로그래밍을수행 원하는 OpenGL 작업이실제로하드웨어를제어하고있는사용윈도우시스템이효율적으로이해할수있는형태로전환 6 Windows Programming Example Win32 Program 작성예 윈도우프로그램작성예 GLUT와 OpenGL 프로그램작성예 윈도우클래스 (window class) typedef struct UINT cbsize; UINT style; WNDPROC lpfnwndproc; int cbclsextra; int cbwndextra; HINSTANCE hinstance; HICON hicon; HCURSOR hcursor; HBRUSH hbrbackground; LPCTSTR lpszmenuname; LPCTSTR lpszclassname; HICON hiconsm; WNDCLASSEX; 메시지 (Message) typedef struct HWND hwnd; UINT message; WPARAM wparam; LPARAM lparam; DWORD time; POINT pt; MSG; 디바이스문맥 (DC, Device Context) GDI (Graphics Device Interface) 7 8
I 9 10 OpenGL/GLUT Program 작성예 OpenGL I 실리콘그래픽스사 (SGI) 가개발한 3 차원그래픽스라이브러리 API 2차원그래픽스는 (z축의값을 0으로처리한 ) 3차원의특수경우로봄 OpenGL 그래픽스함수는프로그래밍언어에독립적인기능으로지정되어있음 C/C++, Java, Fortran, Python 등다수언어와사용가능 OpenGL 은하드웨어에중립적임 No I/O library No specific model loading mechanism No Hardware specific functions (but available as extensions) 11 12
OpenGL and Windowing Toolkits OpenGL is hardware neutral Problems displaying OpenGL scenes in a specific windowing environment Different platforms have different ways to integrate OpenGL with their windowing environment X Window System (GLX) Apple (AGL) Windows (WGL) IBM OS/2 (PGL) 13 OpenGL & GLUT OpenGL consists of commands related to drawing 2D & 3D objects (e.g., draw a triangle, define material properties & texture, etc). Drawing takes place in some sort of window, controlled by an operating system. OpenGL avoids including any sort of functions to create or manipulate windows, or to do other user interface tasks (keyboard/mouse, etc). GLUT (GL Utility Toolkit) provides functions for windowing and interaction. It defines a simple interface that hides the OS-specific details of these tasks. GLUT functions can be used in the same way under Unix, MacOS, and Windows, making GLUT-based programs more portable. 14 GLUT (OpenGL Utility Toolkit) GLUT Program Structure Provides a library of functions for interacting with any screen-windowing system (portable across all PC and workstation OS platforms) Prefixed with glut Supports: Multiple windows for OpenGL rendering Callback driven event processing Sophisticated input devices An idle routine and timers A simple, cascading pop-up menu facility Utility routines to generate various solid and wire frame objects Supports for bitmap and stroke fonts Miscellaneous window management functions 15 16
단순히윈도우를여는프로그램예제 int main(int argc, char *argv[]) glutinit(&argc, argv); glutcreatewindow(argv[0]); glutdisplayfunc(display); glutmainloop(); return 0; void glutinit([int *argc, char **argv) GLUT 와 OpenGL 환경을초기화. 인수에는 main 의인수를그대로건네줌. int glutcreatewindow(char *name) 윈도우를여는함수. 인수 name 은그윈도우의이름이타이틀바에표시됨. void glutdisplayfunc(void (*func)(void)) 인수 func 는열린윈도우내에디스플레이하는 ( 즉, 그림을그리는 ) callback 함수포인터. 윈도우가열리거나다른윈도우에의해숨겨진윈도우가다시디스플레이될때이함수가실행 void glutmainloop(void) GLUT 루프. 이함수의호출로프로그램은이벤트를기다리는상태임. 17 18 윈도우를전부파란색으로칠하는프로그램예제 void init (void) glclearcolor(0.0, 0.0, 1.0, 1.0); int main(int argc, char *argv[]) glutinit(&argc, argv); glutinitdisplaymode(glut_rgba); glutcreatewindow(argv[0]); glutdisplayfunc(display); init(); glutmainloop(); return 0; 19 void glutinitdisplaymode(unsigned int mode) 디스플레이의표시모드를설정. Mode 에 GLUT_RGBA 를지정했을경우는색의지정을 RGB 로사용함을지정. 그밖에인덱스칼라모드 (GLUT_INDEX) 를지정하면효율을향상시킬수있음. void glclearcolor(glclampf R, Glclampf G, Glclampf B, Glclampf A) 윈도우를전부칠할때의색을지정. R, G, B, A 는 0~1 사이의값을가짐. (0, 0, 0, 1) 을지정하면백색의불투명을그림. void glclear(glbitfield mask) 윈도우를전부칠함. Mask 에는전부칠하는버퍼를지정한다. OpenGL 이관리하는화면상의버퍼 ( 메모리 ) 에는 color buffer, depth buffer, stencil buffer, overlay buffer, 등이겹쳐서존재함. GL_COLOR_BUFFER 를지정했을때는컬러버퍼만전부칠해짐. void glflush(void) 이함수는아직실생되지않은 OpenGL 명령을전부실행. 20
OpenGL/GLUT Program 작성예 윈도우내에선을그리는프로그램예제 glbegin(gl_line_loop); glvertex2d(-0.9, -0.9); glvertex2d(0.9, -0.9); glvertex2d(0.9, 0.9); glvertex2d(-0.9, 0.9); glend(); void init (void) /* 변경없음 */ int main(int argc, char *argv[]) /* 변경없음 */ 21 void glbegin(glenum mode) void glend(void) 도형을그리려면, glbegin() 과 glend() 사이에그도형의각정점의좌표치를설정하는함수를둠. Mode에 GL_POINTS, GL_LINES, GL_POLYGON, 등등도형의타입을지정. void glvertex2d(gldouble x, GLdouble y) 이함수는 2 차원의좌표치를설정하는사용. 인수의형태는 Gldouble 임. Float 형태는 glvertex2f(..) 를 int 형태는 glvertex2i(..) 를사용함. 22 윈도우내에도형을전부칠하는프로그램예제 glcolor3d(1.0, 0.0, 0.0); glbegin(gl_polygon); glvertex2d(-0.9, -0.9); glvertex2d(0.9, -0.9); glvertex2d(0.9, 0.9); glvertex2d(-0.9, 0.9); glend(); void glcolor3d(gldouble r, GLdouble g, GLdouble b) 이함수는그림의색을지정. 인수는 GLdouble 형태로, r, g, b에는각각 0~1의범위에서지정함. 인수가 Float 형태는 glcolor3f(..) 를 int 형태는 glcolor3i(..) 를사용함. 23 24
도형의색을정점마다지정하는프로그램예제 glbegin(gl_polygon); glcolor3d(1.0, 0.0, 0.0); // red glvertex2d(-0.9, -0.9); glcolor3d(0.0, 1.0, 0.0); // green glvertex2d(0.9, -0.9); glcolor3d(0.0, 0.0, 1.0); // blue glvertex2d(0.9, 0.9); glcolor3d(1.0, 1.0, 0.0); // yellow glvertex2d(-0.9, 0.9); glend(); OpenGL Syntax Basic OpenGL Syntax Function names are prefixed with gl E.g.: glbegin, glclear, glcopypixels, glpolygonmode Symbolic constants are prefixed with GL and each word is separated with _ E.g.: GL_2D, GL_RGB, GL_CCW, GL_POLYGON, GL_AMBIENT_AND_DIFFUSE Data types are prefixed with GL without any underscore E.g. GLbyte, GLshort, GLint, GLfloat, GLdouble, GLboolean 25 26 References http://www.cosc.brocku.ca/offerings/3p98/course/o pengl/3p98examples/gettingstarted/msvcnetglut.ht ml OpenGL/GLUT installation 27