#### Archived

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

# A Simple Cube

This topic is 5442 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.

##### Share on other sites
quote:
Original post by Anonymous Poster
width /= width;
height /= height;
depth /= depth;

HAHAHAHAHAHAHA
YOU are right!!!!
I didn''t see it... ( I think I am blind )
should be
width /= 2.0f;
height /= 2.0f;
depth /= 2.0f;

Thanks...
;-)

##### Share on other sites
Only one more question...

I have 2 squares...
Front Square and Back square, ok ?

How is the order of the vertices ?
Back Square            Front Square0----1                 4----5|    |                 |    |2----3                 6----7

is it ?

I don''t know what is the right order...

##### Share on other sites
Your question depends on which side of the square you are facing(assuming you are using clockwise winding). When you create a polygon, by specifying the winding of the polygon, you are telling Opengl(or directx or whatever) that a polygon has 2 sides; a front and a back face(this allows you to see the inside of a box if you leave 1 face missing). When you use a specific culling mode(you could use no cull mode and then not worry about any of this) you are telling the graphics API not to draw a backface. So how does this apply to your question? Lets look at just 1 of your squares:
0---1
| |
| |
2---3

If we say that this is the front of the square, then we would use these indices to create that effect: 013, 032 or whatever order you prefer, as long as they are being wound clockwise. However, to make this the backface, you would use the winding: 102, 231. Now you would not see the polygon, as you are removing backfaces by using backface culling. I hope this is enough to help you figure out your question. If you are confused, feel free to inquire some more!

##### Share on other sites
quote:

If we say that this is the front of the square, then we would use these indices to create that effect: 013, 032 or whatever order you prefer, as long as they are being wound clockwise. However, to make this the backface, you would use the winding: 102, 231. Now you would not see the polygon, as you are removing backfaces by using backface culling. I hope this is enough to help you figure out your question. If you are confused, feel free to inquire some more!

Thanks, you help me a lot...
I finally understand the index buffer.

##### Share on other sites
and now...
I have an doubt about Normal.

What does the Normals do ?
Is it only for lighting calculations ?

I thought that The normals are used to specify the front face of the polygon. Am I wrong ?

##### Share on other sites
Yes, The normal is for lighting only. The ''winding'' of the vertices is what determines the front or back of a face/triangle.