views:

78

answers:

2

Okay this is a hard question. I'm creating a cube and a pyramid in one vertex array. My problem is to rotate only pyramid vertex not the cube vertex but I don't know any function that can rotate some vertex. If I try to rotate the vertex I'll get pyramid and cube rotated.

+1  A: 

First thought - ensure the vertices for the pyramid and cube occupy separate non-overlapping ranges in the array. When rotating the pyramid vertices, only tell DirectX about that range of the array (use the base pointer and size of the range rather than the full array).

Will now check the DirectX APIs...

EDIT

Confirmed. The key function is...

D3DXVECTOR4 * D3DXVec4TransformArray(D3DXVECTOR4 * pOut,
                                     UINT OutStride,
                                     CONST D3DXVECTOR4* pV,
                                     UINT VStride,
                                     CONST D3DXMATRIX* pM,
                                     UINT n
                                    );

In C++, a subrange of an array is in many ways an array in its own right, or more to the point, it's mostly just a block of memory with no indication within itself of where its bounds are. So you just pass in a different pV and a different n to indicate the subrange you want to apply the transform to, and as far as the function is concerned, that is the whole array. You'll probably need to copy the untransformed part separately - a job for memcpy, I expect.

The stride values are normally just the size of a vector plus any alignment padding, but it's possible to "lie" about this too, and transform e.g. every third vector in the array.

Steve314
This will work... but it's basically equivalent to regenerating the vertex array (using CPU) each time you want to rotate the objects (and then the vertex buffers will need uploading to the GPU again). This isn't really how modern graphics hardware is designed to be used. While it will work fine for simple cases, the approach won't scale up to hundreds of thousands of vertices where you really need to minimise the amount of CPU intervention needed and let the GPU get on with things.
timday
@timday - OK. I'm not an expert game dev by any means (just vaguely interested), and it's quite likely that there's a lot in DirectX that I'm not aware of.
Steve314
+2  A: 

Either

  • put the cube and the pyramid in different vertex arrays and use different transforms to render each array

or

  • apply the rotations in a vertex shader, and pass in some auxiliary per-vertex info which lets the vertex shader decide whether each vertex should be treated as part of the cube or the pyramid (ie apply different transforms in each case). This'd be a bit like using a "blend weight" to do "vertex blending"; except you're only interested in the binary case.
timday
+1 for vertex shader in particular. Calling it a "shader" seems odd to me, since this is about how transforms blend at a joint in a skeletal model (e.g. how to handle the knee when a leg bends) and not about colours/textures/etc, but I can't blame you for that I suppose.
Steve314
I know what you mean; most fields of science and engineering are sprinked with bizarre nomenclature which made sense once but now seems perverse (astronomy's "planetary nebulae" particularly amuses me).
timday