Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

codingfarm

기본 예제 분석 본문

computer graphics/opengl

기본 예제 분석

scarecrow1992 2020. 3. 21. 00:46

OpenGL Super Bible 책의 모든 예제에 쓰이는 간단한 애플리케이션 프레임워크를 살펴본다.

책의 애플리케이션 프레임워크를 통해 어떻게 메인 윈도우를 만드는지 살펴보고 간단한 그래픽스 렌더링을 해본다.

간단한 GLSL 쉐이더가 어떻게 생겼는지, 쉐이더를 사용하여 단순한 점들을 어떻게 렌더링 하는지 살펴본다.

 

 

기본 프레임 워크

$\circ$ sb6.h는 sb6라는 네임스페이스를 정의하며, 안에는 애플리케이션 클래스인 sb6::application 에 대한 선언이 들어있다.

$\circ$ 위 예제에서는 이 애플리케이션 클래스를 상속받은 클래스를 사용한다.

 

sb6::application를 활용하여 애플리케이션을 작성하기 위해선

1. ab6.h 헤더파일을 인클루드 한다.

2. sb6::application 클래스로부터 상속받는다.

3. DECLARE_MAIN 매크로의 인스턴스를 소스 파일 중 하나에 포함시킨다.

DECKARE_MAIN 매크로는 애플리케이션의 메인 엔트리 포인트(main entry point; 주 진입점)을 정의하며, 여기에서 크래스의 인스턴스를 생성하고, run() 메서드를 호출한다. 이 메서드가 애플리케이션의 메인 루프를 정의한다.

다음으로 startup() 메서드를 호출하여 초기화를 수행하고, render() 메서드를 루프안에서 호출한다.

기본 구현에서 두함수는 모두 비어있는 가상함수이다.

파생클래스에서는 render() 메서드를 오버라이드하여 그 안에 렌더링 코드를 작성한다.

애플리케이션 프레임워크는 윈도우 생성, 입력처리, 렌더링된 결과를 사용자에 표시하는 역할을 맡는다.

 

 

빨간색 배경 출력

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
 * Copyright ?2012-2013 Graham Sellers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
 
#include <sb6.h>
 
class simpleclear_app : public sb6::application
{
    void init()
    {
        static const char title[] = "OpenGL SuperBible - Simple Clear";
 
        sb6::application::init();
 
        memcpy(info.title, title, sizeof(title));
    }
 
    virtual void render(double currentTime)
    {
        static const GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
        glClearBufferfv(GL_COLOR, 0, red);
    }
};
 
DECLARE_MAIN(simpleclear_app);
 
cs

 

위코드는 glClearBufferfv() OpenGL 함수를 통해 전체화면을 빨간색으로 가득 채우는 역할을 한다.

모든 OpenGL 함수는 gl로 시작하며, 해당 함수의 일부 인자 타입을 함수 이름 끝에 접미사로 줄여쓰는 네이밍 컨벤션을 따른다.

위와같은 규칙을 통해 오버로딩이 지왼안되는 특정 언어에서도 제한된 형태의 오버로딩이 가능하다.

fv는 위 함수가 부동소수점(floating point, f) 값을 갖는 벡터(vector, v)를 사용한다는 의미이다. OpenGL에서는 배열과 벡터는 동일한 의미로 혼용된다.

1
2
3
void glClearBufferfv(    GLenum buffer,
     GLint drawbuffer,
     const GLfloat * value);
cs

첫번째인자인 버퍼를 세번째 인자의 값으로 지우라(clear)는 명령을 OpenGL에 내린다.

두번째인자 drawBuffer는 지울 출력 버퍼가 여럿일 때 사용한다.

예제에서는 하나의 버퍼만 사용하며 drawBuffer는 0 기반의 인덱스를 사용하므로, 이 예제에서는 값을 0으로 설정한다.

색은 배열 red에 저장한다.

배열은 네 개의 부동 소수점 값인 빨간색, 녹색, 파란색, 알파의 색정보를 가진다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
 * Copyright ?2012-2013 Graham Sellers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
 
#include <sb6.h>
#include<vmath.h>
 
class simpleclear_app : public sb6::application
{
    void init()
    {
        static const char title[] = "OpenGL SuperBible - Simple Clear";
 
        sb6::application::init();
 
        memcpy(info.title, title, sizeof(title));
    }
 
    virtual void render(double currentTime)
    {
        const GLfloat color[] = { (float)sin(currentTime) * 0.5f + 0.5f,
                                (float)cos(currentTime) * 0.5f + 0.5f,
                                0.0f, 1.0f };
 
        glClearBufferfv(GL_COLOR, 0, color);
    }
};
 
DECLARE_MAIN(simpleclear_app);
 
cs

위 코드로 화면의 색깔이 계속해서 변하는것을 확인 할 수 있다.

 

 

 

 

 

 

 

 

 

 

 

 

 

'computer graphics > opengl' 카테고리의 다른 글

설치 및 셋팅  (0) 2020.03.20
OpenGL 기초  (0) 2020.03.18
opengl 설치 및 예제, 컴파일  (0) 2019.12.16
Comments