#include #include #include #include #define WINDSIZEX 100 #define WINDSIZEY 100 GLfloat *buf; GLubyte *clearBuf; GLubyte *bitmap; void init(void) { } static void test(void) { int i, bufHeight = 3; GLint mask = 189; clearBuf = malloc(sizeof(GLubyte) * WINDSIZEX * bufHeight); bitmap = malloc(sizeof(GLubyte) * WINDSIZEX * bufHeight); for (i = 0; i < WINDSIZEX * bufHeight; i++) { clearBuf[i] = i % 256; bitmap[i] = 0xFF; } glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_STENCIL_TEST); glStencilMask(255); //stencil bits is 8 glColor4f(1.0, 1.0, 0.0, 1.0); glRasterPos2f((float)0.20, (float)0.20); glDrawPixels(WINDSIZEX, bufHeight, GL_STENCIL_INDEX , GL_UNSIGNED_BYTE, clearBuf); glStencilMask(mask); glStencilFunc(GL_NEVER, 0, ~0); glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP); //glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP); glRasterPos2f(0.25, 0.25); glBitmap(WINDSIZEX, bufHeight, 0, 0, 0, 0, bitmap); buf = malloc(sizeof(GLfloat) * WINDSIZEX * bufHeight); glReadPixels(0, 0, WINDSIZEX, bufHeight, GL_STENCIL_INDEX, GL_FLOAT, buf); for (i = 0; i < 20; i++) { // printf("expected at (%d): %f, actual: %f\n", i, (float)(clearBuf[i] & ~mask), buf[i]); GLint pre = clearBuf[i]; GLint expected = pre & ~mask; printf("expected at (%d): %d, actual: %f\n", i, expected, buf[i]); } } void display(void) { glViewport(0, 0, WINDSIZEX, WINDSIZEY); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, WINDSIZEX, 0, WINDSIZEY); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); test(); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(WINDSIZEX, WINDSIZEY); glutInitWindowPosition(100, 100); glutCreateWindow(argv[0]); init(); glutDisplayFunc(display); glutMainLoop(); return 0; }