• Create Account

## Problem dynamically creating a vertex Buffer.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

9 replies to this topic

### #1Enerjak  Members

277
Like
0Likes
Like

Posted 23 April 2011 - 04:07 PM

Ok, i'm not what to do on this one...basically i have this:


[size="2"][color="#0000ff"][size="2"][color="#0000ff"]namespace[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#010001"][size="2"][color="#010001"]IEngine

[/color][/size][/color][/size][size="2"]{

[/size][size="2"][color="#010001"][size="2"][color="#010001"]IPolygon[/color][/size][/color][/size][size="2"]::[/size][size="2"][color="#010001"][size="2"][color="#010001"]IPolygon[/color][/size][/color][/size][size="2"]()

{

[/size][size="2"][color="#010001"][size="2"][color="#010001"]primitiveCount[/color][/size][/color][/size][size="2"] = 0;

}

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

{

}

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

{

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]return[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]true[/color][/size][/color][/size][size="2"];

}

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

{

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

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

[/size][size="2"][color="#010001"][size="2"][color="#010001"]D3DPOOL_DEFAULT[/color][/size][/color][/size][size="2"],

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

[/size][size="2"][color="#010001"][size="2"][color="#010001"]NULL[/color][/size][/color][/size][size="2"]))

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

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

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

}

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

{

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

[/size][size="2"][color="#010001"][size="2"][color="#010001"]primitiveCount[/color][/size][/color][/size][size="2"]++;

}

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

{

}

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

{

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

}

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

{

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

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

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

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

}

}

[/size]


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?

### #2Starnick  Members

1806
Like
0Likes
Like

Posted 23 April 2011 - 04:45 PM

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

Starnick

### #3Enerjak  Members

277
Like
0Likes
Like

Posted 23 April 2011 - 05:35 PM

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

### #4MJP  Moderators

18235
Like
0Likes
Like

Posted 23 April 2011 - 06:41 PM

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.

### #5Enerjak  Members

277
Like
0Likes
Like

Posted 23 April 2011 - 07:04 PM

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

### #6Hodgman  Moderators

49429
Like
1Likes
Like

Posted 23 April 2011 - 11:11 PM

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, "&size" is the address of a local variable -- you're copying random local variable data from the stack into your vertex buffer!
Also, "sizeof(verts)" 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 "primitiveCount*sizeof(VertexPos)"
ID3D9Renderer::getDev9()->SetStreamSource(0,pVertexBuffer,0,sizeof(verts.size()));
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());
}

### #7Enerjak  Members

277
Like
0Likes
Like

Posted 24 April 2011 - 07:48 AM

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)

### #8Enerjak  Members

277
Like
0Likes
Like

Posted 24 April 2011 - 08:00 PM

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:

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

{

}

{
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;
}

{
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

### #9Hodgman  Moderators

49429
Like
0Likes
Like

Posted 25 April 2011 - 03:35 AM

What is the type of verts? You're putting VertexPosColors into it, but are computing it's size in bytes as if it contains VertexDatas.

Again, sizeof(verts.size()) will always return 4 (the size of the of an unsigned int -- the type that's returned by size()).

Also, DrawPrimitive 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 numVerts/3, for a tri-strip it's numVerts-2, etc...)

### #10Enerjak  Members

277
Like
0Likes
Like

Posted 25 April 2011 - 07:59 AM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.