![]() |
Tutorial 10: Transparency Some polygons are transparent (see through), and we need to render them as such. The window of a car and a glass cup are examples of transparent objects. This tutorial shows you how to add transparency to your polygons and models. |
![]() |
![]() |
![]() |
Control | Name | DoubleBuffer | Left | Top | Maximize Button |
Window | SurfaceWindow | - | - | - | ON |
OpenGLSurface | Surface | ON | 0 | 0 | - |
![]() |
Self.MouseCursor = System.Cursors.StandardPointer |
Surface.Render |
X3_Initialize X3_EnableLight OpenGL.GL_LIGHT0, new X3Core.X3Light(0, 0, 1) |
X3_SetPerspective Surface |
OpenGL.glEnable OpenGL.GL_DEPTH_TEST OpenGL.glDepthMask OpenGL.GL_TRUE OpenGL.glCullFace OpenGL.GL_BACK OpenGL.glEnable OpenGL.GL_CULL_FACE OpenGL.glEnable OpenGL.GL_LIGHTING OpenGL.glEnable OpenGL.GL_COLOR_MATERIAL OpenGL.glEnable OpenGL.GL_BLEND OpenGL.glBlendFunc OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA |
' Replace... ' OpenGL.glColor3d(poly.FillColor.Red, poly.FillColor.Green, poly.FillColor.Blue) ' with OpenGL.glColor4d(poly.FillColor.Red, poly.FillColor.Green, poly.FillColor.Blue, poly.FillColor.Alpha) ' and replace... ' OpenGL.glColor3d(1, 1, 1) ' with OpenGL.glColor4d(1, 1, 1, 1) |
OpenGL.glClearColor(0, 0, 0, 1) OpenGL.glClear(OpenGL.GL_COLOR_BUFFER_BIT + OpenGL.GL_DEPTH_BUFFER_BIT) ' render colored cube in background OpenGL.glPushMatrix OpenGL.glTranslatef 2.5, 0, -8.0 OpenGL.glRotated(30, 1, 0, 0) OpenGL.glRotated(30, 0, 1, 0) X3_RenderModel X3Test_Cube1 OpenGL.glPopMatrix ' render transparent cube OpenGL.glPushMatrix OpenGL.glTranslatef 0, 0, -4.0 OpenGL.glRotated(20, 1, 0, 0) OpenGL.glRotated(25, 0, 1, 0) X3_RenderModel X3Test_Cube3 OpenGL.glPopMatrix |
OpenGL.glEnable OpenGL.GL_DEPTH_TEST OpenGL.glDepthMask OpenGL.GL_TRUE OpenGL.glCullFace OpenGL.GL_BACK OpenGL.glEnable OpenGL.GL_CULL_FACE OpenGL.glEnable OpenGL.GL_LIGHTING OpenGL.glEnable OpenGL.GL_COLOR_MATERIAL OpenGL.glEnable OpenGL.GL_BLEND OpenGL.glBlendFunc OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA |
|
We added two new instructions to our initialization routine. For transparent rendering we first enabled GL_BLEND with the glEnable instruction. Next we used glBlendFunc to indicate to OpenGL how the source color (the color of the polygon we're drawing) should be combined with the destination color (the color already in the color buffer). The final color is calculated as follow:
Using GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA as source and destination factors respectively, is a common method to render semi-transparent polygons. |
OpenGL.glColor4d(poly.FillColor.Red, poly.FillColor.Green, poly.FillColor.Blue, poly.FillColor.Alpha) |
Notice that we now use the glColor4d function, instead of the glColor3d function, to set our drawing color. The additional alpha value added to the parameter list, gives us the ability to specify the transparency of the color, in addition to its red, green and blue components. |
![]() |
![]() |