Jump to content
  • Advertisement
Sign in to follow this  
shomaster

segmentation fault - program based on NeHe lesson 10

This topic is 4449 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

Im writing a program based on NeHe's lesson 10. I changed the file loader to load XML files such as the following using tinyXML:
<?xml version="1.0" encoding="utf-8"?>

<world triangles="12" texturenum="3">

<textures>
<texture img="data/shomaster2.bmp" />
<texture img="data/mud.bmp" />
<texture img="data/grass.bmp" />
</textures>

<triangle texture="2">
<vertex p1="10.0" p2="-0.1" p3="10.0" t1="20.0" t2="20.0" />
<vertex p1="-10.0" p2="-0.1" p3="10.0" t1="0.0" t2="20.0" />
<vertex p1="-10.0" p2="-0.1" p3="-10.0" t1="0.0" t2="0.0" />
</triangle>

<triangle texture="2">
<vertex p1="-10.0" p2="-0.1" p3="-10.0" t1="0.0" t2="0.0" />
<vertex p1="10.0" p2="-0.1" p3="-10.0" t1="20.0" t2="0.0" />
<vertex p1="10.0" p2="-0.1" p3="10.0" t1="20.0" t2="20.0" />
</triangle>

<triangle texture="2">
<vertex p1="-10.0" p2="-0.1" p3="-10.0" t1="0.0" t2="1.0" />
<vertex p1="-10.0" p2="10.1" p3="-10.0" t1="0.0" t2="0.0" />
<vertex p1="10.0" p2="-0.1" p3="-10.0" t1="1.0" t2="1.0" />
</triangle>

<triangle texture="2">
<vertex p1="-10.0" p2="10.1" p3="-10.0" t1="0.0" t2="0.0" />
<vertex p1="10.0" p2="10.1" p3="-10.0" t1="1.0" t2="0.0" />
<vertex p1="10.0" p2="-0.1" p3="-10.0" t1="1.0" t2="1.0" />
</triangle>

<triangle texture="2">
<vertex p1="-10.0" p2="-0.1" p3="10.0" t1="0.0" t2="1.0" />
<vertex p1="-10.0" p2="-0.1" p3="-10.0" t1="1.0" t2="1.0" />
<vertex p1="-10.0" p2="10.1" p3="10.0" t1="0.0" t2="0.0" />
</triangle>

<triangle texture="2">
<vertex p1="-10.0" p2="10.1" p3="-10.0" t1="1.0" t2="0.0" />
<vertex p1="-10.0" p2="-0.1" p3="-10.0" t1="1.0" t2="1.0" />
<vertex p1="-10.0" p2="10.1" p3="10.0" t1="0.0" t2="0.0" />
</triangle>

<triangle texture="2">
<vertex p1="-10.0" p2="-0.1" p3="10.0" t1="1.0" t2="1.0" />
<vertex p1="-10.0" p2="10.1" p3="10.0" t1="1.0" t2="0.0" />
<vertex p1="10.0" p2="-0.1" p3="10.0" t1="0.0" t2="1.0" />
</triangle>

<triangle texture="2">
<vertex p1="10.0" p2="10.1" p3="10.0" t1="0.0" t2="0.0" />
<vertex p1="-10.0" p2="10.1" p3="10.0" t1="1.0" t2="0.0" />
<vertex p1="10.0" p2="-0.1" p3="10.0" t1="0.0" t2="1.0" />
</triangle>

<triangle texture="2">
<vertex p1="10.0" p2="-0.1" p3="-10.0" t1="0.0" t2="1.0" />
<vertex p1="10.0" p2="10.1" p3="-10.0" t1="0.0" t2="0.0" />
<vertex p1="10.0" p2="10.1" p3="10.0" t1="1.0" t2="0.0" />
</triangle>

<triangle texture="2">
<vertex p1="10.0" p2="10.1" p3="10.0" t1="1.0" t2="0.0" />
<vertex p1="10.0" p2="-0.1" p3="-10.0" t1="0.0" t2="1.0" />
<vertex p1="10.0" p2="-0.1" p3="10.0" t1="1.0" t2="1.0" />
</triangle>

<triangle texture="2">
<vertex p1="10.0" p2="10.1" p3="10.0" t1="1.0" t2="1.0" />
<vertex p1="-10.0" p2="10.1" p3="10.0" t1="0.0" t2="1.0" />
<vertex p1="-10.0" p2="10.1" p3="-10.0" t1="0.0" t2="0.0" />
</triangle>

