Created
April 9, 2013 00:38
-
-
Save jra101/5341926 to your computer and use it in GitHub Desktop.
Test using multiple sampler objects with a single texture object
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
bool TestSamplers() | |
{ | |
GLuint c; | |
glGenRenderbuffers(1, &c); | |
glNamedRenderbufferStorageEXT(c, GL_RGBA8, 4, 1); | |
GLuint f; | |
glGenFramebuffers(1, &f); | |
glNamedFramebufferRenderbufferEXT(f, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, c); | |
glBindFramebuffer(GL_FRAMEBUFFER, f); | |
glViewport(0, 0, 4, 1); | |
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); | |
glClear(GL_COLOR_BUFFER_BIT); | |
GLubyte input[8] = { | |
255, 255, 255, 255, | |
0, 0, 0, 255 | |
}; | |
GLuint t; | |
glGenTextures(1, &t); | |
glTextureImage2DEXT(t, GL_TEXTURE_2D, 0, GL_RGBA8, 2, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, input); | |
glBindMultiTextureEXT(GL_TEXTURE0, GL_TEXTURE_2D, t); | |
glBindMultiTextureEXT(GL_TEXTURE1, GL_TEXTURE_2D, t); | |
GLuint s0; | |
glGenSamplers(1, &s0); | |
glSamplerParameteri(s0, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
glSamplerParameteri(s0, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
GLuint s1; | |
glGenSamplers(1, &s1); | |
glSamplerParameteri(s1, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
glSamplerParameteri(s1, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
glBindSampler(0, s0); | |
glBindSampler(1, s1); | |
GLuint p = glCreateProgram(); | |
const GLchar *vsSource = "attribute vec4 a_position; varying vec2 v_coord; void main() { gl_Position = a_position; v_coord = (a_position.xy + 1.0) / 2.0; }"; | |
GLuint vs = glCreateShader(GL_VERTEX_SHADER); | |
glShaderSource(vs, 1, &vsSource, NULL); | |
glCompileShader(vs); | |
glAttachShader(p, vs); | |
glDeleteShader(vs); | |
const GLchar *psSource = "varying vec2 v_coord; uniform sampler2D t0; uniform sampler2D t1; void main() { gl_FragColor = texture2D(t1, v_coord); }"; | |
GLuint ps = glCreateShader(GL_FRAGMENT_SHADER); | |
glShaderSource(ps, 1, &psSource, NULL); | |
glCompileShader(ps); | |
glAttachShader(p, ps); | |
glDeleteShader(ps); | |
glBindAttribLocation(p, 0, "a_position"); | |
glLinkProgram(p); | |
glUseProgram(p); | |
glUniform1i(glGetUniformLocation(p, "t0"), 0); | |
glUniform1i(glGetUniformLocation(p, "t1"), 1); | |
GLfloat vertices[] = { | |
-1.0f, -1.0f, | |
3.0f, -1.0f, | |
-1.0f, 3.0f, | |
}; | |
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices); | |
glEnableVertexAttribArray(0); | |
glDrawArrays(GL_TRIANGLES, 0, 3); | |
glDisableVertexAttribArray(0); | |
GLubyte expected[4 * 4] = { | |
255, 255, 255, 255, | |
255, 255, 255, 255, | |
0, 0, 0, 255, | |
0, 0, 0, 255 | |
}; | |
GLubyte output[4 * 4]; | |
glReadPixels(0, 0, 4, 1, GL_RGBA, GL_UNSIGNED_BYTE, output); | |
glDeleteRenderbuffers(1, &c); | |
glDeleteFramebuffers(1, &f); | |
glDeleteTextures(1, &t); | |
glDeleteSamplers(1, &s0); | |
glDeleteSamplers(1, &s1); | |
glDeleteProgram(p); | |
for (int i = 0; i < 4 * 4; i++) { | |
if (output[i] != expected[i]) | |
return false; | |
} | |
glClearColor(0.0f, 1.0f, 0.0f, 1.0f); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment