-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSpriteRender.js
115 lines (90 loc) · 3.8 KB
/
SpriteRender.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
This file contains all the code needed to render a .spr file (Version 2)
by converting the sprite into a texture.
**/
define(["GameInfo"], function(GameInfo) {
var fragmentShader =
" precision mediump float;" +
" varying vec2 vTexCoord;" +
" uniform sampler2D uSampler;" +
" void main(void) {" +
" gl_FragColor = texture2D(uSampler, vec2(vTexCoord.s, vTexCoord.t));" +
" }";
var vertexShader =
" attribute vec3 aVertexPosition;" +
" attribute vec2 aTexCoord;" +
" varying vec2 vTexCoord;" +
" uniform mat4 uMVMatrix;" +
" uniform mat4 uPMatrix;" +
" void main(void) {" +
" gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);" +
" vTexCoord = aTexCoord;" +
" }";
return function(gl, sprite) {
function getShader(gl, shaderCode, shaderType) {
var shader = gl.createShader(shaderType);
gl.shaderSource(shader, shaderCode);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
var shaderProgram = (function() {
var sFragmentShader = getShader(gl, fragmentShader, gl.FRAGMENT_SHADER);
var sVertexShader = getShader(gl, vertexShader, gl.VERTEX_SHADER);
var program = gl.createProgram();
gl.attachShader(program, sVertexShader);
gl.attachShader(program, sFragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
alert("Could not initialise shaders");
}
gl.useProgram(program);
program.vertexPositionAttribute = gl.getAttribLocation(program, "aVertexPosition");
program.texCoordAttribute = gl.getAttribLocation(program, "aTexCoord");
program.pMatrixUniform = gl.getUniformLocation(program, "uPMatrix");
program.mvMatrixUniform = gl.getUniformLocation(program, "uMVMatrix");
program.samplerUniform = gl.getUniformLocation(program, "uSampler");
return program;
})();
var buffer = gl.createBuffer();
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, sprite.frames[0].width, sprite.frames[0].height, 0, gl.RGBA, gl.UNSIGNED_BYTE, sprite.frames[0].imageData);
gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameterf(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
//Subsprites are not guaranteed to be of size 2^n for some n,
//so we disable this restriction
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
this.render = function() {
gl.useProgram(shaderProgram);
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
gl.enableVertexAttribArray(shaderProgram.texCoordAttribute);
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
gl.disable(gl.DEPTH_TEST);
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
var vertices = [
1.0, 1.0, 0.0, 1.0, 0.0,
-1.0, 1.0, 0.0, 0.0, 0.0,
1.0, -1.0, 0.0, 1.0, 1.0,
-1.0, -1.0, 0.0, 0.0, 1.0
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.uniform1i(shaderProgram.samplerUniform, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, 3, gl.FLOAT, false, 20, 0);
gl.vertexAttribPointer(shaderProgram.texCoordAttribute, 2, gl.FLOAT, false, 20, 12);
//mat4.scale(cs.mvMatrix, cs.mvMatrix, [1/sprite.header.maxWidth, 1/sprite.header.maxHeight, 1]);
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, GameInfo.pMatrix);
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, GameInfo.mvMatrix);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
gl.enable(gl.DEPTH_TEST);
gl.disable(gl.BLEND);
};
};
});