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

📄 demo.cpp

📁 Cal3D实现虚拟角色 Cal3D实现虚拟角色
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//----------------------------------------------------------------------------//// demo.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.                                                         ////----------------------------------------------------------------------------//#if defined(_MSC_VER) && _MSC_VER <= 0x0600#pragma warning(disable : 4786)#endif//----------------------------------------------------------------------------//// Includes                                                                   ////----------------------------------------------------------------------------//#if defined(_MSC_VER) && _MSC_VER <= 0x0600#pragma warning(disable : 4786)#endif#include "demo.h"#include "tick.h"#include "tga.h"#include "coremodel.h"

///////////////////////////////////////////////////////////////////////////////
////////////////////////////////控制摄像机/////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#include <dinput.h>
int CtlDevice=0;//0:操纵杆,1:键盘
float camera_x=0,camera_y=10,camera_z=1,camera_theta=0,camera_psi=0,camera_gama=0,camera_hfov=45,camera_speed=0.5;
float XX=0,YY=0,ZZ=0,LL=0,RR=0;
LPDIRECTINPUT8       g_pDI;         
LPDIRECTINPUTDEVICE8 g_pJoystick;

void InitDirectInput()//操纵杆初始化
{
    DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION,IID_IDirectInput8, (VOID**)&g_pDI, NULL );
    if(g_pDI->CreateDevice( GUID_Joystick, &g_pJoystick, NULL )!=DI_OK)
	{
		CtlDevice=1;
        return;
	}
	g_pJoystick->SetDataFormat( &c_dfDIJoystick2 );

    DIPROPRANGE diprg; 
    diprg.diph.dwSize       = sizeof(DIPROPRANGE); 
    diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER); 
    diprg.diph.dwHow        = DIPH_BYOFFSET; 
    diprg.diph.dwObj        = DIJOFS_X;
    diprg.lMin              = -1000; 
    diprg.lMax              = +1000; 
    g_pJoystick->SetProperty( DIPROP_RANGE, &diprg.diph );

    diprg.diph.dwSize       = sizeof(DIPROPRANGE); 
    diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER); 
    diprg.diph.dwHow        = DIPH_BYOFFSET; 
    diprg.diph.dwObj        = DIJOFS_Y;
    diprg.lMin              = -1000; 
    diprg.lMax              = +1000; 
    g_pJoystick->SetProperty( DIPROP_RANGE, &diprg.diph );

	//增加(为适应saitek操纵杆)
	diprg.diph.dwSize       = sizeof(DIPROPRANGE); 
    diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER); 
    diprg.diph.dwHow        = DIPH_BYOFFSET; 
    diprg.diph.dwObj        = DIJOFS_Z;
    diprg.lMin              = -1000; 
    diprg.lMax              = 0; 
    g_pJoystick->SetProperty( DIPROP_RANGE, &diprg.diph ); 

    diprg.diph.dwSize       = sizeof(DIPROPRANGE); 
    diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER); 
    diprg.diph.dwHow        = DIPH_BYOFFSET; 
    diprg.diph.dwObj        = DIJOFS_RZ;
    diprg.lMin              = -1000; 
    diprg.lMax              = +1000; 
    g_pJoystick->SetProperty( DIPROP_RANGE, &diprg.diph );

    diprg.diph.dwSize       = sizeof(DIPROPRANGE); 
    diprg.diph.dwHeaderSize = sizeof(DIPROPHEADER); 
    diprg.diph.dwHow        = DIPH_BYOFFSET; 
    diprg.diph.dwObj        = DIJOFS_SLIDER(0);
    diprg.lMin              = -1000; 
    diprg.lMax              = 0; 
    g_pJoystick->SetProperty( DIPROP_RANGE, &diprg.diph ); 

    g_pJoystick->Acquire();
}

//操纵杆退出
void FreeDirectInput()
{
    if(g_pJoystick)g_pJoystick->Unacquire();
    g_pJoystick=NULL;
    g_pDI=NULL;
}

