MD2 models - Trying to understand how it works....HELP :)

Started by
5 comments, last by fatherjohn666 21 years, 9 months ago
I have a question regarding loading of md2 models. I do understand a fair bit of intermediate c stuff but I am hazy a bit on malloc. Always have been, because of the way the * character is used. Ok let me get this right: This is from page 592 and 593 of the open gl game programming book by the guys on this site modelHeader = (modelHeader_t*)buffer; ok…now according to the comment in the book, this line will “extract” the data for the modelHeader instance variables. Should I have to know how it magically assigns each int in the modelHeader_t typedef structure, or do I just accept that the way the file is structured, this information is found in the md2 files header in THAT order. Im kind of frazzled as to how it knows what value to give to each int in our modelHeader_t structure. The reason I ask is because in the next few lines of code to try are: model->numPoints = modelHeader->numXYZ; // number of vertices model->numFrames = modelHeader->numFrames; // number of frames model->frameSize = modelHeader->frameSize; // size of each frame which to me says that modelHeader->numXYZ has a value that I will use. Im kind of amazed that the value is automatically assigned purely by the one simple line of code pasted first. How does this work? Is it because an int is of a specific bytes size, thus c++ “magically” just knows when we execute the first line that we are kind of saying: Ok here is our model header instance. Fill it up with the values in buffer…but ….here I kind of just don’t understand how does the * character work in the first line of code I pasted. Is it like a symbolic gesture to tell c to grab all the values that fit (in the order specified in our struct declaration) in our modelHeader_t structure….wowa confused hahaha… Cheers for any guidance you may have….. Im just a beginner!!! http://www.actsofgord.com
~~~~~~~~~~~~~~~~~~~~~~~~~http://on.to/oni
Advertisement
modelHeader = (modelHeader_t *)buffer;

is not ''extracting'' (or copying) data from buffer to modelHeader. modelHeader is a pointer to a modelHeader_t type, and is being assigned to point to buffer. buffer still exists, and when you do something of the sort:

model->numPoints = modelHeader->numXYZ; // number of vertices

model is actually looking in the memory space that buffer has for the data.

i believe your confusion is on pointer manipulation and assignment. my suggestion would be to look at kernigan and ritchie''s ''ansi c'' book (or do a search on the web, but that might not be as fruitful).

quote:Original post by trainedmonkey
modelHeader = (modelHeader_t *)buffer;

is not ''extracting'' (or copying) data from buffer to modelHeader. modelHeader is a pointer to a modelHeader_t type, and is being assigned to point to buffer. buffer still exists, and when you do something of the sort:

model->numPoints = modelHeader->numXYZ; // number of vertices

model is actually looking in the memory space that buffer has for the data.



I understand the instance assignments etc, but I dont understand how just by declaring a structure and my pointer to it, how I can say ....hang on.......AAAAHHHHHHHHHHHHHHHHHHHHHH!!!!!!!!!!!!!!!!!!!!!! I just reread and reread and reread what you said......

so modelHeader may be declared as a pointer to a modelHeader_t struct BUT with the above line, we are saying
make modelHeader become a pointer to the buffer.
How does c know what values go where in the structure though, or do I have to strictly declare the values (int this, int that) in the right order in my modelHeader_t structure?

hmmmm im still hazy, if buffer is of x size in bytes, how does my c++ compiler know what buffer->numXYZ value is?
how does it know what value in our buffer to assign...or does it come back to the size of an int in bytes?

by declaring modelHEader as a pointer to buffer, by saying (modelHeader_t*) are we then saying create a modelHeader_t pointer and fill in those values from the buffer....ak...I hope this makes sense!

cheers!

Im just a beginner!!!

http://www.actsofgord.com
~~~~~~~~~~~~~~~~~~~~~~~~~http://on.to/oni
buffer is just some memory with data in it.
This line:
modelHeader = (modelHeader_t*)buffer;

says that the variable modelHeader now holds the address of the buffer, and that the buffer is to be used as type modelHeader_t.

For example, if you were to create this yourself, you'd do something like:
modelHeader_t *modelHeader = new modelHeader_t;
modelHeader->thing1 = x;
modelHeader->thing2 = y;
etc...

which you could then reference in your code later like you pasted here:
model->numPoints = modelHeader->numXYZ; // number of vertices
model->numFrames = modelHeader->numFrames; // number of frames
model->frameSize = modelHeader->frameSize; // size of each frame

It's constructing your modelHeader_t struct blindly in memory (buffer) for you, and you just assign your pointer to point to it, while telling the compiler what type of data is stored in that spot:

(type) (pointer to..) = (tell the compiler that buffer is actually a pointer to this type)
modelHeader_t *modelHeader = (modelHeader_t*)buffer


EDIT:
I really hope this is clear, what I'm trying to say.

[edited by - demigod on June 25, 2002 4:31:11 PM]
wicked

Im just a beginner!!!

http://www.actsofgord.com
~~~~~~~~~~~~~~~~~~~~~~~~~http://on.to/oni
The way you can think of this type-casting situation is as follows...

When you declare your struct, say, like this:

typedef struct {
int x;
int y;
} point_t;

You know that sizeof(int) == 4 bytes. So, the compiler knows that when you do:

point_t pt = (point_t *)buffer;

... where buffer is a raw byte pointer to data, the compiler knows that pt->x will be the first 4 bytes of the memory and y will be the second 4 bytes of the memory. You can kind of think of the structure as an offset "map" into a chunk of memory.

This works so well with MD2''s because you can read the entire file into memory, and then read the header information directly from that buffer without having to read it into some intermidiary variable. And, as a plus, the offset values in the MD2 header are offsets from the start of the file that you''ve already read into memory. This means you can just skip around within that one big chunk instead of having to do tons of individual fread''s for each part of the file.

Hope you understand...
cheers guys, you have helped clear some stuff in my heaqd completely, thanks as well to kevin hawkins for returning my email regarding this issue. Thanks heaps. I finally got my darkthvader interpolating all his frames on a reflective surface

big cheers

Im just a beginner!!!

http://www.actsofgord.com
~~~~~~~~~~~~~~~~~~~~~~~~~http://on.to/oni

This topic is closed to new replies.

Advertisement