Hey all,
I want to draw moving shapes on top of an image (a world map), for example ellipses representing airplanes moving over the map to their destination. I am also practicing using a shader to improve performance, because there will be thousands of airplanes moving at any given point.
My approach at this point, without a shader, is to use P3D and to draw 2 ‘layers’, and use the z value to make the ellipses appear on top of the world map image. Without shaders this works wonderfullly with translate, but when I am using a shader to draw an ellipse, it does not appear on the screen. I do get the world map to appear when I create a shape and use the image as texture, but the ellipses are nowhere to be seen…
My first question is: is this the correct approach? And if not, any pointer in the right direction would be very much appreciated.
Any comments also on the code will be greatly appreciated
PImage img;
PShader texShader;
int h;
int w;
float circleSize = 0.1;
float[] circlePosition = {0.5,0.2,0.1,0.4};
void setup(){
size(2048,1024, P3D);
h = height;
w = width;
texShader = loadShader("frag.glsl", "vert.glsl");
img = loadImage("worldmap2048x1024-ChinaCentered.png");
}
void draw(){
shader(texShader);
textureMode(NORMAL);
beginShape();
texture(img);
vertex(0, 0, 0, 0);
vertex(w, 0, 1, 0);
vertex(w, h, 1, 1);
vertex(0, h, 0, 1);
endShape();
texShader.set("circleSize", circleSize);
texShader.set("circlePosition", circlePosition);
vertex shader
uniform mat4 transform;
uniform mat4 texMatrix;
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
varying vec4 vertColor;
varying vec4 vertTexCoord;
uniform vec2 resolution;
varying vec2 uv;
void main() {
gl_Position = transform * position;
uv = (gl_Position.xy * resolution.xy) / resolution.y;
vertColor = color;
vertTexCoord = texMatrix * vec4(texCoord, 1.0, 1.0);
}
Fragment shader
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
uniform sampler2D texture;
uniform float circleSize;
uniform float[4] circlePosition;
varying vec4 vertColor;
varying vec4 vertTexCoord;
varying vec2 uv;
void main() {
vec4 col = vec4(0, 0, 0, 1);
for(int i = 0; i < 4; i+=2){
vec2 circlePos = vec2(circlePosition[i], circlePosition[i+1]);
float d = length(uv-circlePos);
float m = smoothstep(circleSize, .05, d);
col += m;
}
//gl_FragColor = col;//texture2D(texture, vertTexCoord.st) * vertColor + col;
gl_FragColor = texture2D(texture, vertTexCoord.st) * vertColor;
}