Hi everyone!
I recently followed cone3d's tutorial for the md2 loader.. Everything works just fine except for the textures :(.. When I load the model it just turns out white and does not load the pcx file.
Here is the PCX loader class
/*******************************************************
cone3dmd2.cpp - description
-------------------
copyright : (C) 2000 by Marius Andra
email : cone3d@hotmail.com
ICQ : 43999510
The model loading part of this file was created by Naveed Ahmed
his e-mail is Greatman@softhome.net. He also created the original code
to display models and animation one frame at a time. That code was
taken from his 3D engine (based on nehe tut 23) with his permission
I modified it to make smooth animations and added the support for guns
and made some more small improvements (e.g classes) ...
*******************************************************/
/*******************************************************
*
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
*
*******************************************************/
#include "WKEngine.h"
#define NUMVERTEXNORMALS 162
// Quake 2 normals are indexed. Use avertexnormals[normalindex][x/y/z] and
// you'll have you normals.
float avertexnormals[NUMVERTEXNORMALS][3] = {
{-0.525731f, 0.000000f, 0.850651f},
{-0.442863f, 0.238856f, 0.864188f},
{-0.295242f, 0.000000f, 0.955423f},
{-0.309017f, 0.500000f, 0.809017f},
{-0.162460f, 0.262866f, 0.951056f},
{0.000000f, 0.000000f, 1.000000f},
{0.000000f, 0.850651f, 0.525731f},
{-0.147621f, 0.716567f, 0.681718f},
{0.147621f, 0.716567f, 0.681718f},
{0.000000f, 0.525731f, 0.850651f},
{0.309017f, 0.500000f, 0.809017f},
{0.525731f, 0.000000f, 0.850651f},
{0.295242f, 0.000000f, 0.955423f},
{0.442863f, 0.238856f, 0.864188f},
{0.162460f, 0.262866f, 0.951056f},
{-0.681718f, 0.147621f, 0.716567f},
{-0.809017f, 0.309017f, 0.500000f},
{-0.587785f, 0.425325f, 0.688191f},
{-0.850651f, 0.525731f, 0.000000f},
{-0.864188f, 0.442863f, 0.238856f},
{-0.716567f, 0.681718f, 0.147621f},
{-0.688191f, 0.587785f, 0.425325f},
{-0.500000f, 0.809017f, 0.309017f},
{-0.238856f, 0.864188f, 0.442863f},
{-0.425325f, 0.688191f, 0.587785f},
{-0.716567f, 0.681718f, -0.147621f},
{-0.500000f, 0.809017f, -0.309017f},
{-0.525731f, 0.850651f, 0.000000f},
{0.000000f, 0.850651f, -0.525731f},
{-0.238856f, 0.864188f, -0.442863f},
{0.000000f, 0.955423f, -0.295242f},
{-0.262866f, 0.951056f, -0.162460f},
{0.000000f, 1.000000f, 0.000000f},
{0.000000f, 0.955423f, 0.295242f},
{-0.262866f, 0.951056f, 0.162460f},
{0.238856f, 0.864188f, 0.442863f},
{0.262866f, 0.951056f, 0.162460f},
{0.500000f, 0.809017f, 0.309017f},
{0.238856f, 0.864188f, -0.442863f},
{0.262866f, 0.951056f, -0.162460f},
{0.500000f, 0.809017f, -0.309017f},
{0.850651f, 0.525731f, 0.000000f},
{0.716567f, 0.681718f, 0.147621f},
{0.716567f, 0.681718f, -0.147621f},
{0.525731f, 0.850651f, 0.000000f},
{0.425325f, 0.688191f, 0.587785f},
{0.864188f, 0.442863f, 0.238856f},
{0.688191f, 0.587785f, 0.425325f},
{0.809017f, 0.309017f, 0.500000f},
{0.681718f, 0.147621f, 0.716567f},
{0.587785f, 0.425325f, 0.688191f},
{0.955423f, 0.295242f, 0.000000f},
{1.000000f, 0.000000f, 0.000000f},
{0.951056f, 0.162460f, 0.262866f},
{0.850651f, -0.525731f, 0.000000f},
{0.955423f, -0.295242f, 0.000000f},
{0.864188f, -0.442863f, 0.238856f},
{0.951056f, -0.162460f, 0.262866f},
{0.809017f, -0.309017f, 0.500000f},
{0.681718f, -0.147621f, 0.716567f},
{0.850651f, 0.000000f, 0.525731f},
{0.864188f, 0.442863f, -0.238856f},
{0.809017f, 0.309017f, -0.500000f},
{0.951056f, 0.162460f, -0.262866f},
{0.525731f, 0.000000f, -0.850651f},
{0.681718f, 0.147621f, -0.716567f},
{0.681718f, -0.147621f, -0.716567f},
{0.850651f, 0.000000f, -0.525731f},
{0.809017f, -0.309017f, -0.500000f},
{0.864188f, -0.442863f, -0.238856f},
{0.951056f, -0.162460f, -0.262866f},
{0.147621f, 0.716567f, -0.681718f},
{0.309017f, 0.500000f, -0.809017f},
{0.425325f, 0.688191f, -0.587785f},
{0.442863f, 0.238856f, -0.864188f},
{0.587785f, 0.425325f, -0.688191f},
{0.688191f, 0.587785f, -0.425325f},
{-0.147621f, 0.716567f, -0.681718f},
{-0.309017f, 0.500000f, -0.809017f},
{0.000000f, 0.525731f, -0.850651f},
{-0.525731f, 0.000000f, -0.850651f},
{-0.442863f, 0.238856f, -0.864188f},
{-0.295242f, 0.000000f, -0.955423f},
{-0.162460f, 0.262866f, -0.951056f},
{0.000000f, 0.000000f, -1.000000f},
{0.295242f, 0.000000f, -0.955423f},
{0.162460f, 0.262866f, -0.951056f},
{-0.442863f, -0.238856f, -0.864188f},
{-0.309017f, -0.500000f, -0.809017f},
{-0.162460f, -0.262866f, -0.951056f},
{0.000000f, -0.850651f, -0.525731f},
{-0.147621f, -0.716567f, -0.681718f},
{0.147621f, -0.716567f, -0.681718f},
{0.000000f, -0.525731f, -0.850651f},
{0.309017f, -0.500000f, -0.809017f},
{0.442863f, -0.238856f, -0.864188f},
{0.162460f, -0.262866f, -0.951056f},
{0.238856f, -0.864188f, -0.442863f},
{0.500000f, -0.809017f, -0.309017f},
{0.425325f, -0.688191f, -0.587785f},
{0.716567f, -0.681718f, -0.147621f},
{0.688191f, -0.587785f, -0.425325f},
{0.587785f, -0.425325f, -0.688191f},
{0.000000f, -0.955423f, -0.295242f},
{0.000000f, -1.000000f, 0.000000f},
{0.262866f, -0.951056f, -0.162460f},
{0.000000f, -0.850651f, 0.525731f},
{0.000000f, -0.955423f, 0.295242f},
{0.238856f, -0.864188f, 0.442863f},
{0.262866f, -0.951056f, 0.162460f},
{0.500000f, -0.809017f, 0.309017f},
{0.716567f, -0.681718f, 0.147621f},
{0.525731f, -0.850651f, 0.000000f},
{-0.238856f, -0.864188f, -0.442863f},
{-0.500000f, -0.809017f, -0.309017f},
{-0.262866f, -0.951056f, -0.162460f},
{-0.850651f, -0.525731f, 0.000000f},
{-0.716567f, -0.681718f, -0.147621f},
{-0.716567f, -0.681718f, 0.147621f},
{-0.525731f, -0.850651f, 0.000000f},
{-0.500000f, -0.809017f, 0.309017f},
{-0.238856f, -0.864188f, 0.442863f},
{-0.262866f, -0.951056f, 0.162460f},
{-0.864188f, -0.442863f, 0.238856f},
{-0.809017f, -0.309017f, 0.500000f},
{-0.688191f, -0.587785f, 0.425325f},
{-0.681718f, -0.147621f, 0.716567f},
{-0.442863f, -0.238856f, 0.864188f},
{-0.587785f, -0.425325f, 0.688191f},
{-0.309017f, -0.500000f, 0.809017f},
{-0.147621f, -0.716567f, 0.681718f},
{-0.425325f, -0.688191f, 0.587785f},
{-0.162460f, -0.262866f, 0.951056f},
{0.442863f, -0.238856f, 0.864188f},
{0.162460f, -0.262866f, 0.951056f},
{0.309017f, -0.500000f, 0.809017f},
{0.147621f, -0.716567f, 0.681718f},
{0.000000f, -0.525731f, 0.850651f},
{0.425325f, -0.688191f, 0.587785f},
{0.587785f, -0.425325f, 0.688191f},
{0.688191f, -0.587785f, 0.425325f},
{-0.955423f, 0.295242f, 0.000000f},
{-0.951056f, 0.162460f, 0.262866f},
{-1.000000f, 0.000000f, 0.000000f},
{-0.850651f, 0.000000f, 0.525731f},
{-0.955423f, -0.295242f, 0.000000f},
{-0.951056f, -0.162460f, 0.262866f},
{-0.864188f, 0.442863f, -0.238856f},
{-0.951056f, 0.162460f, -0.262866f},
{-0.809017f, 0.309017f, -0.500000f},
{-0.864188f, -0.442863f, -0.238856f},
{-0.951056f, -0.162460f, -0.262866f},
{-0.809017f, -0.309017f, -0.500000f},
{-0.681718f, 0.147621f, -0.716567f},
{-0.681718f, -0.147621f, -0.716567f},
{-0.850651f, 0.000000f, -0.525731f},
{-0.688191f, 0.587785f, -0.425325f},
{-0.587785f, 0.425325f, -0.688191f},
{-0.425325f, 0.688191f, -0.587785f},
{-0.425325f, -0.688191f, -0.587785f},
{-0.587785f, -0.425325f, -0.688191f},
{-0.688191f, -0.587785f, -0.425325f},
};
// Reset the time back to 0
int WKMD2::ResetTime()
{
frametime=0;
framestarttime=0;
return 1;
}
// The main animation.
int WKMD2::Animate(float x, float y, float z,float time)
{
glPushMatrix();
// Calculate how much time has passed
newtime = SDL_GetTicks();
if(framestarttime==0) framestarttime=newtime;
frametime=newtime-framestarttime;
float tim=frametime/time;
// and Display the model
if(GetFrame()+1>model->IntroFrames[1])
{
Render(x,y,z,GetFrame(),model->IntroFrames[0],tim);
if(isweapon)
RenderWeapon(x,y,z,GetFrame(),model->IntroFrames[0],tim);
} else {
Render(x,y,z,GetFrame(),GetFrame()+1,tim);
if(isweapon)
RenderWeapon(x,y,z,GetFrame(),GetFrame()+1,tim);
}
// If the animation has reached it's end start at the beginning and return 2
if((frametime/time)>=1)
{
framestarttime=newtime;
SetFrame(GetFrame()+1);
if(GetFrame() > model->IntroFrames[1])
SetFrame(model->IntroFrames[0]);
return 2;
}
// else return 1
return 1;
}
// Animate from frame1 to frame2
int WKMD2::Animate2(float x, float y, float z,float frame1,
float frame2,float time)
{
glPushMatrix();
newtime = SDL_GetTicks();
frametime=newtime-framestarttime;
Render(x,y,z,frame1,frame2,frametime/time);
if(isweapon)
RenderWeapon(x,y,z,frame1,frame2,frametime/time);
if((frametime/time)>=1)
{
framestarttime=newtime;
return 2;
}
return 1;
}
// Set the animation range
int WKMD2::Animation(int startframe, int stopframe)
{
model->IntroFrames[0] = startframe;
model->IntroFrames[1] = stopframe;
return 1;
}
// Set the current frame
int WKMD2::SetFrame(int frame)
{
model->CurrFrame = frame;
return 1;
}
// Get the current frame
int WKMD2::GetFrame()
{
return model->CurrFrame;
}
// Get the number of the last frame
int WKMD2::GetLastFrameNum()
{
return model->numFrames-1;
}
// Render the current frame (no animation)
// (Useful when you have some still statues
// in the wall that come to life.)
int WKMD2::RenderCurrentFrame(float x,float y,float z)
{
Render(x,y,z,GetFrame(),GetFrame(),1);
if(isweapon)
RenderWeapon(x,y,z,GetFrame(),GetFrame(),1);
return 1;
}
// Render the weapon
int WKMD2::RenderWeapon(float x, float y, float z, int curframe,
int nextframe,float pol)
{
float x1, y1, z1, x2, y2, z2;
float nx1,ny1,nz1,nx2,ny2,nz2;
if(pol>1) pol=1;
// Get the points to the current and the next frame.
md2coord *pointListPtr;
md2coord *pointListPtr2;
pointListPtr = &weapon->pointList[ weapon->numPoints * curframe];
pointListPtr2 = &weapon->pointList[ weapon->numPoints * nextframe];
// Bind the texture and begin drawing triangles
glBindTexture(GL_TEXTURE_2D, weapont.texID);
glBegin(GL_TRIANGLES);
for(int i=0; i<weapon->numTriangles; i++ )
{
glTexCoord2f(weapon->st[ weapon->triIndex.stIndex[0] ].s, weapon->st[ weapon->triIndex.stIndex[0] ].t);
// Get the points into shorter-named variables.
x1 = pointListPtr[ weapon->triIndex.meshIndex[0] ].pt[0];
y1 = pointListPtr[ weapon->triIndex.meshIndex[0] ].pt[1];
z1 = pointListPtr[ weapon->triIndex.meshIndex[0] ].pt[2];
x2 = pointListPtr2[ weapon->triIndex.meshIndex[0] ].pt[0];
y2 = pointListPtr2[ weapon->triIndex.meshIndex[0] ].pt[1];
z2 = pointListPtr2[ weapon->triIndex.meshIndex[0] ].pt[2];
nx1 = pointListPtr[ weapon->triIndex.meshIndex[0] ].ln[0];
ny1 = pointListPtr[ weapon->triIndex.meshIndex[0] ].ln[1];
nz1 = pointListPtr[ weapon->triIndex.meshIndex[0] ].ln[2];
nx2 = pointListPtr2[ weapon->triIndex.meshIndex[0] ].ln[0];
ny2 = pointListPtr2[ weapon->triIndex.meshIndex[0] ].ln[1];
nz2 = pointListPtr2[ weapon->triIndex.meshIndex[0] ].ln[2];
// Get a normal between the current frame and the next frame.
// Same with the points.
glNormal3f( x+nx1 + pol * (nx2 - nx1), y+ny1 + pol * (ny2 - ny1), z+nz1 + pol * (nz2 - nz1) );
glVertex3f( x+x1 + pol * (x2 - x1), y+y1 + pol * (y2 - y1), z+z1 + pol * (z2 - z1) );
glTexCoord2f( weapon->st[ weapon->triIndex.stIndex[1] ].s, weapon->st[ weapon->triIndex.stIndex[1] ].t );
x1 = pointListPtr[ weapon->triIndex.meshIndex[1] ].pt[0];
y1 = pointListPtr[ weapon->triIndex.meshIndex[1] ].pt[1];
z1 = pointListPtr[ weapon->triIndex.meshIndex[1] ].pt[2];
x2 = pointListPtr2[ weapon->triIndex.meshIndex[1] ].pt[0];
y2 = pointListPtr2[ weapon->triIndex.meshIndex[1] ].pt[1];
z2 = pointListPtr2[ weapon->triIndex.meshIndex[1] ].pt[2];
nx1 = pointListPtr[ weapon->triIndex.meshIndex[1] ].ln[0];
ny1 = pointListPtr[ weapon->triIndex.meshIndex[1] ].ln[1];
nz1 = pointListPtr[ weapon->triIndex.meshIndex[1] ].ln[2];
nx2 = pointListPtr2[ weapon->triIndex.meshIndex[1] ].ln[0];
ny2 = pointListPtr2[ weapon->triIndex.meshIndex[1] ].ln[1];
nz2 = pointListPtr2[ weapon->triIndex.meshIndex[1] ].ln[2];
glNormal3f( x+nx1 + pol * (nx2 - nx1), y+ny1 + pol * (ny2 - ny1), z+nz1 + pol * (nz2 - nz1) );
glVertex3f( x+x1 + pol * (x2 - x1), y+y1 + pol * (y2 - y1), z+z1 + pol * (z2 - z1) );
glTexCoord2f( weapon->st[ weapon->triIndex.stIndex[2] ].s, weapon->st[ weapon->triIndex.stIndex[2] ].t );
x1 = pointListPtr[ weapon->triIndex.meshIndex[2] ].pt[0];
y1 = pointListPtr[ weapon->triIndex.meshIndex[2] ].pt[1];
z1 = pointListPtr[ weapon->triIndex.meshIndex[2] ].pt[2];
x2 = pointListPtr2[ weapon->triIndex.meshIndex[2] ].pt[0];
y2 = pointListPtr2[ weapon->triIndex.meshIndex[2] ].pt[1];
z2 = pointListPtr2[ weapon->triIndex.meshIndex[2] ].pt[2];
nx1 = pointListPtr[ weapon->triIndex.meshIndex[2] ].ln[0];
ny1 = pointListPtr[ weapon->triIndex.meshIndex[2] ].ln[1];
nz1 = pointListPtr[ weapon->triIndex.meshIndex[2] ].ln[2];
nx2 = pointListPtr2[ weapon->triIndex.meshIndex[2] ].ln[0];
ny2 = pointListPtr2[ weapon->triIndex.meshIndex[2] ].ln[1];
nz2 = pointListPtr2[ weapon->triIndex.meshIndex[2] ].ln[2];
glNormal3f( x+nx1 + pol * (nx2 - nx1), y+ny1 + pol * (ny2 - ny1), z+nz1 + pol * (nz2 - nz1) );
glVertex3f( x+x1 + pol * (x2 - x1), y+y1 + pol * (y2 - y1), z+z1 + pol * (z2 - z1) );
}
glEnd();
return 1;
}
int WKMD2::Render(float x, float y, float z, int curframe,int nextframe,float pol)
{
float x1, y1, z1, x2, y2, z2;
float nx1,ny1,nz1,nx2,ny2,nz2;
if(pol>1) pol=1;
// Get the points to the current and the next frame.
md2coord *pointListPtr;
md2coord *pointListPtr2;
pointListPtr = &model->pointList[ model->numPoints * curframe];
pointListPtr2 = &model->pointList[ model->numPoints * nextframe];
// Bind the texture and begin drawing triangles
glBindTexture(GL_TEXTURE_2D, modelt.texID);
glBegin(GL_TRIANGLES);
for(int i=0; i<model->numTriangles; i++ )
{
glTexCoord2f(model->st[ model->triIndex.stIndex[0] ].s, model->st[ model->triIndex.stIndex[0] ].t);
// Get the points into shorter-named variables.
x1 = pointListPtr[ model->triIndex.meshIndex[0] ].pt[0];
y1 = pointListPtr[ model->triIndex.meshIndex[0] ].pt[1];
z1 = pointListPtr[ model->triIndex.meshIndex[0] ].pt[2];
x2 = pointListPtr2[ model->triIndex.meshIndex[0] ].pt[0];
y2 = pointListPtr2[ model->triIndex.meshIndex[0] ].pt[1];
z2 = pointListPtr2[ model->triIndex.meshIndex[0] ].pt[2];
nx1 = pointListPtr[ model->triIndex.meshIndex[0] ].ln[0];
ny1 = pointListPtr[ model->triIndex.meshIndex[0] ].ln[1];
nz1 = pointListPtr[ model->triIndex.meshIndex[0] ].ln[2];
nx2 = pointListPtr2[ model->triIndex.meshIndex[0] ].ln[0];
ny2 = pointListPtr2[ model->triIndex.meshIndex[0] ].ln[1];
nz2 = pointListPtr2[ model->triIndex.meshIndex[0] ].ln[2];
// Get a normal between the current frame and the next frame.
// Same with the points.
glNormal3f( x+nx1 + pol * (nx2 - nx1), y+ny1 + pol * (ny2 - ny1), z+nz1 + pol * (nz2 - nz1) );
glVertex3f( x+x1 + pol * (x2 - x1), y+y1 + pol * (y2 - y1), z+z1 + pol * (z2 - z1) );
glTexCoord2f( model->st[ model->triIndex.stIndex[1] ].s, model->st[ model->triIndex.stIndex[1] ].t );
x1 = pointListPtr[ model->triIndex.meshIndex[1] ].pt[0];
y1 = pointListPtr[ model->triIndex.meshIndex[1] ].pt[1];
z1 = pointListPtr[ model->triIndex.meshIndex[1] ].pt[2];
x2 = pointListPtr2[ model->triIndex.meshIndex[1] ].pt[0];
y2 = pointListPtr2[ model->triIndex.meshIndex[1] ].pt[1];
z2 = pointListPtr2[ model->triIndex.meshIndex[1] ].pt[2];
nx1 = pointListPtr[ model->triIndex.meshIndex[1] ].ln[0];
ny1 = pointListPtr[ model->triIndex.meshIndex[1] ].ln[1];
nz1 = pointListPtr[ model->triIndex.meshIndex[1] ].ln[2];
nx2 = pointListPtr2[ model->triIndex.meshIndex[1] ].ln[0];
ny2 = pointListPtr2[ model->triIndex.meshIndex[1] ].ln[1];
nz2 = pointListPtr2[ model->triIndex.meshIndex[1] ].ln[2];
glNormal3f( x+nx1 + pol * (nx2 - nx1), y+ny1 + pol * (ny2 - ny1), z+nz1 + pol * (nz2 - nz1) );
glVertex3f( x+x1 + pol * (x2 - x1), y+y1 + pol * (y2 - y1), z+z1 + pol * (z2 - z1) );
glTexCoord2f( model->st[ model->triIndex.stIndex[2] ].s, model->st[ model->triIndex.stIndex[2] ].t );
x1 = pointListPtr[ model->triIndex.meshIndex[2] ].pt[0];
y1 = pointListPtr[ model->triIndex.meshIndex[2] ].pt[1];
z1 = pointListPtr[ model->triIndex.meshIndex[2] ].pt[2];
x2 = pointListPtr2[ model->triIndex.meshIndex[2] ].pt[0];
y2 = pointListPtr2[ model->triIndex.meshIndex[2] ].pt[1];
z2 = pointListPtr2[ model->triIndex.meshIndex[2] ].pt[2];
nx1 = pointListPtr[ model->triIndex.meshIndex[2] ].ln[0];
ny1 = pointListPtr[ model->triIndex.meshIndex[2] ].ln[1];
nz1 = pointListPtr[ model->triIndex.meshIndex[2] ].ln[2];
nx2 = pointListPtr2[ model->triIndex.meshIndex[2] ].ln[0];
ny2 = pointListPtr2[ model->triIndex.meshIndex[2] ].ln[1];
nz2 = pointListPtr2[ model->triIndex.meshIndex[2] ].ln[2];
glNormal3f( x+nx1 + pol * (nx2 - nx1), y+ny1 + pol * (ny2 - ny1), z+nz1 + pol * (nz2 - nz1) );
glVertex3f( x+x1 + pol * (x2 - x1), y+y1 + pol * (y2 - y1), z+z1 + pol * (z2 - z1) );
}
glEnd();
return 1;
}
// Load the model
int WKMD2::LoadModel( const char *fileName, const char *textureName)
{
FILE *hFile = fopen( fileName, "rb" );
/* check the file open command */
if( hFile != NULL )
{
int fileLen;
char *buffer;
md2modelHeader *mdh;
md2frame *frm;
md2coord *pointList, *pointListPtr;
md2mesh *triIndex, *bufIndexPtr;
md2stTexCoord *st;
md2stTexture *stPtr;
int i, j;
/* find length of file */
fseek( hFile, 0, SEEK_END );
fileLen = ftell( hFile );
fseek( hFile, 0, SEEK_SET );
/* read in file */
buffer = (char *)malloc( fileLen+1 );
fread( buffer, sizeof( char ), fileLen, hFile );
/* get pointer to file header */
mdh = (md2modelHeader *)buffer;
/* construct the model data structure */
model = (md2modelData *)malloc( sizeof( md2modelData ));
/* create a pointlist */
pointList = (md2coord *)malloc( sizeof( md2coord ) * mdh->numXYZ * mdh->numFrames );
model->pointList = pointList;
model->numPoints = mdh->numXYZ;
model->numFrames = mdh->numFrames;
model->currentmorph = (md2coord *)malloc( sizeof( md2coord ) * mdh->numXYZ);
/* loop for number of frames in file */
for( j=0; j<mdh->numFrames; j++ )
{
/* offset to points in frame */
frm = (md2frame *)&buffer[ mdh->offsetFrames + mdh->framesize * j ];
pointListPtr = (md2coord *)&pointList[ mdh->numXYZ * j ];
for( i=0; i<mdh->numXYZ; i++ )
{
pointListPtr.pt[0] = frm->scale[0] * frm->fp.v[0] + frm->translate[0];
pointListPtr.pt[2] = frm->scale[1] * frm->fp.v[1] + frm->translate[1];
pointListPtr.pt[1] = frm->scale[2] * frm->fp.v[2] + frm->translate[2];
pointListPtr.ln[0] = avertexnormals[frm->fp.lightNormalIndex][0];
pointListPtr.ln[1] = avertexnormals[frm->fp.lightNormalIndex][1];
pointListPtr.ln[2] = avertexnormals[frm->fp.lightNormalIndex][2];
pointListPtr.pt[0] /= 64;
pointListPtr.pt[1] /= 64;
pointListPtr.pt[2] /= 64;
}
}
do {
/* see if there is a texture associated with the file */
modelt.width = 1;
modelt.height = 1;
if (modelt.LoadPCX((char *)textureName))
{
WKLog("Textures found and loaded.");
/* create the texture list */
st = (md2stTexCoord *)malloc( sizeof( md2stTexCoord ) * mdh->numST );
model->numST = mdh->numST;
model->st = st;
stPtr = (md2stTexture *)&buffer[mdh->offsetST];
for( i=0; i<mdh->numST; i++ )
{
st.s = (float)stPtr.s / (float)modelt.width;
st.t = (float)stPtr.t / (float)modelt.width;
}
model->zoomstop = false;
}
} while( 0 ); /* only do once */
/* create a mesh list */
triIndex = (md2mesh *)malloc( sizeof( md2mesh ) * mdh->numTris );
model->numTriangles = mdh->numTris;
model->triIndex = triIndex;
/* point to indexes in buffers */
bufIndexPtr = (md2mesh *)&buffer[mdh->offsetTris];
for( i=0; i<mdh->numTris; i++ )
{
triIndex.meshIndex[0] = bufIndexPtr.meshIndex[0];
triIndex.meshIndex[1] = bufIndexPtr.meshIndex[1];
triIndex.meshIndex[2] = bufIndexPtr.meshIndex[2];
triIndex.stIndex[0] = bufIndexPtr.stIndex[0];
triIndex.stIndex[1] = bufIndexPtr.stIndex[1];
triIndex.stIndex[2] = bufIndexPtr.stIndex[2];
}
/* cleanup */
free( buffer );
}
return 1;
}
// Load the weapon
int WKMD2::LoadWeapon( const char *fileName, const char *textureName)
{
FILE *hFile = fopen( fileName, "rb" );
/* check the file open command */
if( hFile != NULL )
{
int fileLen;
char *buffer;
md2modelHeader *mdh;
md2frame *frm;
md2coord *pointList, *pointListPtr;
md2mesh *triIndex, *bufIndexPtr;
md2stTexCoord *st;
md2stTexture *stPtr;
int i, j;
/* find length of file */
fseek( hFile, 0, SEEK_END );
fileLen = ftell( hFile );
fseek( hFile, 0, SEEK_SET );
/* read in file */
buffer = (char *)malloc( fileLen+1 );
fread( buffer, sizeof( char ), fileLen, hFile );
/* get pointer to file header */
mdh = (md2modelHeader *)buffer;
/* construct the model data structure */
weapon = (md2modelData *)malloc( sizeof( md2modelData ));
/* create a pointlist */
pointList = (md2coord *)malloc( sizeof( md2coord ) * mdh->numXYZ * mdh->numFrames );
weapon->pointList = pointList;
weapon->numPoints = mdh->numXYZ;
weapon->numFrames = mdh->numFrames;
weapon->currentmorph = (md2coord *)malloc( sizeof( md2coord ) * mdh->numXYZ);
/* loop for number of frames in file */
for( j=0; j<mdh->numFrames; j++ )
{
/* offset to points in frame */
frm = (md2frame *)&buffer[ mdh->offsetFrames + mdh->framesize * j ];
pointListPtr = (md2coord *)&pointList[ mdh->numXYZ * j ];
for( i=0; i<mdh->numXYZ; i++ )
{
pointListPtr.pt[0] = frm->scale[0] * frm->fp.v[0] + frm->translate[0];
pointListPtr.pt[2] = frm->scale[1] * frm->fp.v[1] + frm->translate[1];
pointListPtr.pt[1] = frm->scale[2] * frm->fp.v[2] + frm->translate[2];
pointListPtr.pt[0] /= 64;
pointListPtr.pt[1] /= 64;
pointListPtr.pt[2] /= 64;
}
}
do {
/* see if there is a texture associated with the file */
weapont.width = 1;
weapont.height = 1;
if (weapont.LoadPCX((char *)textureName))
{
/* create the texture list */
st = (md2stTexCoord *)malloc( sizeof( md2stTexCoord ) * mdh->numST );
weapon->numST = mdh->numST;
weapon->st = st;
stPtr = (md2stTexture *)&buffer[mdh->offsetST];
for( i=0; i<mdh->numST; i++ )
{
st.s = (float)stPtr.s / (float)weapont.width;
st.t = (float)stPtr.t / (float)weapont.width;
}
weapon->zoomstop = false;
}
} while( 0 ); /* only do once */
/* create a mesh list */
triIndex = (md2mesh *)malloc( sizeof( md2mesh ) * mdh->numTris );
weapon->numTriangles = mdh->numTris;
weapon->triIndex = triIndex;
/* point to indexes in buffers */
bufIndexPtr = (md2mesh *)&buffer[mdh->offsetTris];
for( i=0; i<mdh->numTris; i++ )
{
triIndex.meshIndex[0] = bufIndexPtr.meshIndex[0];
triIndex.meshIndex[1] = bufIndexPtr.meshIndex[1];
triIndex.meshIndex[2] = bufIndexPtr.meshIndex[2];
triIndex.stIndex[0] = bufIndexPtr.stIndex[0];
triIndex.stIndex[1] = bufIndexPtr.stIndex[1];
triIndex.stIndex[2] = bufIndexPtr.stIndex[2];
}
/* cleanup */
free( buffer );
}
isweapon=1;
return 1;
}
I know there's a little bit of code, but please.. I'm desperate.. I've tried everything!
Thanks! :)
EDIT:
And here is the rendering code:
#include "WKEngine.h"
HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; // Permanent Rendering Context
HWND hWnd=NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application
bool keys[256]; // Array Used For The Keyboard Routine
bool active=TRUE; // Window Active Flag Set To TRUE By Default
bool fullscreen=TRUE; // Fullscreen Flag Set To Fullscreen Mode By Default
GLfloat LightDiffuse[]= { 0.1f, 2.0f, 1.0f, 1.0f }; // Diffuse Light Values ( NEW )
GLfloat LightPosition[]= { 1.0f, 0.5f, 2.0f, 1.0f }; // Light Position ( NEW )
GLfloat LightAmbient[]= { 0.5f, 2.0f, 0.5f, 1.0f }; // Ambient Light Values ( NEW )
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0) // Prevent A Divide By Zero By
{
height=1; // Making Height Equal One
}
glViewport(0,0,width,height); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// Calculate The Aspect Ratio Of The Window
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glEnable( GL_TEXTURE_2D );
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}
int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(3.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
glEnable(GL_LIGHT1);
return TRUE; // Initialization Went OK
}
WKMD2 yoshi;
GLfloat z = -55.0f;
GLfloat x = -1.1f;
GLfloat y = -7.8f;
GLfloat t = 1.0f;
int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(x,y,z);
if(keys[VK_SHIFT]) z += 0.02f;
if(keys[VK_SPACE]) z -= 0.02f;
if(keys[VK_RIGHT]) x -= 0.01f;
if(keys[VK_LEFT]) x +=0.01f;
if(keys[VK_UP]) y -= 0.01f;
if(keys[VK_DOWN]) y += 0.01f;
glRotatef(t,0.0f,1.0f,0.0f);
t += 0.05f;
yoshi.Animate(0,0,0,100);
return TRUE; // Everything Went OK
}
GLvoid KillGLWindow(GLvoid) // Properly Kill The Window
{
if (fullscreen) // Are We In Fullscreen Mode?
{
ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop
ShowCursor(TRUE); // Show Mouse Pointer
}
if (hRC) // Do We Have A Rendering Context?
{
if (!wglMakeCurrent(NULL,NULL)) // Are We Able To Release The DC And RC Contexts?
{
MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC?
{
MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
hRC=NULL; // Set RC To NULL
}
if (hDC && !ReleaseDC(hWnd,hDC)) // Are We Able To Release The DC
{
MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hDC=NULL; // Set DC To NULL
}
if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window?
{
MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hWnd=NULL; // Set hWnd To NULL
}
if (!UnregisterClass("OpenGL",hInstance)) // Are We Able To Unregister Class
{
MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hInstance=NULL; // Set hInstance To NULL
}
}
/* This Code Creates Our OpenGL Window. Parameters Are: *
* title - Title To Appear At The Top Of The Window *
* width - Width Of The GL Window Or Fullscreen Mode *
* height - Height Of The GL Window Or Fullscreen Mode *
* bits - Number Of Bits To Use For Color (8/16/24/32) *
* fullscreenflag - Use Fullscreen Mode (TRUE) Or Windowed Mode (FALSE) */
BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenflag)
{
GLuint PixelFormat; // Holds The Results After Searching For A Match
WNDCLASS wc; // Windows Class Structure
DWORD dwExstyle; // Window Extended style
DWORD dwstyle; // Window style
RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
WindowRect.left=(long)0; // Set Left Value To 0
WindowRect.right=(long)width; // Set Right Value To Requested Width
WindowRect.top=(long)0; // Set Top Value To 0
WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height
fullscreen=fullscreenflag; // Set The Global Fullscreen Flag
hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages
wc.cbClsExtra = 0; // No Extra Window Data
wc.cbWndExtra = 0; // No Extra Window Data
wc.hInstance = hInstance; // Set The Instance
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
wc.hbrBackground = NULL; // No Background Required For GL
wc.lpszMenuName = NULL; // We Don't Want A Menu
wc.lpszClassName = "OpenGL"; // Set The Class Name
if (!RegisterClass(&wc)) // Attempt To Register The Window Class
{
MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
if (fullscreen) // Attempt Fullscreen Mode?
{
DEVMODE dmScreenSettings; // Device Mode
memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared
dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure
dmScreenSettings.dmPelsWidth = width; // Selected Screen Width
dmScreenSettings.dmPelsHeight = height; // Selected Screen Height
dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;
// Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
{
// If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.
if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
{
fullscreen=FALSE; // Windowed Mode Selected. Fullscreen = FALSE
}
else
{
// Pop Up A Message Box Letting User Know The Program Is Closing.
MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
return FALSE; // Return FALSE
}
}
}
if (fullscreen) // Are We Still In Fullscreen Mode?
{
dwExstyle=WS_EX_APPWINDOW; // Window Extended style
dwstyle=WS_POPUP; // Windows style
ShowCursor(FALSE); // Hide Mouse Pointer
}
else
{
dwExstyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended style
dwstyle=WS_OVERLAPPEDWINDOW; // Windows style
}
AdjustWindowRectEx(&WindowRect, dwstyle, FALSE, dwExstyle); // Adjust Window To True Requested Size
// Create The Window
if (!(hWnd=CreateWindowEx( dwExstyle, // Extended style For The Window
"OpenGL", // Class Name
title, // Window Title
dwstyle | // Defined Window style
WS_CLIPSIBLINGS | // Required Window style
WS_CLIPCHILDREN, // Required Window style
0, 0, // Window Position
WindowRect.right-WindowRect.left, // Calculate Window Width
WindowRect.bottom-WindowRect.top, // Calculate Window Height
NULL, // No Parent Window
NULL, // No Menu
hInstance, // Instance
NULL))) // Dont Pass Anything To WM_CREATE
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
bits, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
ShowWindow(hWnd,SW_SHOW); // Show The Window
SetForegroundWindow(hWnd); // Slightly Higher Priority
SetFocus(hWnd); // Sets Keyboard Focus To The Window
ReSizeGLScene(width, height); // Set Up Our Perspective GL Screen
if (!InitGL()) // Initialize Our Newly Created GL Window
{
KillGLWindow(); // Reset The Display
MessageBox(NULL,"Initialization Failed.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return FALSE; // Return FALSE
}
return TRUE; // Success
}
LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window
UINT uMsg, // Message For This Window
WPARAM wParam, // Additional Message Information
LPARAM lParam) // Additional Message Information
{
switch (uMsg) // Check For Windows Messages
{
case WM_ACTIVATE: // Watch For Window Activate Message
{
if (!HIWORD(wParam)) // Check Minimization State
{
active=TRUE; // Program Is Active
}
else
{
active=FALSE; // Program Is No Longer Active
}
return 0; // Return To The Message Loop
}
case WM_SYSCOMMAND: // Intercept System Commands
{
switch (wParam) // Check System Calls
{
case SC_SCREENSAVE: // Screensaver Trying To Start?
case SC_MONITORPOWER: // Monitor Trying To Enter Powersave?
return 0; // Prevent From Happening
}
break; // Exit
}
case WM_CLOSE: // Did We Receive A Close Message?
{
PostQuitMessage(0); // Send A Quit Message
return 0; // Jump Back
}
case WM_KEYDOWN: // Is A Key Being Held Down?
{
keys[wParam] = TRUE; // If So, Mark It As TRUE
return 0; // Jump Back
}
case WM_KEYUP: // Has A Key Been Released?
{
keys[wParam] = FALSE; // If So, Mark It As FALSE
return 0; // Jump Back
}
case WM_SIZE: // Resize The OpenGL Window
{
ReSizeGLScene(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height
return 0; // Jump Back
}
}
// Pass All Unhandled Messages To DefWindowProc
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
int WINAPI WinMain( HINSTANCE hInstance, // Instance
HINSTANCE hPrevInstance, // Previous Instance
LPSTR lpCmdLine, // Command Line Parameters
int nCmdShow) // Window Show State
{
yoshi.LoadModel("horse/horse.md2", "horse/horse.pcx");
yoshi.SetFrame(0);
yoshi.Animation(0,10);
WKLog("\n\n **** START OF NEW LOG ****");
WKLog("World loaded. Ready to kick ass and chew bubble gum.");
MSG msg; // Windows Message Structure
BOOL done=FALSE; // Bool Variable To Exit Loop
fullscreen=TRUE;
// Create Our OpenGL Window
if (!CreateGLWindow("BKS now in 3D",1024,768,16,fullscreen))
{
return 0; // Quit If Window Was Not Created
}
while(!done) // Loop That Runs While done=FALSE
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting?
{
if (msg.message==WM_QUIT) // Have We Received A Quit Message?
{
done=TRUE; // If So done=TRUE
}
else // If Not, Deal With Window Messages
{
TranslateMessage(&msg); // Translate The Message
DispatchMessage(&msg); // Dispatch The Message
}
}
else // If There Are No Messages
{
// Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
if (active) // Program Active?
{
if (keys[VK_ESCAPE] || keys[VK_RETURN]) // Was ESC Pressed?
{
done=TRUE; // ESC Signalled A Quit
}
else // Not Time To Quit, Update Screen
{
DrawGLScene(); // Draw The Scene
SwapBuffers(hDC); // Swap Buffers (Double Buffering)
}
}
if (keys[VK_F1]) // Is F1 Being Pressed?
{
keys[VK_F1]=FALSE; // If So Make Key FALSE
KillGLWindow(); // Kill Our Current Window
fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode
// Recreate Our OpenGL Window
if (!CreateGLWindow("BKS now in 3D",1024,768,16,fullscreen))
{
return 0; // Quit If Window Was Not Created
}
}
}
}
// Shutdown
KillGLWindow(); // Kill The Window
return (msg.wParam); // Exit The Program
}