Sign in to follow this  

Simple algorithm using vertex buffer problem

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

Hi all, This is driving me up the wall. I'm learning proper C++ for the first time while doing this project, and I'm sure the problem has got more to do with me not using pointers properly than anything else, but I just can't see what's going wrong. I'm trying to take all vertices from the mesh and store them in an Open Dynamics Engine compatible format (typedef dReal dVector3[4], I think this means each dVector3 is an array of 4 dReals, which are effectively floats in my build of ODE) so I can use the engine to do lots of fun physics things for me. Here's the algorithm: try { int numVertices = in->GetNumVertices(); dVector3* verticesODE = (dVector3*)malloc(numVertices*sizeof(dVector3)); Vertex* verticesDX = 0; in->LockVertexBuffer(0,(void**)&verticesDX); for (int i = 0; i<numVertices; i++) { verticesODE[i][0] = verticesDX[i]._x; verticesODE[i][1] = verticesDX[i]._y; verticesODE[i][2] = verticesDX[i]._z; verticesODE[i][3] = 0.0f; *(verticesODE++); } in->UnlockVertexBuffer(); } catch (int e) { char str[20]; sprintf(str,"Exception: %i",e); ::MessageBox(0, str, 0, 0); return false; } No exception is caught, it seems to iterate about 2100 times (out of a needed 3162 vertices) then crash. I'm totally lost on this, and for such a simple algorithm, it's really been frustrating me for the past 2 days! Any ideas? Cheers for any help, Duncan

Share this post


Link to post
Share on other sites
Hmm. I think this is the wrong line: *(verticesODE++); Your buffer pointer value is incrementing every cycle. If i'm not wrong.

If you use c++, then forget malloc.

Share this post


Link to post
Share on other sites
Hey, cheers for the reply. Yeah through careful placement of message box alerts I think it crashes on the first assignment to verticesODE after around 2163 iterations. I.e.

...
for (int i = 0; i<numVertices; i++) {
verticesODE[i][0] = verticesDX[i]._x; //here
...

I dunno if this'll help but initially I started off with this algorithm:

int numVertices = in->GetNumVertices();
dVector3 *verticesODE;
verticesODE = (dVector3*) malloc(sizeof(dVector3)*numVertices);
Vertex* verticesDX = 0;
in->LockVertexBuffer(0,(void**)&verticesDX);
for (int i = 0; i<numVertices; i++) {
dVector3 temp;
temp[0] = verticesDX[i]._x; //crashes here
temp[1] = verticesDX[i]._y;
temp[2] = verticesDX[i]._z;
temp[3] = 0.0f;
verticesODE = &temp;
verticesODE++;
}
in->UnlockVertexBuffer();

Then someone said this to me:

"The bug is due to your inappropriate handling of pointers in
these particular lines.

dVector3 *verticesODE;
verticesODE = (dVector3*) malloc(sizeof(dVector3)*numVertices);

dVector3 temp;
verticesODE = &temp;
verticesODE++;

This has nothing to do with ODE [OpenDynamicsEngine]. If
you had stepped through the code in your debugger, looking at
all the values, you would easily have caught this."


So I tried to adjust my code as much as I could. My MSVS debugger doesn't work because of windows crashing, and I can't reinstall it cos I'm not at uni at the moment (it's their CDs I need) - so I'm still at a loss!


Let me just get a few basics straight if I can, typedef dReal dVector3[4] means each dVector3 is an array of 4 dReals values? If this is the case, am I using malloc correctly to get and use memory? As a test, how could I allocate memory for a 2d array like dReal verticesODE[numVertices][4]? For some reason, anytime I try to use C++ dynamic allocation (dReal verticesODE[numVertices][4] = new dReal[numVertices][4]) I get these errors which I just don't understand:

error C2057: expected constant expression,
error C2466: cannot allocate an array of constant size 0,
error C2440: 'initializing' : cannot convert from 'dReal (*)[4]' to 'dReal [][4]'.

Ergh, sorry for the dense post but I thought I'd get as much info in there as possible!

Cheers, Duncan

Share this post


Link to post
Share on other sites
and if you simple try to do something like that:


struct MyVertex
{
float x,y,z,w;
};

MyVertex* vertices = new MyVertex[num_vertices];
..
for(...)
{
vertices[i].x = verticesDX[i]._x;
...
};

dVector3* verticesODE = (dVector3*)vertices;



Just an idea. I myself don't really like to work with arrays of arrays...

Cheers,
Alex

Share this post


Link to post
Share on other sites
Quote:
Original post by EverIce
Hmm. I think this is the wrong line: *(verticesODE++); Your buffer pointer value is incrementing every cycle. If i'm not wrong.

If you use c++, then forget malloc.


Hi, cheers for the reply, yeah the *(verticesODE++); was a typo, it's not in the actual code that's still causing the trouble, if it was in there it'd certainly be part of the problem though.

I've tried using the C++ allocation operators, but I just get compile time errors all the time (see my previous post) and I don't know why. Perhaps you could give a couple of examples for me to look at?

Cheers, Duncan

Share this post


Link to post
Share on other sites
I tried using another simple struct, and exactly the same problem.

struct VertexODE {
VertexODE(float x, float y, float z, float a) {
_x = x; _y = y; _z = z; _a = a;
}
float _x, _y, _z, _a;
} ;
...
VertexODE* verticesODE = (VertexODE*)malloc(numVertices*(sizeof(VertexODE)));
...for (etc)
verticesODE[i]._x = verticesDX[i]._x;
...

Could it be something with the vertex buffer? Am I breaking some rule about accessing the vertex buffer?

Cheers again, Duncan

Share this post


Link to post
Share on other sites
dReal** verticesODE = new dReal*[numVertices];
for (int i = 0; i<numVertices; i++) {
verticesODE[i] = new dReal[4];
}

Works, so i'm not using malloc anymore. So I'm thinking it's got something to do with the vertex buffer... but what could it be?

Share this post


Link to post
Share on other sites
I found out the problem. I had to clone the mesh into a new mesh that used an FVF with only xyz components, create a struct with only those components and use that struct in the algorithm. The crash was caused when using a larger FVF, the loop stepped beyond the bounds of the vertex buffer.

Cheers for your help guys, Duncan

Share this post


Link to post
Share on other sites

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