[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3534
  • Last Modified:

rendering normals/drawing lines

Hi

I'm busy trying to write a 3d engine in Direct3D, but am having issues with my normals. That, however, is not what this question is about (unless you can quickly explain how I can use 3D Studio Max's smoothing groups to calculate the normals). At the moment, I'm trying to render the normals so that I can see what is going on (more of a debugging thing). At the moment, I've got the model loading and rendering, so that's not really a problem. The real problem comes in when I try to render lines in 3D. I have tried a number of methods, all of which break in one way or another. The following code samples have been executed each frame in a function called RenderNormals() (but don't work).

m_pDevice->SetMaterial(NULL);
m_pDevice->SetRenderState(D3DRS_LIGHTING , false);
m_pDevice->SetStreamSource( 0, norms, 0, sizeof(FVF) );
m_pDevice->SetFVF( line_fvf );
   
//m_pDevice->DrawPrimitiveUP(D3DPT_LINELIST, 2,normData ,                                                //sizeof(line_vertex));  
m_pDevice->DrawPrimitive( D3DPT_LINELIST, 0, 1 );
m_pDevice->SetRenderState(D3DRS_LIGHTING , true);

After copying some data into the norms vertex buffer, this code is executed, but doesn't work. I forget what exactly it did, but suffice to say that it broke.

      D3DXVECTOR3 p[4];
      p[0] = D3DXVECTOR3(0.0f,0.0f,0.0f);
      p[1] = D3DXVECTOR3(20.0f,0.0f,0.0f);
      p[2] = D3DXVECTOR3(20.0f,20.0f,0.0f);
      p[3] = D3DXVECTOR3(20.0f,20.0f,20.0f);
      
      ID3DXLine *Line;
      D3DXCreateLine(m_pDevice, &Line);
      D3DCOLOR col = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);

      Line->Begin();
      
      D3DXMATRIX out, view, proj, world;

      m_pDevice->GetTransform(D3DTS_VIEW, &view);
      m_pDevice->GetTransform(D3DTS_PROJECTION, &proj);
      m_pDevice->GetTransform(D3DTS_WORLD, &world);
      
      D3DXMatrixIdentity(&out);
      out = world * view * proj;
      Line->DrawTransform(p, 4, &out, col);
      Line->End();
      Line->Release();

This code was my more recent attempt, where I tried to fiddle with the matrices myself and draw the transformed points. Also, no good.

      ID3DXLine *Line;
      D3DXCreateLine(m_pDevice, &Line);
      D3DCOLOR col = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);

      Line->Begin();
      
      D3DXMATRIX out, view, proj, world;

      m_pDevice->GetTransform(D3DTS_VIEW, &view);
      m_pDevice->GetTransform(D3DTS_PROJECTION, &proj);
      m_pDevice->GetTransform(D3DTS_WORLD, &world);
      
      D3DXMatrixIdentity(&out);
      out = world * view * proj;
      D3DXVECTOR2 list[4];

      D3DXVECTOR4 test;
      for (int i = 0; i < 4; i++)
      {
            D3DXVec3Transform(&test, &p[i], &out);
            list[i].x = test.x;
            list[i].y = test.y;
      }

      Line->Draw(list,4,col);
      Line->End();
      Line->Release();

This was my last attempt to try to transform the 3D points in p(almost) manually and then draw them in screen space. This kind of worked, but I started getting weird artifacts where the lines started jumping all over the show and moved depending on where the camera was looking.

Please help me. This is really starting to annoy me, since I can't figure out what I'm doing wrong or if there is an easier way to do this whole thing.

Thanks
Phoenix260182
0
phoenix260182
Asked:
phoenix260182
1 Solution
 
davebytesCommented:
Nah, don't go transforming yourself.  Should just work.  Here's a DX8 example, but it looked mostly-relevant:

http://www.flipcode.com/articles/article_guidedx82d.shtml

Obviously, minor changes for DX9 (like SetFVF, I believe...).  If you can't get it to work, lemme know.  Guessing you might have just been missing something -- but incomplete code, so I can't tell.  Could be your vertex data/format, could be you need to disable/enable some other DX state flag.  Not gonna blind guess... ;)

-d
0
 
phoenix260182Author Commented:
Well, while going through the code sample, I discovered, my one line which was:

m_pDevice->SetStreamSource( 0, norms, 0, sizeof(FVF) );

needed to be:

m_pDevice->SetStreamSource( 0, norms, 0, sizeof(line_vertex) );

Oops. Anyway, moving on... Thanks for your help.

0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now