Skip to content

Instantly share code, notes, and snippets.

@tonykwok
Last active August 18, 2022 10:27
Show Gist options
  • Save tonykwok/0ae573d67cfa557080c676e2ea20f072 to your computer and use it in GitHub Desktop.
Save tonykwok/0ae573d67cfa557080c676e2ea20f072 to your computer and use it in GitHub Desktop.
OpenGL ES 3.0 and 3DLUT
// http://mahisorn.blogspot.com/2009/01/3d-lut-using-glsl.html
uniform sampler2D image;
uniform sampler3D lookup;
uniform float imageWidth;
uniform float imageHeight;
varying vec2 pos;
void main(void) {
// find the texture coordinate corresponding to this fragment
vec2 texCoord = vec2(pos.x/imageWidth, pos.y/imageHeight);
// get the corresponding color
vec4 colorIn = texture2D(image, texCoord);
//apply look up tables
vec4 colorOut;
colorOut.rgb = texture3D(lookup, colorIn.rgb).rgb;
// final color
gl_FragColor = colorOut;
}
GLuint g_TexturesArray[MAX_TEXTURES];
static const int dim = 256;
GLubyte lut3D[dim][dim][dim][3];
void init_lut3D() {
for(int i = 0; i < dim; i++) {
for(int j = 0; j < dim; j++) {
for(int k = 0; k < dim; k++) {
lut3D[k][j][i][0] = k;
lut3D[k][j][i][1] = j;
lut3D[k][j][i][2] = i;
}
}
}
}
*****************************
Create 3D texture in OpenGL
*****************************
glActiveTexture(GL_TEXTURE1);
init_lut3D();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &g_TexturesArray[1]);
glBindTexture(GL_TEXTURE_3D, g_TexturesArray[1]);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB8, dim, dim, dim, 0, GL_RGB, GL_UNSIGNED_BYTE, lut3D);
// http://mahisorn.blogspot.com/2009/01/3d-lut-using-glsl.html
// Multitexturing
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, imageName);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_3D, lutName);
// Set Texture in Shader Program
texLoc = glGetUniformLocation(programObj, "image");
glUniform1i(texLoc, 0); //texture0
texLoc = glGetUniformLocation(programObj, "lookup");
glUniform1i(texLoc, 1); //texture1
// http://mahisorn.blogspot.com/2009/01/3d-lut-using-glsl.html
varying vec2 pos;
void main(void) {
pos = vec2(gl_Vertex.xy);
gl_Position = ftransform();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment