Getting the true z value from the depth buffer

From http://web.archive.org/web/20130416194336/http://olivers.posterous.com/linear-depth-in-glsl-for-real // == Post-process frag shader =========================================== uniform sampler2D depthBuffTex; uniform float zNear; uniform float zFar; varying vec2 vTexCoord; void main(void) { float z_b = texture2D(depthBuffTex, vTexCoord).x; float z_n = 2.0 * z_b – 1.0; float z_e = 2.0 * zNear * zFar / (zFar + zNear – z_n * (zFar – zNear)); } … Read more

Do conditional statements slow down shaders?

What is it about shaders that even potentially makes if statements performance problems? It has to do with how shaders get executed and where GPUs get their massive computing performance from. Separate shader invocations are usually executed in parallel, executing the same instructions at the same time. They’re simply executing them on different sets of … Read more

What’s the origin of this GLSL rand() one-liner?

Very interesting question! I am trying to figure this out while typing the answer 🙂 First an easy way to play with it: http://www.wolframalpha.com/input/?i=plot%28+mod%28+sin%28x*12.9898+%2B+y*78.233%29+*+43758.5453%2C1%29x%3D0..2%2C+y%3D0..2%29 Then let’s think about what we are trying to do here: For two input coordinates x,y we return a “random number”. Now this is not a random number though. It’s the … Read more

What is the correct file extension for GLSL shaders? [closed]

There’s no official extension in the spec. OpenGL doesn’t handle loading shaders from files; you just pass in the shader code as a string, so there’s no specific file format. However, glslang, Khronos’ reference GLSL compiler/validator, uses the following extensions to determine what type of shader that the file is for: .vert – a vertex … Read more

What is state-of-the-art for text rendering in OpenGL as of version 4.1? [closed]

Rendering outlines, unless you render only a dozen characters total, remains a “no go” due to the number of vertices needed per character to approximate curvature. Though there have been approaches to evaluate bezier curves in the pixel shader instead, these suffer from not being easily antialiased, which is trivial using a distance-map-textured quad, and … Read more

Random / noise functions for GLSL

For very simple pseudorandom-looking stuff, I use this oneliner that I found on the internet somewhere: float rand(vec2 co){ return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453); } You can also generate a noise texture using whatever PRNG you like, then upload this in the normal fashion and sample the values in your shader; I can dig … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)