Archived

This topic is now archived and is closed to further replies.

Mercury_Linx

OpenGL Source key in OpenGL ?

Recommended Posts

Dear All, Thanks in advance I have a bitmap and happend to have a colour RGB(0, 0, 0) as the source key for this bitmap, and how do I set the source key value in OpenGL so that it would not blit this part, and I don''t want to have a saperate image as the mask ? Mercury_Linx

Share this post


Link to post
Share on other sites
Thanks for the tip.

But what I did in Dirext Draw is setting the source key onto the offscreen surface e.g: lpDDSurf->SetColorKey( ... , ... )

I was wonder if there is anything similar in OpenGL, which I don''t need to go thru pixel by pixel in the bitmap file myself ... I try to avoid that



A programmer, is the soul creator for computers.

Share this post


Link to post
Share on other sites
>>But what I did in Dirext Draw is setting the source key onto the offscreen surface e.g: lpDDSurf->SetColorKey( ... , ... )
I was wonder if there is anything similar in OpenGL, which I don''t need to go thru pixel by pixel in the bitmap file myself ... I try to avoid that<<

no, u need an alpha channel its not all bad news though with an alpha channel u can do nice blending effects

http://members.xoom.com/myBollux

Share this post


Link to post
Share on other sites
Thanks guys ! I''m really appreciate it

Oh ... by the way, is there any significant or different between using a bit mask or inspect the colour bit one by one during image loading time ? I''m talking in concern of hits on execution time and memory management.



...
A programmer, is the soul creator for computers.

Share this post


Link to post
Share on other sites
as most texture loading is done during init time, having an alpha channel will be far quicker.

http://members.xoom.com/myBollux

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Of course if you load a 32 bit (RGBA) image instead of a 24 bit image (RGB) the operation takes 25% more time.
But you load the texture one time at the beginning of your app or when you need it.
In hardware OpenGL implementation you get the same speed (32 bit parallelism).
In other words you pay for extra 25% memory use but not in speed.

One of the reasons for the fact that does not exist any operation like SetTransparentColor() is that in general the alpha byte is used for blend (8 bit transparency) so every texel needs its own alpha level. It''s not an OpenGL limit but another example of glSimplicity.

Share this post


Link to post
Share on other sites

What you need is load your 24 bit bitmap into a 32 bit image

LPBYTE pImage = new BYTE[width*height*4];

When you load the bitmap pixel per pixel simply skip one byte (your file is a 24 bit image).

Then process your image with a loop like this

DWORD transparent_color = RGB(tred, tgreen, tblue);
// for example transparent_color = 0 (black)

for(long pix=0; pix< width*height; pix++)
{
BYTE r = pImage[(pix<<2) + 0];
BYTE g = pImage[(pix<<2) + 1];
BYTE b = pImage[(pix<<2) + 2];
BYTE a; // not still defined

DWORD rgb = RGB(r, g, b);

if(rgb == transparent_color)
a = 0;
else
a = 255;

pImage[(pix<<2) + 3] = a;
}

Remember to define glAlphaFunc(GL_GREATER, 0) and
glEnable(GL_ALPHA_TEST); Of course you need also to SetUp your image as GL_RGBA texture!

It''s not difficult!

Share this post


Link to post
Share on other sites
Thanks alot for all the info

To Andrea:

Any reasons "pix << 2" ? Is it that you assume that r, g, b and a is all together 4 byte ? But why ? I'm a little puzzled here ...

By the way, do you have a complete sample which I can have a look and try out ?

Thanks again ...


...
A programmer, is the soul creator for computers.


Edited by - Mercury_Linx on May 2, 2001 8:28:06 PM

Share this post


Link to post
Share on other sites
Yes, I''ve assumed you are using 32 bit RGBA (it''s one of the most used format);

Every pixel is described by 4 components : red, green and blue (for 24 bit RGB) and an additional component (alpha for 32 bit RGBA). Every component is one byte lenght so you have 256 different levels for every component.

There are different method to store data images however the most typical is to store images by rows in RGBA order.
If you use 32 bit RGBA images you have 4 byte for every pixel
( infact in my example pix<<2 == pix*4 )

NOTE: Some video cards don''t support 32 bit texture however you can send a 32 bit texture to the card (OpenGL drivers should convert the image in the right format)(I use also a Vodoo3)

I''m sorry for the code but I''ve not a simple example to show you ( I''ve written my own cryptic C++ classes )...you can find some good example on NeHe site.

Share this post


Link to post
Share on other sites
You may want to consider writing a small wrapper function for the loader, like:

Load_Masked_Texture(char *Filename, BYTE r, BYTE g, BYTE b);

the r g and b would be 0-255, and would specify the color to be masked out..

You''re going to have to write the loading code anyway, you might as well make a nice interface to do it for any color..

Sorry if this one''s obvious, but I felt it was worth mentioning



"Like all good things, it starts with a monkey.."

Share this post


Link to post
Share on other sites
Thank you all for helping me figure out about the loading part

By the way, is it true that it does not matter what type of images that I''m loading, but as long as I have it properly convert into a byte string :

i.e: RGBARGBARGBARGBARGBARGBARGBARGBARGBARGBARGBARGBA

Then it''ll be ready to be blit onto a surface or as a texture in both OpenGL and DirectX ?



...
A programmer, is the soul creator for computers.

Share this post


Link to post
Share on other sites

You should read some example-code to understand how to texture map a polygon and on this topic there are A LOT OF tutorials!
However you have understood what exactly an image is : a byte buffer! And of course the first step to create a texture is the image that describes it.


Share this post


Link to post
Share on other sites

  • Partner Spotlight

  • Forum Statistics

    • Total Topics
      627638
    • Total Posts
      2978330
  • Similar Content

    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
    • By cebugdev
      hi guys, 
      are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
      Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic 
      let me know if you guys have recommendations.
      Thank you in advance!
    • By dud3
      How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below? 
      Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
      Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.
       
      References:
      Code: https://pastebin.com/Hcshj3FQ
      The video shows the difference between blender and my rotation:
       
    • By Defend
      I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
      My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
      * make lots of VAO/VBO pairs and flip through them to render different objects, or
      * make one big VBO and jump around its memory to render different objects. 
      I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
      If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?
       
    • By test opty
      Hello all,
       
      On my Windows 7 x64 machine I wrote the code below on VS 2017 and ran it.
      #include <glad/glad.h>  #include <GLFW/glfw3.h> #include <std_lib_facilities_4.h> using namespace std; void framebuffer_size_callback(GLFWwindow* window , int width, int height) {     glViewport(0, 0, width, height); } //****************************** void processInput(GLFWwindow* window) {     if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)         glfwSetWindowShouldClose(window, true); } //********************************* int main() {     glfwInit();     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);     //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);     GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);     if (window == nullptr)     {         cout << "Failed to create GLFW window" << endl;         glfwTerminate();         return -1;     }     glfwMakeContextCurrent(window);     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))     {         cout << "Failed to initialize GLAD" << endl;         return -1;     }     glViewport(0, 0, 600, 480);     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);     glClearColor(0.2f, 0.3f, 0.3f, 1.0f);     glClear(GL_COLOR_BUFFER_BIT);     while (!glfwWindowShouldClose(window))     {         processInput(window);         glfwSwapBuffers(window);         glfwPollEvents();     }     glfwTerminate();     return 0; }  
      The result should be a fixed dark green-blueish color as the end of here. But the color of my window turns from black to green-blueish repeatedly in high speed! I thought it might be a problem with my Graphics card driver but I've updated it and it's: NVIDIA GeForce GTX 750 Ti.
      What is the problem and how to solve it please?
  • Popular Now