Thread: making some progress; .fbx file translator

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    26

    making some progress; .fbx file translator

    I haven't been writing much for the past week with work but I have been making some progress and started writing this program. it still has errors like memory leaks and it's only half complete (I need to re-organize it also), it only pulls out the polygon offsets in the file. I still have to pull out the vertices and translate the offsets to memory locations, pull out other stuff like vertex vectors, and either write the data to a binary file in a personal format, display the mesh or even do both. I think I really like linked lists, they seem to be really great for dealing with data structures that have an unknown size. This program loads the file into memory and then searches for data, instead of searching for data in the file, I figured it would be a lot faster that way. It looks messy now, it's just a scratch pad at the moment, i should start getting it organized before i go much further. any way here's the code so far

    Code:
    #include "stdio.h"
    #include "conio.h"
    #include "stdlib.h"
    #include "string.h"
    #include "math.h"
    
    int loadfile();
    int getpolly();
    
    char buffer[128];
    char *cptr;
    int loaded;
    
    typedef struct pollystruct{
        struct pointxyz *p1;
        struct pointxyz *p2;
        struct pointxyz *p3;
        struct pointxyz *p4;
        struct pollystruct *next;
    } pollystruct;
    
    typedef struct pointxyz{
        double x;
        double y;
        double z;
        struct pointxyz *next;
    } pointxyz;
    
    int main(void)
    {
        while(1)
        {
        puts("\nmain menu:\n\t           load file: 1\n\tprint file to screen: 2\n\t                exit: 3\n\t            system(): 4\n\t       get pollygons: 5 ");
        switch(getch())
        {
            case '1':
                if(loaded == 1)
                {
                    free(cptr);
                }
                loadfile();
                break;
            case '2':
                if(loaded != 1)
                {
                    puts("\n please load a file first\n");
                    break;
                }
                printf("\n%s", cptr);
                break;
            case '3':
                if(loaded == 1)
                {
                    free(cptr);
                }
                exit(0);
            case '4':
                fgets(buffer, 128, stdin);
                system(buffer);
                break;
            case '5':
                if(loaded != 1)
                {
                    printf("\nPlease load a file first\n");
                    break;
                }
                getpolly();
                break;
        }
        }
    }
    int loadfile()
    {
        long int ctr;
        FILE *fptr;
        char filename[128];
    
        puts("\nfile name?: ");
        gets(filename);
        if(!(fptr = fopen(filename, "r")))
        {
            puts("\n----Error opening file-----\n\b\b");
            return -1;
        }
        for(ctr = 0; !(feof(fptr)); ctr++)
        {
            fgetc(fptr);
        }
        if(!(cptr = (char*)malloc(sizeof(char)*(ctr))))
        {
            puts("\n-----error allocating memory-----\n");
            return 1;
        }
        rewind(fptr);
        fread(cptr, sizeof(char), ctr-1, fptr);
        cptr[ctr-1]= '\0';
        puts("\nfile loaded\n");
        loaded = 1;
    }
    int getpolly()
    {
        char search1[] = "PolygonVertexIndex:";
        char search2[] = "a:";
        char search3[] = ",";
        char search4[] = "}";
    
        char conbuf[128];
        int ctr;
        int offsetbuf;
        int ctr2 = 1;
    
        char *pollyptr;
        char *conptr;
    
        struct pollystruct *head;
        struct pollystruct *conduit;
        void *freeptr;
    
        pollyptr = strstr(cptr, search1);       // moving around file pointer to the right data in file
        pollyptr = strstr(pollyptr, search2);
        pollyptr += 3;
    
        head=(pollystruct*)malloc(sizeof(pollystruct));// create head list
        conduit = head;
        conduit->next = (pollystruct*)"\0"; // setting first link to null
    
        while(strstr(pollyptr, search4)-pollyptr > strstr(pollyptr, search3)-pollyptr ) // checking to see if we are still in the data block
        {
            conptr = strstr(pollyptr, search3);   // finding the end of the number
            for(ctr = conptr - pollyptr; ctr > 0; ctr--) // writing the offset into a char buffer
            {
                conbuf[ctr-1] = pollyptr[ctr-1];
            }
            conbuf[conptr - pollyptr] = '\0'; // keeping the string from running out
            pollyptr = conptr;   // setting up for the next number
            pollyptr += 1;
            offsetbuf = atoi(conbuf);  // converting the character string into a real number
            if(offsetbuf != abs(offsetbuf)) // seeing if we are at the end of the first pollygon
            {
                if(ctr2 < 4 || ctr2 > 5) // shouldn't be true if only quads are in the file
                {
                    printf("\nInvalid pollygon, quads only\n");
                    conduit = head;
                    while(conduit->next != (pollystruct*)"\0") // getting rid of memmory leaks
                    {
                        freeptr = conduit;
                        conduit = conduit->next;
                        free(freeptr);
                    }
                    free(conduit);
                    return -1;
                }
                offsetbuf = abs(offsetbuf)-1; // converts last point from notation used in fbx file 
                conduit->p4 = (struct pointxyz*)offsetbuf;// write to p4 in current list
                conduit->next= (pollystruct*)malloc(sizeof(pollystruct)); //create next list, 
                conduit = conduit->next;
                conduit->next = (pollystruct*)"\0"; //set last link too null
                ctr2 = 1; // reseting for the next list
                continue;
            }
            switch(ctr2)
            {
                case 1:
                    conduit->p1 = (struct pointxyz*)offsetbuf;// write to p1 in current list
                    break;
                case 2:
                    conduit->p2 = (struct pointxyz*)offsetbuf;// write to p2 in current list
                    break;
                case 3:
                    conduit->p3 = (struct pointxyz*)offsetbuf;// write to p3 in current list
                    break;
            }
            ctr2++;
        }
        conduit = head;
        for(ctr2= 0; conduit->next != (pollystruct*)"\0"; ctr2++) // printing out pointer offsets for testing
        {
            printf("\n%d", conduit->p1);
            printf("\n%d", conduit->p2);
            printf("\n%d", conduit->p3);
            printf("\n%d", conduit->p4);
            conduit = conduit->next;
        }
        printf("\npolly tottal: %d", ctr2); // printing out tottal number of linked lists created
        return 0;
    }
    Last edited by seanJ; 01-26-2012 at 01:11 PM.

  2. #2
    Registered User
    Join Date
    Jan 2012
    Posts
    26
    and a "," has to be added the the last number in the polygonvertexindex: data field inside the .fbx file in order for it to fill in the last conduit->p4.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    lines 36 and 52 the ifs will always be true.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    26
    gah, I always do that

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Polygon (and poly) only have one letter el.

    Instead of this
    Code:
    if(ctr2 < 4 || ctr2 > 5)
    it's more clear to simply say
    Code:
    if (ctr2 != 4)
    Forgot to say that you can display your menu like this:
    Code:
        puts("\nmain menu:\n"
             "\t           load file: 1\n"
             "\tprint file to screen: 2\n"
             "\t                exit: 3\n"
             "\t            system(): 4\n"
             "\t       get pollygons: 5 ");
    Last edited by oogabooga; 01-26-2012 at 01:32 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    26
    i like polly better than poly. I was looking at that, i was going to change it to < 4 and add something to dump memory and exit the function if ctr2 hits 5 in the while statement since the whole thing will run out if it never hits a negative number, and it also needs something to check that the correct file type has been opened, there's a lot of bugs in there.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    26
    ah! i didn't know that lol. thanks, that long line of code was bugging me

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Nobody's going to trust a program about polygons with the word polygon (and the abbreviation poly) misspelled. But whatever.
    And now that I look at it again, if(ctr2 < 4 || ctr2 > 5) means if (ctr2 != 4 && ctr2 != 5), which is probably not what you meant.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    26
    no, i was thinking (ctr2 < 4 || ctr 2 > 4) or (ctr2 != 4) like you said. I don't think anyone would trust anything i wrote at the moment anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making a text based progress bar
    By TmX in forum Windows Programming
    Replies: 1
    Last Post: 04-09-2010, 09:32 AM
  2. html translator progress
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-02-2009, 07:45 PM
  3. progress indicator for file copying
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 05-24-2002, 02:30 PM
  4. making much progress with encryptor
    By Denethor2000 in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2002, 04:21 PM
  5. File Through Winsock & Progress on bar
    By (TNT) in forum Windows Programming
    Replies: 4
    Last Post: 12-05-2001, 10:50 PM