2012-07-25 58 views
3

在Khronos OpenGL 2.1规范中,他们说glStencilMask应该会影响glClear的操作,但是,在我的机器上,这似乎并不正确。glClear和glStencilMask没有按预期运行

目前我得到的输出是:

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

(很明显,我正在执行各种其他图纸等牛逼这表明我看到它)

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); 

也许问题)

+0

什么是您的机器/操作系统/图形卡?司机是最新的吗? – Shahbaz 2012-07-25 16:30:40

+0

使用你的可视化配置,模板缓冲区有多少位? – Luca 2012-07-25 17:22:41

+0

我正在Lenovo 7522P1U上运行Windows XP Professional,并配有英特尔G41 Express芯片组。并且有8个模板位平面。 – Luke 2012-07-25 18:00:17

回答

3

从文档上glReadPixels(你正确地写入值,但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]); 
+0

我只是尝试了两种,都没有改变。虽然有趣的glPixelTransferi。 – Luke 2012-07-25 18:11:41