Jump to content
  • Advertisement
Sign in to follow this  
glunoy

OpenGL Help:How to write a texture into stencil buffer using its not transparent pixels?

This topic is 841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everybody,

       I want to write a texture with alpha channel into a stencil buffer,like:mask.png.It contains only one circle in the center,not transparent,Then i want to draw another texture which is also contain alpha channel,like:cloud.png.What i want is only display the clound with the circle rect?meanwhile the cloud.png should not be black in its transparent rect. But this code blow just make the screen flicker?and i don't know what's wrong with that.I am a newbie in openGL,so please anyone can tell me,Am I write to the stencil buffer right?Or both them is wrong ?Thanks again.

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer
	glLoadIdentity();									// Reset The Modelview Matrix
	glTranslatef(0.0f, 0.0f, -2.0f);
	glClearColor(1.0f, 0.0f, 1.0f, 0.0f);

	glDisable(GL_DEPTH_TEST);
	glEnable(GL_STENCIL_TEST);
	glEnable(GL_BLEND);									

	//writing mask.png into stencil...only the not transparent pixels set to 1
	glEnable(GL_ALPHA_TEST);
	glAlphaFunc(GL_NOTEQUAL, 0.0);//	glAlphaFunc(GL_GREATER, 0.5);
	glStencilFunc(GL_ALWAYS, 1, 1);
	glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
	
	glBindTexture(GL_TEXTURE_2D, mask_img);
	glBegin(GL_QUADS);							
	glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.1f, -1.1f, 0.0f);	
	glTexCoord2f(1.0f, 0.0f); glVertex3f(1.1f, -1.1f, 0.0f);	
	glTexCoord2f(1.0f, 1.0f); glVertex3f(1.1f, 1.1f, 0.0f);	
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.1f, 1.1f, 0.0f);
	//glEnd();
	
	//now write the img.png just pass the stencil test part 
	glDisable(GL_ALPHA_TEST);
	glStencilFunc(GL_ALWAYS, 0, 1);
	glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	glBindTexture(GL_TEXTURE_2D, img);
	glBegin(GL_QUADS);								
	glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.1f, -1.1f, 0.0f);	
	glTexCoord2f(1.0f, 0.0f); glVertex3f(1.1f, -1.1f, 0.0f);	
	glTexCoord2f(1.0f, 1.0f); glVertex3f(1.1f, 1.1f, 0.0f);	
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.1f, 1.1f, 0.0f);
	glEnd();									


	glEnable(GL_DEPTH_TEST);	
	glDisable(GL_STENCIL_TEST);
	glDisable(GL_BLEND);	

Share this post


Link to post
Share on other sites
Advertisement

I found this, perhaps it might help.

http://stackoverflow.com/questions/11964593/how-to-use-a-stencil-buffer-with-an-alpha-mask

Looks like this is the function you need to use (which I see you are using)

https://www.opengl.org/sdk/docs/man2/xhtml/glAlphaFunc.xml

 

I attempted something similar not to long ago but in the end I just accessed the mask directly in my shader and used the alpha value there which saved me a lot of trouble messing with a stencil buffer. Is that an option for you? I was just attempting to merge two textures so it was fairly basic but if you are doing more complex rendering I'd imagine a stencil buffer might be needed.

Edited by Nanoha

Share this post


Link to post
Share on other sites
 

I found this, perhaps it might help.
http://stackoverflow.com/questions/11964593/how-to-use-a-stencil-buffer-with-an-alpha-mask
Looks like this is the function you need to use (which I see you are using)
https://www.opengl.org/sdk/docs/man2/xhtml/glAlphaFunc.xml
 
I attempted something similar not to long ago but in the end I just accessed the mask directly in my shader and used the alpha value there which saved me a lot of trouble messing with a stencil buffer. Is that an option for you? I was just attempting to merge two textures so it was fairly basic but if you are doing more complex rendering I'd imagine a stencil buffer might be needed.

 
Thanks for your help.The first link you gave is much similiar as mine.He said he had sloved the problem,but my sample still not work.That's really strange.
And yes, pixel shader is a much easy way to do this.I will try that.

Share this post


Link to post
Share on other sites

Thanks @Nanoha,finally i made it.here is what i want.

[attachment=31282:QQ??20160329164414.png]

[attachment=31283:QQ??20160329164108.png]

and......the code is so simple!!

here is the pixel shader files

#version 130
 
varying vec2 TextCoord;
uniform sampler2D tex1;
uniform sampler2D tex2;
 
void main()
{
vec4 clr_tex1=texture2D(tex1, TextCoord);
vec4 clr_mask=texture2D(tex2, TextCoord);
gl_FragColor =clr_tex1*clr_mask.a;
}

 

 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!