D3DXMatrixes and Vertices

Started by
1 comment, last by UltimateWalrus 17 years ago
I'm working on a 2D engine in Direct3D. It currently renders sprites using ID3DXSPRITE, and can zoom/rotate the entire screen in realtime. It also has built-in parallax scrolling, which accounts for not only screen translation but also screen zooming (e.g., the moon in the background won't zoom in/out at all, while the mountains will zoom slightly, etc.). Up to this point, I have been using Matrixes to accomplish all this. I scale and rotate the sprites all over the place by building up a Matrix, and then feeding it into ID3DXSPRITE::DrawTransform. (Note: I am using an outdated SDK, because the newest one won't work with MSVC++ 6.0. I think it's just DirectX 9.0) Anyway, I've now added the capability of drawing primitives on the screen. This is done by using the D3DXMatrixOrthoLH function for the projection matrix, and then drawing primitives normally. I would like to make it so that when the screen zooms and rotates, the primitives zoom along with it. I could do this mathematically; however, it would be much easier and possibly more efficient if there was a way to use the screen matrix that I've already calculated. So essentially, what I'm asking is this: Is there an easy way to "tranform" a vertex into a 2D onscreen position using a matrix, much in the same way that sprites can be transformed by matrixes? Thanks!
Advertisement
you can use D3DXVec3TransformCoordinate to transform a vector by a matrix. This would be similar in behaviour to what ID3DXSprite does when you use SetTransform, though it would still be done on the CPU (just like what ID3DXSprite does).

Since you're now drawing vertices directly, you could pass this extra work to the GPU's Vertex Processing Pipeline, which is probably underworked in most 2D apps. To do this, all you probably need to do is set this transformation matrix as the World matrix, and all vertices will be transformed by it by the hardware. A few notes, though:
1) This assumes you're using the Fixed Funtion Pipeline (not shaders)
2) This assumes you're drawing unprojected vertices (i.e. D3DFVF_XYZ)
3) All vertices in one DrawPrimitive will need to use the same matrix

Hope this helps.
Sirob Yes.» - status: Work-O-Rama.
Man, this is so perfect... :D

Thank you very much, sirob! That function works exactly how I would expect. I've now been able to add a 3D-esque parallax ocean to the background of my game... and I'm thinking of many more applications (such as intersplicing 3D and 2D as I think was done in the first Grand Theft Auto). My code is much cleaner and straightforward than when I was calculating each point mathematically, and I suspect it's more efficient as well. But more importantly, I learned something today :)

I may play around with passing the work to the GPU a bit. I'm not sure if I can use it for drawing the primitives, because different matrixes are applied to each vertex (in order to do the parallax). But if I can get it to work with ID3DXSPRITE, I might be able to squeeze a little more efficiency out of my engine. ;)

This topic is closed to new replies.

Advertisement