Archived

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

memcopying from a pointer.

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

I'm having trouble copying from a pointer to an array of vertices. Heres some code:
	CUSTOMVERTEX *Vertices;
	Vertices = new CUSTOMVERTEX[1890];


	// inserts vertex data here...



	d3dDevice->CreateVertexBuffer((sizeof(CUSTOMVERTEX) * 1890), 0,
							D3DFVF_VERTEX, D3DPOOL_DEFAULT, &m_VB, NULL);
	VOID* pVertices;
	m_VB->Lock(0, sizeof(Vertices), (void**)&pVertices, 0);
	memcpy( pVertices, Vertices, sizeof(Vertices) );
	m_VB->Unlock();	
I know the memcpy( pVertices, Vertices, sizeof(Vertices) ); is incorrect, but I don't know what I need to replace "Vertices" with. Tried stuff like "*Vertices" and "&*Vertices" but no luck so far. Any ideas? [edited by - sirob on August 8, 2003 5:28:05 AM]

Share this post


Link to post
Share on other sites
Unfortunatly, it only solved part of my problem. I have 2 VB''s, and only one is drawing.

Any ideas on what might cause the polygons not to show up from one VB and still show up on another? I don''t change any renderstates or anything, and the framerate does fall when I draw the second VB, but nothing shows up.

As far as I can tell, the data is correct. Any ideas?

Share this post


Link to post
Share on other sites
There''s so many reasons for such odd behavior... :/
Do you have two identical VBs, or what?
Could be the vertex format change, or something...

I''ll post if something obvious comes to mind

Nik

Share this post


Link to post
Share on other sites
I get a weird access violation if I allocate too much memory while dealing with a VB. Never could figure it out .

Anywho, I've got 2 VB's, and as far as I can tell, theres no reason why the second shows nothing, but who knows these days .

heres some code:

Render():

bool cObject::Render(LPDIRECT3DDEVICE9 d3dDevice)
{
D3DXMATRIXA16 matTmp;
D3DXMatrixRotationY(&matTmp, 0.002f);
D3DXMatrixMultiply(&m_MatWorld, &matTmp, &m_MatWorld);
d3dDevice->SetStreamSource( 0, m_VB, 0, sizeof(CUSTOMVERTEX));
d3dDevice->SetFVF( D3DFVF_VERTEX );
d3dDevice->SetTransform( D3DTS_WORLD , &m_MatWorld);
d3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 630);
d3dDevice->SetStreamSource( 0, m_VB2, 0, sizeof(CUSTOMVERTEX));
d3dDevice->SetFVF( D3DFVF_VERTEX );
d3dDevice->SetTransform( D3DTS_WORLD , &m_MatWorld);
d3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 256 * 256 * 2);
return true;
}



Load(): ( a little long, sorry.)