<triangle texture="2">
<vertex p1="-10.0" p2="10.1" p3="-10.0" t1="0.0" t2="0.0" />
<vertex p1="10.0" p2="10.1" p3="-10.0" t1="1.0" t2="0.0" />
<vertex p1="10.0" p2="10.1" p3="10.0" t1="1.0" t2="1.0" />
</triangle>
</world>

After the file is read it renders it to the screen.
the texturenum is the amount of texture files used. Each file creates 3 textures, one GL_NEAREST, one GL_LINEAR, and one mipmap texture. Each triangle has a texture number standing for the image it will have on it. starting from 0 to the number of textures - 1 coresponding with the textures order of listing in the textures section. if i have more the 7 textures as defined by <world triangles="12" texturenum="3"> the program creates a segmentation fault.
I also believe it creates a segmentation fault when texture 0 or 1 is used. These are the typedefs I used.
typedef struct
{
    float x, y, z; /* 3D Coordinates */
    float u, v;    /* Texture Coordinates */
    int tex;
}
vertext;

/* Build Our Triangle Structure */
typedef struct
{
    vertext vertex[3]; /* Array Of Three Vertices */
}
trianglet;

typedef struct
{
    const char *texture;
}
texturet;

/* Build Our Sector Structure */
typedef struct
{
    int numTriangles;   /* Number Of Triangles In Sector */
    int numTextures;
    texturet *texture;
    trianglet *triangle; /* Pointer To Array Of Triangles */
}
sectort;


This is the texture loader and the second function is used for reading and interpreting the xml file.

int LoadGLTextures( )
{
    /* Status indicator */
    int Status = FALSE;
    int imgLoop = 0;
    int texLoop = 0;

    /* Create storage space for the texture */
    SDL_Surface *TextureImage[sector1.numTextures];

    texture = (GLuint*)malloc((3 * sector1.numTextures));
    glGenTextures((3 * sector1.numTextures) , &texture[0]);

    for(imgLoop = 0; imgLoop < sector1.numTextures; imgLoop++)
    {
        if(TextureImage[imgLoop] = SDL_LoadBMP(sector1.texture[imgLoop].texture))
fprintf(stderr, "%s", sector1.texture[imgLoop].texture);

    //for(texLoop = 0; texLoop

        /* Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit */


            /* Set the status to true */
            Status = TRUE;


            /* Load in texture 1 */
            /* Typical Texture Generation Using Data From The Bitmap */
            glBindTexture( GL_TEXTURE_2D, texture[imgLoop * 3] );

            /* Generate The Texture */
            glTexImage2D( GL_TEXTURE_2D, 0, 3, TextureImage[imgLoop]->w,
                          TextureImage[imgLoop]->h, 0, GL_BGR,
                          GL_UNSIGNED_BYTE, TextureImage[imgLoop]->pixels );

            /* Nearest Filtering */
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                             GL_NEAREST );
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
                             GL_NEAREST );

            /* Load in texture 2 */
            /* Typical Texture Generation Using Data From The Bitmap */
            glBindTexture( GL_TEXTURE_2D, texture[imgLoop * 3 + 1] );

            /* Linear Filtering */
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                             GL_LINEAR );
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
                             GL_LINEAR );

            /* Generate The Texture */
            glTexImage2D( GL_TEXTURE_2D, 0, 3, TextureImage[imgLoop]->w,
                          TextureImage[imgLoop]->h, 0, GL_BGR,
                          GL_UNSIGNED_BYTE, TextureImage[imgLoop]->pixels );

            /* Load in texture 3 */
            /* Typical Texture Generation Using Data From The Bitmap */
            glBindTexture( GL_TEXTURE_2D, texture[imgLoop * 3 + 2] );

            /* Mipmapped Filtering */
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                             GL_LINEAR_MIPMAP_NEAREST );
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
                             GL_LINEAR );

            /* Generate The MipMapped Texture ( NEW ) */
            gluBuild2DMipmaps( GL_TEXTURE_2D, 3, TextureImage[imgLoop]->w,
                               TextureImage[imgLoop]->h, GL_BGR,
                               GL_UNSIGNED_BYTE, TextureImage[imgLoop]->pixels );
    }

        /* Free up any memory we may have used */
        if ( TextureImage[0] )
            SDL_FreeSurface( TextureImage[0] );

        return Status;
    }


    /* Setup Our World */
    void SetupWorld( char* worldFile )
    {
        int NumPollies = 0; //Number of triangles
        int triLoop = 0; //Loop var to store the coords in the structs
        int verLoop = 0; //Loop var to store the coords in the structs
        int texLoop = 0;
        float x, y, z, u, v; //Temporary coord storage
        int textureNum = 0; //Number of textures
        int tex;
        const char *textureName; //Temporary texture name storage

        TiXmlDocument *xmlDoc = new TiXmlDocument(worldFile); //XML file loader

        if(!xmlDoc->LoadFile()) //XML file loaded
            return;

        TiXmlElement *world = 0;
        world = xmlDoc->FirstChildElement("world");
        NumPollies = atoi(world->Attribute("triangles"));
        sector1.numTriangles = NumPollies;
        sector1.triangle = (trianglet*)malloc( NumPollies * sizeof( trianglet ) ); //Triangle allocation

        textureNum = atoi(world->Attribute("texturenum")); //Number of textures
        sector1.texture = (texturet*)malloc(textureNum * sizeof(texturet));
        sector1.numTextures = textureNum;

        //Texture loading
        TiXmlElement *textures = 0;
        textures = world->FirstChildElement("textures");
        TiXmlElement *texture = 0;
        texture = textures->FirstChildElement("texture");
        if(textureNum > 0)
        {
            textureName = texture->Attribute("img"); //Load first texture image
            sector1.texture[0].texture = textureName;

            for(texLoop = 1; sector1.numTextures > texLoop; texLoop++)
            {
                texture = texture->NextSiblingElement("texture");
                textureName = texture->Attribute("img");

                sector1.texture[texLoop].texture = textureName;
            }

        }

        TiXmlElement *triangle = 0;
        triangle = world->FirstChildElement("triangle");

        TiXmlElement *vertex = 0;
        vertex = triangle->FirstChildElement("vertex");

         if(tex = atoi(triangle->Attribute("texture")))
            sector1.triangle[0].vertex[0].tex = tex;

        for ( verLoop = 0; verLoop < 3;  )
        {



            x = atof(vertex->Attribute("p1"));
            y = atof(vertex->Attribute("p2"));
            z = atof(vertex->Attribute("p3"));
            u = atof(vertex->Attribute("t1"));
            v = atof(vertex->Attribute("t2"));
            vertex = vertex->NextSiblingElement("vertex");



            sector1.triangle[triLoop].vertex[verLoop].x = x;
            sector1.triangle[triLoop].vertex[verLoop].y = y;
            sector1.triangle[triLoop].vertex[verLoop].z = z;
            sector1.triangle[triLoop].vertex[verLoop].u = u;
            sector1.triangle[triLoop].vertex[verLoop].v = v;



            verLoop++;
            if (verLoop == 3 and triLoop < NumPollies - 1)
            {
                verLoop = 0;
                triLoop++;
                triangle = triangle->NextSiblingElement("triangle");

                 tex = atoi(triangle->Attribute("texture"));
                 sector1.triangle[triLoop].vertex[0].tex = tex;

                vertex = triangle->FirstChildElement("vertex");

            }

        }
        return;


    }


I think it has something to do with the pointers but it is a very strange bug because when ever i change a something there is no effect on the bug. Also i realize that im not releasing the images after creating the textures, but i don't think thats the problem (I will fix that in the future). What is wrong with this code?

Share this post


Link to post
Share on other sites
Advertisement
Can you point out the line where the porgram segfaults, that would be helpful.

One thing you can do is put some more error-checking in the xml loading part. For example:
TiXmlElement *textures = 0;
textures = world->FirstChildElement("textures");
assert(textures);


Do this for every tixml pointer. Then if it's that the xml file doesn't conform which crashes your program, you'll find it out more quickly, and where it is. Good luck.

Share this post


Link to post
Share on other sites
Sorry I took so long to respond. I had the AP bio exam and a lot of work. I asserted all of the TIXML pointers and found that vertex was a problem. I fixed the problem by puting
 if(verLoop < 2)
vertex = vertex->NextSiblingElement("vertex");


in the code to fix the error.


Now i still have a different continuation of this error. I found that the error is occuring at the line:

TextureImage[imgLoop] = SDL_LoadBMP(sector1.texture[imgLoop].texture);



This is only a problem when 11 or more images are loaded. Anything less doesn't create a problem. SDL_Surface *TextureImage[sector1.numTextures]; initializes the variable but it should be able to hold the images i think, because it works with less. Unless there is something i don't know about this.

Share this post


Link to post
Share on other sites
I solved the problem. I noticed that the texture variable was using a malloc. I changed it to calloc and it fixed all of the problems.
 texture = (GLuint*)calloc(3 * sector1.numTextures, sizeof(int)); 

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!