gpt4 book ai didi

textures - 如何在OpenGL-ES 2.0中创建带有纹理(图像)的模板缓冲区

转载 作者:行者123 更新时间:2023-12-04 11:43:19 24 4
gpt4 key购买 nike

我可以在OpenGL 2.0中准备带有纹理(图像)的模板吗?
这样图像的某些部分将是透明的,因此将按原样传输
到模板缓冲区,然后将使用此模板缓冲区进行进一步绘制。

由datenwolf进行编辑,以说明答案中的OP问题更新:
通过@InfiniteLoop:
@datenwolf非常感谢您的回复,但没有成功:(这是我的代码

- (void)render 
{
[EAGLContext setCurrentContext:context];
glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
glUseProgram(program);
glClearColor(0, 104.0/255.0, 55.0/255.0, 1.0);
glViewport(0, 0, backingWidth, backingHeight);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Set the stencil clear value
glClearStencil ( 0x1 );

// Set the depth clear value
glClearDepthf( 0.75f );



////////
////
GLfloat threshold = 0.5f;//half transparency
/* the order in which orthogonal OpenGL state is set doesn't matter */
glEnable(GL_STENCIL_TEST);
glEnable(GL_ALPHA_TEST);

/* don't write to color or depth buffer */
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);

glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

/* first set alpha test so that fragments greater than the threshold
will pass thus will set nonzero bits masked by 1 in stencil */
glStencilFunc(GL_ALWAYS, 1, 1);//set one
glAlphaFunc(GL_GREATER, threshold);//greater than half transparency

glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 0, fullVertices);
glVertexAttribPointer(colorLoc, 4, GL_FLOAT, GL_FALSE, 0, colorVertices);
glVertexAttribPointer(textureLoc, 2, GL_FLOAT, GL_FALSE, 0, textureVertices);
[self drawTexture:texture2DMaskImage];
/* second pass of the fragments less or equal than the threshold
will pass thus will set zero bits masked by 1 in stencil */
glStencilFunc(GL_ALWAYS, 0, 1);//set zero
glAlphaFunc(GL_LEQUAL, threshold);//Less than Half transparency
glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 0, fullVertices);
glVertexAttribPointer(colorLoc, 4, GL_FLOAT, GL_FALSE, 0, colorVertices);
glVertexAttribPointer(textureLoc, 2, GL_FLOAT, GL_FALSE, 0, textureVertices);
[self drawTexture:texture2DMaskImage];


// till here as suggested by u after this I have added to draw
// the next Image which will use the created Stencil
glDepthMask(GL_TRUE);
glDisable(GL_ALPHA_TEST);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
//
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);//stop further write to stencil buffer
glStencilFunc(GL_EQUAL, 0, 1);//pass if 0
glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 0, fullVertices);
glVertexAttribPointer(colorLoc, 4, GL_FLOAT, GL_FALSE, 0, colorVertices);
glVertexAttribPointer(textureLoc, 2, GL_FLOAT, GL_FALSE, 0, textureVertices);
[self drawTexture:texture2D];
////
///////



// Validate program before drawing. This is a good check,
// but only really necessary in a debug build.
// DEBUG macro must be defined in your debug configurations
// if that's not already the case.
#if defined(DEBUG)
if (![self validateProgram:program])
{
NSLog(@"Failed to validate program: %d", program);
return;
}
#endif
// draw
[context presentRenderbuffer:GL_RENDERBUFFER];//Present
}

- (void) drawTexture:(Texture2D*)texture
{
// handle viewing matrices
GLfloat proj[16], modelview[16], modelviewProj[16];
// setup projection matrix (orthographic)
mat4f_LoadOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, proj);
// glUniformMatrix4fv(uniforms[UNIFORM_PROJECTION], 1, 0, proj);
// setup modelview matrix (rotate around z)
mat4f_LoadIdentity(modelview);
mat4f_LoadZRotation(0.0, modelview);
mat4f_LoadTranslation3D(0.0, 0.0, 0.0, modelview);
// projection matrix * modelview matrix
mat4f_MultiplyMat4f(proj, modelview, modelviewProj);
glUniformMatrix4fv(_modelViewUniform, 1, 0, modelviewProj);
// update uniform values
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture.name);
glUniform1i(_textureUniform, 0);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, cubeIndices);
}
请阐明一些我非常需要的:(再次感谢。

最佳答案

是的,您可以使用alpha测试(glEnable(GL_ALPHA_TEST); glAlphaFunc(COMPARISION, VALUE);)丢弃片段;丢弃的片段将不会进行模板测试,因此您可以使用传递的片段来构建模板蒙版。

编辑代码示例

/* the order in which orthogonal OpenGL state is set doesn't matter */
glEnable(GL_STENCIL_TEST);
glEnable(GL_ALPHA_TEST);

/* don't write to color or depth buffer */
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepshMask(GL_FALSE);

glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);

/* first set alpha test so that fragments greater than the threshold
will pass thus will set nonzero bits masked by 1 in stencil */
glStencilFunc(GL_ALWAYS, 1, 1);
glAlphaFunc(GL_GREATER, threshold);
draw_textured_primitive();


/* second pass of the fragments less or equal than the threshold
will pass thus will set zero bits masked by 1 in stencil */
glStencilFunc(GL_ALWAYS, 0, 1);
glAlphaFunc(GL_LEQUAL, threshold);
draw_textured_primitive();

不要忘了还原OpenGL状态供以后绘制。

编辑以说明更新的问题:

由于您实际上是在使用iOS,因此OpenGL-ES 2.0会有所不同。 OpenGL-ES2中没有alpha测试。相反,如果您使用 discard关键字/语句,如果片段着色器低于/高于所选阈值,则应该丢弃片段着色器中的片段。

关于textures - 如何在OpenGL-ES 2.0中创建带有纹理(图像)的模板缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7571075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com