//Mis C++ ISO
//Charge le mod鑜e Half-Life, Quake
//MDL Model.CPP
#include <windows.h>
#include <math.h>
#include <stdio.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include "MdlModel.h"
DWORD CurrentTexture = 1;
vec3_t TransformVertices[MAXVERTICES]; // Transformed vertices
vec3_t LightValues[MAXVERTICES]; // Light surface normals
vec3_t *TransformVertPtr;
vec3_t *LightValuesPtr;
vec3_t LightVector; // Light vector in model reference frame
vec3_t BoneLightVector[MAXBONES]; // Light vectors in bone reference frames
long AmbientLightColor; // Ambient world light
float ShadeLight; // Direct world light
vec3_t LightColor;
float BoneTransforms[MAXBONES][3][4]; // Bone transformation matrix
void TMDLModel::Init(char *Filename)
{
char TextureName[256]; //存放纹理名称
char SeqGroupName[256]; //存放骨头名称
Header = LoadModel(Filename);// 导入mdl文件
if (0 == Header->NumTextures)
{
strcpy(TextureName, Filename);
strcpy(&TextureName[strlen(TextureName) - 4], "T.mdl");
TextureHeader = LoadModel(TextureName);
}
else
{
TextureHeader = Header;
}
if (Header->NumSeqGroups > 1)
{
for (long Loop = 1; Loop < Header->NumSeqGroups; Loop++)
{
strcpy(SeqGroupName, Filename);
sprintf(&SeqGroupName[strlen(SeqGroupName) - 4], "%02d.mdl", Loop);
AnimationHeader[Loop] = LoadDemandSequences(SeqGroupName);
}
}
}
void TMDLModel::DrawModel()
{
TransformVertPtr = &TransformVertices[0];
LightValuesPtr = &LightValues[0];
if (0 == Header->NumBodyParts)
{
return;
}
glPushMatrix();
glTranslatef(Origin[0], Origin[1], Origin[2]);
glRotatef(Rotation[1], 0, 0, 1);
glRotatef(Rotation[0], 0, 1, 0);
glRotatef(Rotation[2], 1, 0, 0);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
SetUpBones();
SetupLighting();
for (long Loop = 0; Loop < Header->NumBodyParts; Loop++)
{
SetupModel(Loop);
DrawPoints();
}
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glPopMatrix();
}
void TMDLModel::AdvanceFrame(float Time)
{
tagMDLSeqDescription *SeqDescription = (tagMDLSeqDescription *)((BYTE *)Header +
Header->SequenceOffset) + CurrentSequence;
if (Time > 0.1f)
{
Time = 0.1f;
}
CurrentFrame += Time * SeqDescription->Timing;
if (SeqDescription->NumFrames <= 1)
{
CurrentFrame = 0;
}
else
{
CurrentFrame -= (long)(CurrentFrame / (SeqDescription->NumFrames - 1)) *
(SeqDescription->NumFrames - 1);
}
}
void TMDLModel::ExtractBoundBox(float *Minimums, float *Maximums)
{
tagMDLSeqDescription *SeqDescription = (tagMDLSeqDescription *)((BYTE *)Header +
Header->SequenceOffset);
Minimums[0] = SeqDescription[CurrentSequence].BoundingBoxMinimum[0];
Minimums[1] = SeqDescription[CurrentSequence].BoundingBoxMinimum[1];
Minimums[2] = SeqDescription[CurrentSequence].BoundingBoxMinimum[2];
Maximums[0] = SeqDescription[CurrentSequence].BoundingBoxMaximum[0];
Maximums[1] = SeqDescription[CurrentSequence].BoundingBoxMaximum[1];
Maximums[2] = SeqDescription[CurrentSequence].BoundingBoxMaximum[2];
}
long TMDLModel::SetSequence(long Sequence)
{
if (Sequence > Header->NumSequences)//是否超过帧的数目
{
Sequence = 0;
}
else if (Sequence < 0)
{
Sequence = Header->NumSequences - 1;
}
CurrentSequence = Sequence;
CurrentFrame = 0;
return CurrentSequence;
}
long TMDLModel::GetSequence()
{
return CurrentSequence;
}
void TMDLModel::GetSequenceInfo(float *FrameRate, float *GroundSpeed)
{
tagMDLSeqDescription *SeqDescription = (tagMDLSeqDescription *)((BYTE *)Header +
Header->SequenceOffset) + CurrentSequence;
if (SeqDescription->NumFrames > 1)
{
*FrameRate = 256 * SeqDescription->Timing / (SeqDescription->NumFrames - 1);
*GroundSpeed = (float)sqrt(SeqDescription->LinearMovement[0] *
SeqDescription->LinearMovement[0] + SeqDescription->LinearMovement[1] *
SeqDescription->LinearMovement[1] + SeqDescription->LinearMovement[2] *
SeqDescription->LinearMovement[2]);
*GroundSpeed = *GroundSpeed * SeqDescription->Timing / (SeqDescription->NumFrames - 1);
}
else
{
*FrameRate = 256.0f;
*GroundSpeed = 0.0f;
}
}
float TMDLModel::SetController(long ControllerIndex, float Value)
{
tagMDLBoneController *BoneController =
(tagMDLBoneController *)((BYTE *)Header + Header->BoneControllerOffset);
long Loop;
for (Loop = 0; Loop < Header->NumBoneControllers; Loop++, BoneController++)
{
if (BoneController->Index == ControllerIndex)
{
break;
}
}
if (Loop >= Header->NumBoneControllers)
{
return Value;
}
if (BoneController->Type & (TRANSITION_XR | TRANSITION_YR | TRANSITION_ZR))
{
if (BoneController->End < BoneController->Start)
{
Value = -Value;
}
if (BoneController->Start + 359.0f >= BoneController->End)
{
if (Value > ((BoneController->Start + BoneController->End) / 2.0f) + 180)
{
Value = Value - 360.0f;
}
if (Value < ((BoneController->Start + BoneController->End) / 2.0f) - 180)
{
Value = Value + 360.0f;
}
}
else
{
if (Value > 360.0f)
{
Value = Value - (long)(Value / 360.0f) * 360.0f;
}
else if (Value < 0.0f)
{
Value = Value + (long)((Value / -360.0f) + 1.0f) * 360.0f;
}
}
}
long Setting = (long)(255.0f * (Value - BoneController->Start) / (BoneController->End -
BoneController->Start));
if (Setting < 0)
{
Setting = 0;
}
else if (Setting > 255)
{
Setting = 255;
}
Controller[ControllerIndex] = (BYTE)Setting;
return Setting * (1.0f / 255.0f) * (BoneController->End - BoneController->Start) +
BoneController->Start;
}
float TMDLModel::SetMouth(float Value)
{
tagMDLBoneController *BoneController =
(tagMDLBoneController *)((BYTE *)Header + Header->BoneControllerOffset);
for (long Loop = 0; Loop < Header->NumBoneControllers; Loop++, BoneController++)
{
if (4 == BoneController->Index)
{
break;
}
}
if (BoneController->Type & (TRANSITION_XR | TRANSITION_YR | TRANSITION_ZR))
{
if (BoneController->End < BoneController->Start)
{
Value = -Value;
}
if (BoneController->Start + 359.0f >= BoneController->End)
{
if (Value > ((BoneController->Start + BoneController->End) / 2.0f) + 180.0f)
{
Value = Value - 360.0f;
}
if (Value < ((BoneController->Start + BoneController->End) / 2.0f) - 180.0f)
{
Value = Value + 360.0f;
}
}
else
{
if (Value > 360.0f)
{
Value = Value - (long)(Value / 360.0f) * 360.0f;
}
else if (Value < 0.0f)
{
Value = Value + (long)((Value / -360.0f) + 1.0f) * 360.0f;
}
}
}
long Setting = (long)(64.0f * (Value - BoneController->Start) / (BoneController->End -
BoneController->Start));
if (Setting < 0)
{
Setting = 0;
}
if (Setting > 64)
{
Setting = 64;
}
MouthPosition = (BYTE)Setting;
return Setting * (1.0f / 64.0f) * (BoneController->End - BoneController->Start) +
BoneController->Start;
}
float TMDLModel::SetBlending(long Blender, float Value)
{
tagMDLSeqDescription *SeqDescription =
(tagMDLSeqDescription *)((BYTE *)Header + Header->SequenceOffset) + CurrentSequence;
if (0 == SeqDescription->BlendType[Blender])
{
return Value;
}
if (SeqDescription->BlendType[Blender] & (TRANSITION_XR | TRANSITION_YR | TRANSITION_ZR))
{
if (SeqDescription->BlendEnd[Blender] < SeqDescription->BlendStart[Blender])
{
Value = -Value;
}
if (SeqDescription->BlendStart[Blender] + 359.0f >= SeqDescription->BlendEnd[Blender])
{
if (Value > ((SeqDescription->BlendStart[Blender] +
SeqDescription->BlendEnd[Blender]) / 2.0f) + 180.0f)
{
Value = Value - 360.0f;
}
if (Value < ((SeqDescription->BlendStart[Blender] +
Seq
OPengl.rar_opengl 漫游
版权申诉
182 浏览量
2022-09-20
18:55:13
上传
评论
收藏 6.37MB RAR 举报
邓凌佳
- 粉丝: 65
- 资源: 1万+
最新资源
- Screenshot_20240522_084328_com.tencent.mm.jpg
- 附件计算机专业课选课说明-1.xlsx
- 基于TypeScript的ahousepet-admin-web管理系统设计源码
- 《广东开放大学学习指引》期末考核要求0522.zip
- mkl-2021.1.1-py2.py3-none-macosx-10-15-x86-64.whl
- mkl-2018.0.3-py2.py3-none-manylinux1-x86-64.whl
- halcon实现两幅图像相减
- mkl-2021.3.0-py2.py3-none-manylinux1-x86-64.whl
- IAR9.3主题配置,包含字体颜色等
- 才从简来.apk
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