본문 바로가기

Game/Graphics

OpenGL-Tutorial 3 : 행렬(매트릭스)

link : http://www.opengl-tutorial.org/kr/beginners-tutorials/tutorial-3-matrices/


이번 튜토리얼에서는 행렬에 대해 다룬다.


강의시간에 배웠던 내용을 복습한다고 생각하고 공부했다.


[c++ , using GLM]


1) 평행이동 행렬

glm::mat4 myMatrix = glm::translate(glm::mat4(), glm::vec3(A,B,C)); //A,B,C는 이동시킬 값 (float)

glm::vec4 myVector(X,Y,Z,W); // X,Y,Z,W 좌표

glm::vec4 transformedVector = myMatrix * myVector;


2) 단위 행렬

glm::mat4 myIdentityMatrix = glm::mat4(1.0f);


3) 스케일링 행렬

glm::mat4 myScalingMatrix = glm::scale(A,B,C);


4) 회전 행렬

glm::vec3 myRotationAxis(??,??,??);

glm::rotate(angle_in_degrees,myRotationAxis);



[The Model, View and Projection matrices]

1) 뷰 매트릭스

glm::mat4 CameraMatrix = glm::lookAt(

cameraPosition,    //월드 공간에서 카메라 좌표

cameraTraget,      //월드 공간에서 카메라가 볼곳

upVector            //glm:vec(0,1,0)이 기본 .. (0,-1,0)으로 화면을 뒤집을 수 있다.

);


2) 프로젝션 매트릭스

glm::mat4 projectionMatrix = glm::perspective(

glm::radians(FoV),    //수직방향 시야각 : "줌"의 크기. "카메라 렌즈"를 생각해보아라. 보통 90도(엑스트라 와이드)에서 30도(크게 확대한 경우)사이에 있다.

4.0f / 3.0f              //화면 비율이다. 이 것은 윈도우 크기에 의존한다.

0.1f                      //Near clipping plane (근거리 잘라내기 평면). 최대한 크게 해라, 아니면 정확도에 문제가 생길 수 있다.

100.0f                   //Far clipping plane (원거리 잘라내기 평면). 최대한 작게 해라.

);



유니티를 공부하면서 두루뭉실하게 알고 있던 개념을 다시 자세히 봄으로서 카메라 뷰에 대한 개념을 확실히 이해했다.


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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <GL/glew.h>
#include <glfw3.h>
GLFWwindow* window;
 
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
using namespace glm;
 
GLuint LoadShaders(const char *const char *);
 
int main() {
 
    // Initialise GLFW
    if (!glfwInit())
    {
        fprintf(stderr, "Failed to initialize GLFW\n");
        getchar();
        return -1;
    }
 
    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
 
    // Open a window and create its OpenGL context
    window = glfwCreateWindow(1024768"QBOT_opengl"NULLNULL);
    if (window == NULL) {
        fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
        getchar();
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
 
    // Initialize GLEW
    glewExperimental = true;
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        getchar();
        glfwTerminate();
        return -1;
    }
 
    // Ensure we can capture the escape key being pressed below
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
 
    // Dark blue background
    glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
 
    GLuint VertexArrayID;
    glGenVertexArrays(1&VertexArrayID);
    glBindVertexArray(VertexArrayID);
 
    //Shader를 불러온다.
    GLuint programID = LoadShaders("SimpleTransform.vertexshader""SingleColor.fragmentshader");
 
    //매트릭스ID 추가
    GLuint MatrixID = glGetUniformLocation(programID, "MVP");
 
    glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
 
    //카메라 매트릭스
    glm::mat4 View = glm::lookAt(
                glm::vec3(433),    //카메라 위치
                glm::vec3(000), //보는 Look
                glm::vec3(010)  //head-up
    );
 
    //모델
    glm::mat4 Model = glm::mat4(1.0f);
 
    glm::mat4 MVP = Projection * View * Model;
 
    static const GLfloat g_vertex_buffer_data[] = {
        -1.0f,-1.0f,0.0f,
        1.0f, -1.0f, 0.0f,
        0.0f,1.0f,0.0f,
    };
    static const GLushort g_element_buffer_data[] = { 0,1,2 };
 
    GLuint vertexbuffer;
    glGenBuffers(1&vertexbuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
 
    do {
        // Clear the screen. It's not mentioned before Tutorial 02, but it can cause flickering, so it's there nonetheless.
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
        glUseProgram(programID);
 
        //transformation을 현재 쉐이더에 보냄
        glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
 
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
        glVertexAttribPointer(
            0,            //0번째 속성. 0이 될 특별한 이유는 없지만 쉐이더의 레이아웃과 반드시 맞춰야함
            3,            //크기(size)
            GL_FLOAT,    //타입(type)
            GL_FALSE,    //정규화(normalized)?
            0,            //다음 요소까지의 간격(stride)
            (void*)0    //배열 버퍼의 오프셋(offset)
        );
 
        glDrawArrays(GL_TRIANGLES, 03);    //버텍스 0에서 시작해서~ 총 3개의 버텍스로 -> 하나의 삼각형
 
        glDisableVertexAttribArray(0);
 
        // Swap buffers
        glfwSwapBuffers(window);
        glfwPollEvents();
 
    } // Check if the ESC key was pressed or the window was closed
    while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
        glfwWindowShouldClose(window) == 0);
 
    // Cleanup VBO
    glDeleteBuffers(1&vertexbuffer);
    glDeleteVertexArrays(1&VertexArrayID);
    glDeleteProgram(programID);
 
    // Close OpenGL window and terminate GLFW
    glfwTerminate();
 
    return 0;    
}
 
