Author Topic: [C++] [DirectX9] Hardware rot/scale/trans just not working for me  (Read 11024 times)

0 Members and 1 Guest are viewing this topic.

Offline XiA

  • ZX 81
  • *
  • Posts: 18
  • Karma: 1
    • View Profile
Hi!

I'm trying to make hardware scaling/rotating/translating work in DirectX9, using the Drunken Hyena example at
http://www.drunkenhyena.com/cgi-bin/view_cpp_article.pl?chapter=2;article=28
as a template.

Here's my init code:
Code: [Select]
// g_pd3dDevice is LPDIRECT3DDEVICE9
D3DXMatrixIdentity(&X_H3D_view_matrix);
D3DXMatrixIdentity(&X_H3D_projection_matrix);
D3DXMatrixIdentity(&X_H3D_rotation_matrix);
D3DXMatrixIdentity(&X_H3D_translation_matrix);
D3DXMatrixIdentity(&X_H3D_world_matrix);

X_H3D_eye_vector=D3DXVECTOR3( 0.0f, 0.0f,-8.0f );
X_H3D_lookat_vector=D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
X_H3D_up_vector=D3DXVECTOR3(0.0f,1.0f,0.0f);
D3DXMatrixLookAtLH(&X_H3D_view_matrix,&X_H3D_eye_vector,
                   &X_H3D_lookat_vector,
                   &X_H3D_up_vector);
g_pd3dDevice->SetTransform(D3DTS_VIEW,&X_H3D_view_matrix);

X_H3D_aspect=((float)WindowWidth / (float)WindowHeight);
D3DXMatrixPerspectiveFovLH(&X_H3D_projection_matrix,
                           D3DX_PI/4,
                           X_H3D_aspect,
                           -1.0f,
                           100.0f );
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &X_H3D_projection_matrix);

D3DXMatrixIdentity(&X_H3D_world_matrix);
g_pd3dDevice->SetTransform(D3DTS_WORLD,&X_H3D_world_matrix);

