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 > Wierd float problem

#109292 - Dan Forever - Thu Nov 16, 2006 11:40 pm

I'm having a problem when it comes to drawing a quad on an orthogonal view.

With a width and height of upto 7, the quad draws correctly. If I have a size on either axes of 8 or larger, it inverts on those axes.

so a quad at x 10.0 with a width of 5 will correctly have it's left at 10 and right at 15, but a width of 8 will have the left at 2 and right at 10.

Are floats one byte fixed point real numbers, or is there something inherently wrong with my drawing code?

Code:
void RenderManager::Draw(Renderable& quad)
{
   if(quad.texID == NO_TEXTURE && quad.texID < NUM_TEXTURES)
      glBindTexture(GL_TEXTURE_2D, 0);                  // No Texture
   else
      glBindTexture(GL_TEXTURE_2D, m_texture[quad.texID]);   // Texture

   glColor3b(quad.red, quad.green, quad.blue);
   glTranslatef(quad.x, quad.y, 0.0f);
   glBegin(GL_QUADS);                                 // Draw A Quad

      glTexCoord2f(quad.uv[0][0], quad.uv[0][1]);            // Top Left
      glVertex3f(quad.width, 0.0f, 0.0f);                  // Top Right

      glTexCoord2f(quad.uv[1][0], quad.uv[0][1]);            // Top Right
      glVertex3f(quad.width, quad.height, 0.0f);            // Bottom Right

      glTexCoord2f(quad.uv[1][0], quad.uv[1][1]);            // Bottom Right
      glVertex3f(0.0f, quad.height, 0.0f);               // Bottom Left

      glTexCoord2f(quad.uv[0][0], quad.uv[1][1]);            // Bottom Left
      glVertex3f(0.0f, 0.0f, 0.0f);                     // Top Left

   glEnd();                                       // Done Drawing The Quad
}

#109293 - Mighty Max - Thu Nov 16, 2006 11:44 pm

The hardware registers use a fixed point format(4.12) that does not allow values smaller then -8 and equal or higher then +8.
_________________
GBAMP Multiboot

#109295 - Dan Forever - Thu Nov 16, 2006 11:56 pm

hmm, thanks for the quick response. Whats the best method for converting a real to a fixed point number stored in a 32 bit int?

#109296 - Mighty Max - Fri Nov 17, 2006 12:02 am

Not using floating point at all :p (the arm has no native support for it, it does floating point math by software)

there should be some defines in ndslib that does this conversation for you. See the nds/arm9/videoGL.h:

Code:
typedef short int v16;       // vertex 4.12 fixed format
#define inttov16(n)          ((n) << 12)
#define f32tov16(n)          (n)
#define v16toint(n)          ((n) >> 12)
#define floattov16(n)         ((v16)((n) * (1 << 12)))
#define VERTEX_PACK(x,y)       (((y) << 16) | ((x) & 0xFFFF))

_________________
GBAMP Multiboot

#109396 - Dan Forever - Sat Nov 18, 2006 12:09 am

I'm having problems with the fixed point maths.
Presumably adding or subtracting two f32s is still by using the + and - binary operators. Multiplication and division is via the handy functions provided in math.h (in the ndslib include dir) and converting between formats required by the "opengl" stuff is provided in videogl.h as pointed out above.

Switching over to fixed point has caused my quads to be "stuck" in the top left corner and render as completely black (as opposed to textured).

I start with literal floats that I convert to f32 using the floattof32() macro.
I then convert them using f32tov16() or f32tot16() when the values are passed into glVertex3v16() or glTexCoord2t16() respectively.

I can move the quad, but when it moves into the screen an amount greater than it's width or height it dissappears.

Textures render correctly when I switch the tex-coord function back to glTexCoord2f() and converting back to float with f32tofloat(). But positioning is still screwed.

Unfortunately I'm left without the ability to debug at all, so I'm left with educated guesses and blind luck :( (and turning to you guys for help :D)

Am I using these functions correctly or have I misunderstood something?

#109636 - Rajveer - Sun Nov 19, 2006 11:21 pm

1) Some code would be nice to see how you're using the fixed-point math, especially for the quad/its textures :)

2) Adding/Subtracting is still done with + and -, and mult/div is with the functions you specified. If, for f32's, you remember 1 is 4096, therefore 2 is 8192 e.t.c. you won't even need the macros ;)

#109991 - Payk - Fri Nov 24, 2006 1:18 am

First: How do you move that mesh? with translate there shouldnt be any problem at all. If its still not good enough try to use that values better.
For example when loading a model, i divide each vertex by 128 so that i use that value range better. When rendering i scale it up again. That often helps much. (when working with fixed points)

Second: Which fixed point do u use for texturecoords? T16 is made for that. So convert an UV from an int to it should be fine inttot16(64); u can store preconverted t16 and f32 for texture and mesh. its best for models.
And fastest way of course.