PDA

View Full Version : Painting the same texture multiple times



fhazubski
05-25-2016, 04:10 PM
Hello.

I am working with Qt 5.5.1, QtQuick, with ShaderEffect object. In it I can specify vertex and fragment shaders. I have managed to create a copy of the texture and render two objects at the same time but I do not know how can I move textures. I know how to resize them but they are always in the upper left corner of the ShaderEffect.

Here is a source of my shaders:



vertexShader: "
uniform highp mat4 qt_Matrix;
attribute highp vec4 qt_Vertex;
attribute highp vec2 qt_MultiTexCoord0;
varying highp vec2 coord;
varying highp vec2 coord2;
uniform lowp float asd;
void main() {
coord = qt_MultiTexCoord0;
coord2 = qt_MultiTexCoord0;
coord2.x *= 2.0;
coord2.y *= 2.0;
gl_Position = qt_Matrix * qt_Vertex;
}"

fragmentShader: "
varying highp vec2 coord;
varying highp vec2 coord2;
uniform sampler2D ses;

uniform lowp float r;
uniform lowp float g;
uniform lowp float b;

void main() {
lowp vec4 clr = texture2D(ses, coord);
lowp vec4 clr2 = texture2D(ses, coord2);
gl_FragColor = vec4((r * clr.r + r * clr2.r) / 2.0,
(g * clr.g + g * clr2.g) / 2.0,
(b * clr.b + b * clr2.b) / 2.0, clr.a);
}
"


Any help would be appreciated. Thank you.

fhazubski
05-26-2016, 03:25 AM
I have found a solution that I was looking for. I have managed to make the texture smaller 4 times (2 times in each dimension) and place 5 copies of it - 4 in each of the ShaderEffect's corners and one in the center.



vertexShader: "
uniform highp mat4 qt_Matrix;
attribute highp vec4 qt_Vertex;
attribute highp vec2 qt_MultiTexCoord0;
varying highp vec2 coord;
uniform lowp float asd;
void main() {
coord = qt_MultiTexCoord0;
gl_Position = qt_Matrix * qt_Vertex;
}"

fragmentShader: "
varying highp vec2 coord;
uniform sampler2D ses;

uniform lowp float r;
uniform lowp float g;
uniform lowp float b;

void main() {
lowp vec4 clr1 = texture2D(ses, vec2(coord.x * 2.0, coord.y * 2.0));
lowp vec4 clr2 = texture2D(ses, vec2(coord.x * 2.0 - 1.0, coord.y * 2.0));
lowp vec4 clr3 = texture2D(ses, vec2(coord.x * 2.0, coord.y * 2.0 - 1.0));
lowp vec4 clr4 = texture2D(ses, vec2(coord.x * 2.0 - 1.0, coord.y * 2.0 - 1.0));
lowp vec4 clr5 = texture2D(ses, vec2(coord.x * 2.0 - 0.5, coord.y * 2.0 - 0.5));
gl_FragColor = vec4((r * (clr1.r + clr2.r + clr3.r + clr4.r + clr5.r)),
(g * (clr1.g + clr2.g + clr3.g + clr4.g + clr5.g)),
(b * (clr1.b + clr2.b + clr3.b + clr4.b + clr5.b)),
(clr1.a + clr2.a + clr3.a + clr4.a + clr5.a));
}
"


It works for me very well but if someone has some guidance for me it would still be appreciated.