Why is the texture function not known? I am using Processing

Viewed 869

I am playing around with GLSL in Processing but the texture function doesn't work.

This is the error that I got:

RuntimeException: Cannot compile fragment shader:
ERROR: 0:8: 'texture' : no matching overloaded function found (using implicit conversion) 
ERROR: 0:8: 'texture' : function is not known 
ERROR: 0:7: '=' :  cannot convert from 'const highp float' to '4-component vector of highp float'

This is my code:

PShader shad;
PImage testImg;
void setup() {
  size(800, 600, P2D);
  testImg = loadImage("testImg.jpg");
  shad = loadShader("frag.frag", "vert.vert");
  shad.set("asdfIMG", testImg);
  shad.set("resolution", (float)width, (float)height);
  noLoop();
}
void draw() {
  shader(shad);
  beginShape();
  vertex(-1.0, 1.0);
  vertex(1.0, 1.0);
  vertex(1.0, -1.0);
  vertex(-1.0, -1.0);
  endShape();
}

Here is my Fragment Shader code:

#version 330

uniform sampler2D asdfIMG;
uniform vec2 resolution;
void main()
{
  vec4 texel = texture(asdfIMG, 0.5, 0.5);

  gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);
}

This is my Vertex Shader Code:

#version 330 core
attribute vec4 position;
void main() {
  gl_Position = position;
}
1 Answers

The texture function is well known, but there is no overloaded texture function which can handle this 3 arguments.
If the sampler type is sampler2D, then the 2nd argument has to be a vec2 with the texture cooridantes:

vec4 texel = texture(asdfIMG, 0.5, 0.5);

vec4 texel = texture(asdfIMG, vec2(0.5, 0.5));

Anyway, it makes no snese to do a texture look up with constant coordinates. Use gl_FragCoord to get the the window-relative coordinates of the current fragment and compute the texture coordiantes. e.g:

vec2 uv = gl_FragCoord.xy / resolution.xy;
vec4 texel = texture(asdfIMG, uv);
Related