Problem dynamically creating a vertex Buffer.

Started by
8 comments, last by Enerjak 12 years, 12 months ago
Ok, i'm not what to do on this one...basically i have this:



[color="#0000ff"][color="#0000ff"]namespace [color="#010001"][color="#010001"]IEngine

{

[color="#010001"][color="#010001"]IPolygon::[color="#010001"][color="#010001"]IPolygon()

{

[color="#010001"][color="#010001"]primitiveCount = 0;

}

[color="#010001"][color="#010001"]IPolygon::~[color="#010001"][color="#010001"]IPolygon()

{

}

[color="#0000ff"][color="#0000ff"]bool [color="#010001"][color="#010001"]IPolygon::[color="#010001"][color="#010001"]createRenderable([color="#010001"][color="#010001"]std::[color="#010001"][color="#010001"]string [color="#010001"][color="#010001"]name)

{



[color="#0000ff"][color="#0000ff"]return [color="#0000ff"][color="#0000ff"]true;

}

[color="#0000ff"][color="#0000ff"]void [color="#010001"][color="#010001"]IPolygon::[color="#010001"][color="#010001"]beginRenderable()

{

[color="#010001"][color="#010001"]DWORD [color="#010001"][color="#010001"]size = ([color="#010001"][color="#010001"]DWORD)[color="#010001"][color="#010001"]verts.[color="#010001"][color="#010001"]size();

[color="#010001"][color="#010001"]HR([color="#010001"][color="#010001"]ID3D9Renderer::[color="#010001"][color="#010001"]getDev9()->[color="#010001"][color="#010001"]CreateVertexBuffer([color="#010001"][color="#010001"]primitiveCount*[color="#0000ff"][color="#0000ff"]sizeof([color="#010001"][color="#010001"]VertexPos),0,[color="#010001"][color="#010001"]D3DFVF_XYZ,

[color="#010001"][color="#010001"]D3DPOOL_DEFAULT,

&[color="#0000ff"][color="#0000ff"]this->[color="#010001"][color="#010001"]pVertexBuffer,

[color="#010001"][color="#010001"]NULL))





[color="#0000ff"][color="#0000ff"]void* [color="#010001"][color="#010001"]pDat;

[color="#010001"][color="#010001"]HR([color="#010001"][color="#010001"]pVertexBuffer->[color="#010001"][color="#010001"]Lock(0,0,([color="#010001"][color="#010001"]LPVOID*)&[color="#010001"][color="#010001"]pDat,0));



[color="#010001"][color="#010001"]memcpy([color="#010001"][color="#010001"]pDat,&[color="#010001"][color="#010001"]size,[color="#0000ff"][color="#0000ff"]sizeof([color="#010001"][color="#010001"]verts));





}

[color="#0000ff"][color="#0000ff"]void [color="#010001"][color="#010001"]IPolygon::[color="#010001"][color="#010001"]setPosition([color="#0000ff"][color="#0000ff"]float [color="#010001"][color="#010001"]X, [color="#0000ff"][color="#0000ff"]float [color="#010001"][color="#010001"]Y, [color="#0000ff"][color="#0000ff"]float [color="#010001"][color="#010001"]Z)

{

[color="#010001"][color="#010001"]verts.[color="#010001"][color="#010001"]push_back([color="#0000ff"][color="#0000ff"]new [color="#010001"][color="#010001"]VertexPos([color="#010001"][color="#010001"]X,[color="#010001"][color="#010001"]Y,[color="#010001"][color="#010001"]Z));

[color="#010001"][color="#010001"]primitiveCount++;

}

[color="#0000ff"][color="#0000ff"]void [color="#010001"][color="#010001"]IPolygon::[color="#010001"][color="#010001"]setColour([color="#010001"][color="#010001"]DWORD [color="#010001"][color="#010001"]color)

{

}

[color="#0000ff"][color="#0000ff"]void [color="#010001"][color="#010001"]IPolygon::[color="#010001"][color="#010001"]endRenderable()

{

[color="#010001"][color="#010001"]HR([color="#010001"][color="#010001"]pVertexBuffer->[color="#010001"][color="#010001"]Unlock());

}

[color="#0000ff"][color="#0000ff"]void [color="#010001"][color="#010001"]IPolygon::[color="#010001"][color="#010001"]drawRenderable()

{

[color="#010001"][color="#010001"]ID3D9Renderer::[color="#010001"][color="#010001"]getDev9()->[color="#010001"][color="#010001"]SetRenderState([color="#010001"][color="#010001"]D3DRS_CULLMODE,[color="#010001"][color="#010001"]D3DCULL_CCW);

[color="#010001"][color="#010001"]ID3D9Renderer::[color="#010001"][color="#010001"]getDev9()->[color="#010001"][color="#010001"]SetStreamSource(0,[color="#010001"][color="#010001"]pVertexBuffer,0,[color="#0000ff"][color="#0000ff"]sizeof([color="#010001"][color="#010001"]verts.[color="#010001"][color="#010001"]size()));

[color="#010001"][color="#010001"]ID3D9Renderer::[color="#010001"][color="#010001"]getDev9()->[color="#010001"][color="#010001"]SetFVF([color="#010001"][color="#010001"]D3DFVF_XYZ);



[color="#010001"][color="#010001"]ID3D9Renderer::[color="#010001"][color="#010001"]getDev9()->[color="#010001"][color="#010001"]DrawPrimitive([color="#010001"][color="#010001"]D3DPT_TRIANGLELIST,0,[color="#010001"][color="#010001"]primitiveCount);



}



}




