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

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

by 임 낭 만 2023. 6. 17.

OpenGL Shading Language

Passing of Vertex Attributes

  • Per-Vertex attributes can be passed to a vertex shader using in variables
  • Distinctions can be made regarding special in variables, which are automatically generated, and user-defined in variables, which must be passed by the application
  • The vertex data is transferred to the graphics card via Vertex Buffer Objects (VBOs)
  • To use the data of VBOs for the user-defined in variables, the usual procedure will be as follows:
  • With glGetAttribLocation a location identifier for an in variable is queried. For example, when the variable in the shader code is called “inputColor”
int colorLoc = glGenAttribLocation(progID, "inputColor");
  • The function glVertexAttribPointer(colorLoc, size, type, normalized, stride, offset) then defines the mapping between an in variable and VBO attribute
  • Finally, the attribute needs to be activated by
glEnableVertexAttribArray(colorLoc);
  • In a subsequent call to glDrawArray or glDrawElements with activated VBO the in variable of the vertex shader is then filled with the attribute data for each drawn vertex


Data Input and Output in the Fragment Shader

  • User-defined out variables of the vertex shader are interpolated in the rasterizer and the interpolated values are supplied in variables to the fragment shader
  • The out variables of the fragment shader will be sent to the framebuffer
  • For example, if the out variable “outputColor” should be written into the first color buffer of the framebuffer, this can be achieved by
glBindFragDataLocation(progID, 0, "outputColor");


Uniform Variables

  • In addition to in variables, uniform variables are another way to pass data to a shader
  • In contrast to the in variables that contain the vertex attributes, the uniform variables include data that remains constant for all vertices during a call to glDrawArray or glDrawElements
  • The location identifier of a uniform variable can be queried by
paraLoc = glGenUniformLocation(progID, "parameter");
  • Afterwards, the uniform variable can be set with
glUniform1i(paraLoc, 123);
  • or, if the variable is a float-vector
glUniform3fv(threeVectorLoc, 1, threeVetor);
  • and in case of a matrix
glUniformMatrix4fv(modelviewLoc, 1, transpose, modelview);


Example: Passing the Transformation Matrices As Uniforms

  • In this example, the modelview matrix and the projection matrix are passed as uniform variables. The functionality of the GL_MODELVIEW and GL_PROJECTION matrices (which are known from the fixed-function pipeline) are simulated

  • Vertex Shader
#version 140
in vec3 inputPosition;
in vec4 inputColor;
uniform mat4 projection;
uniform mat4 modelview;
out vec3 forFragColor;

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

void main() {
    outputColor = vec4(forFragColor, 1.0);
}

댓글