Sign in to follow this  
jwein

Custom vertices arrays and DirectX APIs

Recommended Posts

Hello. I am programming the rendering part of my little engine and I am stuck in a little problem. I hope you could help me. I have written a VertexBuffer class which contains a dynamic array of Vertices (of type Vertex). It's something like "Vertex* vertices". The vertices are allocated by the constructor of the VertexBuffer class. Vertex is structured as follows:
struct Vertex
{
	Vector3D	position;
	Vector3D	normal;
	Vector3D	tangent;
	Vector3D	bitangent;
	Color		color;
};
Vector3D and Color are classes, they contains methods, operators and members (position and color) as an array of floats. I have noticed that DirectX10 requires a pointer to the starting address where I have saved my vertices, I am referring to:
	D3D10_SUBRESOURCE_DATA initData;
	initData.pSysMem = address_of_vertices;
I have tried to pass DirectX the address of the array "Vertex* vertices" which is contained in the VertexBuffer class but I have noticed that, obviously, it doesn't draw anything because it's an array of structures ( Vertex ) which contain classes and not structures. They doesn't contain just positions and colors. How could I solve this situation? I have seen that in the official DirectX10 tutorials they represent the Vertex as a structure which contains some D3DXVECTOR3 instances for position and color and they passes the address of this array of Vertices to DirectX. How do they do, considering the fact that D3DXVECTOR3 is a class and not a structure? The area of memory of the vertices should contain members and functions of D3DXVECTOR3 and not just vertices, so I don't know how it could work. I am probably wrong in something. Have I to define the overloading of some operators? Thanks in advance.

Share this post


Link to post
Share on other sites
If your classes do not have virtual functions, it is just the same as a struct.
In cpp on win32 platform, the codes and data are in different address spaces.So it may be caused by another part of your codes.

Share this post


Link to post
Share on other sites
1. A class and a struct are exactly the same when it comes to memory layout.
2. Member functions aren't instanced per-class, they live in another segment of memory.
3. If your class/struct just contains member variables and (non-virtual) member functions, then you can treat it as just one chunk of memory as if it was just a struct with no member functions.

Share this post


Link to post
Share on other sites
Ok thanks for all your information.
I am probably wrong in another part of my code.
Your answers have been very useful.

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