반응형
본 글은 국민대학교 김준호 교수님의 "컴퓨터 그래픽스" 강의자료를 정리 목적으로 만들었습니다.
글 자료의 모든 권한은 김준호 교수님께 있습니다.
Shader Programming at a Glance
GLSL Programming at a glance
코드 불러오는 순서
void init_shader_program()
{
// shaderfile불러오기
GLuint vertex_shader = create_shader_from_file('~~~',GL_VERTEX_SHADER);
GLuint fragment_shader = create_shader_from_file('~~~', GL_FRAGMENT_SHADER);
//프로그램 만들기
program = glCreateProgram();
//프로그램에 shader 붙이기
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
//gpu에서 uniform 변수의 위치 =>값 할당은 나중에해줌
loc_u_PVM = glGetUniformLocation(program, "u_PVM");
//gpu에서 attribute 변수 의 위치 =>값 할당은 나중에해줌
loc_a_position = glGetAttribLocation(program, "a_position");
loc_a_color = glGetAttribLocation(program, "a_color");
}
void render_object()
{
//use a program
glUseProgram(program);
//Load uniforms
mat_PVM = mat_proj * mat_view * mat_model;
glUniformMatrix4fv(loc_u_PVM, 1, GL_FALSE, mat_PVM);
//Load attributes as per-vertex data
glBindBuffer(...);
glEnableVertexAttribArray(...);
glVertexAttribPointer(...);
//Draw with per-vertex data
glDrawArrays(...);
}
More about Shader Programming
shader들이 input을 바꿔서는 안된다. Input값은 Read only
I/O Storage Qualifiers in GLSL 1.2
Three types of I/O storage qualifiers in shader
Uniforms
Declare global variables whose values are the same across the entire shaders
Attributes
- Declare variables that are passed to a vertex shader from OpenGL on a per-vertex basis
Varyings
- Variables that provide the interface between the vertex shader, the fragment shader, and the fixed functionality between them
Built-in variables and generic variables are availabe
Built-in type : OpenGL pre-defined constants and uniform state
Generic type : User-defined variables
Uniform
Declare values, which do not change
during a rendering
Available in both of vertex shader and fragment shader
Read-only
- Initialized either directly by an appliction via API commands or indirectly by OpenGL
Attribute
Declare values, which vary for per-vertex
Available in vertex shader only
Read-only
- Passed through the OpenGL vertex API or as part of a vertex array
Varyings
Used for passing data from vertex shader to fragment shader
Read/writable in vertex shader
read-only in fragment shader
shader code example
//vertex shader
uniform mat4 u_PVM;
attribute vec3 a_position;
attribute vec43 a_color;
varying vec3 v_color;
void main()
{ // Uniform 변수 PVM과 attribute 변수 a_position을 이용해 vertex마다 위치계산
gl_Position = u_PVM * vec4(a_position, 1.0f); //gl_position : 예약어
v_color = a_color; // 여기서 color를 어떻게 가중치를 부여하냐에따라 색이 다르게 나올 수 있음.
}
//fragment shader
varying vec3 v_color; //vertexshader에서 계산한 color를 받아옴
void main()
{
gl_FragColor = vec4(v_color,1.0f);
}
반응형
'컴퓨터그래픽스' 카테고리의 다른 글
[게시판 안내사항] (0) | 2023.07.15 |
---|---|
[23-1 컴퓨터 그래픽스 정리] shader programing part1 (0) | 2023.07.13 |
[23-1 컴퓨터 그래픽스 정리] Clipping, Rasterization (0) | 2023.07.13 |
[23-1 컴퓨터 그래픽스 정리] Vertex Processor (0) | 2023.07.13 |
[23-1 컴퓨터 그래픽스 정리] transformations (0) | 2023.07.13 |