Light, distance and fading.

Greetings!

So I’ve finally decided to pull my head out Paint and get some decent-looking effects in my game… Obviously by learning OpenGL. Although I am using ES1.1 at the moment, I believe my question is pretty generic within OpenGL-land.

In my 2D-platform shooter, I’m currently implementing lights. I obviously want the objects to cast shadow - which in my case is “absence of light” more than actual shadows.

Here’s a screenshot of my current result:
http://i260.photobucket.com/albums/ii36/gneldre/Skjermbilde2011-08-11kl230543.png

I’m not gonna post the entire code itself, as it’s probably gonna be a bitch to read. Here’s the pseudo-version of it.


-(void) update:(ccTime)dt {
    do {
        b2Vec2 point1 = RayCast( a, b );
        b2Vec2 point2 = RayCast( a, c );
        
        //Emit-position. relative to source's position
        vertices[Vcounter] = 0.0; Vcounter++; 
        vertices[Vcounter] = 0.0; Vcounter++;
        //P1
        vertices[Vcounter] = point1.x; Vcounter++;
        vertices[Vcounter] = point1.y; Vcounter++;
        //p2
        vertices[Vcounter] = point2.x; Vcounter++;
        vertices[Vcounter] = point2.y; Vcounter++;

        //Emit-color
        color[Ccounter] = {0.6, 0.6, 0.5, 0.4}; Ccounter++;
        //Faded color
        color[Ccounter] = {0.0, 0.0, 0.0, 0.0}; Ccounter++;
        color[Ccounter] = {0.0, 0.0, 0.0, 0.0}; Ccounter++;

    }   while ( curAngle < endAngle );
}

As you can see, the color is fading regardless of the distance, which in fact is exactly what I told it to do. How can I avoid this? I want the colors to fade when they reach N meters away from the source, not N percents as it is now.

One fix I’ve been considering is calculating this in the function I posted. Setting the color of point B and C based on their relative position to A. I think this will take too much processing power though.

Any input is highly appreciated! :slight_smile:

/Pimms

Yea this is the way to do it, but it’s not that much processing.
basically it goes something like this

L=sqrt((point1.xpoint1.x)+(point1.ypoint1.y));
c=(max_distance-L)(1/max_distance);
color[Ccounter] = {0.6
c, 0.6c, 0.5c, 0.4*c}; Ccounter++;

if you want you can remove that sqrt if max_distance is really max_distance^2

Thanks a lot for your reply, Zeoverlord. Apreciate it!

The code you posted seems to be just what I’m looking for, and I would definately try it out, except for one thing.

glDrawArrays() hangs my game. It doesn’t crash, but the function-call produces a SIGTERM. I looked it up, it’s some Unix-code telling the app to terminate. (Developing for iOS)

I’ve had this issue since shortly after I posted my first post and hesitated to ask about it hoping I could resolve it myself. I’ve found nothing even remotely similar - most crashes in this function is a EXC_BAD_ACCESS (exceeding an array’s limit) from what I understand.

I’m fairly sure I’m dealing with undefined behaviour as well, bare with me on this one.


    glDisable(GL_TEXTURE_2D);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);
    
    glBlendFunc(GL_DST_ALPHA, GL_ZERO);
    glVertexPointer(2, GL_FLOAT, 0, vertices);
    glColorPointer(4, GL_FLOAT, 0, colors);
    glDrawArrays(GL_TRIANGLES, 0, vertexCount*6);
    
    glDisableClientState(GL_VERTEX_ARRAY);
    glEnable(GL_TEXTURE_2D);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

Now, I mentioned undefined, right?

The above code hangs 95% of the time after the 5th or 6th draw. However, if I add:

int a = 0;

before the openGL calls it crashes only ~10% of the time. Which is good, except it’s still 10% too much.

If I put a breakmark on the call and manually tell the debugger to continue 10-15 times and then remove said breakmark, it will run flawlessly regardless of declared integer values. Usually never crash beyond that point.

All of this hapens in debug and release mode - iPad and iPad simulator.

Telling it to draw fewer vertices has no impact.

If it doesn’t hang within the first second of running, it mostly doesn’t hang. Mostly.

I’ve ran it through the debugger countless times looking for wether or not the color / vertex array was not fully set, but that is never the case.

The arrays being drawn are big enough:
colors holds 4(453) GL_FLOATS
vertices holds (45
6) GL_FLOATS

I’m litterally screaming WTF here. This makes NO sense!

/pimms