Page 2 of 5 FirstFirst 1234 ... LastLast
Results 11 to 20 of 43

Thread: 2D using OpenGL

  1. #11

    2D using OpenGL

    Quote Originally Posted by savage
    Have a look at the SDL code here -
    http://osdl.sourceforge.net/OSDL/OSD...-examples.html
    It shows another way of achieving colour keyed transparency.

    Don't forget that since you are using OpenGL you will need to tell users of your software to create Power of 2 textures if they want the least amount of hassle amongst graphics.
    Or convert all of the graphics to power of 2 textures on the fly if you don't want to place the limit on your users. Many times you can combine textures and then perform fills as necessary behind the scenes.

  2. #12

    2D using OpenGL

    The power of 2 textures shouldn't be a problem. I just told them allready I have a folder of example images... Under no circumstances they are allowed to change the size unless they ask me about it first...

    About the tutorial... It uses SDL again, and somehow most computers here have some working fast with SDL.

    Can anyone tell me how to do some sort of loop over the pixels of an OpenGL texture and generate a mask accordingly?

    Example:
    - I have a texture.
    - Do a for loop on each pixel.
    - If the pixel is pink, add a black pixel on the same coordinates on the mask.
    - Otherwise, add a white pixel on the same coordinates on the mask.

    Or better yet:
    - Load a texture.
    - Do a loop on each pixel.
    - If pink, change the color to the transparent color (like in gifs)
    - Otherwise leave it the way it is.

    I believe that should do the trick, but now.... how to do that?

    Don't get me wrong here, but this time I want to do something without SDL. The previous school project was all SDL and I don't think the people juding my internship would like me using that again...
    Huhuhu

  3. #13

    2D using OpenGL

    I'm not sure I understand you entirely, but it is really not needed to have a seperate file to get transparency. Tga files for example have an extra alpha layer (provided they're saved correctly (32 bit)) Using Paint Shop Pro or PhotoShop its fairly easy to create an alpha layer.

    The whole suggestion of looping through an image, reading each pixel and change it to a certain color is not only a long and difficult process, it is also completely unnecessary.

    Have a look at this sample. It has everything you need.

  4. #14

    2D using OpenGL

    Guess I'm stuck with either a mask or an image with transparency....

    Shouldn't be a really big problem... Just takes some getting used to... I know I can make the TGA or GIF images, but unfortunatelly I'm working on computers where they've only heard of paint :s

    I'll just make a written tutorial on how to make them, then.
    Either that or a seperate program which makes makes masks of each of my images
    Huhuhu

  5. #15

    2D using OpenGL

    Or you can simply use alpha channels. Where you would normally give opengl an GL_RGB data, you can also give 4 bytes per pixel, GL_RGBA. This allows using of translucent png textures too. There you could give texture loading function a colorkey.

  6. #16

    2D using OpenGL

    Quote Originally Posted by Smotsholle
    Guess I'm stuck with either a mask or an image with transparency....

    Shouldn't be a really big problem... Just takes some getting used to... I know I can make the TGA or GIF images, but unfortunatelly I'm working on computers where they've only heard of paint :s

    I'll just make a written tutorial on how to make them, then.
    Either that or a seperate program which makes makes masks of each of my images
    I could be wrong, but I don't think gif supports true transparency like TGA or Png. I think it only does Color key transparency. There is also the licencing issues with gif as well.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  7. #17

    re-

    Load a texture.
    - Do a loop on each pixel.
    - If pink, change the color to the transparent color (like in gifs)
    - Otherwise leave it the way it is.

    I believe that should do the trick, but now.... how to do that?
    Actually, your are right; that is the way most people do (including my self) when you wants to use transparency (but avoiding the exra work to use a mask black/white texture) defining a pre-accorded color.

    You have to load the whole texture data from file into a block memory and then process every pixel, if the RGB pixel color is 255,0,255 (or any other color you want) then put the alpha component equal to 0, if the pixel IS NOT pinky then put alpha = 1.

    How to load a bitmap and inspect/edit every pixel?,

    The easy way is to use standars BMP file (you say you preffer to use paint.exe program) in 32bit color format, so for every pixel you wil have 4 bytes (RGBA), tell to your artis to use full 255,0,255 color for define transparent areas in the sprite.

    Then in delphi use a temporal TBITMAP for load your bmp file, then allocate a block memory using getmem(pixels, bitmap.width*bitmap.height*4) and then extract there all pixels from the bitmap using Bitmap.scanline() function. Every 4 bytes starting from the pixel pointer are the RGBA pixel color, procces the whole block memory.

    *Hint: There is a delphi units out there called GraphicsEX which expand the TIMAGE component to load almost every picture file format (jpg, tga, tif, psd, pcx.. etc) using Timage.picture.loadfromfile() then you can use TBITMAP.canvas.draw(0,0,timage.picture) for convert what ever picture you loaded (in whatever bit color format) into your 24bit color bitmap.

    Once you have your picture in memory with the pinky color with alpha=0 then build the texture fragment and enable the apropiate rendering parameters using the API you choose, opengl or directx.

    I have implemented this technique full working but using directx, for opengl i found this example:

    http://www.codesampler.com/oglsrc/og...#ogl_color_key

    I have checked and seem the opengl way it is much more easier than in directx...


    define the texture like this:

    Code:
            glGenTextures&#40; 1, &g_textureID &#41;;
            glBindTexture&#40; GL_TEXTURE_2D, g_textureID &#41;;
            glTexParameteri&#40;GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST&#41;;
            glTexParameteri&#40;GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST&#41;;
    
            // Don't forget to use GL_RGBA for our new image data... we support Alpha transparency now!
            glTexImage2D&#40; GL_TEXTURE_2D, 0, GL_RGBA, pImage_RGB->sizeX, pImage_RGB->sizeY, 0,
                          GL_RGBA, GL_UNSIGNED_BYTE, pImage_RGBA &#41;;

    Then every time you are going to render the rectangles with transperent pixels do this:


    Code:
           glEnable&#40; GL_BLEND &#41;;
            glBlendFunc&#40; GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA &#41;;
    Again, i have not tried that technique in opengl, but in directx, let me know if it work in your opengl test.


    * This technique also works in 16 bit color mode, but it takes a litle more work to convert full 32 bit color to 16bit color, this is only necesary if your target machine specs are low.


    good luck.

    tp.

  8. #18

    2D using OpenGL

    I've tried to convert that example source to delphi and I cannot get the .data attribute of a PTAUX_RGBImageRec to give me any info... It is a pointer, but to what? That is an eternal mystery
    Second problem there is that there isn't a RGBA brother of PTAUX_RGBImageRec.

    Tried to get a TBitmap to link to PTAUX_RGBImageRec too, but with no succes. Too bad...

    But I've got a demo with masking working, so that's good news
    Huhuhu

  9. #19

    Re: re-

    Quote Originally Posted by tpascal
    if the pixel IS NOT pinky then put alpha = 1.
    Actually alpha would be 255 for visible areas, 0 for complete invisibility But yes, that's the technique i used too.

    For data passing it could be something like this:

    [pascal]// Define record for each pixel
    TPixelData = record
    r,g,b,a: byte;
    end;

    // Way of using dynamic arrays...
    // Expected data is actually 2 dimensional but it is
    // continuous data in width blocks
    TPixelArray = array[0..0] of TPixelData;
    PPixelArray = ^TPixelArray;

    // Then to allocate:
    procedure LoadTexture(...);
    var data: PPixelArray;
    begin
    data:=allocmem(width*height*sizeof(TPixelData));
    // Do data filling and texture loading here...
    Freemem(data);
    end;[/pascal]

  10. #20

    re-

    Actually alpha would be 255 for visible areas, 0 for complete invisibility But yes, that's the technique i used too.
    You are right, i was using 1 value coz i am working in 16 bit color mode (1555 bit scheme), for 32 bit color Alpha=0 mean full transparent, alpha=255 mean full opaque.


    I've tried to convert that example source to delphi and I cannot get the .data attribute of a PTAUX_RGBImageRec to give me any info...
    That is why i hate C language, :roll:, the example was more meaning to show you the opengl commands and parameters that enable the transparent rendering using alpha channel.

    For loading and proccesing the bitmap file please check your delphi help about Tbitmap class methods loadfromfile() and scanline property or search for any custom bitmap file loading rutine out there.

    tp.

Page 2 of 5 FirstFirst 1234 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •