shaders

package module
v0.0.0-...-63defff Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2023 License: MIT Imports: 7 Imported by: 0

README

shaders

I'm learning shaders, why don't you too?

Hello Magenta World

The following "shader" is a program that sets the whole viewport to magenta. So you get a magenta screen.

void main() {
    gl_FragColor = vec4(0.7,0.0,1.0,1.0);
}

It is a fragment shader. This means it is executed for every fragment (usually a single pixel) in the data pipeline (which may or may not be all of the screens pixels). gl_FragColor is an output variable to the function. We set it to the pixel's desired color. We may get the pixel's coordinates on screen with gl_FragCoord (vec4)

Below is the same program but with a bunch of other GLSL language constructs to get familiar:

// Data detailed here is for OpenGL.
// This is an example of a fragment shader.
// It sets a certain pixel to a solid color via the
// pixel-specific variable: gl_FragColor

#ifdef GL_ES
// We can choose the global precision of the shader
// Also, don't end comments before precision call with "precision".
precision lowp float;
#endif

// Uniforms are defined at the top of shader
// After assigning the default floating poing precision
// Uniforms are inputs which are equal for all
// threads and necessarily set to read only.
// Time in seconds since shader loaded.
uniform float u_time;
// Mouse position in screen pixels.
uniform vec2 u_mouse;

// Canvas size (width, height).
uniform vec2 u_resolution;

// These uniform variables are the same as the above
// but are integers (????).

// Time in seconds since load
// Viewport resolution in pixels.
uniform vec3 iResolution; 

// Mouse pixel coordinates. xy:current. zw:click
uniform vec4 iMouse;

// Shader playback time in seconds.
uniform float iTime;

// You can define macros. 
// They need not be limited to numbers. 
// Notice this line has no semicolon!
// The define label is `pi` and everything that follows
// after the space is what is replaced whenever
// the label is encountered in the code.
#define pi 3.1415926535

// You also may not initialize uniform variables:
// uniform float v=1.0; // ERROR: cannot initialize this type of qualifier

// GPU available hardware accelerated functions.
// sin(x), cos(x), tan(x), asin(x), 
// acos(x), atan(x, [y]), pow(x,y), exp(x), 
// log(x), sqrt(), abs(), sign(), 
// floor(), ceil(), fract(), mod(), 
// min(), max() and clamp().
// rand(x)

void main() {
    gl_FragColor = vec4(pi/4.,0.0,1.0,1.0);
    // Code below: EPILEPSY WARNING
    // float frequency = u_mouse.y/60.0;
    // float t = u_time;
    // gl_FragColor = vec4(abs(sin(pi*frequency*t)),0.0,0.0,1.0);
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompileBasic

func CompileBasic(vertexSrcCode, fragmentSrcCode string) (program uint32, err error)

CompileBasic compiles two OpenGL vertex and fragment shaders and returns a program with the current OpenGL context. It returns an error if compilation, linking or validation fails.

func ParseCombinedBasic

func ParseCombinedBasic(r io.Reader) (vertexSrc, fragSrc string, err error)

ParseCombinedBasic parses a file with vertex and fragment #shader pragmas.

https://www.youtube.com/watch?v=2pv0Fbo-7ms&list=PLlrATfBNZ98foTJPJ_Ev03o2oq3-GGOS2&index=9&t=724s&ab_channel=TheCherno

Types

This section is empty.

Directories

Path Synopsis
examples
001-hellocolortriangle
Renders a textured spinning cube using GLFW 3 and OpenGL 4.1 core forward-compatible profile.
Renders a textured spinning cube using GLFW 3 and OpenGL 4.1 core forward-compatible profile.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL