We are about to switch to a new forum software. Until then we have removed the registration on this forum.
If I change:
"gl_Position = transform * (vertex + vec4(offsetX, offsetY, 0, 0));",
to
"gl_Position = transform * (vertex + vec4(300, offsetY, 0, 0));",
for example then it does shift to the right. But for some unclear reason, offsetX and offsetY never seem to work. Why is that?
PShader shader;
PImage img;
void settings() {
size(512, 512, P3D);
}
void setup() {
frameRate(999);
shader = new PShader(this, new String[] {
"#version 410",
"uniform mat4 transform;",
"uniform float offsetX;",
"uniform float offsetY;",
"in vec4 vertex;",
"in vec2 texCoord;",
"out vec2 vertTexCoord;",
"void main() {",
"vertTexCoord = vec2(texCoord.x, 1.0 - texCoord.y);",
// why does offsetX not work but 300 for example does work?
"gl_Position = transform * (vertex + vec4(offsetX, offsetY, 0, 0));",
"}"
},
new String[] {
"#version 410",
"uniform sampler2D texture;",
"in vec2 vertTexCoord;",
"out vec4 fragColor;",
"void main() {",
"fragColor = vec4(1,0,0,1);",
"}"
});
img = loadImage("http://bellard.org/bpg/lena30.jpg");
}
void draw() {
background(0);
shader.set("offsetX", mouseX);
shader.set("offsetY", mouseY);
shader(shader);
translate(width/2, height/2);
sphere(100);
}
Answers
Using
shader.set("offsetX", mouseX);
Processing will try to set anuniform int offsetX;
. Useshader.set("offsetX", (float)mouseX);
and Processing will set the correctuniform float offsetX;
.Anyhow, why not using a vec2 instead of 2 floats:
But always remember to cast. Using
shader.set("offset", mouseX, mouseY);
(without the casting to float) would setuniform ivec2 offset;
for example.Thanks! I would have never thought of that.