casting problem (int * to int*)

G

ghostdog

hi,

i got this opengl/c++ code:

<code>
void render(CMesh *mesh){
...
float *pVertices;
int *pIndices;

//WORKS FINE
pVertices = (GLfloat *)mesh->getVertices(); // (A)
//PROBLEMS HERE!!!!
pIndices = (int *)mesh->getFaceIndices(); // (B)

...
}

class:

int *CMesh::getFaceIndices() const{
return faceIndices;
}

float *CMesh::getVertices() const{
return vertices;
}

Class CMesh
{
...
float *vertices = new float[numVertices * 3];;
int *faceIndices = new int[numFaces * 3];
...
}
</code>

the two arrays get filled at runtime.
for the the opengl rendering part, i need to have a pointer to a
float-array containing the vertices and a pointer to an int-array
conatining the indices of the triangles.

to get them from my mesh-object i cast them in the render-function.
the floatconversion (A) works. "pVertices" points to the same data as
the member variable "vertices" of the mesh-object. but when i try to
cast the pointer of my integerarray (B), a dataloss occurs. when i
loop through pIndices i get lesser data. only every 3rd integer of the
original array. the last 3 values are correct.
this must be a casting problem. why does it work with floats, but not
with int?
i tried (GLint *) and (int *) casting (they seem to be the same size
anyway, according to sizeof())... no success. any ideas?

TIA
ghostdog
 
I

Ivan Vecerina

....
| //WORKS FINE
| pVertices = (GLfloat *)mesh->getVertices(); // (A)
| //PROBLEMS HERE!!!!
| pIndices = (int *)mesh->getFaceIndices(); // (B)
....
| float *vertices = new float[numVertices * 3];;
| int *faceIndices = new int[numFaces * 3];
....
| the two arrays get filled at runtime.
| for the the opengl rendering part, i need to have a pointer to a
| float-array containing the vertices and a pointer to an int-array
| conatining the indices of the triangles.
....
| to get them from my mesh-object i cast them in the render-function.
| the floatconversion (A) works. "pVertices" points to the same data as
| the member variable "vertices" of the mesh-object. but when i try to
| cast the pointer of my integerarray (B), a dataloss occurs. when i
| loop through pIndices i get lesser data. only every 3rd integer of the
| original array. the last 3 values are correct.
| this must be a casting problem. why does it work with floats, but not
| with int?
| i tried (GLint *) and (int *) casting (they seem to be the same size
| anyway, according to sizeof())... no success. any ideas?

I do not think that this is a casting problem. In any case, the code
that you have posted does not contain a problem by itself.

However, there are many caveats when dealing with this kind of
indexed vertex buffers: for example an index value will
(typically) have to be multiplied by 3 to get the first coordinate
of a vertex within your float array.
... but this is getting OT here.

My suggestion would be to post a more complete code example,
and to post it on a technology-specific forum (e.g. in comp.graphics.* ?,
or on a forum supported by an OpenGL vendor... ).


Regards,
 
P

Percy

On 15 Sep 2003 07:30:11 -0700 in comp.lang.c++ :

If your functions return "float*"and "int*" respectively,
and you assigning those values to "float*" and "int*"
types,

I'm curious; Why cast ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top