Jump to content
  • Advertisement
Sign in to follow this  
AfroMogli

OpenGL Problems with drawing a Milk3D-model

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! For a while now I've been trying to implement a modell-class based on a finished model-loader that I've found on Milk3D:s official forum. Now when I'm trying to draw the model the program crashes and the debugger yells about "<bad ptr>"-errors. The bad pointers occur while I'm trying to draw the tringles in my own model-class. Now I've been trying to solve this problem for two weeks now but I really can't see it. Maybe it's the model-loading class that doesn't work... you tell me. Anyway, heres some code from the model-loader.

typedef unsigned char  MS3DByte;
typedef unsigned short MS3DWord;

...

typedef struct MS3DTriangle
{
    MS3DWord    flags;
    MS3DWord    vertexIndices[3];
    float       vertexNormals[3][3];
    float       s[3];
    float       t[3];
    MS3DByte    smoothingGroup;
    MS3DByte    groupIndex;
} PACK_STRUCT;

...

bool MS3D::LoadModel(const char* model)
{
    ...
    
    // Count the number of vertices
    int verticesCount = 0;
    memcpy(&verticesCount, bufferPtr, sizeof(MS3DWord));
    bufferPtr += sizeof(MS3DWord);
    
    // Read the vertices
    MS3DVertex* vertices = new MS3DVertex[verticesCount];
    memset(vertices, 0, (sizeof(MS3DVertex) * verticesCount));
    MS3DVertex* verticesPtr = vertices;
    for (int i = 0; i < verticesCount; i++) {
        const MS3DVertex* vertex = reinterpret_cast<const MS3DVertex*>(bufferPtr);
        memcpy(verticesPtr, bufferPtr, sizeof(MS3DVertex));
        bufferPtr += sizeof(MS3DVertex);
        verticesPtr++;
    }
    
    // Count the triangles
    int triangleCount = 0;
    memcpy(&triangleCount, bufferPtr, sizeof(MS3DWord));
    bufferPtr += sizeof(MS3DWord);
       
    // Read the triangles
    MS3DTriangle* triangles = new MS3DTriangle[triangleCount];
    memset(triangles, 0, (sizeof(MS3DTriangle) * triangleCount));
    MS3DTriangle* trianglesPtr = triangles;
    for (int i = 0; i < triangleCount; i++) {
        const MS3DTriangle* triangle = reinterpret_cast<const MS3DTriangle*>(bufferPtr);
        memcpy(trianglesPtr, bufferPtr, sizeof(MS3DTriangle));
        for (int j = 0; j < (sizeof(trianglesPtr->t) / sizeof(float)); j++) {
            trianglesPtr->t[j] = (1.0f - trianglesPtr->t[j]);
        }
        bufferPtr += sizeof(MS3DTriangle);
        trianglesPtr++;
    }
    
    // Count the amount of groups (Meshes)
    int groupCount = 0;
    memcpy(&groupCount, bufferPtr, sizeof(MS3DWord));
    bufferPtr += sizeof(MS3DWord);

    // Read the groups
    MS3DGroup* groups = new MS3DGroup[groupCount];
    memset(groups, 0, (sizeof(MS3DGroup) * groupCount));
    MS3DGroup* groupsPtr = groups;
    for (int i = 0; i < groupCount; i++) {
    	memcpy(&groupsPtr->flags, bufferPtr, sizeof(MS3DByte));
    	bufferPtr += sizeof(MS3DByte);
        memcpy(&groupsPtr->name, bufferPtr, (sizeof(char) * 32));
        bufferPtr += (sizeof(char) * 32);
        memcpy(&groupsPtr->numtriangles, bufferPtr, sizeof(MS3DWord));
        bufferPtr += sizeof(MS3DWord);
        groupsPtr->triangleIndices = new MS3DWord[groupsPtr->numtriangles];
        memset(groupsPtr->triangleIndices, 0, (sizeof(MS3DWord) * groupsPtr->numtriangles));
        for (int j = 0; j < groupsPtr->numtriangles; j++) {
            memcpy(&groupsPtr->triangleIndices, bufferPtr, sizeof(MS3DWord));
            bufferPtr += sizeof(MS3DWord);  // WICHTIG !!!
        }
        memcpy(&groupsPtr->materialIndex, bufferPtr, sizeof(char));
        bufferPtr += sizeof(char);
        groupsPtr++;
    }
    
    ...
    
    m_triangles     = triangles;

    ...
}

And here's the code from my drawing function.
void CModel::Draw()
{
	for ( int i = 0; i < mModel->m_groupCount; i++ )
	{
		int materialIndex = mModel->m_groups.materialIndex;
		if ( materialIndex >= 0 )
		{
			glMaterialfv( GL_FRONT, GL_AMBIENT, mModel->m_materials[materialIndex].ambient );
			glMaterialfv( GL_FRONT, GL_DIFFUSE, mModel->m_materials[materialIndex].diffuse );
			glMaterialfv( GL_FRONT, GL_SPECULAR, mModel->m_materials[materialIndex].specular );
			glMaterialfv( GL_FRONT, GL_EMISSION, mModel->m_materials[materialIndex].emissive );
			glMaterialf( GL_FRONT, GL_SHININESS, mModel->m_materials[materialIndex].shininess );

			if ( m_materials[materialIndex].texture > 0 )
			{
				glBindTexture( GL_TEXTURE_2D, (int)mModel->m_materials[materialIndex].texture );
				glEnable( GL_TEXTURE_2D );
			}
			else
				glDisable( GL_TEXTURE_2D );
		}
		else
		{
			// Material properties?
			glDisable( GL_TEXTURE_2D );
		}

		glBegin( GL_TRIANGLES );
		{
			// Draw the triangles
			for ( int j = 0; j < mModel->m_groups.numtriangles; j++ )
			{
				int triangleIndex = mModel->m_groups.triangleIndices[j];
				const MS3DTriangle* pTri = &mModel->m_triangles[triangleIndex];

				for ( int k = 0; k < 3; k++ )
				{
					int index = pTri->vertexIndices[k];
					
					glNormal3fv( mModel->m_triangles[triangleIndex].vertexNormals[k] );
					glTexCoord2f( pTri->s[k], pTri->t[k] );
					glVertex3fv( mModel->m_vertices[index].vertex );
				}
			}
		}
		glEnd();
	}

	glEnable(GL_TEXTURE_2D);

}

The debugger points out the data that I'm sending in with the OpenGL-functions in the Triangle-part of the function...

Share this post


Link to post
Share on other sites
Advertisement
Are you forcing 1 byte alignment with your Milkshape3D structs?

Before you declare your structs, #include <pshpack1.h>
After declaring your structs, #include <poppack.h>

Hope this helps.

Share this post


Link to post
Share on other sites
Thanks, but that didn't help. The thing is that the struct code that I listed wasn't complete. In the full header I already have all those headers included. And I definitely believe that the structs are not the problem! There is a bad pointer somewhere but I can't see it! :(

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!