First, glEnable(GL_CULL_FACE)
. Draw the sphere once with glCullFace(GL_FRONT)
. Draw the cube (without culling). Lastly, draw the sphere again with glCullFace(GL_BACK)
.
Making a texture translucent in PyOpenGL
Question
In my scene I have a cube within a sphere. I would like to apply a RGB texture to a sphere and make it translucent. I can import the texture, apply it to the sphere and make it translucent but i am not getting the result I want.
My blend function is glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
My draw method is as follows:
def DrawGLScene():
rotation1 = 60
rotation2 = 60
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glEnable(GL_BLEND)
glLoadIdentity()
glTranslatef(0.0, 0.0, -8.0)
glRotatef(rotation1, 1.0, 0.0, 0.0)
glRotatef(rotation2, 0.0, 1.0, 0.0)
glColor4f(1.0,1.0,1.0,1.0)
glutSolidCube(2);
glEnable(GL_COLOR_MATERIAL)
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D,1)
glColor4f(1.0,1.0,1.0,0.3)
Q=gluNewQuadric()
gluQuadricNormals(Q, GL_SMOOTH)
gluQuadricTexture(Q, GL_TRUE)
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP)
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP)
gluSphere(Q, 2.35, 32, 16)
glDisable(GL_TEXTURE_2D)
glutSwapBuffers()
The result looks like this:
It seemed there were depth problems as I could only see the back side of the sphere in parts in the bottom right. I then set glDepthFunc(GL_LESS) for drawing the cube and glDepthFunc(GL_ALWAYS) for drawing the sphere which resulted in:
Now the back side of the sphere is showing through the cube.
I have tried multiple combinations of glDepthFunc() for the objects but to no avail. Is the problem to do with how I am handling depth, blending or something else?
EDIT:
Using glEnable(GL_CULL_FACE)
and glCullFace(GL_BACK)
in the first example gives me the following which is still not correct as now you cannot see the back of the sphere at all.
Solution
OTHER TIPS
Keep in mind, that blending and depth testing don't mix. You always must manually sort geometry far-to-near when doing blending. Generic order independent transparency is still a largely unsolved problem in rasterizers.
EDIT: Op did already set blend function. You must set a blend function. The default one is glBlendFunc(GL_ONE, GL_ZERO)
which is surely not what you want. Most likely you want glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
for a regular alpha channeled texture or glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)
for a premultiplied alpha texture.