Game Development Reference
In-Depth Information
certain format. Having read the initial header, we will read a TAGS chunk
where a list of string values are stored that can be referred to later in the
file. Following this chunk will be a LAYR chunk, defining the start of a
layer. The Lightwave object format can store several object layers, each
effectively a self-contained mesh. A single LAYR chunk will contain point,
polygon and surface data for that layer. So we can structure our reader to
have an overall function that will read an entire file. This function will
contain a switch statement that looks for a TAG or LAYR chunk. Any other
chunk can be ignored and skipped by seeking through the file by the
amount returned in the length variable passed to the ReadChunkHeader
function. The function is given below. The loop is exited when the function
call ReadChunkHeader returns false to indicate a file error.
BOOL CLWObject::ReadObjectFile(FILE *fp){
char buf[4];
int length;
while (1){
if (!ReadChunkHeader(fp, buf, length)) break;
if (strncmp(buf, “TAGS”, 4)==0){
if (!ReadTags(fp, length)) return FALSE;
continue;
}
if (strncmp(buf, “LAYR”, 4)==0){
if (!ReadLayer(fp)) return FALSE;
continue;
}
if (length%2){
//Skip one extra byte if odd length chunk
fseek(fp, length+1, SEEK_CUR);
}else{
fseek(fp, length, SEEK_CUR);
}
}
return TRUE;
}
Reading the tags chunk is simply a case of reading a series of zero
terminated strings until the read count reaches the length of the chunk. A
function call that will read a zero terminated string from a Lightwave object
file is shown below. The function takes a file pointer, a character buffer, a
reference to a count variable and the length of the passed buffer. If the
Search Nedrilad ::




Custom Search