본문 바로가기
전공/컴퓨터 그래픽스

OpenGL Shading Language (셰이딩) GLSL_(1)

by 임 낭 만 2023. 6. 17.

OpenGL Shading Language

OpenGL Shading Language

  • The OpenGL Shading Language (GLSL) allows writing custom programs for vertex and fragment processors
  • The custom GLSL programs thereby replace parts of the OpenGL pipeline that were previously performed by the fixed-function pipeline
  • The per-vertex-operations are partially replaced by a vertex shader and the per-pixel-operations by a fragment shader

Reiteration: OpenGL Pipeline

Fragment Shader

Parallel Processing

  • In the vertex and fragment shaders the data is processed in parallel
  • Today’s GPUs have up to 10000 processing units that can perform parallel calculations on the data
  • For all data exactly the same shader code is executed in parallel (“Stream Processing”)
  • The parallel processing is possible because the operations have a defined goal (either a transformed vertex or fragment), which can be written without conflicts

Creation of the Shaders Program in OpenGL

  • The shader programs are compiled at runtime and passed to the graphics card
  • To create a shader program that may contain vertex and fragment shaders, the following command is used
progID = glCreateProgram();
  • Creating, assigning, and compiling of shader code for a vertex and fragment shader is done with:
vertID = glCreateShaer(GL_VERTEX_SHADER);
fragID = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vertID, 1, &vertShadersrcCodeString, &vStrLength);
glShaderSource(fragID, 1, &fragShaderSrcCodeString, &fStrLength);
glCompileShader(vertID);
glCompileShader(fragID);
  • Then the shaders are assigned to the program and an executable is generated:
glAttachShader(progID, vertID);
glAttachShader(progID, fragID);
glLinkProgram(progID);

Using Shader Programs in OpenGL

  • To activate a shader program the following command is used
glUseProgram(progID);
  • In this case, OpenGL behaves again as a state-machine, i.e., all subsequent rendering function are using the currently active shader program
  • This also means that swapping between different shader program at runtime is possible (and often occurs in practice). For example, different shaders are used to simulate different materials

Deleting Shader Objects

  • For deletion the following functions are used:
glDeleteProgram(progID);
glDeleteShader(vertID);
glDeleteShader(fragID);

Input and Output of a Vertex Shader

Input and Output of a Fragment Shader

GLSL Syntax

  • The syntax of GLSL is very similar to C (with some elements of C++)
  • Data types for floating point numbers: float, vec2, vec3, vec4, mat2, mat3, mat4
  • Integer (signed + unsigned): int, ivec2, ivec3, ivec4, uint, uvec2, uvec3, uvec4
  • Boolean data types: bool, bvec2, bvec3, bvec4
  • Sampler data types for accessing textures: sampler2D, sampler3D, samplerCube, …
  • Struct statements (as known from C) are permitted:
struct Vertex {
    float val1;
    int val2;
    bool vale;
};
  • Arrays (as known from C) are also possible:
float a[16];
  • Data types must be explicitly converted
float a = float(1);
  • Much as in C:
    • Conditions: if, if( ) else
    • Loops: for, while, do { } while ( )
    • Termination: return, break, continue
  • But not everything is allowed:
    • No pointers
    • No strings
    • No unsigned, byte, short, long types, no union
    • No switch( ) case

Construction and Usage of Vectors

// construction
vec2 a = vec2(1.0, 2.0);
vec3 b = vec3(1.0, -1.0, 1.0);
vec4 c = vec4(1.0, 2.0, 3.0, 4.0);
vec4 allOne = vec4(1.0);
vec4 d = vec4(a, a);
//accessing elements
float f= b[1];	//is -1.0
float g = b.y;	//is -1.0 as well
//conversion
d = c.rgba;	//stays a 4-vector
b = c.xyz;	//convert to 3-vector
a = c.xy;	//convert to 2-vector
e = c.xxyy;	//e = (1.0, 1.0, 2.0, 2.0) "swizzling"
e.xz = vec2(0.5, 0.6);	//e=(0.5, 1.0, 0.6, 2.0)

Construction and Usage of Matrices

//construction
mat4 a = mat4(0.0, 0.1, 0.2, 0.3,	//column major object		
              0.4, 0.5, 0.6, 0.7,	//transposed of the
              0.8, 0.9, 1.0, 1.1,	//usual interpretation
              1.2, 1.3, 1.4, 1.5);
mat3 b = mat3(1.0);	//identity matrix
//conversion
mat3 c = mat3(a);	//upper 3x3 matrix
vec4 d = a[1];	//d = (0.4, 0.5, 0.6, 0.7)
//operations
vec4 e = a * vec4(1.0);
mat4 f = 2.0 * a - mat4(1.0);

Example: A First Triangle with GLSL

Example: A First Triangle with GLSL (Shader Code)

  • Vertex Shader
#version 140
in vec3 inputPosition;
in vec4 inputColor;
out vec3 forFragColor;
void main() {
    forFragColor = inputColor.rgb;
    gl_Position = vec4(inputPosition, 1.0);
}
  • Fragment Shader
#version 140
in vec3 forFragColor;
out vec4 outputColor;
void main() {
    outputColor = vec4(forFragColor, 1.0);
}

댓글