Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sampler filter #7

Merged
merged 22 commits into from
Dec 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7c72663
[sampler_filter] - WIP for sampler filter exmample
Dec 24, 2015
a22c42a
Merge remote-tracking branch 'origin/dev' into tle/sampler_filter
Dec 24, 2015
43c8366
[sampler_filter] - WIP uses style.css instead
Dec 24, 2015
d3ab690
[sampler_filter] - WIP cleanup. Split viewports into 4 quads. Replace…
Dec 26, 2015
7f55601
[sampler_filter] - Should have only binded samplers to texture unit 0…
Dec 26, 2015
9ddb18e
[sampler_filter] - Updated to finalized for pull request
Dec 30, 2015
abe582e
[sampler_filter] - Remove sampler_wrap.html for this pull request
Dec 30, 2015
7a38d5e
[sampler_filter] - Use Shrek's custom image
Dec 30, 2015
6cdd5fb
[sampler_filter] - minor fixes for consistent styling
Dec 30, 2015
be87051
[sampler_filter] - Link to correct reference
Dec 30, 2015
6d84a0f
[sampler_filter] - Fixed camel case in frag shader
Dec 30, 2015
cf84759
[sampler_filter] - Fixed camelCase in shader's variable name
Dec 30, 2015
b707a9e
[sampler_filter] - Merge with master (JSHint integration)
Dec 30, 2015
805add6
[sampler_filter] - Fixed jshint warnings
Dec 30, 2015
6ae2843
[sampler_filter] - Remove redundant comment
Dec 30, 2015
a2b62d1
[sampler_filter] - correctly check webgl context loading
Dec 30, 2015
63a9456
[sampler_filter] - Update README.md with samples status for OSX
Dec 30, 2015
281eb7d
[sampler_filter] - Renamed uv to st. Minor fixed on gl.clearColor
Dec 30, 2015
587ef83
[sampler_filter] - Merged with master
Dec 30, 2015
f986ade
Style tweaks
pjcozzi Dec 30, 2015
0a67f09
Better whitespace
pjcozzi Dec 30, 2015
e016b8e
[sampler_filter] - very minor tweak for styling
Dec 30, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ MIT license. See [LICENSE.md](LICENSE.md).

## Samples

