especially @TommyXXX, @ibilon
I’m very sorry to bother you again but I need a little bit of help (a lot easier now) on a different but similar problem.
My objective now is scaling the bitmapData I’ve AntiAliased before with openfl shaders
(I am using the Ibilon’s OpenGL Shader BitmapData example https://github.com/ibilon/BitmapDataShaders with some customizations of mine)
As I know, to scale I need to do something in the Vertex Shader (obviously since the Fragment one basically works per pixel).
I’ve prepared this piece of code
var shader = "
/* CLEAN SHADER */
#version 120
varying vec2 vTexCoord;
uniform sampler2D uImage0; //redered scene texture
void main()
{
gl_FragColor = texture2D(uImage0, vTexCoord.xy);
}";
var vertex = "
attribute vec4 aPosition;
attribute vec2 aTexCoord;
varying vec2 vTexCoord;
uniform mat4 uMatrix;
void main (void) {
vTexCoord = vec2 (aTexCoord.x, 1.0-aTexCoord.y); // flip y
vec4 a = aPosition;
a.x*=0.25;
a.y*=0.25;
gl_Position = uMatrix * a;
}";
So basically the fragment shader (var shader) do nothing.
The vertex shader (var vertex) execute a transformation scaling to 25% of the original one.
I have modified the code to accept a custom vertex shader at the INIT function
ShaderCompositing.init(bitmapdata.width, bitmapdata.height, vertex);
so I can pass a custom one that override the default one.
It works.
Problems: the picture is scaled but instead of having the new scaled texture at 0,0
I get it anchored to bottom left of the original boundaries.
Example:
I don’t understand why…
I also don’t understand the reason of the flipping
vTexCoord = vec2 (aTexCoord.x, 1.0-aTexCoord.y); // flip y
since if I set vTexCoord = aTexCoord the result is EXACTLY the same
Second problem:
I’ve tried to parametrize the scaling
Basically I wrote this
var vertex = "
attribute vec4 aPosition;
attribute vec2 aTexCoord;
varying vec2 vTexCoord;
uniform mat4 uMatrix;
uniform float uScale;
void main (void) {
vTexCoord = vec2 (aTexCoord.x, 1.0-aTexCoord.y); // flip y
vec4 a = aPosition;
a.x*=uScale;
a.y*=uScale;
gl_Position = uMatrix * a;
}";
And then I pass to the composite function the parameter
bp = ShaderCompositing.compositeParams(composite, shader,
[{name: "uScale", value: scale, type: Float}]);
I know this was intended for the Fragment Shader
but I don’t see why it doesn’t work for the Vertex Shader as well since it is the same GLProgram.
Am I missing something?
The result is always no image (like if uScale is == 0.0 and the scaling is factor 0)
Third problem:
I need in the end to CROP the bitmapData (or to get selective part of the GLView to the BitmapData)
Like customizing this to the resulting size.
GL.readPixels (0, 0, group.width, group.height, GL.RGBA, GL.UNSIGNED_BYTE, result_rgba);
It is early to think about this now. I have some ideas how to solve it efficiently anyway.
PS: I know I can scale the Bitmap with OpenFL instead of OpenGL, but there is no comparison in the results. The OpenGL scaling is way better and smoother.