Skip to content

Commit 9136460

Browse files
committed
WebGL Utils
1 parent ddfeed8 commit 9136460

10 files changed

Lines changed: 144 additions & 0 deletions
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var Attribute = function (location, size, type, normalize, stride, offset)
2+
{
3+
this.location = location;
4+
this.size = size;
5+
this.type = type;
6+
this.normalize = normalize;
7+
this.stride = stride;
8+
this.offset = offset;
9+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var BindVertexArray = function (gl, vao)
2+
{
3+
var attributes = vao.attributes;
4+
gl.bindBuffer(gl.ARRAY_BUFFER, vao.buffer);
5+
for (var index = 0, length = attributes.length; index < length; ++index)
6+
{
7+
var attrib = attributes[index];
8+
var location = attrib.location;
9+
gl.enableVertexAttribArray(location);
10+
gl.vertexAttribPointer(
11+
location,
12+
attrib.size,
13+
attrib.type,
14+
attrib.normalized,
15+
attrib.stride,
16+
attrib.offset
17+
);
18+
}
19+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var CreateAttribArray = function (gl, program, attributeDescArray)
2+
{
3+
var attributes = [];
4+
for (var index = 0, length = attributeDescArray.length; index < length; ++index)
5+
{
6+
var desc = attributeDescArray[index];
7+
attributes.push(new WebGLPipeline.Attribute(
8+
gl.getAttribLocation(program, desc.name),
9+
desc.size,
10+
desc.type,
11+
desc.normalized ? gl.TRUE : gl.FALSE,
12+
desc.stride,
13+
desc.offset
14+
));
15+
}
16+
return attributes;
17+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var Attribute = require('./Attribute');
2+
3+
var CreateAttribDesc = function (gl, program, name, size, type, normalized, stride, offset)
4+
{
5+
return new Attribute(
6+
gl.getAttribLocation(program, name),
7+
size,
8+
type,
9+
normalized,
10+
stride,
11+
offset
12+
);
13+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var CreateBuffer = function (gl, bufferType, usage, bufferData, bufferSize)
2+
{
3+
var buffer = gl.createBuffer();
4+
gl.bindBuffer(bufferType, buffer);
5+
if (bufferData && ArrayBuffer.isView(bufferData))
6+
{
7+
gl.bufferData(bufferType, bufferData, usage);
8+
}
9+
else
10+
{
11+
gl.bufferData(bufferType, bufferSize, usage);
12+
}
13+
return buffer;
14+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var CreateProgram = function (gl, vertexShader, fragmentShader)
2+
{
3+
var program = gl.createProgram();
4+
gl.attachShader(program, vertexShader);
5+
gl.attachShader(program, fragmentShader);
6+
gl.linkProgram(program);
7+
gl.validateProgram(program);
8+
if (!gl.getProgramParameter(program, gl.LINK_STATUS))
9+
{
10+
console.error('Failed to link program. Error: \n' + gl.getProgramInfoLog(program));
11+
return null;
12+
}
13+
return program;
14+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var CreateShader = function (gl, shaderSource, shaderType)
2+
{
3+
var shader = null;
4+
shader = gl.createShader(shaderType);
5+
gl.shaderSource(shader, shaderSource);
6+
gl.compileShader(shader);
7+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
8+
{
9+
console.error('Failed ' + (shaderType === gl.VERTEX_SHADER ? 'vertex' : shaderType === gl.FRAGMENT_SHADER ? 'fragment' : 'invalid') + ' shader compilation. Error: \n' + gl.getShaderInfoLog(shader));
10+
return null;
11+
}
12+
return shader;
13+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var CreateTexture2DArrayBuffer = function (gl, width, height, border, filter, arrayBuffer)
2+
{
3+
var texture = gl.createTexture();
4+
gl.bindTexture(gl.TEXTURE_2D, texture);
5+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
6+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
7+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter);
8+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filter);
9+
gl.texImage2D(
10+
gl.TEXTURE_2D,
11+
0,
12+
gl.RGBA,
13+
width,
14+
height,
15+
border,
16+
gl.RGBA,
17+
gl.UNSIGNED_BYTE,
18+
arrayBuffer
19+
);
20+
return texture;
21+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var CreateTexture2DImage = function (gl, pixels, filter, mipLevels)
2+
{
3+
var texture = gl.createTexture();
4+
mipLevels = mipLevels || 0;
5+
gl.bindTexture(gl.TEXTURE_2D, texture);
6+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
7+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
8+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter);
9+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filter);
10+
gl.texImage2D(
11+
gl.TEXTURE_2D,
12+
mipLevels,
13+
gl.RGBA,
14+
gl.RGBA,
15+
gl.UNSIGNED_BYTE,
16+
pixels
17+
);
18+
return texture;
19+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var VertexArray = function (vbo, attributes)
2+
{
3+
this.buffer = vbo;
4+
this.attributes = attributes;
5+
};

0 commit comments

Comments
 (0)