rendering normals/drawing lines

Posted on 2005-03-10
Medium Priority
Last Modified: 2013-12-08

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->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);

      D3DXMATRIX out, view, proj, world;

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

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);

      D3DXMATRIX out, view, proj, world;

      m_pDevice->GetTransform(D3DTS_VIEW, &view);
      m_pDevice->GetTransform(D3DTS_PROJECTION, &proj);
      m_pDevice->GetTransform(D3DTS_WORLD, &world);
      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;


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.

Question by:phoenix260182
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 17

Accepted Solution

davebytes earned 600 total points
ID: 13514139
Nah, don't go transforming yourself.  Should just work.  Here's a DX8 example, but it looked mostly-relevant:


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... ;)


Author Comment

ID: 13534461
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.


Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question