• Advertisement
Sign in to follow this  

How to apply Matrix Transformation to individual models.

This topic is 1444 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I've worked to import models from Maya using custom code into a C++/DirectX game. But, while trying to come up with a way to render multiple imported models, I've run into an issue identifying the individual vertex buffer to apply the transformations to. Is this something I can use function "scoping" to separate the models from each other and draw individually? Or are they all stored in the one global buffer?

 

(Vertices are rendered using DrawIndex())

Edited by InfinityMachine

Share this post


Link to post
Share on other sites
Advertisement
I think you should load the models from the maya export into separate meshes/ models, that way you can handle them individually. Transformations, rotations, scaling, rendering, culling etc.

Share this post


Link to post
Share on other sites

Okay. Maybe you could try another Maya file loader. Personally no experience with this, I'm using Max (2011)

I wrote the parser (file loader); it works, but I'm unfamiliar with DirectX methodology, and was coding partially in the dark. I didn't know how to apply matrix transformations to specific vertices in the buffer, particularly enough to provide the feel of a direct transform (done with members of the same class) when multiple objects were in the buffer at once.

Share this post


Link to post
Share on other sites

You aren't changing the coordinates of the vertices, are you? You're supposed to pass the matrix into the shaders and change the coordinates inside the shader. Directly changing the coordinates of vertices will accumulate FP rounding errors and cause you to send the vertex data back and forth between the CPU and GPU every frame.

 

I admit I'm not fully understanding what you're actually doing, nor what you want to do though. If you still need help, perhaps just explain what you're doing as if to a child so you don't leave anything important out. If you're just following a tutorial and trying to modify it to learn something new, link to the tutorial and explain what you're trying to add.

Share this post


Link to post
Share on other sites

When you import your models, they should be centered on themselves, in their "model space".
 

Matrix transforms aren't usually done on the CPU these days (unless targeting specific GPU/CPU load balance I guess), so the idea is that you grab these models, construct a matrix representing their position and orientation in the world, send them off to the GPU, and do the math there. You'll also need other matrices (view matrix for moving everything to camera space, projection matrix to move everything to screen space, and you'll also need a matrix for normals if you're doing lightning).

 

Set up a matrix (world, view and projection combined), set up a buffer with a model and send them to the shader. Rinse and repeat for each model. Forever.

Share this post


Link to post
Share on other sites

You aren't changing the coordinates of the vertices, are you? You're supposed to pass the matrix into the shaders and change the coordinates inside the shader. Directly changing the coordinates of vertices will accumulate FP rounding errors and cause you to send the vertex data back and forth between the CPU and GPU every frame.

 

I admit I'm not fully understanding what you're actually doing, nor what you want to do though. If you still need help, perhaps just explain what you're doing as if to a child so you don't leave anything important out. If you're just following a tutorial and trying to modify it to learn something new, link to the tutorial and explain what you're trying to add.

 

No tutorial, but yes: DirectX documentation and MSDN. I managed to import some of my Maya goods into my exe: Great! Then I thought, I should import another .obj! Of course wrapping up my process. (minus the obj parsing copies and other slow stuff -> gets delete[] in scope) Running the function twice, didn't work, presumably because I neglected to update the total number of indices for DrawIndexed(). Which I could wrap my mind around. But when it comes to performing transforms I gasped! Where's my data? How can I refer to Object 3's vertex data to perform a transform? I felt that Willy Wonka's magic teleporter was upon me, and little chocolate vertices were giggling all at once above my head. Elusive. 

Share this post


Link to post
Share on other sites

When you import your models, they should be centered on themselves, in their "model space".
 

Matrix transforms aren't usually done on the CPU these days (unless targeting specific GPU/CPU load balance I guess), so the idea is that you grab these models, construct a matrix representing their position and orientation in the world, send them off to the GPU, and do the math there. You'll also need other matrices (view matrix for moving everything to camera space, projection matrix to move everything to screen space, and you'll also need a matrix for normals if you're doing lightning).

 

Set up a matrix (world, view and projection combined), set up a buffer with a model and send them to the shader. Rinse and repeat for each model. Forever.

 

Ok something like: (Which I may magic wrap)

Import model01->(VertBuff)(IndiceBuff)
Import model02->(VertBuff)(IndiceBuff)

Render()
Clear the heck out(everything)

Store transforms-> float4x4, Rotation, Translate, Scale
Perform transform->DXfunctionRotX->m1VertBuff
Draw(Vertbuff, ...)

Store transforms-> float4x4, Rotation, Translate, Scale
Perform transform->DXfunctionRotX->m1VertBuff
Draw(Vertbuff, ...)

Swap backbuffer


You know that was so straight forward... I forgot the nature of the pipeline. Thanks! I'll try to implement this tonight!

Edited by InfinityMachine

Share this post


Link to post
Share on other sites

You aren't changing the coordinates of the vertices, are you? You're supposed to pass the matrix into the shaders and change the coordinates inside the shader. Directly changing the coordinates of vertices will accumulate FP rounding errors and cause you to send the vertex data back and forth between the CPU and GPU every frame.

 

I admit I'm not fully understanding what you're actually doing, nor what you want to do though. If you still need help, perhaps just explain what you're doing as if to a child so you don't leave anything important out. If you're just following a tutorial and trying to modify it to learn something new, link to the tutorial and explain what you're trying to add.

 

Although I'm not changing the vertices directly that was a good point. I read about that I believe either during reading up on optimization or maybe newer Dx11 features. I'll provide the link for future eyes when I can find it.

Share this post


Link to post
Share on other sites

You know that was so straight forward... I forgot the nature of the pipeline. Thanks! I'll try to implement this tonight!

Yes and no.

 

I don't know the particulars of DirectX but I think there are a few steps you're missing. When I'm saying you "do the math in the GPU" I'm saying that you do the transforms in a shader. As far as I'm seeing you're not using any shaders.

 

Shaders are programs that are executed in the GPU. They're coded in a specific language, DirectX shader language is HLSL.

 

The vertex shader stage is dedicated for this purpose. When you issue your draw call (after setting up the appropriate vertex buffer and matrix constant buffer) the GPU will grab the vertices (in model space, like when you loaded them) in the order they're presented (or by indexing like you're doing) and it will multiply each one by the matrix you're passing to the vertex shader.

 

In your vertex shader you'll have something like:

 

incoming vertexPosition; // This one will be changed each time the program gets executed for each vertex you send to the GPU.
incoming constant matrixTransform; // This one is constant for the whole model. Changed before you draw the next model.
 
vertex program
{
    finalPosition = vertexPosition * matrixTransform; // Row vertex * 4x4 transform matrix (which is modelMat * viewMat * projMat, precomputed in CPU ).
}
( I could put "correct" GLSL but it wouldn't help you at all :D

 

After that you have a pixel shader stage that will compute each pixel produced by the geometry that was processed by the vertex shader.

 

Now you might be using fixed function pipeline on DirectX (9 I think?), things change quite a bit in that case, and there is where my knowledge ends. No idea how to use OpenGL fixed function pipeline, much less DX's.

Share this post


Link to post
Share on other sites

I modeled a system (Using a object class: I import models, store the start, number of verts/indices in a dynamic data struture (for global/debug) and it looks like it's working. There was a load of screaming, but after having to create masses of functions, literally building from the ground up,Thanks guys! I'm one level closer to the game I want to create and I'm on top of my code!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement