Jump to content

  • Log In with Google      Sign In   
  • Create Account

chiranjivi

Member Since 27 Feb 2010
Offline Last Active Jul 02 2015 11:51 AM

#5238033 Applying alpha gradient to an image

Posted by on 02 July 2015 - 11:51 AM

Can you use custom shaders in your project?

 

In theory I can use shaders, yes - in practise though I've shied away from them because they seem pretty complex, and I barely have a tenuous grasp on the basics at present.

 

 

Alternatively, without shaders, you can make an image that has the alpha channel you want, and draw it onto the framebuffer with a blending mode that basically copies the alpha channel into the framebuffer without harming the RGB channels... But I wouldn't know the proper blend equation to set up for that.

 

This is the kind of thing I was hoping for - some functionality that would allow me to draw another texture over the top of what I currently have, but overwriting only the alpha channel and not touching the RGB. I'll have a look at the gl documentation about blending functions and see if anything looks particularly enlightening.




#5237993 Applying alpha gradient to an image

Posted by on 02 July 2015 - 07:48 AM

I have an image that's dynamically generated. I need to apply an alpha gradient to this image so that it 'fades out' at the edges. How can I do this?

To illustrate things a bit here are some pictures. Let's say I have an image and a background;


1T2hJqW.png

 

Tl5zwoM.png

 

I need to fade in the edges of the image, and then draw it onto the background. If the background were, say, entirely white, then I could 'fake' this fading effect by drawing a pre-created white quad with a linear gradient over the image at each end, like this:

 

CwmGb6V.png

If the background is another image however then I obviously can't do this, because I end up with something like this:

 

DXUOA6g.png

 

 

What I'd like to be ending up with is this:

 

u7ARH4q.png

 

...but I don't know how I can manage that. I'm using LibGDX if that matters, but I suppose this is a framework-agnostic question really?




#5046218 Help me understand .cpp vs .h in C++

Posted by on 24 March 2013 - 08:15 AM

Having trouble getting my head around the concept of separating code into .cpp and .h files. I've been coding in C++ for a while now and have never done anything other than just stick all code into header files, which I guess I'm going to have to grow out of eventually.

Here's an example of the problems I'm having. This is the level class for a toy game. From what I have been reading I believe I'm supposed to split it into level.h and level.cpp something like this:

level.h
#ifndef LEVEL_H_INCLUDED
#define LEVEL_H_INCLUDED

class Level {
    public:
    Level();

    int width, height;
    int map_grid[20][20];

    void display_grid();
};

#endif // LEVEL_H_INCLUDED

level.cpp
#include "level.h"

Level::Level() {
    width = height = 20;
    for ( int i = 0; i != width; ++i ) for ( int j = 0; j != height; ++j ) {
        if ( i == 0 || i == width-1 || j == 0 || j == height-1 ) {
            map_grid[j][i] = 1;
        } else {
            map_grid[j][i] = 0;
        }
    }
}

void Level::display_grid() {
    for (int i = 0; i != width; ++i ) for ( int j = 0; j != height; ++j ) {
        if ( map_grid[j][i] == 1 ) draw_sprite( i*64, j*64, block_blue, screen );
    }
}

When I do this, the compiler doesn't like level.cpp, saying 'draw_sprite', 'block_blue' and 'screen' have not been declared. 'draw_sprite' is a function and 'block_blue' and 'screen' are SDL_Surfaces, and they have been declared in an earlier include ("sdl_functions.h"). Attempting to #include sdl_functions.h again at the start of level.cpp prompts the compiler to get angry about me trying to re-declare a load of stuff.

What I don't understand is that if I just mash level.h and level.cpp together into a single header file:

level.h
#ifndef LEVEL_H_INCLUDED
#define LEVEL_H_INCLUDED

class Level {
    public:
    Level();

    int width, height;
    int map_grid[20][20];

    void display_grid();
};

Level::Level() {
    width = height = 20;
    for ( int i = 0; i != width; ++i ) for ( int j = 0; j != height; ++j ) {
        if ( i == 0 || i == width-1 || j == 0 || j == height-1 ) {
            map_grid[j][i] = 1;
        } else {
            map_grid[j][i] = 0;
        }
    }
}

void Level::display_grid() {
    for (int i = 0; i != width; ++i ) for ( int j = 0; j != height; ++j ) {
        if ( map_grid[j][i] == 1 ) draw_sprite( i*64, j*64, block_blue, screen );
    }
}

#endif // LEVEL_H_INCLUDED

...then it works?

Obviously there are huge gaps in my knowledge as to how header and source files are supposed to interact (as well as in many other areas :P), so please go easy on me for asking a very stupid question. Thanks.


PARTNERS