//实现操纵杆控制
void UpdateInputState()
{
	if(CtlDevice==0)
	{
		//获得操纵杆数据
		DIJOYSTATE2 js; 
		g_pJoystick->Poll();
		g_pJoystick->GetDeviceState( sizeof(DIJOYSTATE2), &js);

		//设置死区防去抖动
		if((js.lX>=-20)&&(js.lX<=20))js.lX=0;
		if((js.lY>=-20)&&(js.lY<=20))js.lY=0;
		if((js.lRz>=-20)&&(js.lRz<=20))js.lRz=0;

		//速度系数(修改:根据不同操纵杆不同)
		if(js.lZ==0)
			camera_speed=-js.rglSlider[0]/1000.0;
		else
			camera_speed=-js.lZ/1000.0;

		//前后运动(axis_y)
		camera_x=camera_x+(js.lY*camera_speed/1000.0)*sin(camera_psi/57.3);
		camera_y=camera_y+(-js.lY*camera_speed/1000.0)*cos(camera_psi/57.3);

		//转向运动(axis_x)
		camera_psi=camera_psi-js.lX/1000.0;

		//上下运动(button5,button3)
		if(js.rgbButtons[4]&0x80)camera_z=camera_z+0.1;
		if(js.rgbButtons[2]&0x80)camera_z=camera_z-0.1;

		//限制摄像机高度
		if(camera_z<=0)camera_z=1;

		//俯仰运动(button6,button4)
		if(js.rgbButtons[5]&0x80)camera_theta=camera_theta+0.5;
		if(js.rgbButtons[3]&0x80)camera_theta=camera_theta-0.5;

		//相机倾斜(axis_rz)
		//camera_gama=js.lRz/20.0;

		//调焦运动(button1,button2)
		if(js.rgbButtons[0]&0x80)camera_hfov=camera_hfov-0.5;
		if(js.rgbButtons[1]&0x80)camera_hfov=camera_hfov+0.5;
	}
}
//----------------------------------------------------------------------------//// The one and only Demo instance                                             ////----------------------------------------------------------------------------//Demo theDemo;//----------------------------------------------------------------------------//// Constructors                                                               ////----------------------------------------------------------------------------//
Demo::Demo() : m_strDatapath("data/"), m_strCal3D_Datapath(""){
  m_width = 640;  m_height = 480;  m_bFullscreen = true;  m_fpsDuration = 0.0f;  m_fpsFrames = 0;  m_fps = 0;  m_mouseX = 0;  m_mouseY = 0;  m_bLeftMouseButtonDown = false;  m_bRightMouseButtonDown = false;  m_lastTick = Tick::getTick();  m_bPaused = false;  m_bOutputAverageCPUTimeAtExit = false;}//----------------------------------------------------------------------------//// Destructor                                                                 ////----------------------------------------------------------------------------//Demo::~Demo(){}//----------------------------------------------------------------------------//// Get the demo caption                                                       ////----------------------------------------------------------------------------//std::string Demo::getCaption(){  return "MyCal3D";}//----------------------------------------------------------------------------//// Get the filepath to the data files                                         ////----------------------------------------------------------------------------//std::string Demo::getDatapath(){  return m_strDatapath;}//----------------------------------------------------------------------------//// Get the fullscreen flag                                                    ////----------------------------------------------------------------------------//bool Demo::getFullscreen(){  return m_bFullscreen;}//----------------------------------------------------------------------------//// Get the window height                                                      ////----------------------------------------------------------------------------//int Demo::getHeight(){  return m_height;}//----------------------------------------------------------------------------//// Get the window width                                                       ////----------------------------------------------------------------------------//int Demo::getWidth(){  return m_width;}//----------------------------------------------------------------------------//// Load a texture from a given file                                           ////----------------------------------------------------------------------------//bool Demo::loadTexture(const std::string& strFilename, GLuint& pId){  // initialize the id  pId = 0;  if (stricmp(strrchr(strFilename.c_str(),'.'),".raw")==0)  {  	 // open the texture file  	 std::ifstream file;  	 file.open(strFilename.c_str(), std::ios::in | std::ios::binary);    if(!file)    {      std::cerr << "Texture file '" << strFilename << "' not found." << std::endl;      return false;    }    // read the width, height and depth of the texture    int width;    file.read((char *)&width, 4);    int height;    file.read((char *)&height, 4);    int depth;    file.read((char *)&depth, 4);    // check if an error has happend    if(!file)    {      std::cerr << "Error while readinf from texture file '" << strFilename << "'." << std::endl;      return false;    }    // allocate a temporary buffer to hold the texture data    unsigned char *pBuffer;    pBuffer = new unsigned char[width * height * depth];    if(pBuffer == 0)    {      std::cerr << "Memory allocation for texture file '" << strFilename << "' failed." << std::endl;      return false;    }      // load the texture    file.read((char *)pBuffer, width * height * depth);    file.close();    // generate the texture    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);    glGenTextures(1, &pId);    glBindTexture(GL_TEXTURE_2D, pId);    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);    glTexImage2D(GL_TEXTURE_2D, 0, (depth == 3) ? GL_RGB : GL_RGBA, width, height, 0, (depth == 3) ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, pBuffer);  }  else if (stricmp(strrchr(strFilename.c_str(),'.'),".tga")==0)  {	  CTga *Tga;	  Tga = new CTga();	  if(Tga->ReadFile(strFilename.c_str())==0)	  {		  Tga->Release();		  return false;	  }	  if(Tga->Bpp()!=32)	  {		  Tga->Release();		  return false;	  }     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);     glGenTextures(1, &pId);     glBindTexture(GL_TEXTURE_2D, pId);     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);     	  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Tga->GetSizeX(), Tga->GetSizeY(), 0, GL_RGBA, GL_UNSIGNED_BYTE, Tga->GetPointer() );	  Tga->Release();  }  return true;}//----------------------------------------------------------------------------//// Create the demo                                                            ////----------------------------------------------------------------------------//bool Demo::onCreate(int argc, char *argv[]){
  //初始化操纵杆
  InitDirectInput();
  // show some information  std::cout << "o----------------------------------------------------------------o" << std::endl;  std::cout << "|                      The Cally Demo 2.10.0                     |" << std::endl;  std::cout << "|       (C) 2001, 2002, 2003 Bruno 'Beosil' Heidelberger         |" << std::endl;  std::cout << "o----------------------------------------------------------------o" << std::endl;  std::cout << "| This program is free software; you can redistribute it and/or  |" << std::endl;  std::cout << "| modify it under the terms of the GNU General Public License as |" << std::endl;  std::cout << "| published by the Free Software Foundation; either version 2 of |" << std::endl;  std::cout << "| the License, or (at your option) any later version.            |" << std::endl;  std::cout << "o----------------------------------------------------------------o" << std::endl;  std::cout << std::endl;  // parse the command line arguments  int arg;  for(arg = 1; arg < argc; arg++)  {    // check for fullscreen flag    if(strcmp(argv[arg], "--fullscreen") == 0) m_bFullscreen = true;    // check for window flag    else if(strcmp(argv[arg], "--window") == 0) m_bFullscreen = false;    // check for dimension flag    else if((strcmp(argv[arg], "--dimension") == 0) && (argc - arg > 2))    {      m_width = atoi(argv[++arg]);      m_height = atoi(argv[++arg]);      if((m_width <= 0) || (m_height <= 0))      {        std::cerr << "Invalid dimension!" << std::endl;        return false;      }    }    else if((strcmp(argv[arg], "--data") == 0) && (argc - arg > 1))    {       m_strCal3D_Datapath = argv[++arg];    }    else if((strcmp(argv[arg], "--bench") == 0))		{			 m_bOutputAverageCPUTimeAtExit = true;		}    else    {      std::cerr << "Usage: " << argv[0] << " [--fullscreen] [--window] [--dimension width height] [--data path_to_cal3d_data]" << std::endl;      return false;    }  }  return true;}//----------------------------------------------------------------------------//// Handle an idle event                                                       ////----------------------------------------------------------------------------//void Demo::onIdle(){  // get the current tick value  unsigned int tick;  tick = Tick::getTick();  // calculate the amount of elapsed seconds  float elapsedSeconds;  elapsedSeconds = (float)(tick - m_lastTick) / 1000.0f;  // adjust fps counter  m_fpsDuration += elapsedSeconds;  if(m_fpsDuration >= 1.0f)  {    m_fps = (int)((float)m_fpsFrames / m_fpsDuration);    m_fpsDuration = 0.0f;    m_fpsFrames = 0;  }	static double start;	static double firstTime, lastTime;	start = Tick::getTime();	static bool bFirst = true;	if (bFirst) {		firstTime = start;	}	else {		lastTime = start;	}

⌨️ 快捷键说明

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