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 > rotate object in 3d?

#41708 - chrissieboy - Sun May 01, 2005 12:42 pm

Hi im busy with 3d on nds now.

But there is a function rotateX for the camera, but can't find in the lib the function to rotate objects. Does anyone know how i can do this?

Because i want to make a boat that moves, but i can only control it with the camera.

also i can move it with moving the verticies, so the boat can move left or right, or up and down. But i want the boat to turn with left and right, and accelerate with up.

thanx for your help !!

#41752 - Ethos - Sun May 01, 2005 5:37 pm

Code:

#define LUT_SIZE (512)
#define LUT_MASK (0x1FF)
void glRotatef32 (f32 angle, f32 x, f32 y, f32 z) {
     f32 v[3];
     f32 sine = SIN[f32toint(divf32(mulf32(angle ,intof32(LUT_SIZE)),intof32(360))) & LUT_MASK];
     f32 cosine = COS[f32toint(divf32(mulf32(angle ,intof32(LUT_SIZE)),intof32(360))) & LUT_MASK];
     f32 cos_off = intof32 (1) - cosine;

   v[0] = x;
   v[1] = y;
   v[2] = z;
    
    normalizef32 (v);
    
   f32 xy = mulf32(v[0], v[1]);
   f32 xz = mulf32(v[0], v[2]);
   f32 yz = mulf32(v[1], v[2]);
   f32 xs = mulf32(v[0], sine);
   f32 ys = mulf32(v[1], sine);
   f32 zs = mulf32(v[2], sine);

     MATRIX_MULT4x4 = mulf32(mulf32(v[0], v[0]), cos_off) + cosine;
     MATRIX_MULT4x4 = mulf32(xy, cos_off) + zs;
     MATRIX_MULT4x4 = mulf32(xz, cos_off) - ys;
     MATRIX_MULT4x4 = 0;

     MATRIX_MULT4x4 = mulf32(xy, cos_off) - zs;
     MATRIX_MULT4x4 = mulf32(mulf32(v[1], v[1]), cos_off) + cosine;
     MATRIX_MULT4x4 = mulf32(yz, cos_off) + xs;
     MATRIX_MULT4x4 = 0;
 
     MATRIX_MULT4x4 = mulf32(xz, cos_off) + ys;
     MATRIX_MULT4x4 = mulf32(yz, cos_off) - xs;
     MATRIX_MULT4x4 = mulf32(mulf32(v[2], v[2]), cos_off) + cosine;
     MATRIX_MULT4x4 = 0;
 
     MATRIX_MULT4x4 = 0;
     MATRIX_MULT4x4 = 0;
     MATRIX_MULT4x4 = 0;
     MATRIX_MULT4x4 = intof32 (1);
}


That is a rotate function.

Maybe this should be in ndslib...(well something like this), get moving dovoto ;)

#41931 - chrissieboy - Tue May 03, 2005 10:55 am

GREAT! thanx! if im home i try to use it!

#41958 - dovoto - Tue May 03, 2005 8:02 pm

Dont you just switch to model view rotate scale and translate your object, draw it...pop the stack and repeate? the rotate, scale, translate functions have nothing to do with the camera...they operate on the current matrix (even the texture matrix if in texture mode).

I may just be totaly confused.
_________________
www.drunkencoders.com

#41967 - dankydoo - Tue May 03, 2005 9:09 pm

Dovoto,

You are right the glRotatef, glTranslatef, and glScalef functions all affect the model matrix, or the models defined in world space, the matrix created by gluLookAt() takes care of the camera by createing the view matrix. Then they are post mutliplied. Dovoto, are these functions being done in hardware?

dankydoo

#41973 - dovoto - Tue May 03, 2005 9:52 pm

Yes these are being done in hardware, the ds has specific fifo commands for rotation and scaling. The code pasted above should work fine although it is a bit unecessary (i think).

And i bellieve all matrix operations operate on the current modes current matrix. Camera operations are still model view as far as i know...but i do know they work on the texture matricies as well.

I am somewhat 3D illiterate so I should probably not be doing the GL port ;) But it seems to work well enough for now with only a few bug reports comming in.
_________________
www.drunkencoders.com

#41979 - Ethos - Tue May 03, 2005 10:37 pm

How do you suggest then to rotate off a vector (0,1,1) at an angle then??

Do three rotations off 3 calculated angles?

