16.12.2012 Views

Trent Polack's OpenGL Game Programming Tutorials

Trent Polack's OpenGL Game Programming Tutorials

Trent Polack's OpenGL Game Programming Tutorials

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Jeff Molofee's <strong>OpenGL</strong> Windows Tutorial #4<br />

{<br />

S3Dlog.Output("%s is not a valid .md2", filename);<br />

return false;<br />

}<br />

This time, we are checking two of the header's variables that are specific to the type of file that we are loading. If<br />

either of those two if statements returns false, then we have a problem because ALL .md2's have should have the<br />

same magic and version number.<br />

frames= new char[header.frameSize*header.numFrames];<br />

As I was saying earlier about dynamic memory allocations, this is one of the cases where we apply it. While<br />

programming, we don't know the exact number of frames we are going to need, so we need to do it while the<br />

program is running. This fills frames with the correct number of frames (and we multiply that by header.frameSize<br />

so that the variable has adequate amounts of memory).<br />

if(frames==NULL)<br />

return false;<br />

This just checks to make sure that the memory was allocated successfully (and its rare that it isn't allocated right).<br />

Now that we have enough room in frames, we can fill it with the model-specific information:<br />

fseek(file, header.offsetFrames, SEEK_SET);<br />

fread(frames, header.frameSize*header.numFrames, 1, file);<br />

First we advance to the spot in the file where the frame information is, then after that, we fill our frames variable<br />

with the information.<br />

glCommands= new long [header.numGlCommands*sizeof(long)];<br />

if(glCommands==NULL)<br />

return false;<br />

fseek(file, header.offsetGlCommands, SEEK_SET);<br />

fread(glCommands, header.numGlCommands*sizeof(long), 1, file);<br />

This is the same case as before, except this time we are loading the glCommands information (to tell us whether<br />

or not we need triangle strips or fans to render our model).<br />

numFrames = header.numFrames;<br />

numGlCommands = header.numGlCommands;<br />

frameSize = header.frameSize;<br />

numTriangles = header.numTriangles;<br />

This stores the global header information that we need to know for rendering inside our class.<br />

fclose(file);<br />

S3Dlog.Output("Loaded %s correctly", filename);<br />

return true;<br />

}<br />

Now, lets close the file that we had open, output the success information to the log, and get out of the function!!!<br />

Phew, that was a pretty complicated function, especially if you've never had any experience with loading in files,<br />

and allocating memory dynamically. Now, on to the rendering function:<br />

void MD2::<br />

Render(int numFrame)<br />

{<br />

static MD2_MODELVERTEX vertList[100];<br />

http://nehe.gamedev.net/gametutorials/lesson04.asp (5 of 15) [20/08/2001 22:34:40]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!