#138293 - a128 - Wed Aug 22, 2007 1:23 pm
Does anyone used Fog in a demo?
#138321 - silent_code - Wed Aug 22, 2007 11:05 pm
i tried it, but somehow the fog just affected the clear color.
when i get back to nds development, i'll sure try again. ;D good luck till then!
#138329 - simonjhall - Wed Aug 22, 2007 11:41 pm
Yeah I used fog. What would you like to know?
_________________
Big thanks to everyone who donated for Quake2
#138364 - zeruda - Thu Aug 23, 2007 8:19 am
Here's what you need:
In a header file:
Code: |
#define POLY_FOG (1<<15)
#define GL_FOG_SHIFT(n) ((n)<<8)
#define GL_RDLINE_UNDERFLOW (1<<12)
#define GL_VERTEX_OVERFLOW (1<<13)
#define GFX_FOG_COLOR (*(vuint32*) 0x04000358)
#define GFX_FOG_OFFSET (*(vuint32*) 0x0400035C)
#define GFX_FOG_TABLE ((vuint8*) 0x04000360)
|
In you code you need the following:
Code: |
void glFogColor(uint8 red, uint8 green, uint8 blue, uint8 alpha) {
GFX_FOG_COLOR = (red) | ((green) << 5) | ((blue) << 10) | ((alpha) << 16);
}
void glFogDepth(uint16 depth) { GFX_FOG_OFFSET = depth; }
void UpdateFog()
{
glFogColor(31, 13, 13, 31);
glFogDepth(24832);
int Mass = 2;
int Shift = 2;
for (int i = 0; i < 32; i++) { GFX_FOG_TABLE[i] = (i << Mass); }
GFX_CONTROL = GL_FOG | GL_FOG_SHIFT(Shift) | GL_RDLINE_UNDERFLOW |
GL_VERTEX_OVERFLOW | GL_TEXTURE_2D | GL_BLEND;
}
|
The colour is rgba, although I haven't got the alpha component to make any difference. The depth is how far back you want the fog. The mass varies from 0-2 and the Shift from 0-15 and those affect the strength of the fog.
You also have to enable Fog in your polyformat for your meshes:
Code: |
glPolyFmt(SETALPHA(31) | POLY_FOG | POLY_ID(17));
glPushMatrix();
DrawMesh();
glPopMatrix(1); |
[/code]
#138365 - a128 - Thu Aug 23, 2007 8:20 am
I know how to setup Fog...
Code: |
#ifdef FOG
GFX_CONTROL = GL_TEXTURE_2D | GL_FOG | GL_FOG_SHIFT(8) | GL_ANTIALIAS | GL_BLEND;
// Set up the fog stuff
glFogColor(31, 0, 0, 31);
glFogDepth(2);//just a test value
int i;
//density
for (i = 0; i < 32; i++) {
GFX_FOG_TABLE[i] = i / 2; //just a fake value NOT the real thing?!
}
#endif
|
What I do not know - and what I want to have with Fog - is , how to setup Fog... so the Fog range is i.e from 40 to 100 for the far plane .
guess the most important think is to calculate some W-values for the GFX_FOG_TABLE
#138367 - zeruda - Thu Aug 23, 2007 8:54 am
a128 wrote: |
I know how to setup Fog...
What I do not know - and what I want to have with Fog - is , how to setup Fog... so the Fog range is i.e from 40 to 100 for the far plane . |
I don't quite get what you mean. If you have a depth of 0 the fog come right up to the camera. If you push it back 10000, it goes back a ways, and set it 30000 or so it goes all the way back.
The mass(fog table) has 3 effective settings. 0 for light mist. 1 for standard smooth fog. 3 for fog that becomes like a wall.
The fog shift then manipulates the fog table. 0 for small gradient. And the 15 for less gradient. If you have mass of 2 and gradient 15 then it will be like a flat wall. But use 2 and 2 and it is smoother.
#138380 - a128 - Thu Aug 23, 2007 1:20 pm
zeruda wrote: |
I don't quite get what you mean. If you have a depth of 0 the fog come right up to the camera. If you push it back 10000, it goes back a ways, and set it 30000 or so it goes all the way back.
|
I tried your code..and I have the same "problem"...
What I want is basicly this
The Camera is at distance 0
THe Fog should start at offset 100 ..so the coloring of the vertex with the fog density should start at 100
What I have now is ...the coloring starts allways at 0
If I apply glFogDepth(100) this doesnot work ...maybe I have to convert the value 100 to DS fixpoint and feed glFogDepth() with this value?!
#138382 - kusma - Thu Aug 23, 2007 1:40 pm
a128 wrote: |
THe Fog should start at offset 100 ..so the coloring of the vertex with the fog density should start at 100
|
Try adjusting the fog-table.
#138543 - zeruda - Sat Aug 25, 2007 8:14 am
a128 wrote: |
If I apply glFogDepth(100) this doesnot work ...maybe I have to convert the value 100 to DS fixpoint and feed glFogDepth() with this value?! |
Well I was using increments of 0x0100 whatever that translates to, just keep testing with bigger values.
#141614 - silent_code - Thu Sep 27, 2007 3:22 pm
i've tested the code and the fog didn't show up, no matter what values i was setting. any special requirements? i use w buffering, but that shouldn't be a problem.
also, i'm calling update at the beginning of the frame and i've got the poly attribute set, as well as everything you've posted. still no fog.
?:^|
could you post some example, please? thanks!
#141685 - a128 - Fri Sep 28, 2007 8:19 am
silent_code wrote: |
i've tested the code and the fog didn't show up, no matter what values i was setting. any special requirements? i use w buffering, but that shouldn't be a problem.
also, i'm calling update at the beginning of the frame and i've got the poly attribute set, as well as everything you've posted. still no fog.
?:^|
could you post some example, please? thanks! |
You must enable fog for each mesh...... Edit:and yes...test fog on hardware !
Code: |
glPolyFmt(POLY_ALPHA(31) | POLY_FOG | ....); |
Last edited by a128 on Mon Oct 01, 2007 8:45 am; edited 1 time in total
#141741 - M3d10n - Sat Sep 29, 2007 1:49 am
silent_code wrote: |
i've tested the code and the fog didn't show up, no matter what values i was setting. any special requirements? i use w buffering, but that shouldn't be a problem.
also, i'm calling update at the beginning of the frame and i've got the poly attribute set, as well as everything you've posted. still no fog.
?:^|
could you post some example, please? thanks! |
Are you testing on actual hardware? No$gba doesn't support fog whatsoever, and I didn't try fog code on other emulators.
#141888 - zeruda - Mon Oct 01, 2007 9:12 am
Fogging does work to some extent on the ideas emulator.
But anyway here is a full example with some fogging, I based it on the
quads example in libnds examples.
Code: |
#include <nds.h>
#define POLY_FOG (1<<15)
#define GL_FOG_SHIFT(n) ((n)<<8)
#define GL_RDLINE_UNDERFLOW (1<<12)
#define GL_VERTEX_OVERFLOW (1<<13)
#define GFX_FOG_COLOR (*(vuint32*) 0x04000358)
#define GFX_FOG_OFFSET (*(vuint32*) 0x0400035C)
#define GFX_FOG_TABLE ((vuint8*) 0x04000360)
void Initialise() // Initialise the DS into 3D mode and setup the camera
{
powerON(POWER_ALL);
videoSetMode(MODE_0_3D); // set mode 0, enable BG0 and set it to 3D
irqInit(); // irqs are nice
irqEnable(IRQ_VBLANK);
glInit();
glClearColor(10, 10, 10,31); // BG must be opaque for AA to work
glClearPolyID(63); // BG must have a unique polygon ID for AA to work
glClearDepth(0x7FFF);
glViewPort(0, 0, 255, 191); // this should work the same as the normal gl call
glMatrixMode(GL_PROJECTION); // Setup the camera
glLoadIdentity();
gluPerspective(35, 256.0 / 192.0, 0.1, 400);
gluLookAt(0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glMatrixMode(GL_MODELVIEW);
}
void DrawQuad() // Draws a quad. Honest.
{
glColor3b(255,255,255);
glBegin(GL_QUAD);
glVertex3v16(inttov16(-1),inttov16(-1),0);
glVertex3v16(inttov16(1), inttov16(-1), 0);
glVertex3v16(inttov16(1), inttov16(1), 0);
glVertex3v16(inttov16(-1), inttov16(1), 0);
glEnd();
}
void glFogColor(u8 red, u8 green, u8 blue, u8 alpha)
{
GFX_FOG_COLOR = (red) | ((green) << 5) | ((blue) << 10) | ((alpha) << 16);
}
void UpdateFog()
{
glFogColor(1, 1, 21, 31); // The colour of the fog in rgba except the alpha does nothing
GFX_FOG_OFFSET = 60032; // This is the fog depth as in how far back the fog starts
for (int i = 0; i < 32; i++) { GFX_FOG_TABLE[i] = (i << 2); } // Overall Heavyness of fog
GFX_CONTROL = GL_FOG | GL_FOG_SHIFT(2) | GL_RDLINE_UNDERFLOW | // Shift is the fog gradient
GL_VERTEX_OVERFLOW | GL_TEXTURE_2D | GL_BLEND;
}
void Input(float &XPos, float &ZPos)
{
scanKeys();
u16 keys = keysHeld();
if((keys & KEY_UP)) { ZPos += 0.02; }
if((keys & KEY_DOWN)) { ZPos -= 0.02; }
if((keys & KEY_LEFT)) { XPos += 0.02; }
if((keys & KEY_RIGHT)) { XPos -= 0.02; }
}
int main()
{
float XPos = 0;
float ZPos = 0;
Initialise();
UpdateFog(); // Setup the fog
while(1) {
Input(XPos, ZPos); // update the camera position
glPushMatrix();
glTranslate3f32(floattof32(XPos), 0, floattof32(ZPos-3)); // move camera away
glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE); // Draw without any fogging
DrawQuad();
glTranslate3f32(floattof32(3), 0, floattof32(-3));
glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE | POLY_FOG); // Start drawing with fogging
for (int i = 0; i < 10; i++) {
DrawQuad();
glTranslate3f32(floattof32(1), 0, floattof32(3));
}
glPopMatrix(1);
glFlush(0);
swiWaitForVBlank();
}
} |
#141892 - silent_code - Mon Oct 01, 2007 10:19 am
thanks for the code!
btw: yes, i'm *obviously* <joking> testing on hardware (i wouln't have made the shadow volume demo with emulators ;^P) - no, really, i do. if no$ isn't complaining, i'm burning it and testing on hw.
i've got it to "work" in the meantime. "work", because it was working all the time, only i had to put some *odd* numbers for shift and mass to actually see the effect. i think shift of 9 (anything higher lower would result in no / subtle fog) and mass of 2++. that's why my previous attempts to fog failed... silly stuff.
now that it works, i have another problem. the fog seems to stop at some point and "restart", means it's repeading the fog table instead of clamping it. well, that may be cause by the shift value or what so ever, maybe i'm using some kind of uncommon coordinate granuality [what the hell did i just write? ;^p]... i have to investigate a bit further. in fact, it's just my shadow volume demo (there it is again) i've added it to. as i've added motion blur, i had to drastically decrease some values, not just adjust them. it was kind of from .4 to .001, so maybe my meshes are a bit too "tight"? really strange stuff.
again, thanks for the help. :^D
#141905 - DiscoStew - Mon Oct 01, 2007 2:21 pm
FORGET THIS POST!
Early in the morning, I had very little time before I had to leave for my first class, and when I looked at this line, my brain clicked as if something was amiss there, when in fact it really wasn't. In my mind, I was going by the last result per loop being shifted, such that when i = 1, the result was 4, but when i = 2, the result was 16 (when shifting 4 to the left twice). So, I thought I found the problem involving a "reset" of the fog from the bits getting shifted outside the 32-bit value, when in fact I wasn't even close. So anyone who raised an eyebrow at my post, you had a good reason to. So to conclude........don't program when your brain is not functioning correctly. Wait to get some coffee first.
------------------------------------------
Original post.....
Though I haven't actually tried doing any fog, I was looking at that code, and I saw this line...
Code: |
for (int i = 0; i < 32; i++) { GFX_FOG_TABLE[i] = (i << 2); } // Overall Heavyness of fog |
If you are using this code in your demo, it would possibly explain why it "restarts", as when 'i' gets to 16, the value to be put in already exceeds what a 32-bit value can hold, so I assume it copies only the lower 32-bits, meaning it would start at 0 again, and work it's way up. If you want to clamp it to the max, then in this particular situation, loop 16 times, and then filling in the remaining table with the max 32-bit value.
_________________
DS - It's all about DiscoStew
#141974 - silent_code - Tue Oct 02, 2007 6:06 pm
that *would* have made sense, but it repeats *periodically*. that means the fog table is repeated after a certain depth offset, again and again, untill the geometry gets clipped by the far plane.
unfortunately i didn't have time to get back to the code. :^C