bool cObject::Load(LPDIRECT3DDEVICE9 d3dDevice)
{
CUSTOMVERTEX *Vertices;
Vertices = new CUSTOMVERTEX[1890];
FILE *m_FilePointer = fopen("cannon1.OBJ", "r");

short CurrVertex = 0, CurrVertexN = 0;
short CurrIndex = 0;
char strLine[255] = {0};
char ch = 0;

while(!feof(m_FilePointer))
{

float x = 0.0f, y = 0.0f, z = 0.0f;
ch = fgetc(m_FilePointer);
switch(ch)
{
case 'v':
ch = fgetc(m_FilePointer);
if(ch == ' ')
{
fscanf(m_FilePointer, "%f %f %f", &x, &y, &z);
fgets(strLine, 100, m_FilePointer);

Vertices[CurrVertex].x = x;
Vertices[CurrVertex].y = y;
Vertices[CurrVertex].z = z;
Vertices[CurrVertex].color = D3DCOLOR_XRGB((int)(x * 256), (int)(y * 256), (int)(z * 256));
CurrVertex++;
}

if(ch == 'n')
{
fscanf(m_FilePointer, " %f %f %f", &x, &y, &z);
fgets(strLine, 100, m_FilePointer);

Vertices[CurrVertexN].nx = x;
Vertices[CurrVertexN].ny = y;
Vertices[CurrVertexN].nz = z;
CurrVertexN++;
}
break;
default:
fgets(strLine, 100, m_FilePointer);
break;
}

}

m_FilePointer = NULL;
CurrVertex--;
CurrIndex--;

m_NumIndices = CurrIndex;
d3dDevice->CreateVertexBuffer((sizeof(CUSTOMVERTEX) * 1890), 0,
D3DFVF_VERTEX, D3DPOOL_DEFAULT, &m_VB, NULL);
VOID* pVertices;
m_VB->Lock(0, sizeof(Vertices), (void**)&pVertices, 0);
memcpy( pVertices, Vertices, sizeof(CUSTOMVERTEX)*1890 );
m_VB->Unlock();

m_FilePointer = fopen("Terrain.raw", "rb");
BYTE *MapRaw;
MapRaw = new BYTE[MAP_SIZE * MAP_SIZE];
fread(MapRaw, 1, MAP_SIZE * MAP_SIZE, m_FilePointer);


CUSTOMVERTEX *Vertices2;
Vertices2 = new CUSTOMVERTEX[256 * 256 * 6];

int tmp = 0;

for(short x=0; x<256; x++)
{
for(short y=0; y<256; y++)
{

Vertices2[tmp].x = (float)(x / 1000) - 0.128f;
Vertices2[tmp].y = (float)(y / 1000) - 0.128f;
Vertices2[tmp].z = (float)(MapRaw[x + (y * 1024)] / 1000.0f);
Vertices2[tmp].nx = 0.0f;
Vertices2[tmp].ny = 0.0f;
Vertices2[tmp].nz = 1.0f;
Vertices2[tmp].color = D3DCOLOR_XRGB(256 - x, 256 - y , MapRaw[tmp]);
tmp++;
Vertices2[tmp].x = (float)(x / 1000) - 0.128f;
Vertices2[tmp].y = (float)(y / 1000) - 0.128f;
Vertices2[tmp].z = (float)(MapRaw[x + 1 + (y * 1024)] / 1000.0f);
Vertices2[tmp].nx = 0.0f;
Vertices2[tmp].ny = 0.0f;
Vertices2[tmp].nz = 1.0f;
Vertices2[tmp].color = D3DCOLOR_XRGB(256 - x, 256 - y , MapRaw[tmp]);
tmp++;
Vertices2[tmp].x = (float)(x / 1000) - 0.128f;
Vertices2[tmp].y = (float)(y / 1000) - 0.128f;
Vertices2[tmp].z = (float)(MapRaw[x + ((y + 1) * 1024)] / 1000.0f);
Vertices2[tmp].nx = 0.0f;
Vertices2[tmp].ny = 0.0f;
Vertices2[tmp].nz = 1.0f;
Vertices2[tmp].color = D3DCOLOR_XRGB(256 - x, 256 - y , MapRaw[tmp]);
tmp++;




Vertices2[tmp].x = (float)(x / 1000) - 0.128f;
Vertices2[tmp].y = (float)(y / 1000) - 0.128f;
Vertices2[tmp].z = (float)(MapRaw[x + ((y + 1) * 1024)] / 1000.0f);
Vertices2[tmp].nx = 0.0f;
Vertices2[tmp].ny = 0.0f;
Vertices2[tmp].nz = 1.0f;
Vertices2[tmp].color = D3DCOLOR_XRGB(256 - x, 256 - y , MapRaw[tmp]);
tmp++;
Vertices2[tmp].x = (float)(x / 1000) - 0.128f;
Vertices2[tmp].y = (float)(y / 1000) - 0.128f;
Vertices2[tmp].z = (float)(MapRaw[x + 1 + (y * 1024)] / 1000.0f);
Vertices2[tmp].nx = 0.0f;
Vertices2[tmp].ny = 0.0f;
Vertices2[tmp].nz = 1.0f;
Vertices2[tmp].color = D3DCOLOR_XRGB(256 - x, 256 - y , MapRaw[tmp]);
tmp++;
Vertices2[tmp].x = (float)(x / 1000) - 0.128f;
Vertices2[tmp].y = (float)(y / 1000) - 0.128f;
Vertices2[tmp].z = (float)(MapRaw[x + 1 + ((y + 1) * 1024)] / 1000.0f);
Vertices2[tmp].nx = 0.0f;
Vertices2[tmp].ny = 0.0f;
Vertices2[tmp].nz = 1.0f;
Vertices2[tmp].color = D3DCOLOR_XRGB(256 - x, 256 - y , MapRaw[tmp]);
tmp++;

}
}
d3dDevice->CreateVertexBuffer(sizeof(CUSTOMVERTEX) * 256 * 256 * 6, 0,
D3DFVF_VERTEX, D3DPOOL_DEFAULT, &m_VB2, NULL);

m_VB2->Lock(0, sizeof(CUSTOMVERTEX) * 256 * 256 * 6, (void**)&pVertices, 0);
memcpy( pVertices, Vertices2, sizeof(CUSTOMVERTEX) * 256 * 256 * 6);
m_VB2->Unlock();


delete[] MapRaw;
delete[] Vertices2;
delete[] Vertices;
return true;
}


[EDIT] As far as I can tell, the load function is putting the correct values into the Verices2 array, but for some reason when rendering, nothing shows.

Like I said before, the framerate DOES drop when I render the second VB.

Sirob Yes.»
Footman, at large.

[edited by - sirob on August 8, 2003 12:57:38 PM]

[edited by - sirob on August 8, 2003 12:58:11 PM]

Share this post


Link to post
Share on other sites
I suggest putting a load of "if(FAILED(...)) ReportError();" in your code to narrow down which lines causing the problem, then once you have the offending line you can put a "HRESULT err = bla()" and look up in the documentation what the possible values for "err" could be, e.g. in my program I have:


HRESULT err = m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &m_Params, &m_pDevice);

switch(err){
case D3D_OK:
break;
case D3DERR_INVALIDCALL:
reportError("D3DERR_INVALIDCALL");
break;
case D3DERR_NOTAVAILABLE:
reportError("D3DERR_NOTAVAILABLE");
break;
case D3DERR_OUTOFVIDEOMEMORY:
reportError("D3DERR_OUTOFVIDEOMEMORY");
break;
default:
reportError("Unknown Error");
}


You could implement something similar:
if (FAILED(d3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 630))
ReportError();

Then something simliar to above to narrow the exact problem.






Dave.

Share this post


Link to post
Share on other sites
arg, happened yet again:

Vertices2[tmp].x = (float)(x / 1000) - 0.128f;


Hate these EXTREMELY stupid typos.

should be:
 
Vertices2[tmp].x = ((float)x / 1000.0f) - 0.128f;



Thanks for the help, anyhow.

Sirob Yes.»
Footman, at large.

[edited by - sirob on August 22, 2003 7:32:29 AM]

Share this post


Link to post
Share on other sites