Transform의 구분
World Transform은 각각 오브젝트가 고유의 변환을 한다
View Transform, Projection Transform은 모든 오브젝트가 공유하는 변환을 한다
Vertex Array
Vertex Array는 position, normal, texture coordinates 등이 포함된다
Vertex Shader는 Vertex Array를 한 번에 병렬 연산으로 처리한다
Vertex Shader Input/Output
Vertex Shader Input은 Attributes와 Uniforms로 구분된다
Attributes는 per-vertex data로 'in' 키워드를 사용한다
Uniforms는 per-object data로 'uniform' 키워드를 사용하고
World Matrix, View Matrix, Projection Matrix가 있다
Vertes Shader Ouput은 gl_Position이라는 데이터를 출력해야 하고
해당 데이터는 Clip-Space에서 정점의 위치를 가져야 하고
normal, texCoord 등의 데이터를 'out' 키워드를 통해 넘겨주며 사용한다
gl vs. GL
gl은 GPU에 명령을 내리는 명령문이다
GL은 상수와 같은 데이터 타입이다
glEnable(GL_CULL_FACE);
Array Buffer
vertex array는 GL_ARRAY_BUFFER를 이용하고
index array는 GL_ELEMENT_ARRAY_BUFFER를 이용한다
Array Buffer 사용법
//---------데이터를 전달하는 과정--------//
unsigned int bufferID;
glGenBuffers(1, &bufferID);//1. 버퍼 생성
glBindBuffer(GL_ARRAY_BUFFER, bufferID);//2. 바인딩("activate")
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(float), positions, GL_STATIC_DRAW);//3. 작업 상태 버퍼에 데이터 전달
//---------데이터를 해석하는(법을 정의하는) 과정--------//
glEnableVertexAttribArray(0);//1. 몇 번째 Location의 attribute를 활성화(enable)
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float)*2, 0);//2. 데이터 해석 방법을 전달
Vertex Attributes
Vertex Attributes를 구분시켜서 Buffer에 넘겨줘야 하고
아래와 같은 코드를 통해 구분시켜서 넘겨줄 수 있다
// Vertex position
glEnableVertexAttribArray(0);//0 번째 Location의 attribute를 활성화(enable)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float)*8, 0);// 데이터 해석 방법 전달
// Vertex normal
glEnableVertexAttribArray(1);//1 번째 Location의 attribute를 활성화(enable)
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float)*8, (void*)(3 * sizeof(float));// 데이터 해석 방법 전달
// Vertex texcoord
glEnableVertexAttribArray(2);//2 번째 Location의 attribute를 활성화(enable)
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(float)*8, (void*)(6 * sizeof(float));// 데이터 해석 방법 전달
Vertex Attributes 활용
만약에 1번 Location으로 색상이 들어오고
그것을 쉐이더에서 활용한다면
아래의 코드와 같이 작성해서 사용할 수 있다
#shader vertex
#version 330 core
layout(location = 0) in vec4 position;
layout(location = 1) in vec4 a_Color;
//layout(location = 1) in vec4 normal;
//layout(location = 2) in vec4 texcoord;
out vec4 v_Color;
void main()
{
gl_Position = position;
v_Color = a_Color;
};
#shader fragment
#version 330 core
layout(location = 0) out vec4 color;
uniform vec4 u_Color;
in vec4 v_Color;
void main()
{
color = v_Color;
};
'그래픽스 > [전북대] OpenGL' 카테고리의 다른 글
[OpenGL Note] Image Texturing (0) | 2024.04.17 |
---|---|
[OpenGL Note] Rasterizer (1) | 2024.04.17 |
[OpenGL Note] Uniforms, VAO (0) | 2024.04.16 |
[OpenGL Note] Projection Transform (0) | 2024.04.15 |
[OpenGL Note] View Frustrum (0) | 2024.04.15 |