Procedural sphere creation

Started by
14 comments, last by Hurp 14 years, 10 months ago
Oh... Direct3D...

I had given it up for a few years already (since DirectX 8). I'm not quite sure whether I can help or not.


Anyway, at least, I should be able to provide some basic information.

Firstly,

lpD3Device->CreateIndexBuffer(m_unNumberOfIndicies*sizeof(int),0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_IB, 0);

This line is probably incorrect. D3DFMT_INDEX16 is probably two bytes integer. However, you copy to the buffer a block of 4 bytes integer later. You must be very unlucky. Otherwise, you should have noticed it because the program crashed during execution.


Secondly, the orientations of Direct3D and OpenGL are different. OpenGL is right-handed and Direct3D is left-handed. Also, the direction of the axis of Direct3D and OpenGL point towards different directions too. Therefore, when you converting a 3D mesh from OpenGL to Direct3D (vice versa), you need to explicitly reverse the winding in order to make culling to work correctly. Also, please pay some attention about the direction of the axis.

(Anyway, if the windings of a 3D model are consistent in the first place, there will be two cases only. You can try them both to see which one is correct simply.)

To reverse the winding, you can
  for( t=0, j=0; j<height-3; j++ )  for(      i=0; i<width-1; i++ )  {    idx[t++] = (j  )*width + i  ;    idx[t++] = (j+1)*width + i+1;  // switch this line with the following line    idx[t++] = (j  )*width + i+1;  // <<    idx[t++] = (j  )*width + i  ;    idx[t++] = (j+1)*width + i  ;  // switch this line with the following line    idx[t++] = (j+1)*width + i+1;  // <<  }  for( i=0; i<width-1; i++ )  {    idx[t++] = (height-2)*width;    idx[t++] = i;  // switch this line with the following line    idx[t++] = i+1;  // <<    idx[t++] = (height-2)*width+1;    idx[t++] = (height-3)*width + i+1;  // switch this line with the following line    idx[t++] = (height-3)*width + i;  // <<  }


[Edited by - ma_hty on June 12, 2009 8:47:52 AM]
Advertisement
By the way, please be careful about the memory management.

The memory you requested from the system using malloc() should be returned to the system by explicitly calling free(). If you didn't, the system will keep reserving that memory block for you. In the worst case, it can exhaust all memory available.

[Edited by - ma_hty on June 12, 2009 7:12:11 AM]
Quote:Original post by ma_hty
lpD3Device->CreateIndexBuffer(m_unNumberOfIndicies*sizeof(int),0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_IB, 0);

This line is probably incorrect. D3DFMT_INDEX16 is probably two bytes integer. However, you copy to the buffer a block of 4 bytes integer later. You must be very unlucky. Otherwise, you should have noticed it because the program crashed during execution.



If not D3DFMT_INDEX16, then what else could it be?

Also, I tried your window order change and I still have the same issue. In fact, it looks the same.

http://img34.imageshack.us/img34/1073/brokenf.jpg
It is a problem that you cannot ignore. (@@... funny Hurp...)

By the way, it is easy to fix too. Just use D3DFMT_INDEX32.
lpD3Device->CreateVertexBuffer(nvec*sizeof(float), 0, 0, D3DPOOL_DEFAULT, &m_VB, 0); //<< wrong


lpD3Device->CreateVertexBuffer(nvec*3*sizeof(float), 0, 0, D3DPOOL_DEFAULT, &m_VB, 0);
Quote:Original post by ma_hty
lpD3Device->CreateVertexBuffer(nvec*sizeof(float), 0, 0, D3DPOOL_DEFAULT, &m_VB, 0); //<< wrong


lpD3Device->CreateVertexBuffer(nvec*3*sizeof(float), 0, 0, D3DPOOL_DEFAULT, &m_VB, 0);


Ah, that did fix it. Thanks a lot. I have two last questions about the code.

1) If I wanted to have the size based off a radius, what would be the best approach? Would it be ..

theta = float(j)/(height-1) * (RADIUS * PI);
phi = float(i)/(width-1 ) * (RADIUS * PI*2);

2) What is "weight" and "height"? Is that stacks and slices?

This topic is closed to new replies.

Advertisement