(I myself use the other functions for vectors (0,0,1), (0,1,0), and (0,0,1) for speed purposes)


Last edited by Ethos on Tue May 03, 2005 10:40 pm; edited 1 time in total

#41982 - dankydoo - Tue May 03, 2005 10:39 pm

dovoto wrote:
Yes these are being done in hardware, the ds has specific fifo commands for rotation and scaling. The code pasted above should work fine although it is a bit unecessary (i think).
.


cool...the ds is a neat little machine...

dovoto wrote:

And i bellieve all matrix operations operate on the current modes current matrix. Camera operations are still model view as far as i know...but i do know they work on the texture matricies as well..


yes, all camera operations should be done in mocelview mode, not perspective mode....doing camera operations in perspective mode can affect lighting, fog, and some other things....

As far as I know, the only things that you wanna do while in perspective are glPerspective/glFrustum/glOrtho... and prolly glLoadIdentity()


dovoto wrote:

I am somewhat 3D illiterate so I should probably not be doing the GL port ;) But it seems to work well enough for now with only a few bug reports comming in.


it does seem to work very well so far from what I read in the forums, I will have to take a look at it....

dankydoo

#41984 - tepples - Tue May 03, 2005 10:44 pm

Try this:
  1. Set up view and projection matrices.
  2. For each object:
    1. Push the model-view matrix.
    2. Set the model matrix.
    3. Draw the object.
    4. Pop the model-view matrix.
  3. Finish rendering.
Would that work?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#41988 - Ethos - Tue May 03, 2005 10:55 pm

dovoto wrote:
Yes these are being done in hardware, the ds has specific fifo commands for rotation and scaling.


Where is the FIFO rotate?? That is why I made this function because I saw ndslib using matrix multi for rotates....

#41998 - LOUD NOISES - Tue May 03, 2005 11:58 pm

dovoto wrote:
Yes these are being done in hardware, the ds has specific fifo commands for rotation and scaling. The code pasted above should work fine although it is a bit unecessary (i think).


Although the glRotateX/Y/Z functions work well enough, it's pretty useful to be able to rotate about an arbitrary axis.

Aside from the lack of glRotatef32(), the only thing really bugging me with the library right now is the glTranslate3f32 function. Normally it's simply glTranslatef (or glTranslated) unlike functions like glVertex3f and glTexCoord2f, so I keep forgetting that extra 3. It's not really that hard for me to just make a macro to fix that, but I thought it might be worth changing in a future release.

#42000 - Ethos - Wed May 04, 2005 12:12 am

Code:
#define f32tot16(n)            ((t16)(n >> 8))
#define f32tov16(n)            (n)
#define f32tov10(n)            ((v10)(n >> 3))

void glTexCoord2f32(f32 u, f32 v) {
   glTexCoord2t16(f32tot16 (u), f32tot16 (v));
}

void glVertex3f32 (f32 x, f32 y, f32 z) {
   glVertex3v16(f32tov16(x), f32tov16(y), f32tov16(z));
}

void glTranslatef32 (f32 x, f32 y, f32 z) {
  MATRIX_TRANSLATE = x;
  MATRIX_TRANSLATE = y;
  MATRIX_TRANSLATE = z;
}

void glScalef32 (f32 x, f32 y, f32 z) {
  MATRIX_SCALE = x;
  MATRIX_SCALE = y;
  MATRIX_SCALE = z;
}

void glNormal3f32 (f32 x, f32 y, f32 z) {
   glNormal(NORMAL_PACK(f32tov10(x),f32tov10(y),f32tov10(z)));
}


That covers the rest of the basics...us OpenGL people are particular it seems :P

#42016 - bluescrn - Wed May 04, 2005 7:46 am

tepples wrote:
Try this:
  1. Set up view and projection matrices.
  2. For each object:
    1. Push the model-view matrix.
    2. Set the model matrix.
    3. Draw the object.
    4. Pop the model-view matrix.
  3. Finish rendering.
Would that work?


Unfortunately, the matrix push/pop functions don't work in the current version of iDeaS, as they seem to use the hardware's matrix stack(s).... (works fine on HW, though)

#42021 - Ethos - Wed May 04, 2005 1:26 pm

Yup, right now I wouldn't suggest using ideas for 3D testing...actually my demos will not run on ideas because of some texture problems and a rotation problem. But as far as emulators go...I luv it :)