GLSL: Difference between revisions
No edit summary |
|||
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
OpenGL Shading Language (GLSL) is the shader language used in OpenGL and WebGL. | OpenGL Shading Language (GLSL) is the shader language used in OpenGL and WebGL. | ||
Note that there are multiple versions of OpenGL such as 3, 4, ES | |||
==Usage== | ==Usage== | ||
===Vertex Shader=== | |||
In the vertex shader, you position vertices by projecting them from object coordinates to camera coordinates. | |||
This is typically done by multiplying each vertex position, passed as an attribute, with the MVP matrices, passed as a uniform. | |||
Oftentimes you will also want to pass the UV coordinates to the fragment shader as a varying. | |||
===Fragment Shader=== | |||
In the fragment shader, you will typically sample a base color and apply lighting to your object. | |||
Sampling is typically done with <code>texture2D</code> which returns an interpolated and mipmaped color from your <code>sampler2D</code> texture. | |||
==Pipeline== | ==Pipeline== | ||
See [https://www.khronos.org/opengl/wiki/Rendering_Pipeline_Overview OpenGL Rendering Pipeline Overview] | |||
===Vertex Shader=== | ===Vertex Shader=== | ||
In the vertex shader, your inputs are a model matrix, view matrix, and projection matrix uniforms as well as one vertex position in local coordinates. | In the vertex shader, your inputs are a model matrix, view matrix, and projection matrix uniforms as well as one vertex position in local coordinates. | ||
Line 15: | Line 28: | ||
These values can be passed to the following stages as <code>varying</code>'s. | These values can be passed to the following stages as <code>varying</code>'s. | ||
<code>varying</code>'s are linearly interpolated using barycentric coordinates. | <code>varying</code>'s are linearly interpolated using barycentric coordinates. | ||
===Tesselation Shader=== | |||
This is an optional shader which can be used to dynamically upsample your triangles using subdivision. | |||
It typically runs once per triangle. | |||
===Geometry Shader=== | ===Geometry Shader=== | ||
This is an optional shader which can be used to generate or remove triangles. | |||
It typically runs once per point, line, or triangle. | |||
The outputs are typically new vertices and new triangles. | |||
===Fragment Shader=== | ===Fragment Shader=== | ||
This shader outputs pixel color values based on information stored in each triangle. | |||
It runs once per pixel. | |||
==Resources== | |||
===Reference cards=== | |||
* [https://www.khronos.org/files/opengl45-quick-reference-card.pdf OpenGL 4.5 Reference card] | |||
* [https://www.khronos.org/opengles/sdk/docs/reference_cards/OpenGL-ES-2_0-Reference-card.pdf OpenGL ES 2.0 Reference card] | |||
* [https://www.khronos.org/assets/uploads/developers/presentations/opengles31-quick-reference-card.pdf OpenGL ES 3.1 Reference card] | |||
* [https://www.khronos.org/files/webgl/webgl-reference-card-1_0.pdf WebGL 1.0 Reference card] | |||
* [https://www.khronos.org/files/webgl20-reference-guide.pdf WebGL 2.0 Reference card] | |||
[[Category:Programming languages]] | |||
[[Category:GPU Programming languages]] |