And this is the code between BeginScene() and EndScene(), which is later Present():ed:
Code: [Select]
// X_TriangleBuffer is a vector containing int's pointing to vertices in X_VertexBuffer
// X_VertexBuffer is a vector containing vertices
  int numTriangles=X_TriangleBuffer.size();
  CUSTOMVERTEX* vertices=NULL;
  CUSTOMVERTEX thisVertex;
  vertices=new CUSTOMVERTEX[3*numTriangles];
  int numVertexIndex=0;
  DWORD colThis=0xff00ff00;

  // Commented data is just to test if the raw vertex data looks the way it should
  const float numOffsetX=0; // WindowWidth/2;
  const float numOffsetY=0; // WindowHeight/2;
  const float numOffsetZ=0; // 30;

  for(int i=0;i<(int)X_TriangleBuffer.size();i++){
    X_Triangle T=X_TriangleBuffer[i];
    int numA=T.a;
    int numB=T.b;
    int numC=T.c;
    thisVertex.X=(float)X_VertexBuffer[numA].x;
    thisVertex.Y=(float)X_VertexBuffer[numA].y;
    thisVertex.Z=(float)X_VertexBuffer[numA].z;
    thisVertex.RHW=0.2f;
    thisVertex.COLOR=colThis;
    thisVertex.X+=numOffsetX;
    thisVertex.Y+=numOffsetY;
    thisVertex.Z+=numOffsetZ;
    vertices[numVertexIndex++]=thisVertex;
    thisVertex.X=(float)X_VertexBuffer[numB].x;
    thisVertex.Y=(float)X_VertexBuffer[numB].y;
    thisVertex.Z=(float)X_VertexBuffer[numB].z;
    thisVertex.RHW=0.2f;
    thisVertex.COLOR=colThis;
    thisVertex.X+=numOffsetX;
    thisVertex.Y+=numOffsetY;
    thisVertex.Z+=numOffsetZ;
    vertices[numVertexIndex++]=thisVertex;
    thisVertex.X=(float)X_VertexBuffer[numC].x;
    thisVertex.Y=(float)X_VertexBuffer[numC].y;
    thisVertex.Z=(float)X_VertexBuffer[numC].z;
    thisVertex.RHW=0.2f;
    thisVertex.COLOR=colThis;
    thisVertex.X+=numOffsetX;
    thisVertex.Y+=numOffsetY;
    thisVertex.Z+=numOffsetZ;
    vertices[numVertexIndex++]=thisVertex;
  }

  struct D3DVIEWPORT9{
    DWORD X;
    DWORD Y;
    DWORD Width;
    DWORD Height;
    float MinZ;
    float MaxZ;
  };

  X_H3D_view_port.X=0;
  X_H3D_view_port.Y=0;
  X_H3D_view_port.Width=WindowWidth;
  X_H3D_view_port.Height=WindowHeight;
  X_H3D_view_port.MinZ=-100.0f;
  X_H3D_view_port.MaxZ=100.0f;

  g_pd3dDevice->SetViewport(&X_H3D_view_port);

  g_pd3dDevice->SetRenderState(D3DRS_LIGHTING,FALSE);

  g_pd3dDevice->CreateVertexBuffer((3*numTriangles)*sizeof(CUSTOMVERTEX),
                             0,
                             CUSTOMFVF,
                             D3DPOOL_MANAGED,
                             &v_buffer,
                             NULL);
  VOID* pVoid;    // a void pointer

  v_buffer->Lock(0, 0, (void**)&pVoid, 0);
  memcpy(pVoid, vertices, 3*numTriangles*sizeof(CUSTOMVERTEX));
  v_buffer->Unlock();

  g_pd3dDevice->SetFVF(CUSTOMFVF);

  g_pd3dDevice->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));

  D3DXMatrixRotationY(&X_H3D_rotation_matrix,0.9f);
  D3DXMatrixRotationX(&X_H3D_rotation_matrix,0.9f);

  D3DXMatrixTranslation(&X_H3D_translation_matrix,0.0f,0.0f,0.0f);
  D3DXMatrixMultiply(&X_H3D_world_matrix,&X_H3D_rotation_matrix,&X_H3D_translation_matrix);

  g_pd3dDevice->SetTransform(D3DTS_WORLD,&X_H3D_world_matrix);

  g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, //PrimitiveType
                              0,                  //StartVertex
                              numTriangles);      //PrimitiveCount

  delete [] vertices;

My vertex data is fine, and the stuff is drawn, but if I comment out all the rotation matrix stuff, I get the exact same result. It just does NOTHING.

I'm sure I'm doing something wrong here, and quite probably even something really stupid, and I'd appreciate any help here.

Thanks in advance!

Offline LittleWhite

  • Senior Member
  • Amiga 1200
  • ********
  • Posts: 418
  • Karma: 31
  • It's me!
    • View Profile
Hello,

I am a bit unsure about this code
Code: [Select]
D3DXMatrixMultiply(&X_H3D_world_matrix,&X_H3D_rotation_matrix,&X_H3D_translation_matrix);
I would have done the following (maybe I am silly because I am not simplifying stuff)
Code: [Select]
D3DXMatrixMultiply(&X_H3D_world_matrix,&X_H3D_world_matrix,&X_H3D_rotation_matrix);
D3DXMatrixMultiply(&X_H3D_world_matrix,&X_H3D_world_matrix,&X_H3D_translation_matrix);
The demoscene will never die, never!

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
All your matrices appear to be unit matrices, so when they are applied they don't do anything. What are you expecting to happen?
Jim
Challenge Trophies Won:

Offline LittleWhite

  • Senior Member
  • Amiga 1200
  • ********
  • Posts: 418
  • Karma: 31
  • It's me!
    • View Profile
