Custom vertices arrays and DirectX APIs

Started by
2 comments, last by jwein 14 years, 10 months ago
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.
Advertisement
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.
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.
Ok thanks for all your information.
I am probably wrong in another part of my code.
Your answers have been very useful.

This topic is closed to new replies.

Advertisement