Skip to content

Instantly share code, notes, and snippets.

@kchapelier
Last active January 15, 2016 12:01
Show Gist options
  • Save kchapelier/51671f5694ecec6f585c to your computer and use it in GitHub Desktop.
Save kchapelier/51671f5694ecec6f585c to your computer and use it in GitHub Desktop.
Test reading integer values from texture in Webgl
struct CARule {
int type;
int iterations;
int bufferLength;
bool[9] birthValues;
bool[9] survivalValues;
};
ivec2 pTGetSize(sampler2D pT) {
vec4 firstPixel = texture2D (pT, vec2(0, 0));
return ivec2(
256 * int(255. * firstPixel.r) + int(255. * firstPixel.g),
256 * int(255. * firstPixel.b) + int(255. * firstPixel.a)
);
}
vec2 pTGetPixelPosition(ivec2 sizePT, ivec2 intendedPixelPosition) {
return vec2(
intendedPixelPosition / sizePT
);
}
vec4 pTGetPixel(sampler2D pT, ivec2 sizePT, ivec2 intendedPixelPosition) {
return texture2D(pT, pTGetPixelPosition(sizePT, intendedPixelPosition));
}
ivec4 pTGetPixelInt(sampler2D pT, ivec2 sizePT, ivec2 intendedPixelPosition) {
vec4 p = pTGetPixel(pT, sizePT, intendedPixelPosition);
return ivec4(
int(255. * p.r),
int(255. * p.g),
int(255. * p.b),
int(255. * p.a)
);
}
CARule pTReadRule(sampler2D pT, ivec2 sizePT, int offset) {
CARule rule;
ivec4 p = pTGetPixelInt(pT, sizePT, ivec2(0, offset));
rule.type = p.r;
rule.iterations = p.g;
rule.bufferLength = 256 * p.b + p.a;
rule.birthValues[0] = true;
return rule;
}
void readPackedTexture(sampler2D pT) {
ivec2 sizePT = pTGetSize(pT);
int offset = 1;
pTReadRule(pT, sizePT, offset);
pTReadRule(pT, sizePT, offset + 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment