shaders

package
v0.0.0-...-e938fac Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

GLSL types

OpenGL shader and program compilation utils

Index

Constants

View Source
const (
	SHIDColorSpriteShader = iota
	SHIDFancySpriteShader
)

Variables

View Source
var CommonAttributes = []string{"vertex", "uv", "color", "translation", "rotation", "scale"}

Common attributes expected to be in all shaders and in this order. The order should match ShaderAttribute.Location. Other (non-common) attributes must come after.

View Source
var DefaultFragment = `#version 410

uniform sampler2D tex;

in vec2 texCoords;
in vec4 vertColor;
out vec4 frag_colour;

void main() {
    vec4 texel = texture(tex, texCoords);
    if(texel.a < 0.5) discard;
    frag_colour = texel * vertColor ;
} 
 ` + "\x00"
View Source
var DefaultVertex = `#version 410

layout (location=0) in vec3 vertex; // vertex position
layout (location=1) in vec4 uv; // per-vertex texture co-ords
layout (location=2) in vec4 color; 
layout (location=3) in vec3 translation;
layout (location=4) in vec3 rotation;
layout (location=5) in vec2 scale;

uniform mat4 projection;
uniform mat4 view;

out vec2 texCoords;
out vec4 vertColor;


void main() {
    texCoords.x = -(vertex.x-0.5)*uv.x  + (vertex.x+0.5)*uv.z;
    texCoords.y = -(vertex.y-0.5)*uv.y  + (vertex.y+0.5)*uv.w;

    mat4 scale_mat = mat4(1.0);
    scale_mat[0][0] = scale.x;
    scale_mat[1][1] = scale.y;

    mat4 rotate_mat_z = mat4(1.0);
    float c = cos(rotation.z);
    float s = sin(rotation.z);
    rotate_mat_z[0][0] = c;
    rotate_mat_z[1][1] = c;
    rotate_mat_z[0][1] = s;
    rotate_mat_z[1][0] = -s;

    mat4 rotate_mat_y = mat4(1.0);
    c = cos(rotation.y);
    s = sin(rotation.y);
    rotate_mat_y[0][0] = c;
    rotate_mat_y[2][2] = c;
    rotate_mat_y[0][2] = -s;
    rotate_mat_y[2][0] = s;

    mat4 rotate_mat_x = mat4(1.0);
    c = cos(rotation.x);
    s = sin(rotation.x);
    rotate_mat_x[1][1] = c;
    rotate_mat_x[2][2] = c;
    rotate_mat_x[1][2] = s;
    rotate_mat_x[2][1] = -s;
    
    mat4 translate_mat = mat4(1.0);
    translate_mat[3] = vec4(translation, 1);

    mat4 model = translate_mat * rotate_mat_x * rotate_mat_y * rotate_mat_z * scale_mat;

    gl_Position =  projection * view  * model * vec4(vertex, 1.0);
    
    vertColor = color;
}




 ` + "\x00"

Functions

This section is empty.

Types

type GLSLType

type GLSLType struct {
	Name string
	Size int32 // number of float32s
}

GLSLType stores the name and size of a GLSL variable

type Shader

type Shader struct {
	Id               int
	Vertex, Fragment string // shader source
	Program          uint32 // opengl program

	// shader parameters - name must match the shader source
	Attributes map[string]ShaderAttribute // named access to shader attributes
	Uniforms   map[string]GLSLType        // named access to shader unifrom parameters
}

Shader stores a GLSL vertex and fragment shader pair and provides easy access to shader attributes

func NewDefaultShader

func NewDefaultShader() (Shader, error)

func NewShader

func NewShader(vertexShader, fragmentShader string, attributes map[string]ShaderAttribute,
	uniforms map[string]GLSLType, id int) (Shader, error)

NewShader creates a Shader with given sources and attributes. A simple sanity check is performed which checks that the attribute name and type are present in the shader source. TODO: check that order of common sprite attributes matches CommonAttributes

func NewShaderFromFiles

func NewShaderFromFiles(vertexShaderFilename, fragmentShaderFilename string,
	attributes map[string]ShaderAttribute, uniforms map[string]GLSLType, id int) (Shader, error)

Same as NewShader but shaders are loaded from files

func (Shader) AttributeData

func (s Shader) AttributeData() map[string]ShaderAttribute

Allocates the storage needed for this shader's attributes

func (*Shader) UpdateUniform

func (s *Shader) UpdateUniform(name string, value []float32) error

UpdateUniform changes the value of a uniform variable in the shader. It handles matrices and vectors of type GLSLType (float vectors and matrices)

type ShaderAttribute

type ShaderAttribute struct {
	Name     string    // Attribute name as it appears in the shader source
	Location uint32    // OpenGL attribute index - assigned with EnableVertexAttribArray
	Type     GLSLType  // GLSL type (vec2, vec3 etc)
	Default  []float32 // default value for this attribute
	Data     []float32 // data of this attribute
}

Stores the location index and type of an attribute in the shader

func (ShaderAttribute) Copy

Jump to

Keyboard shortcuts

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