SDL2 和 OpenGL 模板缓冲区不起作用
SDL2 and OpenGL stencil buffer don't work
我正在用 c++ 和 sdl、opengl 编写游戏,但模板缓冲区似乎没有 运行。我已使用此参数启用模板位:
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1);
代码如下:
glColorMask(0,0,0,0);
_stencil.EnableStencil();
_stencil.StencilFunc(GL_ALWAYS,2,1);
_stencil.StencilOp(GL_REPLACE,GL_REPLACE,GL_REPLACE);
glBegin(GL_QUADS);
glColor3f(0,0,0);
glVertex2f(_size.x,_size.y);
glVertex2f(_size.x,_size.y + _size.h);
glVertex2f(_size.x + _size.w,_size.y + _size.h);
glVertex2f(_size.x + _size.w,_size.y);
glColor3f(1,1,1);
glEnd();
glColorMask(1,1,1,1);
_stencil.StencilFunc(GL_EQUAL,2,1);
_stencil.StencilOp(GL_KEEP,GL_KEEP,GL_KEEP);
for(int i = 0; i < _list.size(); i++)
{
_name.SetFontData(_fdata);
_name.SetLabelText(_list[i].name);
_name.SetLabelColor(255,255,255,255);
_icon.BindTexture( _list[i].data.ID, _list[i].data.width , _list[i].data.height );
/*if( _offsety + (_listGui.GetTextureInfo().height*i) + (_spacing_y*i) >= _size.y && _offsety + ((_listGui.GetTextureInfo().height*i)+_listGui.GetTextureInfo().height) + (_spacing_y*i) <= (_size.y + _size.h) )
{*/
if(i == 0)
{
_listGui.Render( _size.x + _spacing_x, _offsety );
_name.RenderText(_size.x + _spacing_x + _label.x,_offsety + _label.y );
_icon.Render( _size.x + (_iconPos.y + _spacing_x) , _offsety + _iconPos.y );
}
else
{
_listGui.Render( _size.x + _spacing_x, ( _offsety + ((_listGui.GetTextureInfo().height*i) + (_spacing_y*i) ) ) );
_name.RenderText(
_size.x + ( _label.x + _spacing_x),
_offsety + ( (_listGui.GetTextureInfo().height*i) + (_spacing_y*i) ) + _label.y
);
_icon.Render(
_size.x + (_iconPos.x + _spacing_x),
_offsety + _iconPos.y + (_listGui.GetTextureInfo().height*i) + (_spacing_y*i)
);
}
/*}*/
}_stencil.DisableStencil();
_stencil.EnableStencil() 等于 glEnable(GL_STENCIL_TEST);
_stencil.DisableStecil();
相同
您在模板状态中使用的掩码值有问题:
_stencil.StencilFunc(GL_ALWAYS,2,1);
掩码值由 glStencilFunc()
的第三个参数给出,在应用任何模板逻辑之前与参考值和模板缓冲区中的值进行“与”运算。使用规范的语言:
The s least significant bits of mask are bitwise ANDed with both the reference and the stored stencil value, and the resulting masked values are those that participate in the comparison controlled by func.
根据您使用的值,掩码值 1 将与参考值 2 进行“与”运算,从而得到有效参考值 0。二进制形式:
2 = 00000010
1 = 00000001
2 & 1 = 00000000
这意味着在第一遍中,您渲染的所有内容的模板值都设置为 0,这是您已经清除它们的值。在第二遍中,将值与 0 进行比较,所有像素将通过模板测试。
要使其正常工作,您需要更改参考值或掩码,以免参考值被掩码。参考值为2,掩码可以是2或3之类的。但是如果你只想使用一位模板,最简单的方法是使用1作为参考值。
这意味着您要将第一遍的设置更改为:
_stencil.EnableStencil();
_stencil.StencilFunc(GL_ALWAYS, 1, 1);
_stencil.StencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
第二次传递给:
_stencil.StencilFunc(GL_EQUAL, 1, 1);
_stencil.StencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
我正在用 c++ 和 sdl、opengl 编写游戏,但模板缓冲区似乎没有 运行。我已使用此参数启用模板位:
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1);
代码如下:
glColorMask(0,0,0,0);
_stencil.EnableStencil();
_stencil.StencilFunc(GL_ALWAYS,2,1);
_stencil.StencilOp(GL_REPLACE,GL_REPLACE,GL_REPLACE);
glBegin(GL_QUADS);
glColor3f(0,0,0);
glVertex2f(_size.x,_size.y);
glVertex2f(_size.x,_size.y + _size.h);
glVertex2f(_size.x + _size.w,_size.y + _size.h);
glVertex2f(_size.x + _size.w,_size.y);
glColor3f(1,1,1);
glEnd();
glColorMask(1,1,1,1);
_stencil.StencilFunc(GL_EQUAL,2,1);
_stencil.StencilOp(GL_KEEP,GL_KEEP,GL_KEEP);
for(int i = 0; i < _list.size(); i++)
{
_name.SetFontData(_fdata);
_name.SetLabelText(_list[i].name);
_name.SetLabelColor(255,255,255,255);
_icon.BindTexture( _list[i].data.ID, _list[i].data.width , _list[i].data.height );
/*if( _offsety + (_listGui.GetTextureInfo().height*i) + (_spacing_y*i) >= _size.y && _offsety + ((_listGui.GetTextureInfo().height*i)+_listGui.GetTextureInfo().height) + (_spacing_y*i) <= (_size.y + _size.h) )
{*/
if(i == 0)
{
_listGui.Render( _size.x + _spacing_x, _offsety );
_name.RenderText(_size.x + _spacing_x + _label.x,_offsety + _label.y );
_icon.Render( _size.x + (_iconPos.y + _spacing_x) , _offsety + _iconPos.y );
}
else
{
_listGui.Render( _size.x + _spacing_x, ( _offsety + ((_listGui.GetTextureInfo().height*i) + (_spacing_y*i) ) ) );
_name.RenderText(
_size.x + ( _label.x + _spacing_x),
_offsety + ( (_listGui.GetTextureInfo().height*i) + (_spacing_y*i) ) + _label.y
);
_icon.Render(
_size.x + (_iconPos.x + _spacing_x),
_offsety + _iconPos.y + (_listGui.GetTextureInfo().height*i) + (_spacing_y*i)
);
}
/*}*/
}_stencil.DisableStencil();
_stencil.EnableStencil() 等于 glEnable(GL_STENCIL_TEST); _stencil.DisableStecil();
相同您在模板状态中使用的掩码值有问题:
_stencil.StencilFunc(GL_ALWAYS,2,1);
掩码值由 glStencilFunc()
的第三个参数给出,在应用任何模板逻辑之前与参考值和模板缓冲区中的值进行“与”运算。使用规范的语言:
The s least significant bits of mask are bitwise ANDed with both the reference and the stored stencil value, and the resulting masked values are those that participate in the comparison controlled by func.
根据您使用的值,掩码值 1 将与参考值 2 进行“与”运算,从而得到有效参考值 0。二进制形式:
2 = 00000010
1 = 00000001
2 & 1 = 00000000
这意味着在第一遍中,您渲染的所有内容的模板值都设置为 0,这是您已经清除它们的值。在第二遍中,将值与 0 进行比较,所有像素将通过模板测试。
要使其正常工作,您需要更改参考值或掩码,以免参考值被掩码。参考值为2,掩码可以是2或3之类的。但是如果你只想使用一位模板,最简单的方法是使用1作为参考值。
这意味着您要将第一遍的设置更改为:
_stencil.EnableStencil();
_stencil.StencilFunc(GL_ALWAYS, 1, 1);
_stencil.StencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
第二次传递给:
_stencil.StencilFunc(GL_EQUAL, 1, 1);
_stencil.StencilOp(GL_KEEP, GL_KEEP, GL_KEEP);