gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Help needed. Moving from use of floats to fixed point in 3D.

#103910 - Dark Knight ez - Mon Sep 25, 2006 10:32 am

Hey all.

I need to speed up some things in my game.
So far, I've been using only floats. It worked, but with lots of 3D objects to display, the game slows down.

So, I've been converting this code:
Code:
glBindTexture(GL_TEXTURE_2D, textureSurroundings[0]); // front
glBegin(GL_QUADS);
    glColor3f(surroundingsColorRPerc, surroundingsColorGPerc, surroundingsColorBPerc);
    glTexCoord2f(0.0f, 0.0f); glVertex3f(-EDGE,  EDGE, -EDGE);
    glTexCoord2f(0.0f, 1.0f); glVertex3f( EDGE,  EDGE, -EDGE);
    glTexCoord2f(1.0f, 1.0f); glVertex3f( EDGE, -EDGE, -EDGE);
    glTexCoord2f(1.0f, 0.0f); glVertex3f(-EDGE, -EDGE, -EDGE);
glEnd();


To this:
Code:
glBindTexture(GL_TEXTURE_2D, textureSurroundings[0]); // front
glBegin(GL_QUADS);
    glColor3f(surroundingsColorRPerc, surroundingsColorGPerc, surroundingsColorBPerc);
    glTexCoord2t16(floattot16(0.0f), floattot16(0.0f)); glVertex3v16(floattov16(-EDGE), floattov16( EDGE), floattov16(-EDGE));
    glTexCoord2t16(floattot16(0.0f), floattot16(1.0f)); glVertex3v16(floattov16( EDGE), floattov16( EDGE), floattov16(-EDGE));
    glTexCoord2t16(floattot16(1.0f), floattot16(1.0f)); glVertex3v16(floattov16( EDGE), floattov16(-EDGE), floattov16(-EDGE));
    glTexCoord2t16(floattot16(1.0f), floattot16(0.0f)); glVertex3v16(floattov16(-EDGE), floattov16(-EDGE), floattov16(-EDGE));
glEnd();


Of course, glColor3f could be done with glColorb as well, I guess.
Anyway, I tried this out and it appears it's not working correctly.
The quad does exist, and with the glColor3f enabled, it also appears in a colour (although weird enough, that colour appears to be "random"... not white as is specified [the values are all 1.0f]).
When I comment out the glColor3f line, the quad is just black.

So my questions are:
1) Am I moving from floats to fixed points in the right way? (And does this indeed gain in speed?)
2) How do I get the texture binding to work properly again?

#103932 - toa - Mon Sep 25, 2006 3:05 pm

If you look at the implementation of glTexCoord2t16 in libnds:


Code:
//---------------------------------------------------------------------------------
void glTexCoord2f(float s, float t) {
//---------------------------------------------------------------------------------
   glTexCoord2t16(floattot16(s*127), floattot16(t*127));
}


And the implementation of glTexCoord2t16 in libnds:
Code:
GL_STATIC_INL void glTexCoord2t16(t16 u, t16 v) { GFX_TEX_COORD = TEXTURE_PACK(u,v); }


Aren't your version missing the *127 done using the normal glTexCoord2f?


On Speed:
(1) The approach, as shown, dosn't really help you that much (if at all). The idea is right though: going from float to fixed. To get a speed increase you need to precalucate, for example, the texture values before you start rendering and used the precalucated values during rendering.


(2) Improvement: Pack u and v values into a single texture value using TEXTURE_PACK:

Pre-rendering:
Code:
uint 32 packedUV = TEXTURE_PACK(floattot16((floating_u)*127),floattot16((floating_v)*127));

During rendering use this version for setting texture coordinates:
Code:
glTexCoord1i(packedUV);


(3) Improvement: Pack x&y values into a single value using:

pre-rendering:
Code:
int yx = (floattov16(floating_y) << 16) | (floattov16(floating_x) & 0xFFFF);
v16 z =  floattov16(floating_z);


During rendring use:
Code:
glVertex2v16(yx, z);


(all grapped uncerimoneusly by urolling videoGL calls in libnds :-))
that'll give you a rendering time reduced at least 50% compared using the float "standard" gl-versions from the libnds examples. To go even faster precompile display-list and disregard using the gl-methods at all.

#103940 - Dark Knight ez - Mon Sep 25, 2006 3:53 pm

Thank you for your clear explenation, toa. Very helpful. :)