All your matrices appear to be unit matrices, so when they are applied they don't do anything. What are you expecting to happen?
Jim

And this code does not do the job ?
Code: [Select]
D3DXMatrixRotationY(&X_H3D_rotation_matrix,0.9f);
  D3DXMatrixRotationX(&X_H3D_rotation_matrix,0.9f);

  D3DXMatrixTranslation(&X_H3D_translation_matrix,0.0f,0.0f,0.0f);
  D3DXMatrixMultiply(&X_H3D_world_matrix,&X_H3D_rotation_matrix,&X_H3D_translation_matrix);

  g_pd3dDevice->SetTransform(D3DTS_WORLD,&X_H3D_world_matrix);
The demoscene will never die, never!

Offline XiA

  • ZX 81
  • *
  • Posts: 18
  • Karma: 1
    • View Profile
Hello,

I would have done the following (maybe I am silly because I am not simplifying stuff)
Code: [Select]
D3DXMatrixMultiply(&X_H3D_world_matrix,&X_H3D_world_matrix,&X_H3D_rotation_matrix);
D3DXMatrixMultiply(&X_H3D_world_matrix,&X_H3D_world_matrix,&X_H3D_translation_matrix);

Ah, good thinking! But sadly, it didn't make a difference. Thanks for the idea tho!

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Quote
And this code does not do the job ?
That'll teach me.  The code you highlight isn't visible on my Android phone where there are no scrollbars down the side of the code boxes. :-(

Jim
Challenge Trophies Won:

Offline LittleWhite

  • Senior Member
  • Amiga 1200
  • ********
  • Posts: 418
  • Karma: 31
  • It's me!
    • View Profile
I am a bit wondering on what you are trying to do.
In fact, you will just have the cube rotated of 0.9 on 1 axis (because I think you are killing the matrices where you have done the rotation on the other axis).
Moreover, the cube will not move at all, because you are not using some kind of counter ... that would make the rotation angle to change.

So, here are my thoughts:

- Put a counter to change rotation angle
- Debug the code to show the values in the matrices ... when you are passing it to the pipeline.
The demoscene will never die, never!

Offline XiA

  • ZX 81
  • *
  • Posts: 18
  • Karma: 1
    • View Profile
I am a bit wondering on what you are trying to do.

Definitely a valid question! My end goal is to be able to do demos using pixel shaders. And all the reading up I've done have pointed in the direction that I need to get rid of my current code that rotates/scales/transforms manually, and then draws, and replace that with code that sends all these tasks to the hardware.

In fact, you will just have the cube rotated of 0.9 on 1 axis (because I think you are killing the matrices where you have done the rotation on the other axis).
Moreover, the cube will not move at all, because you are not using some kind of counter ... that would make the rotation angle to change.

Agreed. This code wouldn't animate at all. My *real* problem, however, is that all this code for transforming/rotating does NOTHING. If I comment it out, I get the exact same result.

So, here are my thoughts:

- Put a counter to change rotation angle
- Debug the code to show the values in the matrices ... when you are passing it to the pipeline.

Yeah, the matrices look fine, they're changed as the code progresses. If I were to pinpoint a single point of failure, it's as if my SetTransform() call does NOTHING to the DrawPrimitive() call. Or maybe it's affecting a different StreamSource? The code is drawing exactly what I'm putting in the vertex buffer, but none of the transformations are being done.

Oh, and I've tried animating the values for the rotation matrix, made no difference whatsoever; my polygon object just sits there, as if to taunt me  :)

Offline XiA

  • ZX 81
  • *
  • Posts: 18
  • Karma: 1
    • View Profile
none of the transformations are being done.

...IS being done. Performed. Executed.

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Try changing your Z-value of the near view-plane to 1.0f instead -1.0f
Code: [Select]
D3DXMatrixPerspectiveFovLH(&X_H3D_projection_matrix,
                           D3DX_PI/4,
                           X_H3D_aspect,
                           1.0f,
                           100.0f );
Challenge Trophies Won:

Offline XiA

  • ZX 81
  • *
  • Posts: 18
  • Karma: 1
    • View Profile
Try changing your Z-value of the near view-plane to 1.0f instead -1.0f

Yeah, I changed that as a test and forgot to change it back. But my polygons are showing, the problem is they aren't responding to the SetTransform(). Or at least that's what it looks like. But thanks anyway.

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
If you like, you can post your complete msvc source (minimal framework), and I can test it here.
Challenge Trophies Won:

Offline XiA

  • ZX 81
  • *
  • Posts: 18
  • Karma: 1
    • View Profile
If you like, you can post your complete msvc source (minimal framework), and I can test it here.

Thanks for the offer! I've sent you a PM!

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Ok, it was viewport MinZ/MaxZ messing things up, changed it to default config and added a textured cube model to test rotation/translation.
It's hard to spot those problems without seeing the complete non working code :)

Code: [Select]
struct CUSTOMVERTEX2 {FLOAT X, Y, Z; D3DVECTOR NORMAL; FLOAT U, V;};
#define CUSTOMFVF2 (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)
float rotX=0;
float rotY=0;
float rotZ=0;
D3DXMATRIX X_H3D_rotation_matrix_rotX;
D3DXMATRIX X_H3D_rotation_matrix_rotY;
D3DXMATRIX X_H3D_rotation_matrix_rotZ;


void OneFrameHardware3D(){
  bool boolShowVertexData=false;
  initHardware3D();

    // create the vertices using the CUSTOMVERTEX struct
    CUSTOMVERTEX2 cube_vert[] =
    {
        // side 1
        { -1.0f, 1.0f, -1.0f, 0, 0, -1, 0, 1, },
        { 1.0f, 1.0f, -1.0f, 0, 0, -1, 1, 1, },
        { -1.0f, -1.0f, -1.0f, 0, 0, -1, 0, 0, },
        { 1.0f, -1.0f, -1.0f, 0, 0, -1, 1, 0, },

        // side 2
        { -1.0f, 1.0f, 1.0f, 0, 0, 1, 0, 1, },
        { -1.0f, -1.0f, 1.0f, 0, 0, 1, 0, 0, },
        { 1.0f, 1.0f, 1.0f, 0, 0, 1, 1, 1, },
        { 1.0f, -1.0f, 1.0f, 0, 0, 1, 1, 0, },

        // side 3
        { -1.0f, 1.0f, 1.0f, 0, 1, 0, 0, 1, },
        { 1.0f, 1.0f, 1.0f, 0, 1, 0, 1, 1, },
        { -1.0f, 1.0f, -1.0f, 0, 1, 0, 0, 0, },
        { 1.0f, 1.0f, -1.0f, 0, 1, 0, 1, 0, },

        // side 4
        { -1.0f, -1.0f, 1.0f, 0, -1, 0, 0, 1, },
        { -1.0f, -1.0f, -1.0f, 0, -1, 0, 0, 0, },
        { 1.0f, -1.0f, 1.0f, 0, -1, 0, 1, 1, },
        { 1.0f, -1.0f, -1.0f, 0, -1, 0, 1, 0, },

        // side 5
        { 1.0f, 1.0f, -1.0f, 1, 0, 0, 1, 0, },
        { 1.0f, 1.0f, 1.0f, 1, 0, 0, 1, 1, },
        { 1.0f, -1.0f, -1.0f, 1, 0, 0, 0, 0, },
        { 1.0f, -1.0f, 1.0f, 1, 0, 0, 0, 1, },

        // side 6
        { -1.0f, 1.0f, -1.0f, -1, 0, 0, 1, 0, },
        { -1.0f, -1.0f, -1.0f, -1, 0, 0, 0, 0, },
        { -1.0f, 1.0f, 1.0f, -1, 0, 0, 1, 1, },
        { -1.0f, -1.0f, 1.0f, -1, 0, 0, 0, 1, },
    };

  rotX=numRunTime/0.9f;
  rotY=-numRunTime/0.8f;
  rotZ=numRunTime/0.6f;

  struct D3DVIEWPORT9{
    DWORD X;
    DWORD Y;
    DWORD Width;
    DWORD Height;
    float MinZ;
    float MaxZ;
  };

  X_H3D_view_port.X=0;
  X_H3D_view_port.Y=0;
  X_H3D_view_port.Width=WindowWidth;
  X_H3D_view_port.Height=WindowHeight;
  X_H3D_view_port.MinZ=0.0f;
  X_H3D_view_port.MaxZ=1.0f;

  g_pd3dDevice->SetViewport(&X_H3D_view_port);

  g_pd3dDevice->SetRenderState(D3DRS_LIGHTING,FALSE);

  g_pd3dDevice->CreateVertexBuffer(24*sizeof(CUSTOMVERTEX2),
                             0,
                             CUSTOMFVF2,
                             D3DPOOL_MANAGED,
                             &v_buffer,
                             NULL);
  VOID* pVoid;    // a void pointer
 
  // lock v_buffer and load the vertices into it
  v_buffer->Lock(0, 0, (void**)&pVoid, 0);
  memcpy(pVoid, cube_vert, sizeof(cube_vert));
  v_buffer->Unlock();

  // select which vertex format we are using
  g_pd3dDevice->SetFVF(CUSTOMFVF2);
  // select the vertex buffer to display
  g_pd3dDevice->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX2));
 
  D3DXMatrixRotationX(&X_H3D_rotation_matrix_rotX,rotX);
  D3DXMatrixRotationY(&X_H3D_rotation_matrix_rotY,rotY);
  D3DXMatrixRotationZ(&X_H3D_rotation_matrix_rotZ,rotZ);

  D3DXMatrixTranslation(&X_H3D_translation_matrix,0.0f,0.0f,0.0f);

  D3DXMatrixMultiply(&X_H3D_world_matrix,&X_H3D_rotation_matrix_rotX,&X_H3D_rotation_matrix_rotY);
  D3DXMatrixMultiply(&X_H3D_world_matrix,&X_H3D_world_matrix,&X_H3D_rotation_matrix_rotZ);
  D3DXMatrixMultiply(&X_H3D_world_matrix,&X_H3D_world_matrix,&X_H3D_translation_matrix);   //Rot * Trans

  g_pd3dDevice->SetTransform(D3DTS_WORLD,&X_H3D_world_matrix); 

  // set the texture
  g_pd3dDevice->SetTexture(0, textureBackground);

  //Cube
    g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
    g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 2);
    g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 8, 2);
    g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 12, 2);
    g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 16, 2);
    g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 20, 2);

  if(v_buffer!=NULL) v_buffer->Release();

}
Challenge Trophies Won:

Offline XiA

  • ZX 81
  • *
  • Posts: 18
  • Karma: 1
    • View Profile
Ok, it was viewport MinZ/MaxZ messing things up, changed it to default config and added a textured cube model to test rotation/translation.
It's hard to spot those problems without seeing the complete non working code :)

Damn, yup, you sure got the stuff working! Thank you SO MUCH for this, I really appreciate it, I've been stuck on this for 4 days now (well, in all honesty not full-time, luckily). Thanks!

Offline XiA

  • ZX 81
  • *
  • Posts: 18
  • Karma: 1
    • View Profile
Just wanted to report back:

My code is working now (and when I say "my code", I most certainly do not mean my code, I mean the code you wrote and that I stole/modified/used/learned from). Again, thank you!

Offline LittleWhite

  • Senior Member
  • Amiga 1200
  • ********
  • Posts: 418
  • Karma: 31
  • It's me!
    • View Profile
Hum I don't see why the viewport would cause this ... but ... well ... well pointed ;)
The demoscene will never die, never!