Allocate indices and vertices size based on .obj

Hi Guys,

I’m having problem in defining a dynamic array. I hope I’m not in the wrong discussion as my question will be more to C programming.

I’ve successfully read and draw an .obj file while specifying the array size of vertices and indices manually. But this isn’t what I want exactly, since I’ll need to manually change the size every time I load different .obj file.

I’m trying to use malloc, but the value become all wrong, hence nothing is drawn… Here’s my code:


struct Size {
	int vSize;
	int iSize;
};
typedef struct Size sizeStruct;
sizeStruct *sPtr;


void Draw ( ESContext *esContext )
{	
	UserData *userData = esContext->userData;
	int i = 0;
	
	GLfloat matrixId[4][4] = {	{1.0f, 0.0f, 0.0f, 0.0f},
								{0.0f, 1.0f, 0.0f, 0.0f},
								{0.0f, 0.0f, 1.0f, 0.0f},
								{0.0f, 0.0f, 0.0f, 1.0f}
							 };
	
	GLfloat *vertices;
	GLuint *indices;

	GLfloat color[4] = { 0.0f, 1.0f, 0.0f, 1.0f };

	loadVertexIndex(i, &vertices, &indices); // copy vertices and indices value from linked list

	glVertexAttrib4fv(0, color);
	
	glUseProgram(userData->programObject);

	glVertexAttribPointer(userData->a_posLoc, 3, GL_FLOAT, GL_FALSE, 0, vertices);
	glEnableVertexAttribArray(userData->a_posLoc);

	esTranslate(&matrixId, 0.0, -0.9, -1);
	glUniformMatrix4fv(userData->u_mvpLoc, 1, GL_FALSE, &matrixId);

	glDrawElements(GL_TRIANGLES, sizeof(indices) / sizeof(GLuint), GL_UNSIGNED_INT, indices);		

	eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );

	if (isWritten == 0)
		checkIt(&indices);

	free(vertices);
	free(indices);
}

Draw function call this function to copy values:


void loadVertexIndex(int i, GLfloat **vertices, GLuint **indices)
{
	vCurr = vHead;
	
	iCurr = iHead;
	// sPtr->vSize is 1083
	// sPtr->iSize is 2154
	// TO DO: use the size gotten from sPtr->vSize
	*vertices = (GLfloat *)malloc(sPtr->vSize*sizeof(GLfloat));
	*indices = (GLuint *)malloc((sPtr->iSize* sizeof(GLuint));
	
	for (i = sPtr->vSize - 1; i >= 0; --i)
	{
		(*vertices)[i] = vCurr->v;
		vCurr = vCurr->next;
	}
	
	// TO DO: use the size gotten from sPtr->iSize
	for (i = sPtr->iSize - 1; i >= 0; --i)
	{
		(*indices)[i] = iCurr->i;
		iCurr = iCurr->next;
	}
}

The value of sPtr->vSize and sPtr->iSize is increase during the reading of the .obj file.
And at the end of the reading, I got the correct value of the vertex and index of the object. (total vertex: 1083 and total index: 2154)

After I output the sizeof(vertices) and sizeof(indices) , I notice that the size is 4, instead of 1083(vertices) and 2154(indices).

I’ve tried using memcpy as well, but I guess, because of the wrong allocation memory, I keep on getting a crash.

Does anyone know why and how to malloc the value correctly? Is there an easier way to get the vertex and index dynamically in C?
So far, I’m using linked list to store the vertex and index.

Thank you…

The problem is solved.

I pass in wrong size in glDrawElements.

Instead of this:

glDrawElements(GL_TRIANGLES, sPtr->iSize , GL_UNSIGNED_INT, userData->m_indices);

I wrote this:

glDrawElements(GL_TRIANGLES, sizeof(userData->indices) / sizeof(GLuint) , GL_UNSIGNED_INT, userData->m_indices);

And I’ve understood wrongly on the size, the correct value for sizeof(vertices) and sizeof(indices) is 4.

Thank you.

Well done! I was going to recommend the same :slight_smile:

Keep having fun.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.