-
Notifications
You must be signed in to change notification settings - Fork 143
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
Changes from all commits
7c72663
a22c42a
43c8366
d3ab690
7f55601
9ddb18e
abe582e
7a38d5e
6cdd5fb
be87051
6d84a0f
cf84759
b707a9e
805add6
6ae2843
a2b62d1
63a9456
281eb7d
587ef83
f986ade
0a67f09
e016b8e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> | ||
<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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A while back I changed the helper function so |
||
#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([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I renamed this to |
||
-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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the extra calls to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,4 @@ | |
onload(img); | ||
}; | ||
}; | ||
})(); | ||
})(); |
There was a problem hiding this comment.
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."