Quantcast
Channel: Kinect - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 530

change colors of points data from kinect using shaders

$
0
0

Sorry, I am just starting with shaders. I have read a little but not enough to understand how to change this code.

the code below would build a 3D scene from kinect point cloud data and set all points to white. I just wanted to understand how I could set a matrix of colors dynamically for each point. (basically I just want points closer to kinect to be brighter)

I have tried to implement this, but I couldn't understand how to work with textures yet.

Thank you!

import java.nio.*;
import org.openkinect.freenect.*;
import org.openkinect.processing.*;

// Kinect Library object
Kinect kinect;

// Angle for rotation
float a = PI;

//openGL
PGL pgl;
PShader sh;

int  vertLoc;
int  vertLocId;
//int  colorLoc;

void setup() {
  // Rendering in P3D
  size(800, 600, P3D);
  kinect = new Kinect(this);
  kinect.initDepth();
  kinect.enableMirror(true); //added

  //load shaders
  sh = loadShader("frag.glsl", "vert.glsl");
  PGL pgl = beginPGL();

  IntBuffer intBuffer = IntBuffer.allocate(1);
  pgl.genBuffers(1, intBuffer);

  //memory location of the VBO
  vertLocId = intBuffer.get(0);

  endPGL();
}

void draw() {

  background(0);

 image(kinect.getDepthImage(), 0, 0, 320, 240); //why this is not mirrored ?

  pushMatrix();
  translate(width/2, height/2, 600);
  scale(150);
  rotateY(a);
  int vertData = kinect.width * kinect.height;
  FloatBuffer depthPositions =  kinect.getDephToWorldPositions();
  pgl = beginPGL();
  sh.bind();

  vertLoc  = pgl.getAttribLocation(sh.glProgram, "vertex");

  //color for ALL POINTS of the point cloud
  sh.set("fragColor", 1.0f, 1.0f, 1.0f, 1.0f); //<-----

  pgl.enableVertexAttribArray(vertLoc);

  pgl.bindBuffer(PGL.ARRAY_BUFFER, vertLocId);
  pgl.bufferData(PGL.ARRAY_BUFFER, Float.BYTES * vertData *3, depthPositions, PGL.DYNAMIC_DRAW);
  pgl.vertexAttribPointer(vertLoc, 3, PGL.FLOAT, false, Float.BYTES * 3, 0);

  pgl.bindBuffer(PGL.ARRAY_BUFFER, 0);

  //draw the XYZ depth camera points
  pgl.drawArrays(PGL.POINTS, 0, vertData);

  //clean up the vertex buffers
  pgl.disableVertexAttribArray(vertLoc);

  sh.unbind();
  endPGL();

  popMatrix();

  fill(255, 0, 0);
  text(frameRate, 50, 50);
}

frag.glsl:

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

#define PROCESSING_TEXTURE_SHADER //

varying vec4 vertColor;
//varying vec4 vertTexCoord;

//input color
uniform vec4 fragColor;
//uniform sampler2D texture;

void main() {
  //vec4 col = texture2D(texture, vertTexCoord.st);
  //outputColor
  gl_FragColor = fragColor;
  //gl_FragColor = vec4(col.rgb, 1.0);
}

vert.glsl:

uniform mat4 transform;

attribute vec4 vertex;
attribute vec4 color;

varying vec4 vertColor;


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

Viewing all articles
Browse latest Browse all 530

Trending Articles