GLuint LoadShaders(const char * vertex_file_path, const char * fragment_file_path) {
 
    //쉐이더 생성
    GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
 
    //버텍스 쉐이더 코드를 파일에서 읽기
    std::string VertexShaderCode;
    std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
    if (VertexShaderStream.is_open()) {
        std::stringstream sstr;
        sstr << VertexShaderStream.rdbuf();
        VertexShaderCode = sstr.str();
        VertexShaderStream.close();
    }
    else {
        printf("파일 %s를 읽을 수 없음. 정확한 디렉토리를 사용 중입니까?\n", vertex_file_path);
        getchar();
        return 0;
    }
 
    //프래그먼트 쉐이더 코드를 파일에서 읽기
    std::string FragmentShaderCode;
    std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
    if (FragmentShaderStream.is_open()) {
        std::stringstream sstr;
        sstr << FragmentShaderStream.rdbuf();
        FragmentShaderCode = sstr.str();
        FragmentShaderStream.close();
    }
 
    GLint Result = GL_FALSE;
    int InfoLogLength;
 
    //버텍스 쉐이더를 컴파일
    printf("Compiling shader : %s\n", vertex_file_path);
    char const * VertexSourcePointer = VertexShaderCode.c_str();
    glShaderSource(VertexShaderID, 1&VertexSourcePointer, NULL);
    glCompileShader(VertexShaderID);
 
    //버텍스 쉐이더를 검사
    glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    if (InfoLogLength > 0) {
        std::vector<char> VertexShaderErrorMessage(InfoLogLength + 1);
        glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL&VertexShaderErrorMessage[0]);
        printf("%s\n"&VertexShaderErrorMessage[0]);
    }
 
    //프래그먼트 쉐이더를 컴파일
    printf("Compiling shader : %s", fragment_file_path);
    char const * FragmentSourcePointer = FragmentShaderCode.c_str();
    glShaderSource(FragmentShaderID, 1&FragmentSourcePointer, NULL);
    glCompileShader(FragmentShaderID);
 
    //프래그먼트 쉐이더를 검사
    glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
    glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    if (InfoLogLength > 0) {
        std::vector<char> FragmentShaderErrorMessage(InfoLogLength + 1);
        glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL&FragmentShaderErrorMessage[0]);
        printf("%s\n"&FragmentShaderErrorMessage[0]);
    }
 
    //프로그램에 링크
    printf("Linking program\n");
    GLuint ProgramID = glCreateProgram();
    glAttachShader(ProgramID, VertexShaderID);
    glAttachShader(ProgramID, FragmentShaderID);
    glLinkProgram(ProgramID);
 
    //프로그램 검사
    glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
    glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    if (InfoLogLength > 0) {
        std::vector<char> ProgramErrorMessage(InfoLogLength + 1);
        glGetProgramInfoLog(ProgramID, InfoLogLength, NULL&ProgramErrorMessage[0]);
        printf("%s\n"&ProgramErrorMessage[0]);
    }
 
    glDetachShader(ProgramID, VertexShaderID);
    glDetachShader(ProgramID, FragmentShaderID);
 
    glDeleteShader(VertexShaderID);
    glDeleteShader(FragmentShaderID);
    
    return ProgramID;
}
cs


유니티에서는 간단하게 카메라 오브젝트를 생성해서 각도를 잡았는데 직접 코드를 한줄 한줄 작성해 각도를 틀어보니 꽤 재밌었다.


이 파트도 딱히 어려운 부분이 없으니 이대로 넘어가겠다.