OpenGL 프로그래밍/기본/구조

위키책, 위키책

새 프로젝트를 설정[+/-]

시작하기 전에 새로운 C++ 프로젝트의 설정이 필요하다. IDE 프로그램에 따라 (또는 프로그램의 버전에 따라) 설정 방법이 다르니 이 작업을 수행하기전에 해당 IDE 프로그램의 설명서를 다시한번 확인을 하는 것이 필요하다.

GLUT[+/-]

Many OpenGL applications take advantage of a cross-platform (Windows, Mac and Linux compatible) toolkit called GLUT, which is used in conjunction with OpenGL. GLUT is used to draw windows and handle mouse and keyboard events, which OpenGL cannot do on its own. There are alternatives to GLUT, but GLUT is the simplest GUI toolkit that runs cross-platform, so we assume you have GLUT installed.

There are many disadvantages to GLUT, which are related to its inflexibility. Since GLUT has to work the same across three very different operating systems, you don't have any platform-specific features. GLUT itself cannot take advantage of operating-system provided widgets (buttons, pulldown menus). Your application will not be able to change window-level menus (although context-menus can be customized). If these limitations are dealbreakers for your project, you'll have to use an alternative to GLUT. These will be discussed later in this wikibook. For now, just sit tight with GLUT.

나의 첫번째 OpenGL 프로그램[+/-]

main.cpp 파일을 열거나 생성한다. 이 파일의 기본 형식에 대하여 잘 알고 있어야 합니다:

 #include <iostream>
 int main(int argc, char *argv[])
 {
 	return 0;
 }

OpenGL 헤더 불러오기[+/-]

응용프로그램에서 OpenGL을 사용하기 위해서는 여기에 있는 헤더들을 include 할 필요가 있다. 당신이 OpenGL을 어디에 어떻게 설치했는지에 따라 여기의 경로들을 조금 조정해줄 필요가 있습니다.

 #include <GL/gl.h>
 #include <GL/glut.h>
 #include <GL/glu.h>

여기서 포함된 기본적인 OpenGL 명령어와 GLUT 그리고 GLU라고 불리는 유틸리티 툴킷은 이후에 유용합니다. 운영체제가 파일이름의 대소문자를 구분처리하는 경우(예:리눅스)에는 컴파일하기 이전에 반드시 GL을 대문자로 변경해야합니다.

만약 비주얼 스튜디오를 사용하는 경우 OpenGL 헤더들을 include 하기 이전에 windows.h를 include 해야합니다.

창 생성하기[+/-]

Now that we have our library, we can begin to design our interface by creating our window inside our int main() function:

 #ifdef WIN32 //if using windows then do windows specific stuff.
 #define WIN32_LEAN_AND_MEAN //remove MFC overhead from windows.h witch can cause slowness
 #define WIN32_EXTRA_LEAN
 
 #include <windows.h>
 #endif
 
 #include <GL/gl.h>
 #include <GL/glut.h>
 #include <GL/glu.h>
 
 void display() { /* empty function   required as of glut 3.0 */ }
 
 int main(int argc, char *argv[])
 {
 	glutInit(&argc, argv);
 	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
 	glutInitWindowSize(800,600);
 	glutCreateWindow("Hello World");
 	glutDisplayFunc(display);
 	glutMainLoop();
 	return 0;
 }

We first call glutInit(), which starts up GLUT for our use. Next, we set up a display mode. For now, just use the settings we've provided here for glutInitDisplayMode. You might tweak these settings later. Then we tell GLUT how big we want our window to be; 800 by 600 is OK for now. Finally, we actually create the window with glutCreateWindow(), passing the window title as an argument, and give control of our program to GLUT through glutMainLoop(). Never forget to call glutMainLoop() when you use GLUT!

You might have noticed something so far: every function is prefixed with glut-. When we start actually using OpenGL itself, every function will be prefixed with just a gl-. This is for organization.

Try compiling this application. Hopefully, you should get an 800x600 window with the title Hello World. If you don't, refer to the installation section of this wikibook and consider reinstalling.

그리기 위한 준비[+/-]

대부분의 간단한 OpenGL 응용프로그램은 단지 3가지 요소로만 구성되있다:

  • setup 함수: 로 그리기 시작하기 앞서 모든것을 설정한다.
  • display 함수: 실제로 그린다.
  • int main(): 주로 가장 위에 배치된다. int main은 setup함수를 호출하고 display함수를 사용할때 GLUT에게 지시한다.

몇몇 OpenGL 코드는 다음과 같은 방식으로 보일 수 있다:

 #ifndef WIN32 //if using windows then do windows specific stuff.
 #define WIN32_LEAN_AND_MEAN //remove MFC overhead from windows.h witch can cause slowness
 #define WIN32_EXTRA_LEAN
 
 #include <windows.h>
 #endif
 
 #include <GL/gl.h>
 #include <GL/glut.h>
 #include <GL/glu.h>
 
 void setup() { /* empty function  nothing to setup yet */ }
 void display() { /* empty function   required as of glut 3.0 */ }
 
 int main(int argc, char *argv[])
 {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
        glutInitWindowSize(800,600);
        glutCreateWindow("Hello World");
 
        setup();
        glutDisplayFunc(display);
 
        glutMainLoop();
 
        return 0;
 }

display()가 비었기 때문에 이 코드는 스스로 아무것도 그리지 않는다.

다음 단원은 OpenGL_프로그래밍/기본/사각형이다.