Skip to content

Instantly share code, notes, and snippets.

@JakubNei
Last active June 22, 2017 19:17
Show Gist options
  • Save JakubNei/0c0e36ef4bb9dd24d1ad2856cb67c6f9 to your computer and use it in GitHub Desktop.
Save JakubNei/0c0e36ef4bb9dd24d1ad2856cb67c6f9 to your computer and use it in GitHub Desktop.
OpenGL

OpenTK suited tutorial, OpenTK allows you to use OpenGL in C#

good explanation videos

good explanation slides

"If you can’t explain it simply, you don’t understand it well enough." - Albert Einstein

"Laziness can be the downfall of the best of us." - Always Google

What is OpenGL ?

  • "Standartized API for rendering 2D and 3D vector graphic." https://en.wikipedia.org/wiki/OpenGL
  • API = a set of functions you use to render vector graphic
  • Standartized = same API and behaves the same on any platform in any programing language
  • CPU tells what to render -> GPU renders it
  • vendor = GPU manufacturer = NVidia, ATI, ...
  • different vendor = different GPU architecture = different way of inner workings,
  • each vendor implements the same set of functions (OpenGL API), inside the implmenetation each does something different but end result is the same (rendered vector graphics)
  • the OpenGL API implementation is inside vendor drivers, most systems come preinstalled with (outdated?) drivers

What is Shading Language

  • introduced in OpenGL 1.5 (2003)
  • OpenGL Shading Language (glsl) is programing language used to describe how to adjust parts of rendering pipeline
  • before OpenGL 1.5 there was only fixed pipeline, you had little control over the final image
  • is similar to C, has many inbuilt functions and types
  • shader = our own custom part of rendering pipeline written in shading language
  • rendering pipeline has several stages (points) which you can replace with your own shaders
  • each stage has some inbuilt variables

Fragment shader, Vertex shader

How does one render triangle on screen ?

Simplified rendering pipeline

  • lets render 3 points that make up triangle
  • vertex shader -> adjust vertices (or other data)
  • optimization: clipping, if none are visible dont continue
  • rasterization -> turn triangle into set of fragments (pixels)
  • fragment shader -> adjust fragments
  • show fragments on screen

Example Shaders

vertex shader that adds 5 units on X axis to every vertex

vec3 vertex_main(vec3 in)
{
  return in + vec3(5,0,0);
}

fragment shader that outputs color based on screen position

vec3 fragment_main()
{
 return vec3(gl_FragCoord.x, gl_FragCoord.y, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment