We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, do you know how I can acces a black and white texture in a vertex shader design to draw lines and edit the z position of each vertices based on the color of each pixel ?
If in my sketch I put sh.texture(image);
what is the name of the variable I can use in the vertex shader ?
uniform sampler2D texture;
doesn't seems to work.
import java.util.Date;
import peasy.*;
PeasyCam cam;
ArrayList<PVector> grid;
PShape shapeGrid;
boolean shapeMode;
PShader pshader;
PImage image;
int TWIDTH = 640;
int THEIGHT = 480;
void setup() {
size(640, 480, OPENGL);
cam = new PeasyCam(this, 1000);
cam.setMinimumDistance(50);
cam.setMaximumDistance(1500);
image = loadImage("depth.jpg");
pshader = loadShader("frag.glsl", "vert.glsl");
shapeGrid = createGrid(10);
shapeGrid.setStrokeWeight(1);
}
PShape createGrid(int detail) {
PShape sh = createShape();
sh.beginShape(LINES);
sh.stroke(255, 0, 0);
sh.textureMode(NORMAL);
sh.texture(image);
for (int y=0; y<height-detail; y+=detail) {
for (int x=0; x<width-detail; x+=detail) {
PVector tl, tr;
tl = new PVector(x, y);
tr = new PVector(x+detail, y);
sh.vertex(tl.x, tl.y, tl.z, tl.x/TWIDTH, tl.y/THEIGHT);
sh.vertex(tr.x, tr.y, tr.z, tr.x/TWIDTH, tr.y/THEIGHT);
}
}
sh.endShape(CLOSE);
return sh;
}
void keyPressed() {
if (key=='0') {
Date date = new Date();
String name = "data/images/shapes-"+date.getTime()+".png";
save(name);
}
}
void draw() {
background(127);
shapeMode(CENTER);
shader(pshader, LINES);
shape(shapeGrid);
}
Answers
Hello,
I have done exactly what I graphically wanted to do with multiple quads and a vertex shader of type: PROCESSING_TEXTURE_SHADER (see below).
But to have more control over my PShape I'm trying to reproduce it using only lines. Any ideas ?
The result:
The texture I'm currently using:
Ok I have found a solution: I have used the set method
pshader.set("tex1", image);
and set the UVs in the vertex shader withtexture2D()
(without using directly vertTexCoord.st).