Skip to content

Instantly share code, notes, and snippets.

@jbobrow
Last active January 14, 2016 10:14
Show Gist options
  • Save jbobrow/f78aa6e8359895bdc124 to your computer and use it in GitHub Desktop.
Save jbobrow/f78aa6e8359895bdc124 to your computer and use it in GitHub Desktop.

##Shader Question ######by Jonathan Bobrow ######@jonathanbobrow

I am trying to make a simple shader program and have all pieces working happily except for 1. The following should be enough of the code to see what the issue is, and I know it must be a simple thing, that I am surely faultering on because of my unfamiliarity with GLSL and perhaps the limitations of C here or in general. Any help is much appreciated.

In short, I am passing an array of values from a depth image (which works perfectly, the values get there happily) tested by accessing singular values in the array. The part where it isn't working, is when instead of calling array[0] or some arbitrary index, I want to call an informed index about where the pixel being evaluated is. The part that won't compile is my accessing of the array... which I think has something to do with requiring a const or something of that nature, but just can't seem to get it.

Thanks in advance! Code below :)

#ifdef GL_ES
precision highp float;
#endif

#define PROCESSING_COLOR_SHADER

uniform float time;
uniform vec2 resolution;
uniform sampler2D ppixels;
uniform float rawDepth[64*48];
uniform vec2 kinectResolution; // currently set to (64,48)

void main( void ) {
	vec2 position = ( gl_FragCoord.xy / resolution.xy );  // normalize frag coordinate
	vec2 pixel = 1./resolution; // useful for looking at neighboring pixels

	// get a corresponding pixel in the kinect resolution
	vec2 kinectPosition = position.xy * kinectResolution.xy;
	// get then index for that pixel in the kinect resolution, to access our 1D array of depth values
	int index = int(kinectPosition.y * kinectResolution.x + kinectPosition.x); <---- ISSUE PART I
	// kinect depth value already normalized for comparison as float (0-1)
	if(rawDepth[index] < 0.5) { <---- ISSUE PART II
	  // do something
	}
	else {
	  // don't do that thing... that thing we discussed earlier... don't do it ,,,mmmorty :)
	}
}
@jbobrow
Copy link
Author

jbobrow commented Jan 14, 2016

Figured out how to do this without using a 1D array, which it seems can only be accessed with constants (not sure why this makes sense, but I am sure there is a good reason, since people much smarter than I architected GL)

In short, I pass in a texture, then simply look at the same position on the texture. So much simpler!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment