6
\$\begingroup\$

In my game I have model loading functions for Assimp model loading library. I can load the model and render it, but the model displays incorrectly. The models load in as if they were using a seperate projection matrix. I have looked over my code over and over again, but I probably keep on missing the obvious reason why this is happening.

Here is an image of my game: Misshaped cube

It's simply a 6 sided cube, but it's off big time!

Here are my code snippets for rendering the cube to the screen:

void C_MediaLoader::display(void)
{
    float tmp;

    glTranslatef(0,0,0);
    // rotate it around the y axis
    glRotatef(angle,0.f,0.f,1.f);
    glColor4f(1,1,1,1);

    // scale the whole asset to fit into our view frustum 
    tmp = scene_max.x-scene_min.x;
    tmp = aisgl_max(scene_max.y - scene_min.y,tmp);
    tmp = aisgl_max(scene_max.z - scene_min.z,tmp);
    tmp = (1.f / tmp);
    glScalef(tmp/5, tmp/5, tmp/5);

    // center the model
    //glTranslatef( -scene_center.x, -scene_center.y, -scene_center.z );
    // if the display list has not been made yet, create a new one and
    // fill it with scene contents
    if(scene_list == 0) {
        scene_list = glGenLists(1);
        glNewList(scene_list, GL_COMPILE);
            // now begin at the root node of the imported data and traverse
            // the scenegraph by multiplying subsequent local transforms
            // together on GL's matrix stack.
        recursive_render(scene, scene->mRootNode);
        glEndList();
    }
    glCallList(scene_list);
}



void C_MediaLoader::recursive_render (const struct aiScene *sc, const struct aiNode* nd)
{
    unsigned int i;
    unsigned int n = 0, t;
    struct aiMatrix4x4 m = nd->mTransformation;

    // update transform
    aiTransposeMatrix4(&m);
    glPushMatrix();
    glMultMatrixf((float*)&m);

    // draw all meshes assigned to this node
    for (; n < nd->mNumMeshes; ++n) {
        const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];

        apply_material(sc->mMaterials[mesh->mMaterialIndex]);

        if(mesh->mNormals == NULL) {
            glDisable(GL_LIGHTING);
        } else {
            glEnable(GL_LIGHTING);
        }

        for (t = 0; t < mesh->mNumFaces; ++t) {
            const struct aiFace* face = &mesh->mFaces[t];
            GLenum face_mode;

            switch(face->mNumIndices) {
                case 1: face_mode = GL_POINTS; break;
                case 2: face_mode = GL_LINES; break;
                case 3: face_mode = GL_TRIANGLES; break;
                default: face_mode = GL_POLYGON; break;
            }

            glBegin(face_mode);

            for(i = 0; i < face->mNumIndices; i++) {
                int index = face->mIndices[i];

                if(mesh->mColors[0] != NULL)
                    glColor4fv((GLfloat*)&mesh->mColors[0][index]);

                if(mesh->mNormals != NULL) 
                    glNormal3fv(&mesh->mNormals[index].x);

                glVertex3fv(&mesh->mVertices[index].x);
            }

            glEnd();
        }
    }
    // draw all children
    for (n = 0; n < nd->mNumChildren; ++n) {
        recursive_render(sc, nd->mChildren[n]);
    }
    glPopMatrix();
}

Sorry there is so much code to look through, but I really cannot find the problem, and I would love to have help.

\$\endgroup\$
17
  • \$\begingroup\$ Debug it, make sure it's using the correct face_mode and check to make sure all the indices are getting added. \$\endgroup\$
    – House
    Commented Mar 28, 2012 at 4:39
  • \$\begingroup\$ I've already done that, in ANY mode it has the exact same effect as seen in the image. \$\endgroup\$ Commented Mar 28, 2012 at 4:41
  • \$\begingroup\$ Really. If face_mode is set to GL_POINTS, it shows a deformed cube the the one you have in your image? \$\endgroup\$
    – House
    Commented Mar 28, 2012 at 4:43
  • \$\begingroup\$ not exactly, but it does the same thing, where it is smaller and get's larger the farther it is in the z axis \$\endgroup\$ Commented Mar 28, 2012 at 4:44
  • 2
    \$\begingroup\$ It looks like you just have some really weird perspective going on. How are you setting up your view and projection matrices? \$\endgroup\$ Commented Mar 28, 2012 at 8:26

1 Answer 1

6
\$\begingroup\$

Turns out this was an issue with my perspective, I was able to solve my problem by setting the gluPerspective correctly:

void C_MediaLoader::display(void)
{
    glLoadIdentity();
    gluPerspective(45, 640/480,
        1.0, 1000.0);  /* Znear and Zfar */

    // remainder code is the same as before...
}

Thanks for everyone's help!

\$\endgroup\$
3
  • \$\begingroup\$ You should post a little more detail about how you solved the problem instead of what you have here. \$\endgroup\$
    – House
    Commented Mar 28, 2012 at 18:36
  • \$\begingroup\$ OK, I just added in the extra details \$\endgroup\$ Commented Mar 28, 2012 at 18:43
  • 2
    \$\begingroup\$ I edited your post to better reflect the answer. Remember to accept your answer when you're able. \$\endgroup\$
    – House
    Commented Mar 28, 2012 at 20:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .