make window use a shader

This commit is contained in:
faiface 2016-12-02 18:17:40 +01:00
parent f2460b34da
commit 237e2d54cd
1 changed files with 21 additions and 13 deletions

View File

@ -54,7 +54,7 @@ type WindowConfig struct {
type Window struct {
window *glfw.Window
config WindowConfig
defaultShader *pixelgl.Shader
contextHolder pixelgl.ContextHolder
// need to save these to correctly restore a fullscreen window
restore struct {
@ -104,12 +104,14 @@ func NewWindow(config WindowConfig) (*Window, error) {
w.SetFullscreen(config.Fullscreen)
w.defaultShader, err = pixelgl.NewShader(w, defaultUniformFormat, defaultVertexShader, defaultFragmentShader)
defaultShader, err := pixelgl.NewShader(w, defaultVertexFormat, defaultUniformFormat, defaultVertexShader, defaultFragmentShader)
if err != nil {
w.Delete()
return nil, errors.Wrap(err, "creating window failed")
}
w.contextHolder.Context = w.contextHolder.Context.WithShader(defaultShader)
return w, nil
}
@ -309,32 +311,39 @@ func (w *Window) Do(sub func(pixelgl.Context)) {
currentWindow.handler = w
}
sub(pixelgl.Context{})
w.contextHolder.Do(sub)
}
var defaultVertexFormat = pixelgl.VertexFormat{
"position": {Purpose: pixelgl.Position, Type: pixelgl.Vec2},
"color": {Purpose: pixelgl.Color, Type: pixelgl.Vec4},
"texCoord": {Purpose: pixelgl.TexCoord, Type: pixelgl.Vec2},
"isTexture": {Purpose: pixelgl.IsTexture, Type: pixelgl.Int},
}
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;
in vec2 position;
in vec4 color;
in vec2 texCoord;
in int isTexture;
out vec4 Color;
out vec2 TexCoord;
out int IsTexture;
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);
gl_Position = vec4((transform * vec3(position.x, position.y, 1.0)).xy, 0.0, 1.0);
Color = color;
TexCoord = texCoord;
IsTexture = isTexture;
}
`
@ -343,15 +352,14 @@ var defaultFragmentShader = `
in vec4 Color;
in vec2 TexCoord;
in int IsTexture;
out vec4 color;
uniform int isTexture;
uniform sampler2D tex;
void main() {
if (isTexture != 0) {
if (IsTexture != 0) {
color = Color * texture(tex, vec2(TexCoord.x, 1 - TexCoord.y));
} else {
color = Color;