세모그래픽스 III. 게임프로그래밍에필요한 OpenGL Page 1
목차 1. 간단한 OBJ-C 2. IOS의 OGL VS Win32의 OGL 3. IOS개발환경설정 4. 뷰포트, 프로젝션, 모델뷰 ( 회전이먼저냐이동이먼저냐?) Page 2
세모그래픽스 간단한 OBJ-C 2011.07.16 김형석 Page 3
1. Obj-C (test2_cpp) #import <Foundation/Foundation.h> @interface Fraction : NSObject { @private int upnum; int downnum; -(void) print; -(int) upnum; -(int) downnum; -(void) setupnum : (int) val; -(void) setdownnum : (int) val; @property (nonatomic,assign) int numerator; @property (nonatomic,assign) int denumerator; @end Page 4
1. Obj-C (test2_cpp) @implementation Fraction -(void) print { printf("%d/%d = %f", upnum,downnum, upnum/(float)downnum); -(int) upnum { return upnum; -(int) downnum { return downnum; -(void) setupnum : (int) val { upnum = val; -(void) setdownnum : (int) val { downnum = val; @end @synthesize upnum; @synthesize downnum; Page 5
2. Obj-C (test1) #import <Foundation/Foundation.h> @interface Fraction : NSObject { @public int numerator; int denumerator; @property (nonatomic,assign) int numerator; @property (nonatomic,assign) int denumerator; -(void) print; @end @implementation Fraction @synthesize numerator; @synthesize denumerator; -(void) print { printf("%d/%d => %f\n", numerator, denumerator, numerator/(float)denumerator); @end Page 6
3. 소스컴파일옵션변경 - 소스컴파일옵션변경 (.m.mm.cpp.c) - 프레임웍추가 Page 7
세모그래픽스 IOS OpenGL vs Win OpenGL 2011.07.16 김형석 Page 8
1. OpenGL 렌더링순서 I. 렌더링콘텍스트생성 ( 프레임, 렌더링,Z, 스텐실버퍼 ) 1) 현재콘텍스트로설정 2) 뷰포트설정 콘텍스트변경 윈도크기변경 II. 사용자렌더링 3) 프로젝션 M 설정 4) 클리어버퍼 5) 모델뷰 M 설정 III. 렌더링콘텍스트삭제 6) GL 객체렌더링 ( 루프 ) 7) 스웹버퍼 ( 프레젠트 ) Page 9
I. 렌더링콘텍스트생성 ( 프레임, 렌더링,Z, 스텐실버퍼 ) m_context = [[EAGLContext alloc] initwithapi: keaglrenderingapiopengles1]; // Create & bind the color buffer so that the caller can allocate its space. glgenrenderbuffersoes(1, &m_renderbuffer); glbindrenderbufferoes(gl_renderbuffer_oes, m_renderbuffer); // Create the framebuffer object and attach the color buffer. glgenframebuffersoes(1, &m_framebuffer); glbindframebufferoes(gl_framebuffer_oes, m_framebuffer); glframebufferrenderbufferoes(gl_framebuffer_oes, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, m_renderbuffer); hdc = GetDC(hWnd); hrc = wglcreatecontext(hdc); Page 10
II. 사용자렌더링 1) 현재콘텍스트로설정 2) 뷰포트설정 3) 프로젝션 M 설정 if (!m_context![eaglcontext setcurrentcontext:m_context]) { [self release]; return nil; glviewport(0, 0, width, height); glmatrixmode(gl_projection); // Initialize the projection matrix. const float maxx = 2; const float maxy = 3; glorthof(-maxx, +maxx, -maxy, +maxy, -1, 1); wglmakecurrent(hdc, hrc); case WM_SIZE: glviewport(0, 0, LOWORD(lParam), HIWORD(lParam)); break; glmatrixmode(gl_projection);// 기본매트릭스사용 Page 11
II. 사용자렌더링 5) 모델뷰 M 설정 6) GL 객체렌더링 ( 루프 ) 7) 스웹버퍼 ( 프레젠트 ) glclear(gl_color_buffer_bit); glpushmatrix(); glrotatef(m_currentangle, 0, 0, 1); glvertexpointer(2, GL_FLOAT, sizeof(vertex), &Vertices[0].Position[0]); glcolorpointer(4, GL_FLOAT, sizeof(vertex), &Vertices[0].Color[0]); GLsizei vertexcount = sizeof(vertices) / sizeof(vertex); gldrawarrays(gl_triangles, 0, vertexcount); glpopmatrix(); [m_context presentrenderbuffer:gl_renderbuffer]; glclear(gl_color_buffer_bit); 모델뷰, GL 객체렌더링은위와동일 SwapBuffers(hDC); /* nop if singlebuffered */ Page 12
III. 렌더링콘텍스트삭제 - (void)dealloc { [self deleteframebuffer]; [context release]; [super dealloc]; wglmakecurrent(null, NULL); ReleaseDC(hWnd, hdc); wgldeletecontext(hrc); Page 13
세모그래픽스 IOS 에서 MAC_XCode 개발환경설정 for OpenGL (HelloCone) 2011.07.16 김형석 Page 14
1. OpenGL 렌더링순서 I. 렌더링콘텍스트생성 ( 프레임, 렌더링,Z, 스텐실버퍼 ) 1) 현재콘텍스트로설정 2) 뷰포트설정 콘텍스트변경 윈도크기변경 II. 사용자렌더링 3) 프로젝션 M 설정 4) 클리어버퍼 5) 모델뷰 M 설정 III. 렌더링콘텍스트삭제 6) GL 객체렌더링 ( 루프 ) 7) 스웹버퍼 ( 프레젠트 ) Page 15
2. 뷰베이스앱으로개발 (Main.m) int main(int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int retval = UIApplicationMain(argc, argv, nil, @"HelloConeAppDelegate"); [pool release]; return retval; Page 16
3. HelloConeAppDelegate.h/.cpp #include "GLView.h" @interface HelloConeAppDelegate : NSObject <UIApplicationDelegate> { @private UIWindow* m_window; GLView* m_view; @end #import "HelloConeAppDelegate.h" @implementation HelloConeAppDelegate - (void) applicationdidfinishlaunching: (UIApplication*) application { CGRect screenbounds = [[UIScreen mainscreen] bounds]; // 화면크기 m_window = [[UIWindow alloc] initwithframe: screenbounds]; m_view = [[GLView alloc] initwithframe: screenbounds]; [m_window addsubview: m_view]; // GLView를메인위도우의 sub윈도우로.. [m_window makekeyandvisible]; // show window @end Page 17
4. GLView.h/IRenderingEngine.hpp @interface GLView : UIView { @private EAGLContext* m_context; IRenderingEngine* m_renderingengine; float m_timestamp; - (void) drawview: (CADisplayLink*) displaylink; - (void) didrotate: (NSNotification*) notification; @end // Interface to the OpenGL ES renderer; consumed by Objective C. struct IRenderingEngine { virtual void Initialize(int width, int height) = 0; virtual void Render() const = 0; virtual void UpdateAnimation(float timestep) = 0; virtual void OnRotate(DeviceOrientation neworientation) = 0; virtual ~IRenderingEngine() { ; Page 18
5. GLView. hpp if (!m_context) { api = keaglrenderingapiopengles1; m_context = [[EAGLContext alloc] initwithapi:api]; I. 렌더링콘텍스트생성 ( 프레임, 렌더링,Z, 스텐실버퍼 ) if (!m_context![eaglcontext setcurrentcontext:m_context]) { [self release]; return nil; if (api == keaglrenderingapiopengles1) { NSLog(@"Using OpenGL ES 1.1"); m_renderingengine = CreateRenderer1(); else { NSLog(@"Using OpenGL ES 2.0"); m_renderingengine = CreateRenderer2(); [m_context renderbufferstorage:gl_renderbuffer fromdrawable: eagllayer]; m_renderingengine->initialize(cgrectgetwidth(frame), CGRectGetHeight(frame)); Page 19
6. GLView. hpp CADisplayLink* displaylink; displaylink = [CADisplayLink displaylinkwithtarget:self selector:@selector(drawview:)]; [displaylink addtorunloop:[nsrunloop currentrunloop] formode:nsdefaultrunloopmode]; [[UIDevice currentdevice] begingeneratingdeviceorientationnotifications]; [[NSNotificationCenter defaultcenter] addobserver:self selector:@selector(didrotate:) name:uideviceorientationdidchangenotification object:nil]; 반복함수및알람함수연결 Page 20
6. GLView. hpp - (void) didrotate: (NSNotification*) notification { UIDeviceOrientation orientation = [[UIDevice currentdevice] orientation]; m_renderingengine->onrotate((deviceorientation) orientation); [self drawview: nil]; - (void) drawview: (CADisplayLink*) displaylink { printf("."); if (displaylink!= nil) { float elapsedseconds = displaylink.timestamp - m_timestamp; m_timestamp = displaylink.timestamp; m_renderingengine->updateanimation(elapsedseconds); m_renderingengine->render(); [m_context presentrenderbuffer:gl_renderbuffer]; 7) 스웹버퍼 ( 프레젠트 ) Page 21
7. RenderingEngine1 (Z 버퍼설명하기 ) // Create & bind the color buffer so that the caller can allocate its space. glgenrenderbuffersoes(1, &m_colorrenderbuffer); glbindrenderbufferoes(gl_renderbuffer_oes, m_colorrenderbuffer); // Create the depth buffer. glgenrenderbuffersoes(1, &m_depthrenderbuffer); glbindrenderbufferoes(gl_renderbuffer_oes, m_depthrenderbuffer); glrenderbufferstorageoes(gl_renderbuffer_oes GL_DEPTH_COMPONENT16_OES,,width,height); // Create the framebuffer object; attach the depth and color buffers. glgenframebuffersoes(1, &m_framebuffer); glbindframebufferoes(gl_framebuffer_oes, m_framebuffer); glframebufferrenderbufferoes(gl_framebuffer_oes, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, m_colorrenderbuffer); glframebufferrenderbufferoes(gl_framebuffer_oes, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, m_depthrenderbuffer); // Bind the color buffer for rendering. glbindrenderbufferoes(gl_renderbuffer_oes, m_colorrenderbuffer); //khs Page 22
7. RenderingEngine1 glviewport(0, 0, width, height); glenable(gl_depth_test); glmatrixmode(gl_projection); // (0,0,0) -> LB(-1.6,-2.4,-5) RU(1.6,2.4,-10) glfrustumf(-1.6f, 1.6, -2.4, 2.4, 5, 10);//khs: left,right,bottom,top,near,far glmatrixmode(gl_modelview); gltranslatef(0, 0, -2); 뷰포트의미및변경 glfrustum vs glortho (ogl 예제실행 ) Page 23
8. 전체소스리뷰하기 전체소스리뷰하기 Page 24
세모그래픽스 간단한 CPP(abstract class) 2011.07.16 김형석 Page 25
1. CPP (simple_cpp) class IRenderingEngine { Public: virtual void Initialize(int width, int height) = 0; virtual void Render() const = 0; virtual void UpdateAnimation(float timestep) = 0; virtual void OnRotate(DeviceOrientation neworientation) = 0; ; virtual ~IRenderingEngine() { 표준화된인터페이스 Page 26
1. CPP (simple_cpp) class RenderingEngine1 : public IRenderingEngine { public: RenderingEngine1(); ~RenderingEngine1(); //khs void Initialize(int width, int height); void Render() const; void UpdateAnimation(float timestep); void OnRotate(DeviceOrientation neworientation); private: vector<vertex> m_cone; vector<vertex> m_disk; Animation m_animation; GLuint m_framebuffer; GLuint m_colorrenderbuffer; GLuint m_depthrenderbuffer; ; class RenderingEngine2 : public IRenderingEngine { public: RenderingEngine2(); void Initialize(int width, int height); void Render() const; void UpdateAnimation(float timestep); void OnRotate(DeviceOrientation neworientation); private: GLuint BuildShader(const char* source, GLenum shadertype) const; GLuint BuildProgram(const char* vshader, const char* fshader) const; vector<vertex> m_cone; vector<vertex> m_disk; Animation m_animation; GLuint m_simpleprogram; GLuint m_framebuffer; GLuint m_colorrenderbuffer; GLuint m_depthrenderbuffer; ; Page 27
1. CPP (simple_cpp) if (api == keaglrenderingapiopengles1) { NSLog(@"Using OpenGL ES 1.1"); m_renderingengine = new RenderingEngine1() else { NSLog(@"Using OpenGL ES 2.0"); m_renderingengine = new RenderingEngine2() m_renderingengine->initialize(cgrectgetwidth(frame), CGRectGetHeight(frame)); - (void) didrotate: (NSNotification*) notification { UIDeviceOrientation orientation = [[UIDevice currentdevice] orientation]; m_renderingengine->onrotate((deviceorientation) orientation); [self drawview: nil]; Page 28
2. Sound class ISoundEngine { public: ISoundEngine(); Virtual ~ISoundEngine(); void play(int name) = 0; void pause() const = 0; void stop(float timestep) = 0; ; 표준화된인터페이스 class FmodSoundEngine: public ISoundEngine { public: FmodSoundEngine(); ~ FmodSoundEngine(); //khs void play(int name); void pause() const; void stop(float timestep); ; class OALSoundEngine: public ISoundEngine { public: OALSoundEngine(); ~OALSoundEngine(); void play(int name); void pause() const; void stop(float timestep); ; Page 29
3. Idialog 이용실습 Simple_cpp Page 30