Sign in to follow this  
vincent_valentine

Render multiple objects questions

Recommended Posts

I wanna write a demo, where after every given time, the program will render a group of two cube (diffirent colors) on the screen at random positions. But I don't know how to draw the multiple cubes... you know, i'm a newbie and the tutorials go with single cube only... How could I render the twin-cubes without knowing how many there are? If I render a lot of things on the screen (like.. 100 cube), do i have to keep the vertex buffer for all the things i have?

Share this post


Link to post
Share on other sites
circlesoft    1178
You can just render the exact same cube in two different positions. When you apply a translation matrix (using IDirect3DDevice9::SetTransform()) and render a set of vertices using DrawPrimitive() or DrawIndexedPrimitive(), the shader or Fixed-Function pipeline transforms the vertices and renders them to the back-buffer. However, this transformation is not permanent. The vertices in the vertex buffer actually have not changed, leaving you free to re-render in different positions.

So, here is what you can do:

(1) Build and apply matrix for first cube
(2) Render vertices
(3) Build and apply matrix for the second cube
(4) Render the exact same vertices you did in (2)
...

Share this post


Link to post
Share on other sites
SSJCORY    100
I'd say keep the vertex buffer's count and just use it in the functions. For example
int vertexcount = 0;
pd3dd->SetStreamSource(0,pD3DVB,sizeof(vertex) * vertexcount)
for your function that every time a certain ammount of time has passed just do:
timepassed(){
if(timepassed == whatevertime)
vertexcount+=(howevermany vertices there are on two cubes);
}
In the render function i would have it restuff the vertex with the different vertices. It's late i prolly dont know what i'm talkin about..........
Thats how i would try and do it. I'm just a noob so dont take my word for it but just consider it as a POSSIBLE solution.

Share this post


Link to post
Share on other sites
Supernat02    604
To change the color of the cube, you have 3 options.

1) Hard code the color used into render states:

pDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
pDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
pDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CONSTANT);
pDevice->SetTextureStageState(0, D3DTSS_CONSTANT, D3DCOLOR_ARGB(0,255,0,0));


This would set the color to red. Before drawing the next cube, change the CONSTANT texture stage state to another color. I learned recently on the forums that this is only available with DirectX9. You can use D3DTA_TFACTOR and set the constant value with the D3DRS_TEXTUREFACTOR render state for previous versions.

2) Lock the vertex buffer, change the DIFFUSE color for all vertices, then unlock the buffer and draw. Repeat for second color.

3) Make the DIFFUSE color of all vertices opaque white. Enable a light, set a material with its color the color that you want the cube, draw the cube, change the color of the material to the next color you want, then set that material and draw the second cube. I think...this is a bit fuzzy.

Chris

Share this post


Link to post
Share on other sites
Thanks but could you explain it more? Circlesoft gave me 4 step... but i don't what how to do with those steps:

3) Build and apply matrix for the second cube
Build is.. prepare the buffer, right?
What kind of matrix i'm gonna use?

4) Render the exact same vertices you did in (2)
OK I'm gonna put a loop and a count, right?

Share this post


Link to post
Share on other sites
I didn't fully understand about your directions... could you give me more details?
So I'm suppose to use only one vertex buffer, right? And how do i render all those cube altogether? And which matrix will be used?

Share this post


Link to post
Share on other sites
circlesoft    1178
Okay, I'll walk through it with you, step-by-step:
// cubeVB is the IDirect3DVertexBuffer9*
// d3dDevice is the IDirect3DDevice9*
// cubeFVF is the FVF definition: it can be something like this
// #define cubeFVF ( D3DFVF_XYZ | D3DFVF_DIFFUSE )
// or maybe
// const DWORD cubeFVF = D3DFVF_XYZ | D3DFVF_DIFFUSE

// Remember to set the view and perspective matrices first.
// To learn how to do this, check out the FAQ (there are a bunch
// of questions that can be answered there, and some great
// tutorial sites at the bottom), or look at the Matrix tutorial
// in the SDK

/* Step 1 */
// Build the transformation matrix for the first cube
D3DXMATRIX mat;

// Translate it to point (2.0, 2.0, 5.0)
D3DXMatrixTranslation( &mat, 2.0f, 2.0f, 5.0f );

// Apply the matrix
d3dDevice->SetTransform( D3DTS_WORLDMATRIX, &mat );

/* Step2 */
// Render the first cube, using the cubeVB vertex buffer.
// For clarification on this part, check some introductory
// tutorials

// Set the stream (the vertex buffer)
d3dDevice->SetStreamSource( 0, cubeVB, 0, sizeof( YourVertexStruct ) );

// Set the FVF (flexible vertex format)
d3dDevice->SetFVF( cubeFVF );

// Now render
d3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, numTriangles );

/* Step 3 */
// Build the transformation matrix for the second cube
D3DXMatrixTranslation( &mat, -2.0f, -2.0f, 5.0f );
d3dDevice->SetTransform( D3DTS_WORLDMATRIX, &mat );


/* Step 4 */
/* Render the second cube */
d3dDevice->SetStreamSource( 0, cubeVB, 0, sizeof( YourVertexStruct ) );
d3dDevice->SetFVF( cubeFVF );
d3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, numTriangles );
You can repeat steps 3 and 4 for each cube you want to render. You can use any kind of transformation matrix you want. For example, you can use a matrix built with these functions (these are just common ones, remember you can also multiply them together to get the transformation you want):

D3DXMatrixIdentity
D3DXMatrixRotationX
D3DXMatrixRotationY
D3DXMatrixRotationZ
D3DXMatrixRotationYawPitchRoll
D3DXMatrixTransformation
D3DXMatrixTranslation
D3DXMatrixTranspose
Make sure you check all of these parameters with the docs, because I don't have access to the documentation on this machine.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this