Results 1 to 3 of 3

Thread: Allocate indices and vertices size based on .obj

  1. #1
    Junior Member
    Join Date
    Nov 2011

    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:

    Code :
    struct Size {
    	int vSize;
    	int iSize;
    typedef struct Size sizeStruct;
    sizeStruct *sPtr;

    Code :
    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);
    	glVertexAttribPointer(userData->a_posLoc, 3, GL_FLOAT, GL_FALSE, 0, vertices);
    	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)

    Draw function call this function to copy values:
    Code :
    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..

  2. #2
    Junior Member
    Join Date
    Nov 2011

    Re: [SOLVED]Allocate indices and vertices size based on .obj

    The problem is solved.

    I pass in wrong size in glDrawElements.

    Instead of this:
    Code :
    glDrawElements(GL_TRIANGLES, sPtr->iSize , GL_UNSIGNED_INT, userData->m_indices);

    I wrote this:
    Code :
    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.

  3. #3
    Senior Member
    Join Date
    May 2010
    Toronto, Canada

    Re: Allocate indices and vertices size based on .obj

    Well done! I was going to recommend the same

    Keep having fun.
    Disclaimer: Employee of Qualcomm Canada. Any opinions expressed here are personal and do not necessarily reflect the views of my employer. LinkedIn profile.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Proudly hosted by Digital Ocean