Problom with building a cube

Started by
8 comments, last by jollyjeffers 18 years ago
hello all i'm trying to build a 3d cube. i made the front square and the "up" squre. but when i tried to make the "back" square it seems to multipal the "up" square like i see some strange stuff there and i dont see the back. can anyone tell me what is my problom? here is the code:

	{-0.5f, -0.5f, 0.0f,  D3DCOLOR_COLORVALUE( 1.0, 0.0, 0.0, 1.0 ) }, // front
	{ 0.5f, -0.5f, 0.0f,  D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ) },
    {-0.5f, 0.5f, 0.0f,  D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ) },
    { 0.5f, 0.5f, 0.0f,  D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ) },   

	{-0.5f, 0.5f, 0.5f,  D3DCOLOR_COLORVALUE( 1.0, 0.0, 0.0, 1.0 ) }, // up
	{ 0.5f, 0.5f, 0.5f,  D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ) },
    {-0.5f, 0.5f, 0,  D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ) },
    { 0.5f, 0.5f, 0,  D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ) },   

	

		{-0.5f, -0.5f, 0.5f,  D3DCOLOR_COLORVALUE( 1.0, 0.0, 0.0, 1.0 ) }, // back
	{ 0.5f, -0.5f, 0.5f,  D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ) },
    {-0.5f, 0.5f, 0.5f,  D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ) },
    { 0.5f, 0.5f, 0.5f,  D3DCOLOR_COLORVALUE( 0.0, 1.0, 0.0, 1.0 ) },   


Advertisement
For the simplest of cubes you could use D3DXCreateCube(), if that's what it is called.

Dave
It seems like you are trying to make a unit cube centered at the origin. If that is correct, all of your vertices' x, y, and z values must be either 0.5f or -0.5f. The front and back faces have values of 0.0f for some of the z-values.

Also, it depends on how you connect the vertices. You are using trianglestrips for each quad/face. Are you using an index buffer or are you drawing 2 polys (1 quad) at a time with drawprimitive?
-------Harmotion - Free 1v1 top-down shooter!Double Jump StudiosBlog
what is an index buffer?
and another question i have:
i have just entered a tutorial for a cube.
they dont realy explain and they only give the code.
can you explain to me why they wrote it so many times and cant write it in 1 time?
here is the code:
	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  0, 2 );	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  4, 2 );	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  8, 2 );	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 12, 2 );	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 16, 2 );	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 20, 2 );

thanks in advance
Working this out on paper is usually an easy way of working things out [wink]

Also, as I suggested in your other thread, turn D3DRS_CULLMODE to D3DCULL_NONE to make sure you've got the right vertices for the right triangles. Then switch it back to D3DCULL_CCW (or _CW) and sort out re-ordering your vertices...

Quote:what is an index buffer?
Searching for exactly that question yields this page as the 1st result. Seems like a pretty good overview/description to me.

Quote:i have just entered a tutorial for a cube.
they dont realy explain and they only give the code.
can you explain to me why they wrote it so many times and cant write it in 1 time?
The key is in the TRIANGLESTRIP primitive type. Getting a single triangle strip to cover the surface of a cube isn't particularly intuitive, so you have to split it into 6 short triangle strips. I'd personally say it's not a very good way to create a cube - using an indexed triangle list is far better.

There are 8 corners in a cube:

000
001
010
011
100
101
110
111

Where each '0' is a -0.5 and each '1' is a +0.5 (remember what I said in your other thread about binary sequences [grin])...

Define a vertex buffer with these 8 coordinates in.

Then define an index buffer with 36 indices - 3 for each triangle, 2 triangles per face, and 6 faces...

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Something like that...
     void addside(D3DVECTOR p1, D3DVECTOR p2, D3DVECTOR p3, D3DVECTOR p4,      D3DVECTOR normal) {         add(p1.x, p1.y, p1.z, normal.x, normal.y, normal.z, 0);         add(p2.x, p2.y, p2.z, normal.x, normal.y, normal.z, 0);         add(p3.x, p3.y, p3.z, normal.x, normal.y, normal.z, 1);         add(p1.x, p1.y, p1.z, normal.x, normal.y, normal.z, 0);         add(p3.x, p3.y, p3.z, normal.x, normal.y, normal.z, 0);         add(p4.x, p4.y, p4.z, normal.x, normal.y, normal.z, 1);     }        D3DVECTOR p1 = D3DXVECTOR3(minx, miny, minz),                  p2 = D3DXVECTOR3(minx, maxy, minz),                  p3 = D3DXVECTOR3(maxx, maxy, minz),                  p4 = D3DXVECTOR3(maxx, miny, minz),                  p5 = D3DXVECTOR3(minx, miny, maxz),                  p6 = D3DXVECTOR3(minx, maxy, maxz),                  p7 = D3DXVECTOR3(maxx, maxy, maxz),                  p8 = D3DXVECTOR3(maxx, miny, maxz);        addside(p1, p2, p3, p4, D3DXVECTOR3(0, 0, -1)); // front        addside(p4, p3, p7, p8, D3DXVECTOR3(1, 0, 0));  // right        addside(p8, p7, p6, p5, D3DXVECTOR3(0, 0, 1));  // back        addside(p6, p2, p1, p5, D3DXVECTOR3(-1, 0, 0)); // left        addside(p1, p4, p8, p5, D3DXVECTOR3(0, -1, 0)); // bottom        addside(p2, p6, p7, p3, D3DXVECTOR3(0, 1, 0));  // top
Insufficent Information: we need more infromationhttp://staff.samods.org/aiursrage2k/
i read a bit in the website you gave me but....
i still dont understand ... why we need the index buffers?
i can create a square without it... does it help in anything?
Index buffers are used to save loading up your vertexbuffer with identical vertices.

If for instance you want to write a quad using two triangles:

1----3
|\---|
|-\--|
|--\-|
|---\|
2----4

where the numbers represent vertices then you would have (1,4,2) for the first triangle and (1,3,4) for the other triangle. Notice that you would have to load vertex 1 and 4 twice.

If you instead use an indexbuffer then you would load the vertices once for each of them and then just specify in the indexbuffer which vertex you want to use at which position.

EDIT: This is just for showing the point. I would never use two triangles but TRIANGLE_STRIP or similar

[Edited by - thallish on April 19, 2006 2:49:23 AM]
regards/thallishI don't care if I'm known, I'd rather people know me
ok i think i understand now... in order to use less Vertices in creating the cube i need to create an index buffer.
but you still didnt answer my question:
what this code is doing and why they cant write it in 1 line:
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  0, 2 );	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  4, 2 );	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP,  8, 2 );	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 12, 2 );	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 16, 2 );	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 20, 2 );

i think that the second parameter is the number of triangles that will be showen (not sure about that though).
Quote:in order to use less Vertices in creating the cube i need to create an index buffer.
The use of index buffers are both a storage and performance optimization. There isn't really anything you can't do with just vertex buffers, but using VB+IB it'll be more efficient.

Quote:but you still didnt answer my question:
what this code is doing and why they cant write it in 1 line:

Quote:Original post by jollyjeffers
Quote:i have just entered a tutorial for a cube.
they dont realy explain and they only give the code.
can you explain to me why they wrote it so many times and cant write it in 1 time?
The key is in the TRIANGLESTRIP primitive type. Getting a single triangle strip to cover the surface of a cube isn't particularly intuitive, so you have to split it into 6 short triangle strips. I'd personally say it's not a very good way to create a cube - using an indexed triangle list is far better.


Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

This topic is closed to new replies.

Advertisement