Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

OmarShehata

Member Since 09 Feb 2012
Offline Last Active Jan 13 2013 01:15 AM
-----

Topics I've Started

Efficiently Detecting if objects are out of screen?

07 January 2013 - 05:11 AM

I have a small optimization problem in my game. If I have 10,000 objects in-game, but only 10 objects on screen (or no objects on screen), the game will lag, because what I do is I check every single frame, for every object, if the object is inside the screen or not.

 

So obviously as more objects are added, the slower things get. I thought about using some form of spatial indexing, so that if my camera is at (100,100), then instead of looping through 10k objects to know which are in-screen, I will simply ask my grid if there are any objects at 100,100, and do the same for neighbouring cells to get all objects inside the screen without ever having to loop, no matter if I have a thousand, or a million objects.

 

The problem, however, was that this doesn't account for moving objects, because if an object moves, I would have to check every frame if it has entered another grid cell or exited its cell.

 

So how do you detect which objects are inside the screen?


Sending lots of data to the GPU?

01 November 2012 - 04:00 PM

I've got my lighting shader code. And let's say I want to have 10, 20, or 50 lights in my scene. Now the lighting code itself isn't very complex, it should easily be able to handle a lot of lights without too much trouble. However, just by declaring my array to hold 150 vec3's, I just get a black screen, presumably because my graphics card can't handle all that much?

I was thinking a way around this would be to create an image at run time where each pixel represents the XYZ and intensity of the light as RGBA and send that instead to the shader. That way I could theoretically have a thousand lights with no problems. I haven't implemented this yet but I wanted to ask how other people handle stuff like this, or whether I should even be trying to send that much data to my shader. And how you normally optimize such things.

Any tips would be appreciated!

Thanks!

Multiple Lights, adding dot product?

16 October 2012 - 12:01 PM

I know there are a lot of topics asking about multiple lights, but I just have one simple question that I can't seem to find the answer to.

So in order to support multiple lights, I loop over the lights, and add together the intensities of each light source (and things like color etc..)

Now the only thing I'm having trouble with, is how to handle the dot product. I tried adding the dot products, but that just makes the two light sources sort of merge into each other, and when they are far apart the object is completely dark.

How do I handle the angle/dot product for multiple lights? (I'm not sure if code is needed to answer the question, if so I'll upload my shader code).

Thanks in advance!

Simple diffuse light. Weird Behavior.

10 October 2012 - 01:49 AM

So I'm trying to make a simple lighting effect. I have my normal map and my object posted below.

Posted ImagePosted Image

And my code in GLSL is written below:

uniform sampler2D normal;
uniform vec2 mouse;
uniform float Za;
	 vec4 effect(vec4 color,sampler2D tex,vec2 tc,vec2 pc)
	 {
	 vec4 img_color = texture2D( tex, tc );
	 vec4 normalColor = texture2D( normal, tc );
	 float X = (mouse.x - pc.x);
	 float Y = (mouse.y - pc.y);
	 float Z = Za;
	 float dotProduct = X * normalColor.r + Y * normalColor.g + Z * normalColor.b;
	 dotProduct /= sqrt(X * X + Y * Y + Z * Z) * sqrt(normalColor.r * normalColor.r + normalColor.g * normalColor.g + normalColor.b * normalColor.b);
	 float factor = dotProduct;
	 img_color.r = img_color.r + factor;
	 img_color.g = img_color.g + factor;
	 img_color.b = img_color.b + factor;
	 return img_color;
	 }


So basically I had a few questions:

Is my math correct?
What I'm trying to do is get the vector of the mouse (the light source for now) to the pixel, and the normal of that pixel, and then get the dot product between them, normalize that, and that is the brightness factor. However the angle seems to be a bit off. I just wanted to double check if my questions were all right because debugging on the GPU doesn't seem to be very easy

For the usual lighting methods, do you multiply by a distance factor?
Naturally when a light is further away the object should be dimmer. But this doesn't seem to be the case with this approach. Should I just multiply by some arbitrary distance factor?

Finally, I'm basically using 3D light for a 2D game. Although it seems pretty simple in the implementation though. I'm told the Phong Reflection Model may be better suited in this case. Any thoughts on that?

Thanks in advance! Any help or tips on anything would be appreciated!

Lighting newb?

02 October 2012 - 12:41 AM

I'm writing my game in Lua but I'm using GLSL for my shaders.

So I want to add a little light to my game, but I have little to no knowledge about 2D or 3D lighting. Specular lights, normal maps, ambient occlusions..all that stuff, I have no idea how any of it works.

So before diving into anything, I thought I'd ask if anyone had any good articles about delving into the matter, or a set of articles that explain the theory well. My game is in 2D and the end result that I would like to achieve is something like this:



or this:



Any help, general tips and links would be appreciated. Thanks!

PARTNERS