⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 model.cpp

📁 Cal3D实现虚拟角色 Cal3D实现虚拟角色
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//----------------------------------------------------------------------------//// model.cpp                                                                  //// Copyright (C) 2001 Bruno 'Beosil' Heidelberger                             ////----------------------------------------------------------------------------//// 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.                                                         ////----------------------------------------------------------------------------////----------------------------------------------------------------------------//// Includes                                                                   ////----------------------------------------------------------------------------//#if defined(_MSC_VER) && _MSC_VER <= 0x0600#pragma warning(disable : 4786)#endif#include "tga.h"
#include "model.h"
//----------------------------------------------------------------------------//
// Constructors                                                               //
//----------------------------------------------------------------------------//

Model::Model(CalCoreModel* m_coreModel)
{
    m_calModel = new CalModel(m_coreModel);

    // attach all meshes to the model
    int meshId;
    for(meshId = 0; meshId < m_coreModel->getCoreMeshCount(); meshId++)
	{
        m_calModel->attachMesh(meshId);
	}

    // set the material set of the whole model
    m_calModel->setMaterialSet(0);

    m_bSkeleton=0;
    m_bWireframe=false;
    m_bLight=true;

  	x=0.0;
	y=0.0;
	z=0.0;
	theta=0.0;
	psi=0.0;
	gama=0.0;
	sx=1.0;
	sy=1.0;
	sz=1.0;
}


//----------------------------------------------------------------------------//
// Destructor                                                                 //
//----------------------------------------------------------------------------//

Model::~Model()
{
	delete  m_calModel;
}
//----------------------------------------------------------------------------//
// Set a new animation state within a given delay                             //
//----------------------------------------------------------------------------//

void Model::setState(int state, float delay)
{
  // check if this is really a new state
  if(state != m_state)
  {
    if(state == 0)
    {
      m_calModel->getMixer()->blendCycle(0, 1.0f, delay);
      m_calModel->getMixer()->clearCycle(1, delay);
      m_calModel->getMixer()->clearCycle(2, delay);
      m_state = 0;
    }
    else if(state == 1)
    {
      m_calModel->getMixer()->clearCycle(0, delay);
      m_calModel->getMixer()->blendCycle(1, 1.0f, delay);
      m_calModel->getMixer()->clearCycle(2, delay);
      m_state = 1;
    }
    else if(state == 2)
    {
      m_calModel->getMixer()->clearCycle(0, delay);
      m_calModel->getMixer()->clearCycle(1, delay);
      m_calModel->getMixer()->blendCycle(2, 1.0f, delay);
      m_state = 2;
    }
  }
}


//----------------------------------------------------------------------------//
// Get the animation state of the model                                       //
//----------------------------------------------------------------------------//

int Model::getState()
{
  return m_state;
}

//----------------------------------------------------------------------------//// Set the lod level of the model                                             ////----------------------------------------------------------------------------//void Model::setLodLevel(float lodLevel){  m_lodLevel = lodLevel;  // set the new lod level in the cal model renderer  m_calModel->setLodLevel(m_lodLevel);}

//----------------------------------------------------------------------------//
// Get the lod level of the model                                             //
//----------------------------------------------------------------------------//

float Model::getLodLevel()
{
  return m_lodLevel;
}

//----------------------------------------------------------------------------//// Set the motion blend factors state of the model                            ////----------------------------------------------------------------------------//void Model::setMotionBlend(float *pMotionBlend, float delay){  m_motionBlend[0] = pMotionBlend[0];  m_motionBlend[1] = pMotionBlend[1];  m_motionBlend[2] = pMotionBlend[2];/*  m_calModel->getMixer()->clearCycle(m_animationId[STATE_IDLE], delay);  m_calModel->getMixer()->clearCycle(m_animationId[STATE_FANCY], delay);  m_calModel->getMixer()->blendCycle(m_animationId[STATE_MOTION], m_motionBlend[0], delay);  m_calModel->getMixer()->blendCycle(m_animationId[STATE_MOTION + 1], m_motionBlend[1], delay);  m_calModel->getMixer()->blendCycle(m_animationId[STATE_MOTION + 2], m_motionBlend[2], delay);  m_state = STATE_MOTION;*/}

//----------------------------------------------------------------------------//
// Get the motion blend factors state of the model                            //
//----------------------------------------------------------------------------//

void Model::getMotionBlend(float *pMotionBlend)
{
  pMotionBlend[0] = m_motionBlend[0];
  pMotionBlend[1] = m_motionBlend[1];
  pMotionBlend[2] = m_motionBlend[2];
}

//----------------------------------------------------------------------------//
// Execute an action of the model                                             //
//----------------------------------------------------------------------------//

void Model::executeAction(int action)
{
	m_calModel->getMixer()->executeAction(action, 0.3f, 0.3f);
}


//----------------------------------------------------------------------------//
// Render the model                                                           //
//----------------------------------------------------------------------------//

void Model::onRender()
{
  // set global OpenGL states
  glShadeModel(GL_SMOOTH);

  m_calModel->getSkeleton()->calculateBoundingBoxes();

  //设置摄像机
  cam.Setup(cam.fovy,cam.aspect,cam.znear,cam.zfar,cam.vx,cam.vy,cam.width,cam.height);
  cam.Place(cam.x,cam.y,cam.z,cam.theta,cam.psi,cam.gama);

  //设置虚拟人
  Place(x,y,z,theta,psi,gama);
  Setup(sx,sy,sz);

  // check if we need to render the skeleton

  if(m_bSkeleton==1)
  {
    renderSkeleton();
  }
  else if(m_bSkeleton==2)
  {
    renderBoundingBox();
  }
  
  // check if we need to render the mesh
  if(m_bSkeleton==0 || m_bWireframe==true)
  {
    renderMesh(m_bWireframe, m_bLight);
  }
}


//----------------------------------------------------------------------------//
// Update the model                                                           //
//----------------------------------------------------------------------------//

void Model::onUpdate(float elapsedSeconds)
{
  // update the model
  m_calModel->update(elapsedSeconds);
}


//----------------------------------------------------------------------------//
// Render the skeleton of the model                                           //
//----------------------------------------------------------------------------//

void Model::renderSkeleton()
{
  // draw the bone lines
  float lines[1024][2][3];
  int nrLines;
  nrLines =  m_calModel->getSkeleton()->getBoneLines(&lines[0][0][0]);

  glLineWidth(3.0f);
  glColor3f(1.0f, 1.0f, 1.0f);
  glBegin(GL_LINES);
    int currLine;
    for(currLine = 0; currLine < nrLines; currLine++)
    {
      glVertex3f(lines[currLine][0][0], lines[currLine][0][1], lines[currLine][0][2]);
      glVertex3f(lines[currLine][1][0], lines[currLine][1][1], lines[currLine][1][2]);
    }
  glEnd();
  glLineWidth(1.0f);

  // draw the bone points
  float points[1024][3];
  int nrPoints;
  nrPoints =  m_calModel->getSkeleton()->getBonePoints(&points[0][0]);

  glPointSize(4.0f);
  glBegin(GL_POINTS);
    glColor3f(0.0f, 0.0f, 1.0f);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -