Archived

This topic is now archived and is closed to further replies.

Mike

OpenGL Alpha blending with opengl

Recommended Posts

Mike    141
I''m just trying to draw a quad that is semi-transparent over what has already been drawn. I call glBlendFunc( GL_SRC_ALPHA, GL_ONE ); in my gl init code and when i go to draw the quad that i want to blend with what''s on the screen i call glEnable( GL_BLEND ) and after i''m done drawing that quad i call glDisable( GL_BLEND ). This kind of works. The quad blends with what''s there but very poorly. I expected to be able to control the alpha blending with the fourth parameter in glColor4f() which I call like so:
	glBindTexture( GL_TEXTURE_2D, m_texture[1] );

	if( m_bBlend )
	{
		glEnable( GL_BLEND );
		glDisable( GL_DEPTH_TEST );
	}

	glBegin( (m_bWireFrame)? GL_LINE_LOOP : GL_QUADS  );
		glNormal3f( 0.0f, 0.0f, 1.0f );
		glColor4f( 1.0f, 1.0f, 1.0f, 0.5f );
		glTexCoord2f( 0, 0 ); glVertex3f( -60, -10,  -95);
		glTexCoord2f( 10, 0 ); glVertex3f(  60, -10,  -95);
		glTexCoord2f( 10, 1 ); glVertex3f(  60,  10,  -95);
		glTexCoord2f( 0, 1 ); glVertex3f( -60,  10,  -95);
	glEnd();

	if( m_bBlend ) 
	{ 
		glEnable( GL_DEPTH_TEST ); 
		glDisable( GL_BLEND );
	}
 
What is wrong with what i have done?

Share this post


Link to post
Share on other sites
jitspoe    122
try GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA. Also, make sure the transparent polygon is drawn AFTER everything else. If you have multiple transparent polygons, you have to sort them and draw them furthest to closest.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
An alternative to drawing the transparent polygon last is surrounding the drawing of transparent polygons with a disable/enable of depth WRITING (make sure GL_DEPTH_TEST is still enabled though). Disabling is done via glDepthMask(GL_FALSE), and enabling with GL_TRUE.

Share this post


Link to post
Share on other sites
Basiror    241
no it isn t as far as i know you can set specific alpha values for each vertex that way

would be interesting if you could do lighning that way as well
black color for the shadows vertices and white colors for the enlightened*spelling?* vertices

Share this post


Link to post
Share on other sites
zedzeek    528
u have normal + color!
with lioghting enabled color is ignored (unless u use color_material) so first disable lighting + then try it

http://uk.geocities.com/sloppyturds/kea/kea.html
http://uk.geocities.com/sloppyturds/gotterdammerung.html

Share this post


Link to post
Share on other sites
bakery2k1    712
zedzeek is right. With lighting enabled, the alpha component is taken to be that of the diffuse material color, and glColor is ignored. The material color can be set by using glMaterial, or made to track the current color by using glColorMaterial.

http://users.ox.ac.uk/~univ1234

Share this post


Link to post
Share on other sites
zedzeek    528
yes there is BUT u dont want to do it i beieve
use color_material
why dont u want to do it?
cause youre draw one quad over the screen ie 4 vertices
what u shoudl do is turnoff lighting draw quad + then turn lighting back on.

http://uk.geocities.com/sloppyturds/kea/kea.html
http://uk.geocities.com/sloppyturds/gotterdammerung.html

Share this post


Link to post
Share on other sites
zedzeek    528
i assure u it is the right way to do things

http://uk.geocities.com/sloppyturds/kea/kea.html
http://uk.geocities.com/sloppyturds/gotterdammerung.html

Share this post


Link to post
Share on other sites

  • Similar Content

    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
    • By C0dR
      I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
      Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.
       
      The following features are implemented:
      Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping  
      You can find the repository at https://github.com/0x2A/physicam
       
      I would be happy about feedback, suggestions or contributions.

    • By altay
      Hi folks,
      Imagine we have 8 different light sources in our scene and want dynamic shadow map for each of them. The question is how do we generate shadow maps? Do we render the scene for each to get the depth data? If so, how about performance? Do we deal with the performance issues just by applying general methods (e.g. frustum culling)?
      Thanks,
       
  • Popular Now