| | Chrome 49 Windows | Firefox 42 Windows|
|--------------|:-----------------:|:-----------------:|
|[draw_instanced](http://webglsamples.org/WebGL2Samples/#draw_instanced)|:white_check_mark: | :x: crash |
|[glsl_discard](http://webglsamples.org/WebGL2Samples/#glsl_discard) |:white_check_mark: | :white_check_mark:|
|[query_occlusion](http://webglsamples.org/WebGL2Samples/#query_occlusion)|:white_check_mark:| :white_check_mark:|
| | Chrome 49 Windows | 49 OSX 10.10| Firefox 42 Windows| Firefox 42 OSX 10.10|
|--------------|:-----------------:|:--------------------------------------:|:-----------------:|:-----------------------:|
|[draw_instanced](http://webglsamples.org/WebGL2Samples/#draw_instanced)|:white_check_mark: |:white_check_mark:| :x: crash|:white_check_mark:|
|[glsl_discard](http://webglsamples.org/WebGL2Samples/#glsl_discard)|:white_check_mark: |:white_check_mark:| :white_check_mark:|:white_check_mark:|
|[query_occlusion](http://webglsamples.org/WebGL2Samples/#query_occlusion)|:white_check_mark:|:white_check_mark:| :white_check_mark:|:white_check_mark:|
|[sampler_filter](http://webglsamples.org/WebGL2Samples/#sampler_filter)|:x:not tested|:white_check_mark:| :x: not tested|:white_check_mark:|

## Running the Samples Locally

Expand Down
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ <h1>WebGL 2.0 Samples</h1>
"Instancing": [
"draw_instanced"
],
"Sampler": [
"sampler_filter"
],
"GLSL": [
"glsl_discard"
],
Expand Down
264 changes: 264 additions & 0 deletions samples/sampler_filter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
<!DOCTYPE html>
<!-- Ported from the OpenGL Samples Pack https://github.com/g-truc/ogl-samples/blob/master/tests/gl-330-sampler-filter.cpp -->
<html lang="en">

<head>
<title>WebGL 2 Samples - sampler_filter</title>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always use "WebGL", not "webgl."

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" href="style.css">
<script src="utility.js"></script>
</head>

<body>
<div id="info">WebGL 2 Samples - sampler_filter</div>

<script id="vs" type="x-shader/x-vertex">
#version 300 es
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A while back I changed the helper function so #version can be on a new line like this instead of on the same line as the script tag.

#define POSITION_LOCATION 0
#define TEXCOORD_LOCATION 4

precision highp float;
precision highp int;

uniform mat4 mvp;

layout(location = POSITION_LOCATION) in vec2 position;
layout(location = TEXCOORD_LOCATION) in vec2 textureCoordinates;

out vec2 st;

void main()
{
st = textureCoordinates;
gl_Position = mvp * vec4(position, 0.0, 1.0) ;
}
</script>

<script id="fs" type="x-shader/x-fragment">
#version 300 es
#define FRAG_COLOR_LOCATION 0

precision highp float;
precision highp int;

uniform sampler2D diffuse;

in vec2 st;

layout(location = FRAG_COLOR_LOCATION) out vec4 color;

void main()
{
color = texture(diffuse, st);
}
</script>

<script>
(function() {
'use strict';

var canvas = document.createElement('canvas');
canvas.width = Math.min(window.innerWidth, window.innerHeight);
canvas.height = canvas.width;
document.body.appendChild(canvas);

var gl = canvas.getContext( 'webgl2', { antialias: false } );
if (!gl) {
gl = canvas.getContext( 'experimental-webgl2', { antialias: false } );
}
var isWebGL2 = !!gl;
if(!isWebGL2)
{
document.getElementById('info').innerHTML = 'WebGL 2 is not available. See <a href="https://www.khronos.org/webgl/wiki/Getting_a_WebGL_Implementation">How to get a WebGL 2 implementation</a>';
return;
}

// -- Divide viewport

var windowSize = {
x: canvas.width,
y: canvas.height
};

var Corners = {
TOP_LEFT: 0,
TOP_RIGHT: 1,
BOTTOM_RIGHT: 2,
BOTTOM_LEFT: 3,
MAX: 4
};

var viewport = new Array(Corners.MAX);

viewport[Corners.TOP_LEFT] = {
x: 0,
y: 0,
z: windowSize.x / 2,
w: windowSize.y / 2
};

viewport[Corners.TOP_RIGHT] = {
x: windowSize.x / 2,
y: 0,
z: windowSize.x / 2,
w: windowSize.y / 2
};

viewport[Corners.BOTTOM_RIGHT] = {
x: windowSize.x / 2,
y: windowSize.y / 2,
z: windowSize.x / 2,
w: windowSize.y / 2
};

viewport[Corners.BOTTOM_LEFT] = {
x: 0,
y: windowSize.y / 2,
z: windowSize.x / 2,
w: windowSize.y / 2
};

// -- Initialize program

var program = createProgram(gl, getShaderSource('vs'), getShaderSource('fs'));

var uniformMvpLocation = gl.getUniformLocation(program,"mvp");
var uniformDiffuseLocation = gl.getUniformLocation(program,"diffuse");

// -- Initialize buffer

var positions = new Float32Array([
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed this to positions because it is more precise than vertices. In this sample, each vertex has both a position and texture coordinates.

-1.0, -1.0,
1.0, -1.0,
1.0, 1.0,
1.0, 1.0,
-1.0, 1.0,
-1.0, -1.0
]);
var vertexPosBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBuffer);
gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);

var texcoords = new Float32Array([
0.0, 1.0,
1.0, 1.0,
1.0, 0.0,
1.0, 0.0,
0.0, 0.0,
0.0, 1.0
]);
var vertexTexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexTexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, texcoords, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);

// -- Initialize vertex array

var vertexArray = gl.createVertexArray();
gl.bindVertexArray(vertexArray);

var vertexPosLocation = 0; // set with GLSL layout qualifier
gl.enableVertexAttribArray(vertexPosLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBuffer);
gl.vertexAttribPointer(vertexPosLocation, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vertexPosLocation);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the extra calls to enableVertexAttribArray here and below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Thank you.

gl.bindBuffer(gl.ARRAY_BUFFER, null);

var vertexTexLocation = 4; // set with GLSL layout qualifier
gl.enableVertexAttribArray(vertexTexLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, vertexTexBuffer);
gl.vertexAttribPointer(vertexTexLocation, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vertexTexLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, null);

gl.bindVertexArray(null);

// -- Initialize samplers

var samplers = new Array(Corners.MAX);
for (var i = 0; i < Corners.MAX; ++i) {
samplers[i] = gl.createSampler();
gl.samplerParameteri(samplers[i], gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.samplerParameteri(samplers[i], gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.samplerParameteri(samplers[i], gl.TEXTURE_WRAP_R, gl.CLAMP_TO_EDGE);
}

// Min filter
gl.samplerParameteri(samplers[Corners.TOP_LEFT], gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.samplerParameteri(samplers[Corners.TOP_RIGHT], gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.samplerParameteri(samplers[Corners.BOTTOM_RIGHT], gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
gl.samplerParameteri(samplers[Corners.BOTTOM_LEFT], gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);

// Mag filter
gl.samplerParameteri(samplers[Corners.TOP_LEFT], gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.samplerParameteri(samplers[Corners.TOP_RIGHT], gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.samplerParameteri(samplers[Corners.BOTTOM_RIGHT], gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.samplerParameteri(samplers[Corners.BOTTOM_LEFT], gl.TEXTURE_MAG_FILTER, gl.LINEAR);

// -- Load texture then render

var imageUrl = '../assets/img/Di-3d.png';
var texture;
loadImage(imageUrl, function(image) {
texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(
gl.TEXTURE_2D,
0, // Level of details
gl.RGBA, // Format
gl.RGBA,
gl.UNSIGNED_BYTE, // Size of each channel
image
);
gl.generateMipmap(gl.TEXTURE_2D);

render();
});

function render() {
// Clear color buffer
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);

// Bind program
gl.useProgram(program);

var matrix = new Float32Array([
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
]);
gl.uniformMatrix4fv(uniformMvpLocation, false, matrix);
gl.uniform1i(uniformDiffuseLocation, 0);

gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.bindVertexArray(vertexArray);

// Bind samplers
for (var i = 0; i < Corners.MAX; ++i) {
gl.viewport(viewport[i].x, viewport[i].y, viewport[i].z, viewport[i].w);

gl.bindSampler(0, samplers[i]);
gl.drawArraysInstanced(gl.TRIANGLES, 0, 6, 1);
}

// Clean up
gl.deleteBuffer(vertexPosBuffer);
gl.deleteBuffer(vertexTexBuffer);
for (var j = 0; j < samplers.length; ++j) {
gl.deleteSampler(samplers[j]);
}
gl.deleteVertexArray(vertexArray);
gl.deleteTexture(texture);
gl.deleteProgram(program);
}
})();
</script>

</body>

</html>
2 changes: 1 addition & 1 deletion samples/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@
onload(img);
};
};
})();
})();