# Simple algorithm using vertex buffer problem

This topic is 5163 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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[0] = verticesDX._x; verticesODE[1] = verticesDX._y; verticesODE[2] = verticesDX._z; verticesODE[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 on other sites
Sorry, typo :) "*(verticesODE++);" shouldn't be in there.

##### Share on other sites
Hm. At a first glance that looks good to me. Can you provide any information on where exactly the code crashes?

Cheers,
Alex

##### 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 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[0] = verticesDX._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._x; //crashes here
temp[1] = verticesDX._y;
temp[2] = verticesDX._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
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 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.x = verticesDX._x;  ...};dVector3* verticesODE = (dVector3*)vertices;

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

Cheers,
Alex

##### Share on other sites
Quote:
 Original post by EverIceHmm. 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 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._x = verticesDX._x;
...

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

Cheers again, Duncan

##### Share on other sites
dReal** verticesODE = new dReal*[numVertices];
for (int i = 0; i<numVertices; i++) {
verticesODE = 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 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

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 11
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632924
• Total Posts
3009232
• ### Who's Online (See full list)

There are no registered users currently online

×