Such question has been asked earlier but i am not able to get it to work. I have an array of structs and i am copying data from that array to other and passing it to a function. So the data in that array can be modified but when i run i get a seg fault and by gdb it is pointed to point where the function is called. Here is my code: The Struct
typedef struct tex{
vertex3f pos;
vertex2f texCoord;
vertex3f normal;
}vertex;
The making an array of structs
passVert = (vertex*)malloc(sizeof(verticesPos[4]));
memcpy(passVert,verticesPos,sizeof(verticesPos[4]));
calcNormals(indices,indexCount,&(passVert),vertexCount); //here the gdb shows the fault
The function:
void calcNormals(const unsigned int* pIndices,unsigned int indexCount,vertex **pVertices, unsigned int vertexCount){
for(unsigned int i = 0; i < indexCount; i += 3){
unsigned int index0 = pIndices[i];
unsigned int index1 = pIndices[i + 1];
unsigned int index2 = pIndices[i + 2];
vertex3f v1;
subVertices(&v1,pVertices[index1]->pos, pVertices[index0]->pos);
vertex3f v2;
subVertices(&v2,pVertices[index2]->pos, pVertices[index0]->pos);
vertex3f normal;
crossProduct(&normal,v1,v2);
normalize(&normal,normal);
addVertices(&(pVertices[index0]->normal),pVertices[index0]->normal,normal);
addVertices(&(pVertices[index1]->normal),pVertices[index0]->normal,normal);
addVertices(&(pVertices[index2]->normal),pVertices[index0]->normal,normal);
}
for(unsigned int i = 0; i < vertexCount; i++){
normalize(&(pVertices[i]->normal),pVertices[i]->normal);
}
}
PS: I am learning opengl concepts, so i used the guide by ogldev and it resembles it. I am doing it in plain C.
EDIT:
vertex* passVert;
vertex verticesPos[4];
makeCoords(&verticesPos[0],-1.0f, -1.0f, 0.5773f,0.0f,0.0f);
makeCoords(&verticesPos[1],0.0f, -1.0f, -1.15475f,0.5f,0.0f);
makeCoords(&verticesPos[2],1.0f, -1.0f, 0.5773f,1.0f,0.0f);
makeCoords(&verticesPos[3],0.0f,1.0f,0.0f,0.5f,1.0f);
the makeCoords function just initialize the verticesPos to the parameters passed, it only initializes pos and texCoord member of vertex struct
and:
typedef struct vertices3{
float x;
float y;
float z;
}vertex3f;
typedef struct vertex2f{
float x;
float y;
}vertex2f;
EDIT:
Now i check to print out all the four elements it was supposed to copy in passVert i did something like this instead of calling calcNormals
for(int i = 0; i < 4; ++i){
printf("%f %f %f %f %f\n\n",passVert[i].pos.x,passVert[i].pos.y,passVert[i].pos.z,passVert[i].texCoord.x,passVert[i].texCoord.y);
}
and it printed the data which was in verticesPos which it was supposed to copy. It printed out:
-1.000000 -1.000000 0.577300 0.000000 0.000000
0.000000 -1.000000 -1.154750 0.500000 0.000000
1.000000 -1.000000 0.577300 1.000000 0.000000
0.000000 1.000000 0.000000 0.500000 1.000000
which i wanted it to copy. The number of indices which is being passed is an array which is unsigned int indices[] = {0,3,1,1,3,2,2,3,0,0,1,2};
and count of 12 is being passed for indexCount and 4 is being passed for vertexCount in calcNormals function