2014-12-24 03:04:10 +01:00
|
|
|
// Copyright 2014 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-12-09 15:16:04 +01:00
|
|
|
|
2015-01-26 15:32:50 +01:00
|
|
|
package graphics
|
2013-06-15 10:07:14 +02:00
|
|
|
|
|
|
|
import (
|
2015-01-02 15:30:22 +01:00
|
|
|
"strings"
|
2016-06-22 05:37:26 +02:00
|
|
|
|
2016-11-03 15:31:25 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/opengl"
|
2013-06-15 10:07:14 +02:00
|
|
|
)
|
|
|
|
|
2013-12-12 16:31:00 +01:00
|
|
|
type shaderId int
|
|
|
|
|
|
|
|
const (
|
2015-01-17 08:09:09 +01:00
|
|
|
shaderVertexModelview shaderId = iota
|
2015-01-12 10:34:02 +01:00
|
|
|
shaderFragmentTexture
|
2013-12-12 16:31:00 +01:00
|
|
|
)
|
|
|
|
|
2015-01-02 15:30:22 +01:00
|
|
|
func shader(c *opengl.Context, id shaderId) string {
|
|
|
|
str := shaders[id]
|
|
|
|
if !c.GlslHighpSupported() {
|
|
|
|
str = strings.Replace(str, "highp ", "", -1)
|
|
|
|
str = strings.Replace(str, "lowp ", "", -1)
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
var shaders = map[shaderId]string{
|
2015-01-17 08:09:09 +01:00
|
|
|
shaderVertexModelview: `
|
2015-01-02 15:30:22 +01:00
|
|
|
uniform highp mat4 projection_matrix;
|
|
|
|
attribute highp vec2 vertex;
|
|
|
|
attribute highp vec2 tex_coord;
|
2016-10-24 20:04:06 +02:00
|
|
|
attribute highp vec4 geo_matrix_body;
|
2016-10-24 18:56:59 +02:00
|
|
|
attribute highp vec2 geo_matrix_translation;
|
2015-01-02 15:30:22 +01:00
|
|
|
varying highp vec2 vertex_out_tex_coord;
|
2013-06-15 10:07:14 +02:00
|
|
|
|
|
|
|
void main(void) {
|
2014-12-22 13:51:58 +01:00
|
|
|
vertex_out_tex_coord = tex_coord;
|
2016-10-24 20:04:06 +02:00
|
|
|
mat4 geo_matrix = mat4(
|
|
|
|
vec4(geo_matrix_body[0], geo_matrix_body[2], 0, 0),
|
|
|
|
vec4(geo_matrix_body[1], geo_matrix_body[3], 0, 0),
|
2016-10-24 18:56:59 +02:00
|
|
|
vec4(0, 0, 1, 0),
|
|
|
|
vec4(geo_matrix_translation, 0, 1)
|
|
|
|
);
|
2016-10-24 20:04:06 +02:00
|
|
|
gl_Position = projection_matrix * geo_matrix * vec4(vertex, 0, 1);
|
2014-12-20 10:07:25 +01:00
|
|
|
}
|
2013-06-15 10:07:14 +02:00
|
|
|
`,
|
2015-01-12 10:34:02 +01:00
|
|
|
shaderFragmentTexture: `
|
2015-01-02 15:30:22 +01:00
|
|
|
uniform lowp sampler2D texture;
|
|
|
|
uniform lowp mat4 color_matrix;
|
|
|
|
uniform lowp vec4 color_matrix_translation;
|
|
|
|
varying highp vec2 vertex_out_tex_coord;
|
2013-06-15 10:07:14 +02:00
|
|
|
|
|
|
|
void main(void) {
|
2015-01-02 15:30:22 +01:00
|
|
|
lowp vec4 color = texture2D(texture, vertex_out_tex_coord);
|
2014-12-19 21:22:10 +01:00
|
|
|
|
2016-06-22 17:25:31 +02:00
|
|
|
// Un-premultiply alpha
|
2016-06-26 08:57:45 +02:00
|
|
|
if (0.0 < color.a) {
|
|
|
|
color.rgb /= color.a;
|
|
|
|
}
|
2016-06-22 17:25:31 +02:00
|
|
|
// Apply the color matrix
|
|
|
|
color = (color_matrix * color) + color_matrix_translation;
|
|
|
|
color = clamp(color, 0.0, 1.0);
|
|
|
|
// Premultiply alpha
|
|
|
|
color.rgb *= color.a;
|
2014-12-20 10:07:25 +01:00
|
|
|
|
2014-12-22 14:49:14 +01:00
|
|
|
gl_FragColor = color;
|
2014-12-20 10:07:25 +01:00
|
|
|
}
|
2013-12-12 16:31:00 +01:00
|
|
|
`,
|
|
|
|
}
|