basically i wanted people to be able to specify the position of each vertex and let D3D figure out the points then render the primitive count accordingly, but as you can see, that's not happening...So could someone tell me what to do to get it to work? (i'm making a D3D9/10/11 engine (also gonna throw in OpenGL), anyways, any thoughts?
Advertisement
Whoa nelly, your code snippet's formatting needs some fixing ;)

Whoa nelly, your code snippet's formatting needs some fixing ;)


tell me about it......that's how it came out when i used the insert code snippet
You can't use rich text/HTML inside a code block. You can try copying your code into notepad first to strip it of all formatting, and then pasting it into here.

You can't use rich text/HTML inside a code block. You can try copying your code into notepad first to strip it of all formatting, and then pasting it into here.


namespace IEngine
{
IPolygon::IPolygon()
{
primitiveCount = 0;
}

IPolygon::~IPolygon()
{

}

bool IPolygon::createRenderable(std::string name)
{

return true;
}

void IPolygon::beginRenderable()
{
DWORD size = (DWORD)verts.size();
HR(ID3D9Renderer::getDev9()->CreateVertexBuffer(primitiveCount*sizeof(VertexPos),0,D3DFVF_XYZ,
D3DPOOL_DEFAULT,
&this->pVertexBuffer,
NULL))



void* pDat;
HR(pVertexBuffer->Lock(0,0,(LPVOID*)&pDat,0));

memcpy(pDat,&size,sizeof(verts));


}

void IPolygon::setPosition(float X, float Y, float Z)
{
verts.push_back(new VertexPos(X,Y,Z));
primitiveCount++;
}

void IPolygon::setColour(DWORD color)
{

}

void IPolygon::endRenderable()
{
HR(pVertexBuffer->Unlock());
}

void IPolygon::drawRenderable(PRIMITIVE_TYPE primType)
{
D3DPRIMITIVETYPE d3dpType;

switch(primType)
{
case PT_LINE_LIST:
{
d3dpType = D3DPT_LINELIST;
}
break;
case PT_LINE_STRIP:
{
d3dpType = D3DPT_LINESTRIP;
}
break;
case PT_TRIANGLE_LIST:
{
d3dpType = D3DPT_TRIANGLELIST;
}
break;
case PT_TRIANGLE_STRIP:
{
d3dpType = D3DPT_TRIANGLESTRIP;
}
break;
case PT_POINT_LIST:
{
d3dpType = D3DPT_POINTLIST;
}
break;
case PRIMITIVE_TYPE::PT_TRIANGLE_FAN:
{
d3dpType = D3DPT_TRIANGLEFAN;
}
break;
}
ID3D9Renderer::getDev9()->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);

ID3D9Renderer::getDev9()->SetStreamSource(0,pVertexBuffer,0,sizeof(verts.size()));
ID3D9Renderer::getDev9()->SetFVF(D3DFVF_XYZ);

ID3D9Renderer::getDev9()->DrawPrimitive(d3dpType,0,primitiveCount);

}


}


