4

在 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;
}

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

4

1 回答 1

4

来自 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]);
于 2012-07-25T17:11:52.513 回答