glClear和glStencilMask无法按预期运行

时间:2012-07-25 15:47:54

标签: c opengl glut

在Khronos OpenGL 2.1 Specs中他们说glStencilMask会影响glClear的操作,但是,在我的机器上,这似乎不是真的。

我目前得到的输出是:

Stencil value: (0,0) 0xff

我期望的输出是:

Stencil value: (0,0) 0xf0

这是我的代码:

void renderScene(void) {
    unsigned char pix;
    int i [4];

    /* Clear the stencil buffer initially */
    glClearStencil(0x0);
    glClear(GL_STENCIL_BUFFER_BIT);

    /* Applies the following: 1111 0000 & 1111 1111 */
    glStencilMask(0xF0);
    glClearStencil(0xFF);

    glClear(GL_STENCIL_BUFFER_BIT);

    glFlush();

    glReadPixels(0,0,1,1,GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &pix);
    printf("Stencil value (0,0): %x\n",pix);
}   

int main(int argc, char **argv) {
    // init GLUT and create Window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_STENCIL );
    glutInitWindowPosition(500,500);
    glutInitWindowSize(320, 320);
    glutCreateWindow("Trial");

    // register callbacks
    glutDisplayFunc(renderScene);

    // enter GLUT event processing cycle
    glutMainLoop();

    return 1;
}

(显然我正在执行各种其他图纸等,但这证明了我看到的问题)

1 个答案:

答案 0 :(得分:3)

来自glReadPixels()的文档

GL_STENCIL_INDEX 

Stencil values are read from the stencil buffer.
Each index is converted to fixed point, shifted left or right
depending on the value and sign of GL_INDEX_SHIFT,
and added to GL_INDEX_OFFSET. If GL_MAP_STENCIL is GL_TRUE,
indices are replaced by their mappings in the table GL_PIXEL_MAP_S_TO_S.

你可能想通过调用来检查GL_INDEX_SHIFT和GL_INDEX_OFFSET都是零

glPixelTransferi(GL_INDEX_SHIFT,  0);
glPixelTransferi(GL_INDEX_OFFSET, 0);

也许你正确地写了值,但是glReadPixels会对输出进行加扰。还尝试读取多个字节:

unsigned char pix4[4];
glReadPixels(0,0,1,1,GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, pix4);

for(int i = 0 ; i < 4 ; i ++) printf("Stencil value (0,0,%d): %x\n", i, pix4[i]);