hopefully this is better



This is an array of pointers. You can't upload an array of pointers to DirectX -- it wants an array of values. Also, by using new here, you're leaking memory.std::vector<VertexPos*> verts;
verts.push_back(new VertexPos(X,Y,Z));
You want:std::vector<VertexPos> verts;
verts.push_back(VertexPos(X,Y,Z));


Here, "[font="Lucida Console"]&size[/font]" is the address of a local variable -- you're copying random local variable data from the stack into your vertex buffer!
Also, "[font="Lucida Console"]sizeof(verts)[/font]" tells you the size of a std::vector, which is always going to return ~12 IIRC, no matter how many items are in the vector!
What is this memcpy supposed to be doing -- you've only just created the buffer, and don't have any data to copy into it yet?DWORD size = (DWORD)verts.size();
memcpy(pDat,&size,sizeof(verts));


Again, here "sizeof(verts.size())" is telling you the size of an unsigned integer variable, which will always return 4, no matter how many items are in the vector... You did the correct calculation up in CreateVertexBuffer with "[font="Lucida Console"]primitiveCount*sizeof(VertexPos)[/font]"
ID3D9Renderer::getDev9()->SetStreamSource(0,pVertexBuffer,0,sizeof(verts.size()));
[hr]The logic of your class doesn't make much sense to me either.
You initialize primitiveCount to 0, then you use that number (0) to create the vertex buffer (with 0 size), then you copy some random data into the vertex buffer. Then with each call to setPosition, you copy data into a vector (which is never copied into the vertex buffer).

I would guess that the logic should look like this - the size of the vertex buffer isn't known until after all the setPosition calls, which means you've got to create the vertex buffer in the call to endRenderable. void IPolygon::beginRenderable()
{
assert( pVertexBuffer == NULL );
}

void IPolygon::setPosition(float X, float Y, float Z)
{
verts.push_back(VertexPos(X,Y,Z)); // don't need primitiveCount - verts.size() is the same thing
}


void IPolygon::endRenderable()
{
assert( !verts.empty() );
DWORD sizeInBytes = verts.size() *sizeof(VertexPos);//how many items * size of each item
HR(ID3D9Renderer::getDev9()->CreateVertexBuffer(sizeInBytes,0,D3DFVF_XYZ,
D3DPOOL_DEFAULT,
&pVertexBuffer,
NULL));

void* pDat;
HR(pVertexBuffer->Lock(0,0,&pDat,0));
memcpy(pDat,&verts[0],sizeInBytes);//copy from the vector into the buffer
HR(pVertexBuffer->Unlock());
}
Ok, thanks, i got it to work thanks to you, now to do the same with generating indices and texture coordnates based on how many triangles in the vector (also make it shader based rendering so that way D3D9/10/11 are in sync)
Ok, i'm having a little problem.... when i try to add just one triangle it adds like 10 or what ever, I'm trying to use the class to create a screen quad
and i get this output:

Untitled-2.png?t=1303696597




ScreenQuad::ScreenQuad()
{
screen_Object = new DynamicObject9();
}

ScreenQuad::~ScreenQuad()
{

}

bool ScreenQuad::createScreenQuad()
{
screen_Object->beginRenderable();
screen_Object->setPosition(-1,-1,0);
screen_Object->setPosition(1,0,0);
screen_Object->setPosition(0,1,0);
screen_Object->endRenderable();

screen_Object->beginRenderable();
screen_Object->setPosition(0,1,1);
screen_Object->setPosition(-1,0,0);
screen_Object->setPosition(-1,-1,0);
screen_Object->endRenderable();
return true;
}

