Mega Code Archive

 
Categories / Delphi / Graphic
 

OpenGL VI using Alpha blending (transparency) updated

Title: OpenGL VI: using Alpha blending (transparency) - updated Question: The next in the series of articles on OpenGL with Delphi, Alpha Blending which basically is just adding transparency to objects, here's a screenshot of this demo for whoever can't see the article answer in this demo I show you how to Enable it and Disable it for a more realistic usage of it... you wouldn't want all your objects to be transparent most of the time Answer: Previous article on this (OpenGL) subject: OpenGL V: Texture filtering Also you can keep checking here for future articles on OpenGL Alpha Blending: This basically is combining the color of the pixel you are about to draw with the color of the pixel that is already drawn at that position; this gives the effect of transparency to see some theory (the math behind) about Blending read this article: NeHe Productions: OpenGL Lesson #08 We Delphi programmers have seen the alpha blending in the Delphi forms, the AlphaBlend and AlphaBlendValue properties of any Delphi form, it looks pretty cool when you use those, doesn't it? well, same thing can be applied to any shapes you draw using OpenGL, suppose you want to draw some crystal in your OpenGL application, you wouldn't want your crystal to be solid color, would you... you would want to be able to see trough it! that's what we are going to do now enabling Blending in OpenGL is pretty easy, you just call these functions glEnable(GL_BLEND); // Turn blending On glBlendFunc(GL_SRC_ALPHA, GL_ONE); // (Type of blending) Set the blending function for translucency then you can use glColor4f() which takes, RedGreenBlueAlpha (Alpha being the transparency) glColor4f(1.0, 1.0, 1.0, 0.7); the lower the value of the last parameter the more transparent the stuff you draw is, 0 is completly transparent, 1 would be completly opaque so, our initializing code would be something like this: { All Setup For OpenGL Goes Here } function InitGL(Const Width, Height: Glsizei): Bool; var fWidth, fHeight: GLfloat; begin glClearColor(0.0, 0.0, 0.0, 0.0); //Black Background glClearDepth(1.0); //Depth Buffer Setup glDepthFunc(GL_LESS); //Text glShadeModel(GL_SMOOTH); //Enables Smooth Color Shading glMatrixMode(GL_PROJECTION); glLoadIdentity(); //reset the View (move to 0, 0, 0) fWidth := Width; fHeight := Height; gluPerspective(45.0, fWidth/fHeight, 0.1, 100); //Calculate Aspect Ratio Of The Window glMatrixMode(GL_MODELVIEW); glEnable(GL_TEXTURE_2D); //and very important, ENABLE TEXTURES!!! (new) LoadGLTextures; //we load our Textures here glLightfv(GL_LIGHT1, GL_AMBIENT, @LightAmbient); glLightfv(GL_LIGHT1, GL_DIFFUSE, @LightDiffuse); glLightfv(GL_LIGHT1, GL_POSITION,@LightPosition); glEnable(GL_LIGHT1); Angle:=0.0 glEnable(GL_BLEND); // Turn Blending On glColor4f(1.0, 1.0, 1.0, 0.7); // set color to full brighness and 0.7 of alpha blending glBlendFunc(GL_SRC_ALPHA, GL_ONE); // Set The Blending Function For Translucency end; only the last 3 lines are new to what I have shown you in other articles; the rest of the code is exactly the same, that's all it takes to do Alpha Blending in OpenGL and to disable it, is just as simple as calling: glDisable(GL_BLEND); Note: when using blending ON and OFF and want objects behind the blended object (a crystal for example) you have to draw the non-blending first and then the blending stuff (unless of course, the object has to be over the blended object), in the demo I show you this technique //First draw the non-blending (behind objects), notice glDisable(GL_BLEND); glLoadIdentity(); //move to 0, 0, 0 //glRotate(Angle, 0.0, 0.0, 1.0); //put this back if you want this to rotate glTranslatef(0, 0, -12); glBindTexture(GL_TEXTURE_2D, texture[1]); //use our second texture glDisable(GL_BLEND); //don't want blending here glColor3f(1.0, 1.0, 1.0); //full brighness, please play with these! DrawWindow; //then draw the blending stuff, notice glEnable(GL_BLEND), glBlendFunc, glColor4f //you only need this if you changed the colors above glColor4f(1.0, 1.0, 1.0, 0.7); // set color to full brighness and 0.7 of alpha blending glEnable(GL_BLEND); // Turn Blending On glBlendFunc(GL_SRC_ALPHA, GL_ONE); // Set The Blending Function For Translucency glBindTexture(GL_TEXTURE_2D, texture[0]); glLoadIdentity(); //Reset The View (move to 0, 0, 0) glTranslatef(0, 0, -7); glRotate(Angle, 1.0, 0.0, 0.0); DrawWindow; glRotate(-Angle*2, 1.0, 0.0, 0.0); DrawWindow; basically when drawing 3D stuff you always need to have the objects you are drawing in order, so that you draw the farther (is that the right word?) objects first, then the nearest I've been posting one by one the things you can do with OpenGL, just because I think is better to practice and easier to learn one by one, and each thing you learn you can practice it separated instead of trying to understand a big project all at once, graphics programming is not easy, but is really cool, sometimes just by playing with angles and positions of the stuff you draw, you come up with some really cool and unique effects you can download the code for the article here: I strongly encourage you to play with the angles, positions, etc you can find the GLAux unit and dll here: in the next articles I'm going to show you how to add fonts to your applications and sound!, so keep checking here for future articles on OpenGL keep up coding salu2 EberSys