shaders

package
v0.0.0-...-3cbae95 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2024 License: BSD-3-Clause, BSL-1.0 Imports: 3 Imported by: 0

README

Shaders

GoDoc Go Report Card

About

This package contains plain vertex and fragment shaders, version 1.30 (OpenGL 3.0), for Go. It is published on https://github.com/vbsw/shaders and https://gitlab.com/vbsw/shaders.

Copyright 2020, Vitali Baumtrok ([email protected]).

Shaders is distributed under the Boost Software License, version 1.0. (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)

Shaders is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Boost Software License for more details.

Examples

With this package:

import (
	"github.com/go-gl/gl/v3.3-core/gl"
	"gitlab.com/vbsw/shaders"
)

func main() {
	....
	shader := shaders.NewPrimitiveShader()
	shader.VertexShaderID = gl.CreateShader(gl.VERTEX_SHADER)
	gl.ShaderSource(shader.VertexShaderID, 1, shader.VertexShader, nil)
	....
}

Standard way:

import (
	"github.com/go-gl/gl/v3.3-core/gl"
)

func main() {
	....
	vertexShaderStr := "#version 130\nin vec3 vp;\nvoid main () {\n\tgl_Position = vec4(vp, 1.0);\n}"
	shaderID := gl.CreateShader(gl.VERTEX_SHADER)
	vertexShaderC, free := gl.Strs(vertexShaderStr + "\x00")
	gl.ShaderSource(shaderID, 1, vertexShaderC, nil)
	free()
	....
}

Initialize the projection matrix:

import (
	"github.com/go-gl/gl/v3.3-core/gl"
	"gitlab.com/vbsw/shaders"
)

func main() {
	....
	shader.ProjectionLocation = gl.GetUniformLocation(shader.ProgramID, shader.ProjectionUniform)
	gl.UseProgram(shader.ProgramID)
	setProjection(shader.ProjectionLocation, 400, 400)
	....
}

func setProjection(projectionLocation int32, width, height int) {
	projectionMatrix := make([]float32, 4*4)
	projectionMatrix[0] = 2.0 / float32(width)
	projectionMatrix[5] = 2.0 / float32(height)
	projectionMatrix[12] = -1.0
	projectionMatrix[13] = -1.0
	projectionMatrix[15] = 1.0
	gl.UniformMatrix4fv(projectionLocation, 1, false, &projectionMatrix[0]);
}

References

Documentation

Overview

Package shaders contains plain vertex and fragment shaders, version 1.30 (OpenGL 3.0).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewString

func NewString(cstr *uint8) string

NewString converts c-string to string and returns it. Data of c-string is not reallocated, but used in string.

Types

type Shader

type Shader struct {
	VertexShader       **uint8
	FragmentShader     **uint8
	VertexShaderID     uint32
	FragmentShaderID   uint32
	ProgramID          uint32
	PositionAttribute  *uint8
	ColorAttribute     *uint8
	CoordsAttribute    *uint8
	ProjectionUniform  *uint8
	ModelUniform       *uint8
	TextureUniform     *uint8
	PositionLocation   int32
	ColorLocation      int32
	CoordsLocation     int32
	ProjectionLocation int32
	ModelLocation      int32
	TextureLocation    int32
}

Shader is for storing shaders and their attributes.

func NewPrimitiveShader

func NewPrimitiveShader() *Shader

NewPrimitiveShader returns an instance of Shader for primitive types.

Vertex shader:

#version 130

in vec3 positionIn;
in vec4 colorIn;
out vec4 fragementColor;

uniform mat4 projection = mat4(1.0);
uniform mat4 model = mat4(1.0);

void main() {
	gl_Position = projection * model * vec4(positionIn, 1.0f);
	fragementColor = colorIn;
}

Fragment shader:

#version 130

in vec4 fragementColor;
out vec4 color;

void main () {
	color = fragementColor;
}

func NewTextureShader

func NewTextureShader() *Shader

NewTextureShader returns an instance of Shader for textures.

Vertex shader:

#version 130

in vec3 positionIn;
in vec2 textureCoordsIn;
out vec2 fragmentTextureCoords;

uniform mat4 projection = mat4(1.0);
uniform mat4 model = mat4(1.0);

void main() {
	gl_Position = projection * model * vec4(positionIn, 1.0f);
	fragmentTextureCoords = textureCoordsIn;
}

Fragment shader:

#version 130

in vec2 fragmentTextureCoords;
out vec4 color;

uniform sampler2D imageTexture;

void main () {
	color = texture(imageTexture, fragmentTextureCoords);
}

func (*Shader) FragmentShaderStr

func (shader *Shader) FragmentShaderStr() string

FragmentShaderStr returns fragment shader as string.

func (*Shader) VertexShaderStr

func (shader *Shader) VertexShaderStr() string

VertexShaderStr returns vertex shader as string.

Jump to

Keyboard shortcuts

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