void ScreenQuad::drawQuad()
{
screen_Object->drawRenderable(PT_TRIANGLE_LIST);
}


and here is the new version of the dynamic shape


DynamicObject9::DynamicObject9()
{

}

DynamicObject9::~DynamicObject9()
{

}

bool DynamicObject9::createRenderable(std::string name)
{
return true;
}

void DynamicObject9::beginRenderable()
{
//assert(pVertexBuffer == NULL);
}

void DynamicObject9::setPosition(float X, float Y, float Z)
{
VertexPosColor pos;
pos = VertexPosColor(X,Y,Z,0xFFFFFFFF);
verts.push_back(pos);
}

void DynamicObject9::endRenderable()
{
assert( !verts.empty());

DWORD sizeInBytes = verts.size() * sizeof(VertexData);

ID3D9Renderer::getDev9()->CreateVertexBuffer(sizeInBytes,0,D3DFVF_XYZ,D3DPOOL_DEFAULT,&pVertexBuffer,NULL);

void* pData;
pVertexBuffer->Lock(0,0,(void**)&pData,0);
memcpy(pData,&verts[0],sizeInBytes);
pVertexBuffer->Unlock();
}

void DynamicObject9::drawRenderable(PRIMITIVE_TYPE primType )
{
D3DPRIMITIVETYPE primType3D;

switch(primType)
{
case PT_LINE_LIST:
{
primType3D = D3DPT_LINELIST;
}
break;
case PT_LINE_STRIP:
{
primType3D = D3DPT_LINESTRIP;
}
break;
case PT_TRIANGLE_FAN:
{
primType3D = D3DPT_TRIANGLEFAN;
}
break;
case PT_TRIANGLE_LIST:
{
primType3D = D3DPT_TRIANGLELIST;
}
break;
case PT_TRIANGLE_STRIP:
{
primType3D = D3DPT_TRIANGLESTRIP;
}
break;
case PT_POINT_LIST:
{
primType3D = D3DPT_POINTLIST;
}
break;
}
ID3D9Renderer::getDev9()->SetFVF(D3DFVF_XYZ);
ID3D9Renderer::getDev9()->SetStreamSource(0,pVertexBuffer,0,sizeof(verts.size()));

ID3D9Renderer::getDev9()->DrawPrimitive(primType3D,0,verts.size());

}



not sure why it's drawing so many polys when all i want is 2 for the screen quad

What is the type of [font="CourierNew, monospace"]verts[/font]? You're putting [color="#1C2837"][font="CourierNew, monospace"][color="#660066"]VertexPosColor[/font]s into it, but are computing it's size in bytes as if it contains [color="#660066"][font="CourierNew, monospace"]VertexData[/font]s.

Again, [color="#1C2837"][font="CourierNew, monospace"][color="#000088"]sizeof[color="#666600"]([color="#000000"]verts[color="#666600"].[color="#000000"]size[color="#666600"]())[/font] will always return 4 (the size of the of an [font="Courier New"]unsigned int[/font] -- the type that's returned by [font="Courier New"]size()[/font]).

Also, [color="#660066"][font="CourierNew, monospace"]DrawPrimitive[/font] wants you to pass in the number of primitives, but you're passing in the number of vertices. Inside the switch statement, you need to add logic for each primitive type that calculates the number of primitives based on the number of vertices (e.g. for a tri-list, it's [font="Courier New"]numVerts/3[/font], for a tri-strip it's [font="Courier New"]numVerts-2[/font], etc...)
this is still not getting me anywhere, I'll just have to create a new vertex buffer for the screen quad and just fill it that way with the 6 vertices to make the quad..."sigh* i'll have to study this problem more carefully to better understand it, as it stands it only draws one triangle half way the screen thanks for your help

This topic is closed to new replies.

Advertisement