Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

OpenGL fadeIn method

BuxeBuxe Posts: 8Member
Below there is a simple FadeIn method which does not work correctly.
I post it for you may know what is wrong. I use OpenGL 2D, double buffered mode.

Whenever I call fadein() I always get a black screen which is wrong for I expect the current screen to be faded in.

void fadein()
{
CTimer timer; // just a timer
double min_delay_per_step = min_delay / 9.0;

int nWidth = 640;
int nHeight = 480;
int nSize = 4 * 640 * 480;

unsigned char *pImage = new unsigned char[nSize];
unsigned char *pAlpha = new unsigned char[nSize];

glPixelStorei(GL_PACK_ALIGNMENT,1);
glReadBuffer(GL_BACK);
glReadPixels(0,0,nWidth, nHeight, GL_RGBA, GL_UNSIGNED_BYTE,
pImage);

glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glEnable(GL_BLEND);

timer.SetTime();

int shift; // shift amount
for (int i=0; i <= 8; i++)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

unsigned char *pSrc = pImage;
unsigned char *pDest = pAlpha;

shift = 8-i;
for (int j = 0; j < nWidth*nHeight; j++)
{
*pDest = *pSrc >> shift; // blue
pSrc++, pDest++;
*pDest = *pSrc >> shift; // green
pSrc++, pDest++;
*pDest = *pSrc >> shift; // red
pSrc++; pDest++;
pSrc++; pDest++; // alpha
}

glDrawBuffer(GL_BACK);
glDrawPixels(nWidth, nHeight, GL_RGBA,
GL_UNSIGNED_BYTE, pAlpha);

glFlush();

// simply swap opengl buffers (double buffered mode)
swapbuffers((int*)window.hDC);

timer.WaitUntil(min_delay_per_step);
timer.SetTime();
}

delete pImage;
delete pAlpha;

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glDrawBuffer(GL_BACK);
}

Some things are sufficent I know but I hope you can understand it well.
I am thankfull for any kind of help for I have gone mad over this.

Comments

  • gautamgautam Posts: 642Member
    I'll show you a simpler method :). However this uses texture mapping. Its just a quick hack and easier to do IMO :)

    1. First texture map the image. (works fine in ortho mode)
    2. Render the texture using glEnable(GL_BLEND) and glColor4f(1, 1, 1, alpha);
    3. Decrement the alpha slowly at a certain velocity. It would look something like the following

    [code]
    float alpha = 1;
    float alphaVel = 0.1;
    GLuint texture;

    // load the texture before doing this

    glColor4f(1, 1, 1, alpha);
    glBindTexture(GL_TEXTURE_2D, texture);
    glBegin(GL_QUADS);
    glTexCoord2f(...); glVertex3f(...);
    ....
    glEnd();


    if(alpha <= 0) alpha = 0;
    alpha -= alphaVel;
    [/code]

    This is faster than using glDrawPixels which IMHO is slow. :) Hope it helps. If this does not help please let us know.




    : Below there is a simple FadeIn method which does not work correctly.
    : I post it for you may know what is wrong. I use OpenGL 2D, double buffered mode.
    :
    : Whenever I call fadein() I always get a black screen which is wrong for I expect the current screen to be faded in.
    :
    : void fadein()
    : {
    : CTimer timer; // just a timer
    : double min_delay_per_step = min_delay / 9.0;
    :
    : int nWidth = 640;
    : int nHeight = 480;
    : int nSize = 4 * 640 * 480;
    :
    : unsigned char *pImage = new unsigned char[nSize];
    : unsigned char *pAlpha = new unsigned char[nSize];
    :
    : glPixelStorei(GL_PACK_ALIGNMENT,1);
    : glReadBuffer(GL_BACK);
    : glReadPixels(0,0,nWidth, nHeight, GL_RGBA, GL_UNSIGNED_BYTE,
    : pImage);
    :
    : glBlendFunc(GL_SRC_ALPHA, GL_ONE);
    : glEnable(GL_BLEND);
    :
    : timer.SetTime();
    :
    : int shift; // shift amount
    : for (int i=0; i <= 8; i++)
    : {
    : glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    : glLoadIdentity();
    :
    : glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    : glEnable(GL_BLEND);
    :
    : unsigned char *pSrc = pImage;
    : unsigned char *pDest = pAlpha;
    :
    : shift = 8-i;
    : for (int j = 0; j < nWidth*nHeight; j++)
    : {
    : *pDest = *pSrc >> shift; // blue
    : pSrc++, pDest++;
    : *pDest = *pSrc >> shift; // green
    : pSrc++, pDest++;
    : *pDest = *pSrc >> shift; // red
    : pSrc++; pDest++;
    : pSrc++; pDest++; // alpha
    : }
    :
    : glDrawBuffer(GL_BACK);
    : glDrawPixels(nWidth, nHeight, GL_RGBA,
    : GL_UNSIGNED_BYTE, pAlpha);
    :
    : glFlush();
    :
    : // simply swap opengl buffers (double buffered mode)
    : swapbuffers((int*)window.hDC);
    :
    : timer.WaitUntil(min_delay_per_step);
    : timer.SetTime();
    : }
    :
    : delete pImage;
    : delete pAlpha;
    :
    : glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    : glEnable(GL_BLEND);
    : glDrawBuffer(GL_BACK);
    : }
    :
    : Some things are sufficent I know but I hope you can understand it well.
    : I am thankfull for any kind of help for I have gone mad over this.
    :

Sign In or Register to comment.