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

Recommended Posts

Hi guys! I am a beginner in GLUT programming, pleeaase help me out here people... This is what I am trying to do: I want to apply a texture on an object in my scene, and I want the black parts of the texture transparent...that is, I should be able to see the object behind this textured one.... I know its related to blending/masking, etc.. but I cant quite get the full picture... I have been searching the internet for hours now trying to find an example code for this purpose that I can study...Some of the codes done implement fully what I want...some do implement what I want, but are in a different language...the rest do implement what I want, but the code is tooo long and complex to study and understand from: they implement too many things at the same time, and then I get lost... So can someone please tell now can I apply a texture on an object, and make the black parts of the texture/object transparent ... A small and simple code would be really appreciated... thanx in advance...

Share on other sites
I cant remember how to do it in opengl, but the theory of what your doing is setting the alpha channel in the texture. Then you need to set the correct texture format to one that has a texture in, 5551 which would be 5 bits for each RGB and 1 for alpha. The alpha bit would set the transparent pixels.

Share on other sites
Rand thanx for the help, but I already know that alpha channel is involved in this to apply the transparency...I just dont have an undrestandable practical example....I need to see some simple implementation of the idea, that I can learn from...

Share on other sites
Quote:
 Original post by ahmadkaI want to apply a texture on an object in my scene, and I want the black parts of the texture transparent...that is, I should be able to see the object behind this textured one....

Just out of curiosity, why do you want black portions of your texture to be transparent. Can't you just make the portions of the texture that you want to see-through completely transparent in the image itself.

I am assuming you already know that image formats like png, tga and gif can have transparency information. Jpeg formats don't have transparency information.

After exporting the image to a proper format, its just a matter of enabling blending before applying the texture.
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

Here is the link to Nehe's tutorial on blending. He explains things in detail.

Share on other sites
I tried reading Nehe's tutorial...the code does exactly what I require, but the code is too difficult for me :P .... most of the stuff in there is not what I want...I tried to remove the unnecessary functions, to filter out stuff I dont need, but then the code didnt work..it gave some strange runtime error..there was nothing wrong with the code itself though...

Share on other sites
Could you post some portion of your code here. So we can better understand what you do. Did you make the black parts of your image transparent?

Share on other sites
Alright. Here's how it's done. I only know how to with Pygame/PyOpenGL. See nehe tutorial for doing it in C++. Pygame is very intuitive, so even if you don't know it, you should be able to translate it or find someone else to do it for you. However, if you want SIMPLIFIED code, here it is:

import osfrom OpenGL.GL import *from OpenGL.GLU import *import pygame, pygame.imagefrom pygame.locals import *textures = [0]def resize((width, height)):    if height==0:        height=1    glViewport(0, 0, width, height)    glMatrixMode(GL_PROJECTION)    glLoadIdentity()    gluPerspective(45, 1.0*width/height, 0.1, 100.0)    glMatrixMode(GL_MODELVIEW)    glLoadIdentity()def init():    glEnable(GL_TEXTURE_2D)    glShadeModel(GL_SMOOTH)    glClearColor(0.0, 0.0, 0.0, 0.0)    glClearDepth(1.0)    glEnable(GL_DEPTH_TEST)    glDepthFunc(GL_LEQUAL)    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)def main():    video_flags = OPENGL|DOUBLEBUF        pygame.init()    surface = pygame.display.set_mode((640,480), video_flags)    resize((640,480))    init()    BackgroundFile = os.path.join('Data','Background.png')    ImageFile = os.path.join('Data','Image1.png')        BackgroundSurface = pygame.image.load(BackgroundFile).convert()    ImageSurface = pygame.image.load(ImageFile).convert()    ImageSurface.set_colorkey((0,0,0), 0)        BackgroundData = pygame.image.tostring(BackgroundSurface, "RGBA", 1)    ImageData = pygame.image.tostring(ImageSurface, "RGBA", 1)    while 1:        glEnable(GL_BLEND)        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)        glLoadIdentity()        glTranslatef(0.0,0.0,-5.0)        glRotatef(xrot,1.0,0.0,0.0)        glRotatef(yrot,0.0,1.0,0.0)        glRotatef(zrot,0.0,0.0,1.0)        glBindTexture(GL_TEXTURE_2D, textures[0])        glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, BackgroundSurface.get_width(), BackgroundSurface.get_height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, BackgroundData );        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)        glBegin(GL_QUADS)        glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0,  -2.0)        glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0,  -2.0)        glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0,  -2.0)        glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0,  -2.0)        glEnd();        glBindTexture(GL_TEXTURE_2D, textures[2])        glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, ImageSurface.get_width(), ImageSurface.get_height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, ImageData );        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)        glBegin(GL_QUADS)        glTexCoord2f(0.0, 0.0); glVertex3f(-1.0, -1.0,  2.0)        glTexCoord2f(1.0, 0.0); glVertex3f( 1.0, -1.0,  2.0)        glTexCoord2f(1.0, 1.0); glVertex3f( 1.0,  1.0,  2.0)        glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,  1.0,  2.0)        glEnd();        event = pygame.event.poll()        if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):            break        pygame.display.flip()if __name__ == '__main__': main()

That's your entire program! But remember this: It will ONLY WORK if you save you mask to the alpha channel! Do this when you create your image in a paint program. (I have 'Paint Shop Pro 6', so I go "masks->new->from image", then a window pops up and click OK. Then "masks->save to alpha channel". Save it as anything and you are good to go)! Thx to _neutrin0_ for these lines:
glEnable(GL_BLEND);glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

By the way, I had the same problem as you, ahmadka. (That's why I take such a HUGE interest in this problem. I too, spent days actually, searching the internet for any signs of possibility. (See here) : http://www.gamedev.net/community/forums/topic.asp?topic_id=443159

Geometrian

[Edited by - Geometrian on April 14, 2007 1:25:15 PM]

Share on other sites
For a chain-link fence or grass type effect then this is the tutorial you're looking for: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=20

Share on other sites
You cant mask in opengl with one texture you need 2. One will be b/w, with I believe black being the portion that is able to be seen. The nehe tutorial pretty much is what to do for masking. You just need to get your blend modes right.

Share on other sites
NOT TRUE! If you write the program the way I did it, you only need the background and the object with a mask. I know about nehe and his tutorial, but this is a much simpler way to do it; you don't have to make extra files or code!

I do have one problem with it, though. If you happen to have 5 or 6 mask gridlike things like in nehe's tutorial OVER each other, some will not be visible. I am currently researching this. I don't know of any other drawbacks than this to doing it this way. I don't even know if the nehe tutorial doesn't have this problem.

But, if you want to have one mask over a background(s), this is a simple way to do it. :)

• 47
• 12
• 10
• 10
• 9
• Forum Statistics

• Total Topics
631373
• Total Posts
2999643
×