is there anything wrong with this code that could be screwing anything read in

This is a discussion on is there anything wrong with this code that could be screwing anything read in within the C++ Programming forums, part of the General Programming Boards category; is there anything wrong with this code that could be screwing anything read in from a file? I have been ...

  1. #1
    Shadow12345
    Guest

    is there anything wrong with this code that could be screwing anything read in

    is there anything wrong with this code that could be screwing anything read in from a file? I have been reading a .md3 tutorial so I am pretty sure the number of instances i am allocating memory for is correct, i.e the number of tags is equal to the number of tags in the header multiplied by the number of frames. Assuming all of that is correct are there any errors?

    I only provided the actual implementation for the reader, I chose not to include the data structures because if there are any errors they must be in this following implementation. If no one finds any errors in this I am completely stumped because this is exactly how the online tutorial was doing it.

    Code:
    #include "Main.h"
    #include "Md3.h"
    #include "cuf.h"
    
    #define Header tMd3Header
    #define Mesh tMd3MeshInfo
    #define Tag tMd3Tag
    #define Bone tMd3Bone
    #define Triangle tMd3Triangle
    #define Face tMd3Face
    #define TexCoord tMd3TexCoord
    #define Skin tMd3Skin
    
    Header		pHeader;
    Mesh		pMesh;
    Tag			*pTag		= NULL;
    Bone		*pBone		= NULL;
    
    model m1;
    char newline[] = "\n";
    
    float scale = 64.0f;
    
    int location = 0;
    int NumMeshes;
    int numvertices;
    int numtriangles;
    
    int main(void) {
    cout.setf(ios::fixed);
    setprecision(2);
    char NewLine[] = "\n";
    char *Buffer = NULL;
    char *bufferPtr = NULL;
    
    FILE *filePtr = NULL;
    
    char FileName[256];
    
    cout << "Enter the name of a file to read" << endl;
    
    cin.getline(FileName, 256, '\n');
    
    filePtr = fopen(FileName, "rb");
    if(!filePtr) {
    cout << "File doesn't exist" << endl;
    return 0;
    }
    
    memset(&pHeader, 0, sizeof(Header));	//CREATE STORAGE SPACE FOR OUR HEADER
    fread(&pHeader, sizeof(Header), 1, filePtr);
    
    pBone = new Bone[pHeader.numFrames];	//ALLOCATE MEMORY FOR BONES, WE ONLY READ THIS IN TO KEEP EVERYTHING ON TRACK
    fread(pBone, sizeof(Bone), pHeader.numFrames, filePtr);
    delete[] pBone;	//WE JUST DELETE THIS RIGHT AWAY, NO USE KEEPING IT
    
    pTag = new Tag[pHeader.numTags * pHeader.numFrames];	//ALLOCATE MEMORY FOR TAGS 
    fread(pTag, sizeof(Tag), pHeader.numTags * pHeader.numFrames, filePtr);	//READ IN THE TAGS
    
    long offset = ftell(filePtr);	//FIND OUT WHERE WE ARE IN THE FILE
    
    for(int index = 0; index < pHeader.numMeshes; index++) {	
    
    	fseek(filePtr, offset, SEEK_SET);	//GO THE MESH INFORMATION
    	fread(&pMesh, sizeof(Mesh), 1, filePtr);	//READ IN THE MESH
    
    	//CREATE OUR TEMPORARY VARIABLES 
    Skin		*pSkin		= new Skin[pMesh.numSkins];
    TexCoord	*pTexCoord	= new TexCoord[pMesh.numVertices];
    Face		*pTriangle	= new Face[pMesh.numTriangles];
    Triangle	*pVertices  = new Triangle[pMesh.numVertices * pMesh.numMeshFrames];
    
    fread(pSkin, sizeof(Skin), pMesh.numSkins, filePtr);
    
    fseek(filePtr, offset + pMesh.triStart, SEEK_SET);
    fread(pTriangle, sizeof(Face), pMesh.numTriangles, filePtr);
    
    fseek(filePtr, offset + pMesh.uvStart, SEEK_SET);
    fread(pTexCoord, sizeof(TexCoord), pMesh.numVertices, filePtr);
    
    fseek(filePtr, offset + pMesh.vertexStart, SEEK_SET);
    fread(pVertices, sizeof(Triangle), pMesh.numVertices * pMesh.numMeshFrames, filePtr);
    
    for(int index1 = 0; index1 < pMesh.numTriangles; index1++) {
    	cout << pVertices[index1].vertex[0] / scale << endl;
    	cout << pVertices[index1].vertex[1] / scale << endl;
    	cout << pVertices[index1].vertex[2] / scale << endl;
    	cout << endl;
    }
    	cout << "number of verts in this model " << pMesh.numVertices << endl;
    	getch();
    
    delete[] pSkin;
    delete[] pTriangle;
    delete[] pTexCoord;
    delete[] pVertices;
    
    offset += pMesh.meshSize;	//INCREASE THE OFFSET TO THE NEXT MESH IN THE FILE
    
    }
    
    fclose(filePtr);
    
    cout << "You have exited the program" << endl;
    cout << "Everything should have worked correctly" << endl;
    getch();
    
    return 0;
    }

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Did you try running it through the debugger? If you know what data should be read in you can run it through the debugger and compare the values stored in your variables.

    Failing that, you could place cout statements throught which will print exactly what's going on. You can narrow the problem down pretty quickly using those methods.

  3. #3
    Shadow12345
    Guest
    well I am pretty much hopeless then because I have been doing all of that stuff. Nothing works. I have tried plugging different versions into an opengl proggie and nothing gets drawn telling me that the data is getting broken up and won't form triangles (i.e the total number of vertices is no longer divisible by 3)

    EDIT:
    Im really sorry for dumping all that code on you guys, it's just that I honestly cannot figure out why I'm not getting correct data. I know it's not correct because I've viewed the model in milkshape. I've tried drawing the model in opengl. I've done the 'coutting' of the values with the z up orientation but also y up orientation. I feel stupid because this model loading is a basic step in my project and I haven't been able to get it working for 2-3 weeks now and i'm really starting to get depressed.
    Last edited by Shadow12345; 12-07-2002 at 12:47 PM.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Even though the reader doesn't seem to be the most optimized it looks like it should be reading. I'd crack open a hex editor and make sure everything is order in the file you are reading.

    [EDIT]
    The thought just occured to me that maybe data is being misaligned. I know that I've always had trouble directly reading a bitmap file header into a struct since a struct is aligned on the 32-bit boundary and the header is 14 bytes. Look into that too.
    [/EDIT]
    Last edited by master5001; 12-07-2002 at 01:14 PM.

  5. #5
    Shadow12345
    Guest
    well i guess it just comes down to this: I have made a not so obvious mistake somewhere along the lines here. And until I get my head out of my ass and realize what it is this reader will not be spitting out correct information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whats wrong - long code
    By raja9911 in forum C Programming
    Replies: 0
    Last Post: 02-07-2006, 05:10 AM
  2. What's wrong with this code?
    By Finchie_88 in forum Networking/Device Communication
    Replies: 10
    Last Post: 05-27-2005, 09:46 AM
  3. What's wrong with my Win32 Wrapper code?
    By miica in forum Windows Programming
    Replies: 9
    Last Post: 02-22-2005, 07:55 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21