Quantcast
Channel: GLSL / Shaders - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 212

(SOLVED) LowLevelGl & texture , it doesn't work and really don't know why

$
0
0

Hello !

I would like to set a PImage to a "low-level-shader" but it doesn't work...

Because it's boring to test a code with shaders, I put my processing project in a zip, and send it here

beginfill.com/processing/LowLevelGL_test.zip

I took the code from the LowLevelGL example, I added a floatBuffer with texture UV inside it and used PShader.set to send a PImage to the shader.

Here is the code (if you want to see it before getting the zip)

The processing code :

// Draws a triangle using low-level OpenGL calls.
import java.nio.*;

PGL pgl;
PShader sh;

int vertLoc;
int colorLoc;
int uvLoc;

float[] vertices;
float[] colors;
float[] uv;

FloatBuffer vertData;
FloatBuffer colorData;
FloatBuffer uvData;

PImage img;

void setup() {
  size(640, 360, P3D);

  img = loadImage("img.jpg");

  // Loads a shader to render geometry w/out
  // textures and lights.
  sh = loadShader("frag.glsl", "vert.glsl");

  vertices = new float[12];
  vertData = allocateDirectFloatBuffer(12);

  colors = new float[12];
  colorData = allocateDirectFloatBuffer(12);

  uv = new float[6];
  uvData = allocateDirectFloatBuffer(6);
}

void draw() {
  background(0);

  // The geometric transformations will be automatically passed 
  // to the shader.
  rotate(frameCount * 0.01, width, height, 0);

  updateGeometry();

  sh.set("texture",img);

  pgl = beginPGL();
  sh.bind();

  vertLoc = pgl.getAttribLocation(sh.glProgram, "vertex");
  colorLoc = pgl.getAttribLocation(sh.glProgram, "color");
  uvLoc = pgl.getAttribLocation(sh.glProgram, "uv");


  pgl.enableVertexAttribArray(vertLoc);
  pgl.enableVertexAttribArray(colorLoc);
  pgl.enableVertexAttribArray(uvLoc);

  pgl.vertexAttribPointer(vertLoc, 4, PGL.FLOAT, false, 0, vertData);
  pgl.vertexAttribPointer(colorLoc, 4, PGL.FLOAT, false, 0, colorData);
  pgl.vertexAttribPointer(uvLoc, 2, PGL.FLOAT, false, 0, uvData);

  pgl.drawArrays(PGL.TRIANGLES, 0, 3);

  pgl.disableVertexAttribArray(vertLoc);
  pgl.disableVertexAttribArray(colorLoc);
  pgl.disableVertexAttribArray(uvLoc);
  sh.unbind();  

  endPGL();
}

void updateGeometry() {
  // Vertex 1
  vertices[0] = 0;
  vertices[1] = 0;
  vertices[2] = 0;
  vertices[3] = 1;
  colors[0] = 1;
  colors[1] = 0;
  colors[2] = 0;
  colors[3] = 1;
  uv[0] = 0;
  uv[1] = 0;

  // Corner 2
  vertices[4] = width/2;
  vertices[5] = height;
  vertices[6] = 0;
  vertices[7] = 1;
  colors[4] = 0;
  colors[5] = 1;
  colors[6] = 0;
  colors[7] = 1;
  uv[2] = 1;
  uv[3] = 0;

  // Corner 3
  vertices[8] = width;
  vertices[9] = 0;
  vertices[10] = 0;
  vertices[11] = 1;
  colors[8] = 0;
  colors[9] = 0;
  colors[10] = 1;
  colors[11] = 1;
  uv[4] = 0;
  uv[5] = 1;

  vertData.rewind();
  vertData.put(vertices);
  vertData.position(0);

  colorData.rewind();
  colorData.put(colors);
  colorData.position(0);  

  uvData.rewind();
  uvData.put(uv);
  uvData.position(0);  
}

FloatBuffer allocateDirectFloatBuffer(int n) {
  return ByteBuffer.allocateDirect(n * Float.SIZE/8).order(ByteOrder.nativeOrder()).asFloatBuffer();
}

The vertex shader

uniform mat4 transform;

attribute vec4 vertex;
attribute vec4 color;
attribute vec2 uv;

varying vec4 vertColor;
varying vec2 textureUv;

void main() {
  gl_Position = transform * vertex;    
  vertColor = color;
  textureUv = uv;
}

The fragment shader

#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

uniform sampler2D texture;
varying vec4 vertColor;
varying vec2 textureUv;


void main() {
  gl_FragColor = texture2D(texture,textureUv) * vertColor;
}

Processing crash when I set the PImage to the Shader. If I remove that line, my code works without any error (but the triangle is black because there is no texture attached to the shader)

I looked into the PShader class to try to see what's wrong in it, but I really don't understand because it's very straight forward, it should work... I send a PImage to PShader and I'm absolutly sure that my PImage is not null when I send it to the PShader, but when PShader try to use the PImage with PGL, it find a null object...

Any help is welcome ! I'm on it for maybe 6-7 hours now...

Thanks !


Viewing all articles
Browse latest Browse all 212

Trending Articles