Created
February 24, 2014 16:15
-
-
Save fumin/9191341 to your computer and use it in GitHub Desktop.
OpenGL floating point texture experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void floatTextureExperiment | |
{ | |
GLenum err = 0; | |
GLuint texture1; | |
glGenTextures(1, &texture1); | |
glActiveTexture(GL_TEXTURE1); | |
glBindTexture(GL_TEXTURE_2D, texture1); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
GLuint texture2; | |
glGenTextures(1, &texture2); | |
glActiveTexture(GL_TEXTURE2); | |
glBindTexture(GL_TEXTURE_2D, texture2); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
GLuint frameBuffer; | |
glGenFramebuffers(1, &frameBuffer); | |
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer); | |
char* bgVShaderStr = | |
"attribute vec4 vPosition;\n" | |
"attribute vec2 in_texCoord;\n" | |
"varying vec2 texCoord;\n" | |
"void main(){\n" | |
" texCoord = in_texCoord;\n" | |
" gl_Position = vPosition;\n" | |
"}\n"; | |
char* bgFShaderStr = | |
"uniform sampler2D tex;\n" | |
"varying highp vec2 texCoord;\n" | |
"void main(){\n" | |
" highp vec4 color = texture2D(tex, texCoord);\n" | |
" //gl_FragColor = vec4(color.r+1.0/256.0, 1.0, 0.0, 1.0);\n" | |
" gl_FragColor = vec4(color.r/2.0, 1.0, 0.0, 1.0);\n" | |
"}\n"; | |
GLuint program = esLoadProgram(bgVShaderStr, bgFShaderStr); | |
int width = 2; | |
int height = 2; | |
// uint8_t buf[] = {20, 0, 0, 255, | |
// 100, 0, 0, 255, | |
// 180, 0, 0, 255, | |
// 255, 0, 0, 255}; | |
// glActiveTexture(GL_TEXTURE1); | |
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf); | |
float buf[] = {0.2, 0.0, 0.0, 1.0, | |
0.8, 0.0, 0.0, 1.0, | |
1.6, 0.0, 0.0, 1.0, | |
1.9, 0.0, 0.0, 1.0}; | |
hfloat* hfbuf = (hfloat*)malloc(16 * sizeof(hfloat)); | |
for (int i = 0; i < 16; i++) { | |
hfbuf[i] = convertFloatToHFloat(buf+i); | |
} | |
glActiveTexture(GL_TEXTURE1); | |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_HALF_FLOAT_OES, hfbuf); | |
free(hfbuf); | |
glUseProgram(program); | |
static const GLfloat imageVertices[] = { | |
-1.0f, -1.0f, | |
1.0f, -1.0f, | |
-1.0f, 1.0f, | |
1.0f, 1.0f, | |
}; | |
GLint attribLoc = glGetAttribLocation(program, "vPosition"); | |
glVertexAttribPointer(attribLoc, 2, GL_FLOAT, GL_FALSE, 0, imageVertices); | |
static const GLfloat noRotationTextureCoordinates[] = { | |
0.0f, 0.0f, | |
1.0f, 0.0f, | |
0.0f, 1.0f, | |
1.0f, 1.0f, | |
}; | |
attribLoc = glGetAttribLocation(program, "in_texCoord"); | |
glVertexAttribPointer(attribLoc, 2, GL_FLOAT, GL_FALSE, 0, noRotationTextureCoordinates); | |
glViewport(0, 0, width, height); | |
glActiveTexture(GL_TEXTURE2); | |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); | |
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture2, 0); | |
GLenum frameBufferStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER); | |
if (frameBufferStatus != GL_FRAMEBUFFER_COMPLETE) { | |
printf("Incomplete framebuffer: %d\n", frameBufferStatus - GL_FRAMEBUFFER_COMPLETE); | |
printFrameBufferStatus(); | |
} | |
int texLoc = glGetUniformLocation(program, "tex"); | |
glUniform1i(texLoc, GL_TEXTURE1 - GL_TEXTURE0); | |
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | |
uint8_t* m = (uint8_t*)malloc(4 * 4 * sizeof(uint8_t)); | |
memset(m, 0, 4 * 4 * sizeof(uint8_t)); | |
glReadPixels(0, 0, 2, 2, GL_RGBA, GL_UNSIGNED_BYTE, m); | |
for (int i = 0; i < 4; i++) { | |
for (int j = 0; j < 4; j++) { | |
printf("%d, ", m[4*i+j]); | |
} | |
printf("\n"); | |
} | |
free(m); | |
} | |
void printFrameBufferStatus() | |
{ | |
if (frameBufferStatus == GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT) { | |
printf("GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT\n"); | |
} else if (frameBufferStatus == GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS) { | |
printf("GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS\n"); | |
} else if (frameBufferStatus == GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT) { | |
printf("GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT\n"); | |
} else if (frameBufferStatus == GL_FRAMEBUFFER_UNSUPPORTED) { | |
printf("GL_FRAMEBUFFER_UNSUPPORTED\n"); | |
} else { | |
printf("Unknown status!\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment