From 519c9c1c19a1a526e6140d63e44ce77bf9171e7d Mon Sep 17 00:00:00 2001 From: faiface Date: Fri, 2 Dec 2016 01:20:54 +0100 Subject: [PATCH] add default shader --- pixelgl/attr.go | 2 ++ window.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/pixelgl/attr.go b/pixelgl/attr.go index 5c42cb9..2f6d9af 100644 --- a/pixelgl/attr.go +++ b/pixelgl/attr.go @@ -20,6 +20,8 @@ const ( Transform // Camera is a camera view matrix Camera + // IsTexture signals, whether a texture is present. + IsTexture // NumStandardAttrPurposes is the number of standard attribute purposes NumStandardAttrPurposes ) diff --git a/window.go b/window.go index 6b5c85a..9caba6f 100644 --- a/window.go +++ b/window.go @@ -52,8 +52,9 @@ type WindowConfig struct { // Window is a window handler. Use this type to manipulate a window (input, drawing, ...). type Window struct { - window *glfw.Window - config WindowConfig + window *glfw.Window + config WindowConfig + defaultShader *pixelgl.Shader // need to save these to correctly restore a fullscreen window restore struct { @@ -103,6 +104,12 @@ func NewWindow(config WindowConfig) (*Window, error) { w.SetFullscreen(config.Fullscreen) + w.defaultShader, err = pixelgl.NewShader(w, defaultUniformFormat, defaultVertexShader, defaultFragmentShader) + if err != nil { + w.Delete() + return nil, errors.Wrap(err, "creating window failed") + } + return w, nil } @@ -304,3 +311,50 @@ func (w *Window) Do(sub func(pixelgl.Context)) { sub(pixelgl.Context{}) } + +var defaultUniformFormat = pixelgl.UniformFormat{ + "camera": {Purpose: pixelgl.Camera, Type: pixelgl.Mat3}, + "transform": {Purpose: pixelgl.Transform, Type: pixelgl.Mat3}, + "isTexture": {Purpose: pixelgl.IsTexture, Type: pixelgl.Int}, +} + +var defaultVertexShader = ` +#version 330 core + +layout (location = 0) in vec2 position; +layout (location = 1) in vec4 color; +layout (location = 2) in vec2 texCoord; + +out vec4 Color; +out vec2 TexCoord; + +uniform mat3 camera; +uniform mat3 transform; + +void main() { + gl_Position = vec4((camera * transform * vec3(position.x, position.y, 1.0)).xy, 0.0, 1.0); + Color = color; + TexCoord = texCoord; +} +` + +var defaultFragmentShader = ` +#version 330 core + +in vec4 Color; +in vec2 TexCoord; + +out vec4 color; + +uniform int isTexture; + +uniform sampler2D tex; + +void main() { + if (isTexture != 0) { + color = Color * texture(tex, vec2(TexCoord.x, 1 - TexCoord.y)); + } else { + color = Color; + } +} +`