Sign in to follow this  

Dx9 dynamic array + rendering polygons = fail

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

Ok I have a peculiar problem which is probably tied to my limited knowledge of dynamic memory. I wanted to change my application from using a pre-set vertice count to a dynamic one. When I render a model (in this case a simple cube) its more memory efficient. The original code works just fine and displays a square, while the other displays nothing. I need to know what I am doing wrong or assuming incorrectly, again please forgive any silliness in my [lack of] understanding of this process. This is not the entire app, just the code that was changed. There's got to be something simple that I am missing. This is just fundamental dx/c++ stuff. Again thank you for anybody that can clear up this mystery. [ORIGINALLY]
#define FVF_MODEL	(D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_DIFFUSE)

struct STRUCT_MODEL
{
	float    x;	
	float    y;
	float    z;
	float   nx;
	float   ny;
	float   nz;
	unsigned long int	c;
};

extern STRUCT_MODEL Model_Vertice[6];

void Core_System_Initialize_Vertices()
{
	//front
	Calculate_Set_Vertice(  0,-0.1, 0.1, 0.1);
	Calculate_Set_Vertice(  1, 0.1, 0.1, 0.1);
	Calculate_Set_Vertice(  2,-0.1,-0.1, 0.1);
	Calculate_Set_Vertice(  3,-0.1,-0.1, 0.1);
	Calculate_Set_Vertice(  4, 0.1, 0.1, 0.1);
	Calculate_Set_Vertice(  5, 0.1,-0.1, 0.1);
	Model_Vertice[0].c	= Calculate_Color(255,255,0,0);
	Model_Vertice[1].c	= Calculate_Color(255,255,0,0);
	Model_Vertice[2].c	= Calculate_Color(255,255,0,0);
	Model_Vertice[3].c	= Calculate_Color(255,128,0,0);
	Model_Vertice[4].c	= Calculate_Color(255,128,0,0);
	Model_Vertice[5].c	= Calculate_Color(255,128,0,0);
}

void Core_System_Create_Buffers()
{
	
	d3d_device->SetFVF(FVF_MODEL);
	d3d_device->CreateVertexBuffer(	6*sizeof(STRUCT_MODEL),
					0,
					FVF_MODEL,
					D3DPOOL_MANAGED,
					d3d_buffer_vertex_model,
					0);
}

void Graphics_Render_Update_Model_Data()
{
	void* p_void;//stores first type pointed to
	d3d_buffer_vertex_model->Lock(0,0,(void**)&p_void,0);
	memcpy(p_void,Model_Vertice, sizeof(Model_Vertice));
	d3d_buffer_vertex_model->Unlock();
	delete p_void;
	p_void = 0;
}


[EDITED]
#define FVF_MODEL	(D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_DIFFUSE)

struct STRUCT_MODEL
{
	float    x;	
	float    y;
	float    z;
	float   nx;
	float   ny;
	float   nz;
	unsigned long int	c;
};

extern STRUCT_MODEL *Model_Vertice;

void Core_System_Initialize_Vertices()
{
	Model_Vertice = new STRUCT_MODEL[6];
	//front
	Calculate_Set_Vertice(  0,-0.1, 0.1, 0.1);
	Calculate_Set_Vertice(  1, 0.1, 0.1, 0.1);
	Calculate_Set_Vertice(  2,-0.1,-0.1, 0.1);
	Calculate_Set_Vertice(  3,-0.1,-0.1, 0.1);
	Calculate_Set_Vertice(  4, 0.1, 0.1, 0.1);
	Calculate_Set_Vertice(  5, 0.1,-0.1, 0.1);
	Model_Vertice[0].c	= Calculate_Color(255,255,0,0);
	Model_Vertice[1].c	= Calculate_Color(255,255,0,0);
	Model_Vertice[2].c	= Calculate_Color(255,255,0,0);
	Model_Vertice[3].c	= Calculate_Color(255,128,0,0);
	Model_Vertice[4].c	= Calculate_Color(255,128,0,0);
	Model_Vertice[5].c	= Calculate_Color(255,128,0,0);
}

void Core_System_Create_Buffers()
{
	
	d3d_device->SetFVF(FVF_MODEL);
	d3d_device->CreateVertexBuffer(	6*sizeof(STRUCT_MODEL),
					0,
					FVF_MODEL,
					D3DPOOL_MANAGED,
					&d3d_buffer_vertex_model,
					0);
}

void Graphics_Render_Update_Model_Data()
{
	void* p_void;//stores first type pointed to
	d3d_buffer_vertex_model->Lock(0,0,(void**)&p_void,0);
	memcpy(p_void,&Model_Vertice, sizeof(*Model_Vertice));
	d3d_buffer_vertex_model->Unlock();
	delete p_void;
	p_void = 0;
}


Share this post


Link to post
Share on other sites
memcpy(p_void,&*Model_Vertice, sizeof(*Model_Vertice));

I'm pretty sure you shouldn't be deleting the pointer returned by Lock and your assignment p_void = 0; at the end of the function gains you nothing since the variable is about to go out of scope and actually could mask errors. Also, consider using std::vector instead of manually allocating memory using new[].

Σnigma

Share this post


Link to post
Share on other sites
This generates an error:
error C2664: 'memcpy' : cannot convert parameter 2 from 'STRUCT_MODEL' to 'const void *'

memcpy(p_void, *Model_Vertice, sizeof(*Model_Vertice));

This still displays nothing, however it compiles:
memcpy(p_void, &*Model_Vertice, sizeof(*Model_Vertice));

So I still remain confused. I'll explore vectors if all else fails. Partly because I'm stubborn, partly because I want to know how/why it does/doesn't work. I like using as little "stuff" done by others as possible. (Vectors seem to be a standard part of c++ now, still....)

Share this post


Link to post
Share on other sites
And that worked perfectly, you deserve a nice big cookeh. So when you pass a pointer to an "array" you are only dealing with the pointer, not all the elements that it is pointing to (in this case 6)? Or am I misunderstanding. I just wish to make sure I walk away understanding why that worked >_<.

Share this post


Link to post
Share on other sites

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

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