Jump to content
  • Advertisement
Sign in to follow this  
steg

Vertices and cube

This topic is 5408 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

Hi, I'm just implementing my Cube class, as you can guess this has 6 faces, thus 12 primitives (tri's). I want to use an index buffer with this. I believe then that the vertex buffer will contain 6 x 4 vertices, is this correct ? The 4 being, 4 vertices per face if using an index buffer. The index buffer will contain 36 indices ( 6 per face ). Can anyone tell me if I'm going the right way about this ? Kind regards, Steve

Share this post


Link to post
Share on other sites
Advertisement
It depends on the type of shading you want. For a cube with flat shaded faces you'll need 4 verts per face (this is probably what you want). However if you want smoothed vert normals giving your objects a rounded look you'd only need 8 verts for the whole thing. The number of index buffer entries on the other hand is always 'numTris'*3. To see it in action use the D3DXCreateBox function and examine the mesh it outputs.

Share this post


Link to post
Share on other sites
Thanks Motorherp,

So four verts per face is correct for flat shade, I don't understand how you would only need 8 verts for the whole cube if using smooth vert normals ?

Index buffer I have 12 * 3 * 2 - the times two part as it is expecting this as a WORD which is 2 bytes.

I will try the D3DXCreateBox function.

My class is looking like this so far, :


typedef float VERTICES;

typedef struct STANDARDVERTEX {

VERTICES m_x, m_y, m_z;

}_STANDARDVERTEX, *_pSTANDARDVERTEX;


class CShape
{
protected:

VERTICES m_fWorldXPos, m_fWorldYPos, m_fWorldZPos;

LPDIRECT3DVERTEXBUFFER9 m_pVB;
LPDIRECT3DINDEXBUFFER9 m_pIB;

virtual void Initialise() = 0;
virtual void Render() = 0;

virtual void SetWorldPosition( VERTICES fXPos, VERTICES fYPos, VERTICES fZPos )
{
m_fWorldXPos = fXPos;
m_fWorldYPos = fYPos;
m_fWorldZPos = fZPos;
}
};


class CCube : public CShape
{
private:
void Initialise();
void Render();
void SetWorldPosition( VERTICES fXPos, VERTICES fYPos, VERTICES fZPos );

_pSTANDARDVERTEX m_pVertices; // 6 faces = 12 triangles
};


The Initialise method initialises the vertex, index buffers. The Render obviously renders the object. Is this a good way of setting objects up ?

Best regards,
Steve

Share this post


Link to post
Share on other sites
The reason only 8 verts are needed for smooth vert normals is because each corner of the cube takes its normal by averaging between the three adjacent face normals. Hence the vert normals (and obviously pos) at that corner is the same no matter which tri it belongs to and so only needs one instance. Really what this is trying to do is simulate a sphere - just very badly. I only recomend smoothing when the underlying mesh is already a good approximation to a curved surface.

Quote:
Is this a good way of setting objects up ?

Theoretically your method of setting objects up seems sound and easy to use. There are some things you may want to consider though. By using virtual functions you are adding an extra 4 bytes to each class instance and also masking a switch statement with each call to a virtual function. This could cause you a significant performance hit since you are making low level classes and algorithms that'll get used a lot. Instead you may want to use flags to specify object type so you dont get the extra memory requirement and only get the switch hit once when you first determine what type you are dealing with. To determine type you can use something like this:

inline eShapeType GetType() const
{
return eShapeType::Cube;
}

where eShapeType is an enum with all your shapes in. This way you don't get the extra memory requirment.

Another little trick but a good one is instead of this:

void SetWorldPosition( VERTICES fXPos, VERTICES fYPos, VERTICES fZPos )
{
m_fWorldXPos = fXPos;
etc.
}

do this:

inline void SetWorldPosition(STANDARDVERTEX& vertex)
{
m_fWorldXPos = vertex.m_x;
etc.
}

This way you're only passing a reference instead of copying 3 floats (just dont forget the '&'). Also for small functions use inline. It might seem trivial but at these low levels this kind of stuff really counts. Hope this helps and good luck

[Edited by - Motorherp on September 29, 2004 9:16:37 AM]

Share this post


Link to post
Share on other sites
Thanks Motorherp,

I guess I used pure virtual functions because this is the way I've been taught and this is what I use in application development at work or have to, but I guess games coding needs to be more optimized.

My main class for Direct3D initialisation is a singleton class, I thought this a good pattern as I will only be using one instance of the class.

Once again, many thanks!

Steve

Share this post


Link to post
Share on other sites
No problem. Virtuals are fantastic for non performance critical stuff, they make the code much neater and easier to use. But your right, for games they're not the best, at least not at low level. With some clever managment though you should be able to get away without virtuals (and flags and switches for the most part as well). Good choice with the singletons, fantastic things. Have fun

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!