2 questions in DirectX
Is it better to render objects back-to-front or front-to-back? atm, i render my objects back-to-front, and when i tried front-to-back, it just messed up everything, not sure why. I read that front-to-back is more efficient... but im not sure, could someone please explain it?
I was wondering, how would you add a fade effect? I havent really given it much thought yet, but the only way i read about was by setting your gamma to a really low value to make it appear.. faded. I cant really think of any other way and i was wondering if anyone knew of a better way to do this? Im just trying to fade in/out some text to set the tone for the game
Any help is appreciated
First of all you want to render as many objects as you can simply using the frustrum culling and zbuffer. But the order that you want them in is entirely dependent on what you are trying to do.
The only time I order my primitives really is when I'm doing alpha blending. Alpha blending is a bit tricky because if you draw the alpha blended objects in the wrong order...some of the objects in your scene will not be visible through the alpha blended object. This is because if the alpha blended object is drawn before the non-alpha blended object it will alpha blend the pixels with the onscreen pixels. Since no object is there there is nothing to blend with yet. Second when the non-alpha blended object is drawn it will draw over the alpha blended object thus killing the effect. Since you are telling DirectX not to write to the zbuffer for alpha blended objects (because they are not opaque) the ztest when the non-alpha object is drawn...passes and therefore screws the scene up in a big way.
I've had this problem with my sun in my space game. If I draw the sun last it alpha blends correctly with everything....but when you get close to a planet the sun is not visible through the atmosphere of the planet. This is because the planet's atmosphere is rendered and alpha blended BEFORE the sun texture is even rendered. Hence when you look at the sun - it will not be alpha blended with the atmosphere of the planet...it will disappear completely which totally destroys the effect.
I know of several ways to do a fade effect but I will give you the most compatible way. Truthfully the gamma ramp is a finicky beast and not all cards support messing with the gamma ramp. However all cards do support alpha blended primitives. So what you do is create a pre-transformed, pre-lit quadrilateral that fills the entire screen. Set DirectX to use vertex alpha instead of material alpha and set the diffuse color to black as well as the specular. BOTH must be set or you will get a shade of grey. Now when you want to fade simply alter the diffuse alpha component of the vertexes towards 1.0f for a fade to black or towards 0.0f for a fade from black.
It works great. I used this effect for my sun glare. The sun glare is proportional to the dot product of the incoming light vector and the normal vector for the camera....or the look vector since it will always be normal to the viewing plane. I compute a very simple light falloff value and then set the vertex alphas and render the quad. It works very good.
The good part about this is when you have an object with an alpha of 0.0f or that is not visible, DirectX does not even render that object so you gain frames.
Here is a pic of what I'm talking about. The 'white-out' that you see is the screen quad being alpha blended with the existing scene. Of course it has to be drawn last since we want everything in the scene to be affected by the sun glare. If my color was black...this picture would be very dark since it would be about half intensity since half of the black would be blended with the on screen image.
Here is a pic with the same quad only the quad color is now black.
Notice how when you point at the sun it fades to black instead of white. The arccosine of the angle between the sun-to-ship vector and the normal vector of the camera is very near 90 degrees - hence a dot product of of near 1.0f. In order to avoid fading completely in and out (white or black) I use a falloff amount that estimates how quickly the light fallsoff from the sun and also clamps the low and high alpha values so they don't go beserko.
ah i see, thanks bubba, by the way, those pics look really cool, good job on the space game
i got 1 last question:
I was going through some of my older programs, and i noticed that for:
I always seem to use either 45 or 60 for x and i was wondering, is that the right value? i tried higher values (nothing past 100 or so worked) and lower values, im guessing its meant to be somewhere between 45 and 90 but im not sure which 1 would be best...
D3DXMatrixPerspectiveFovLH(&matProjection, D3DXToRadian(x), (float)g_DisplayMode.Width / (float)g_DisplayMode.Height, 1.0f, 50.0f);
in the second picture you posted, shouldnt it be brighter? since your looking at the sun, shouldnt it be a white blinding light instead of the dark almost tinted picture you posted?
I use a 60 degree field of view. That's also what I used in my raycasting engines as well. 45 just doesn't look right to me.
And for your second question:
No because I changed the specular and diffuse colors of the quad to black. So when the value approaches 1.0f it gets darker because more of the quad color is blended in and vice versa.
Here is an example with a red quad color.