본문 바로가기

OGRE 엔진활용

[Ogre] 랜더윈도우 수동으로 생성시 주의사항

Ogre 에서 createRenderWindow() 함수에서 렌더윈도우를 생성하는 방법은 자동으로 생성과, 사용자가 지정해주는 수동 방식 두가지가 존재한다.

먼저 자동을 하기위해서는 " createRenderWinodw() 함수내에 mRoot->initialise(true, "윈도우 타이틀명") " 으로 해주면 별다른 문제가 발생하지 않는다.

void createRenderWindow()
{

mRoot->initialise(true, "윈도우 타이틀명");

}

하지만 수동으로 지정해줬을때는 NULL 포인터참조로 프로그램이 죽는현상을 볼수 있다.(Ogre 공식 튜토리얼 6번을 보고 따라하다가, 수동으로 지정해줬을때 라면 말이다.)

우선 수동으로 렌더윈도우를 지정해주는 부분은 아래와 같다. 물론 여기에서는 아무문제없이 렌더윈도우가 생성된다.

void createRenderWindow()
{

mRoot->initialise(false);
  HWND hWnd = 0; //어플리케이션의 hWnd를 얻는다
  NameValuePairList misc;
  misc["externalWindowHandle"] = StringConverter::toString((int)hWnd);
  mRenderWindow = mRoot->createRenderWindow("Project D-Vision", 800, 600, false, &misc);

}

문제가 발생하는 부분은 카메라를 생성하고, 생성된 렌더윈도우에 뷰포트를 지정하는 부분이다.
  mSceneMgr = mRoot->createSceneManager(ST_GENERIC, "Default SceneManager");
  mCamera = mSceneMgr->createCamera("Camera");
  Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(mCamera);

위의 뷰포트생성시 널포인트 참조로 프로그램이 사망한다. 잘 살펴보면 mCamera에도 mRoot에도 mRenderWindow 모두 정상적으로 값이 할당되었는데 왜 그럴까?

원인은 렌더윈도우를 생성하는 initialise() 함수의 원형이 있는 ogreroot.h 의 설명을 보면 알수 있다.

        /** Initialises the renderer.
            @remarks
                This method can only be called after a renderer has been
                selected with Root::setRenderSystem, and it will initialise
                the selected rendering system ready for use.
            @param
                autoCreateWindow If true, a rendering window will
                automatically be created (saving a call to
                Root::createRenderWindow). The window will be
                created based on the options currently set on the render
                system.
            @returns
                A pointer to the automatically created window, if
                requested, otherwise <b>NULL</b>.

        */

보이는가? 자동으로 생성되었을때는 자동으로 생성된 렌더윈도우의 포인터를 가지는 반면, 그렇지 않을때는 NULL 포인터를 가진다고 한다.

결국 " Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(mCamera) " 에서의 getAutoCreateWindow()가 가르키는 것은 NULL 포인터 이므로 사망..

해결책은 생성에 성공한 mRenderWindow 포인터를 이용한 addViewport(mCamera)를 호출하는 것이다.

->   Viewport *vp = mRenderWindow->addViewport(mCamera);