Thread: Lighting question - need opinions

  1. #1
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320

    Lighting question - need opinions

    I am at a crossroads I believe I can do this lighting one of two ways. One being on the fly calculating the circles and blending with the background or just prerendoring a mask and then blending that as I need to. This is only 2D I was just wondering which would be the better way to do it or if there is another solution that is even better. I am using allegro and mscv++2003.net to compile code so no directx or openGL rants plz. One of the features of this game is that the maps are easy to edit along with scripts for NPCs and such. To me this looks like a simple where do I want the lagg to be issue but maybe it is something more. Opinions anyone?

  2. #2
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    As an update to this - I have blended on the fly and dropped my fps from 110 to 15 so now I am going to try the prerendoring during map load and see if that speeds the FPS back up to a decent amount. Will post progress once that is done and I am happy with it. EDIT: Also if anyone can think of a way to help make this code work faster it would be appreciated!

    Code:
    void OnTheFlyLighting()
    {
    
    	int cx,cy;
    	int tr,tg,tb;
    	cx = 48 + (tmpObj->GetX()/2) + (tmpObj->GetMapX()*32);
    	cy = 48 + (tmpObj->GetY()/2) + (tmpObj->GetMapY()*32);
    	for(int LCVY = cy-tmpObj->GetBrightness(); LCVY < cy+tmpObj->GetBrightness();LCVY++)
    	{
    		for(int LCVX = cx-tmpObj->GetBrightness(); LCVX < cx+tmpObj->GetBrightness();LCVX++)
    		{
    			if(LCVY < 0 || LCVX < 0 || LCVY > scry || LCVX > scrx)
    			{
    
    			}
    			else
    			{
    				
    				for(int grade = 0;grade < 8;grade++)
    				{
    					int shade = tmpObj->GetBrightness()/8;
    					if( ((LCVX - cx)*(LCVX - cx) +(LCVY - cy)*(LCVY - cy) <= (tmpObj->GetBrightness() - (grade*shade))*(tmpObj->GetBrightness() - (grade*shade)))
    						&&
    						((LCVX - cx)*(LCVX - cx) +(LCVY - cy)*(LCVY - cy) > (tmpObj->GetBrightness() - ((grade+1)*shade))*(tmpObj->GetBrightness() - ((grade+1)*shade)))
    					  )
    					{
    						int c = getpixel(BUFFER,LCVX,LCVY);
    						int alpha = (grade*8);//to upgrade the strength of the light
    						tr=((tmpObj->GetR()*alpha)+((getr32(c)*(255-alpha))))/256;
    						tg=((tmpObj->GetG()*alpha)+((getg32(c)*(255-alpha))))/256;
    						tb=((tmpObj->GetB()*alpha)+((getb32(c)*(255-alpha))))/256;
    
    						putpixel(BUFFER,LCVX,LCVY,makecol(tr,tg,tb));
    						break;
    					}
    				}
    			}
    		}
    	}
    }
    Last edited by ~Kyo~; 12-15-2005 at 10:44 PM.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well you said no DX or GL and the method you are using is..um..interesting.

    Now if you want to know how to do lightmapping or simple cosine lighting then I'm sure some of us can help.

  4. #4
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Is it compatable with allegro? I found this method on a website and I used some methods to make the light not look so.... rough it is smoother with this code. I am happy to hear any info people may have as long as it isn't some Junk all your code and goto DX or GL cosine lighting tho lets hear it or see it in this case. I think I have a clue what your talking about so I may go try it out thanks for the reply.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well cosine lighting is:

    Code:
    ...
    ...
    Vector3 LightPos;
    Vertex3D Vertex;
    
    Vector3 ToLight=LightPos-Vertex.Pos;
    Normalize(&ToLight,&ToLight);
    
    float dot=Dot(&ToLight,Vertex.Normal);
    
    if (dot>0) Vertex.Color*=dot;  //could be *=(1.0f-dot)
    ...
    ...
    Lightmapping is when you use a texture as the lightmap and perhaps a texture as the specular map.

    Something like this:

    FinalTexelColor=(DiffuseColor*LightMapColor)+Specu larColor

    There are about a billion equations for different type of lighting. Google it.

  6. #6
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Nod did google was just wondering which would be the best on a 2d world since I am really just taking a circle from where the origon point is and spreading it out, but also wanting it to degrade over time so you dont get an "edge" on the far reach of the light. Perhaps a screenshot will explain what I am doing and tring to accomplish.

    http://www.geocities.com/meanguy76012/PASss.html

    in case you wanted to see what page I was reading from:

    http://www.allegro.cc/forums/thread/158652
    Programming Questions: hi color pixel blending

    EDIT: just a thought but couldn't I make a table and generate all the values for a blend before I need it then refer to it when I need it?

    EDIT#2: Just tried the lookup table and it didn't change my FPS at all! arrgg...

    EDIT#3: I have found where most of my FPS went to: The evaluation of the circles for checking where what degree of light should be shown(removing this check resulted in a 40FPS jump). In second place we have the brightness of 256 to thank(reducing it to 64 resulted in 35 frames). So now I guess the question is what is a faster way to solve these equations? Then faster drawing routines.
    Last edited by ~Kyo~; 12-16-2005 at 01:37 AM.

  7. #7
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195

    http://www.gamedev.net/reference/articles/article2032.asp

    Here is an article on 2d lighting. http://www.gamedev.net/reference/art...rticle2032.asp

  8. #8
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Hmmm thanks for the article thats my next thing after the FPS get back to reasonable levels. I doubled my FPS justa few mins ago by making a lookup table for the circles instead of using the circle eqns in the function.

    Ok I have worked a table out based on brightness(radius) of the source and the number of sources. Is this a fairly good FPS for light sources?

    Code:
             Number Of sources on screen
     
            |1 |2 |3 |4 |5 |6 |7 |8 |9 |10| 
    b   16  |91|88|85|  |  |  |  |  |  |  |
    r   32  |78|74|70|  |  |  |  |  |  |  |
    i   64  |58|44|40|  |  |  |  |  |  |  |
    g   128 |30|20|14|  |  |  |  |  |  |  |
    h   256 |12| 6| 5|  |  |  |  |  |  |  |
    t   512 |5 | 3|--|--|--|--|--|--|--|--|
    n   1024|2 | 2|--|--|--|--|--|--|--|--|
    e   
    s   
    s
    Last edited by ~Kyo~; 12-16-2005 at 04:06 PM.

  9. #9
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Apologize for the bump but I can't edit. Anyways for allegro users when using PutPixel and GetPixel these functions are much faster:

    Code:
    void fast_putpixel(BITMAP *bmp, int x, int y, int color)
      {
            ((long *)bmp->line[y])[x] = color;
      }
      
    int fast_getpixel(BITMAP *bmp, int x, int y)
      {
            return ((long *)bmp->line[y])[x];
      }
    I do have a new problem tho if I use a large source say 512 or more brightness and move above the object that is sourcing the light my program crashes... been trying to debug it for a while now want to try a few more things before posting it all here.
    Last edited by ~Kyo~; 12-24-2005 at 01:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GLSL lighting (again)
    By psychopath in forum Game Programming
    Replies: 6
    Last Post: 12-19-2005, 01:34 PM
  2. Replies: 6
    Last Post: 11-12-2005, 11:57 AM
  3. Lighting a Direct3D 9 mesh sphere
    By bennyandthejets in forum Game Programming
    Replies: 12
    Last Post: 02-14-2005, 01:19 AM
  4. OpenGL lighting question...
    By psychopath in forum Game Programming
    Replies: 3
    Last Post: 07-31-2004, 03:28 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM