#### Archived

This topic is now archived and is closed to further replies.

# A Simple Cube

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

## Recommended Posts

I'm trying to do a simple cube and it doesn't work (grrr...), I not sure if I'm right. Actually, I'm WRONG. Could you look what is my mistake ?
	int i;
// Center in 0,0,0
width /= width;
height /= height;
depth /= depth;
for (i = 0; i < 8; i++ )
{
if ( i < 4 )
m_vertex.z  = -depth;
else
m_vertex[i].z  =  depth;
}
m_vertex[0].color = 0x0000ff; // Azul (Blue)
m_vertex[1].color = 0x00FF00; // Verde (Green)
m_vertex[2].color = 0xFF0000; // Vermelho (Red)
m_vertex[3].color = 0x000000; // Preto (Black)
m_vertex[4].color = ~m_vertex[0].color;//0x0000ff ^ 0xffffff;
m_vertex[5].color = ~m_vertex[1].color;//0x00FF00 ^ 0xffffff;
m_vertex[6].color = ~m_vertex[2].color;//0xBB0000 ^ 0xffffff;
m_vertex[7].color = ~m_vertex[3].color;//0x000000 ^ 0xffffff; // Branco (White)

i = 0;
// First and Fifth Point
m_vertex[i+4].x  = m_vertex[i].x  = -width;
m_vertex[i+4].y  = m_vertex[i].y  =  height;
i++;
// Second and Sixth Point
m_vertex[i+4].x = m_vertex[i].x   =  width;
m_vertex[i+4].y = m_vertex[i].y   =  height;
i++;
// Third and Seventh Point
m_vertex[i+4].x = m_vertex[i].x   =  -width;
m_vertex[i+4].y = m_vertex[i].y   =  -height;
i++;
// Fourth e Eighth Point
m_vertex[i+4].x = m_vertex[i].x   =  width;
m_vertex[i+4].y = m_vertex[i].y   = -height;

// Normal, It is a Cube so, I only extend the line
for ( i = 0; i < 8; i++ )
{
m_vertex[i].nx = m_vertex[i].x*2;
m_vertex[i].ny = m_vertex[i].y*2;
m_vertex[i].nz = m_vertex[i].z*2;
}
return true;

Now, My IndexBuffer
	WORD index[] = {
0,1,2,1,3,2,
1,5,3,3,5,7,
0,5,1,0,4,5,
2,6,7,2,7,3,
2,0,4,2,4,6,
6,4,5,6,5,7};

[edited by - fabio on March 26, 2003 8:28:16 AM] [edited by - fabio on March 26, 2003 8:31:17 AM]

##### Share on other sites
sorry, I don''t speak English weel, but I''m learning :-)

Do you undestand my problem ?

##### Share on other sites
Well whats the problem? Does the cube not show up? does the application crash? does half the cube show up? Does the cube show up but it looks like a circle?

:::: [ Triple Buffer V2.0 ] ::::

##### Share on other sites
my guess would be you''re not drawing in a clockwise order...

##### Share on other sites
Half of you polygons are not "winding" in clockwise order.
0,1,2,1,3,2,
1,5,3,3,5,7,
0,5,1,0,4,5,
2,6,7,2,7,3,
2,0,4,2,4,6,
6,4,5,6,5,7

However, the last three rows are off, and your index buffer should read:
0,1,2,1,3,2,
1,5,3,3,5,7,
0,5,1,0,4,5,
2,7,6,2,3,7,
2,4,0,2,6,4,
6,5,4,6,7,5

Assuming that you are doing clockwise winding. Also, i might be wrong, but your normals may be off. The normals used for lighting in your cube use the corners of the cube. This may cause strange shading effects if you are using the gourad shading model(the shading will attempt to make the cube look more "rounded". Of course, if you are using a falt shading model then this has no consequences.
One question: Why are you changing the width, height, and depth to 1?
Hope this helps.

##### Share on other sites
I think you want too much at a start. First draw the cube with simple triangles without lighting and only when everything works, add the normals and see whats wrong step by step.

As for normals Id advise you to calculate them with separate function that takes 3 points (in correct order) and returns normalized vector.

Also be sure to set up ambient lighting that is bright enough so that you can see it. Do not ever make the ambient color black or something like 0a0a0a. Then you wont see anything(depending on your monitor brightness, 0a0a0a might not be seen at all).

Id recommend following render states for development to see if something is wrong or not.

// Turn off culling, so we see the front and back of the triangle
g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
// Turns on the Wireframe mode
g_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME);
// Turns on the Solid Fill mode
// g_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID);
// Turn off D3D lighting, since we are providing our own vertex colors
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );

Notice that I simply exchange commentary between solid mode and wireframe mode and recompile (you can naturally put thi fucntionality for some key pressing - I use it all the time in my development).

##### Share on other sites
quote:
However, the last three rows are off, and your index buffer should read:
0,1,2,1,3,2,
1,5,3,3,5,7,
0,5,1,0,4,5,
2,7,6,2,3,7,
2,4,0,2,6,4,
6,5,4,6,7,5

Why are you changing the width, height, and depth to 1?

Yes, you''re right.
Thanks, It''s works....
quote:

Why are you changing the width, height, and depth to 1?

Where am I do it ? ( Can you show me it ? )

##### Share on other sites
quote:
Original post by IFooBar
Well whats the problem? Does the cube not show up? does the application crash? does half the cube show up? Does the cube show up but it looks like a circle?

The cube faces are wrong...
It does not render rigth...
quote:

Does the cube show up but it looks like a circle?

HAHAHAHAHAHA, I like it...

[edited by - fabio on March 27, 2003 7:52:41 AM]

##### Share on other sites
You change the width, height and depth to one at this line of code:

width /= width;
height /= height;
depth /= depth;

Any number diveded by itself is 1.
Hope this helps.

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 13
• 10
• ### Forum Statistics

• Total Topics
632582
• Total Posts
3007198

×