PSP程序开发例子解析(九)3DPrimer

阅读更多
3D例子显示一个Cube和一个人物 不断旋转 具体底部底层API没有列出 只注视了每个语句的含义 显示了3D的大概逻辑流程
#include 


#include 
#include 
#include 
#include 
#include 

#include "GameApp.h"

#include 


GameApp::GameApp()
{
mTexture = NULL;
mBgTex = NULL;
mBg = NULL;

mPlayer = NULL;
mGun = NULL;
}


GameApp::~GameApp()
{
}


void GameApp::Create()
{
mAngle = 0.0f;//角度?
mState = STATE_IDLE;//2D动画状态

JRenderer* renderer = JRenderer::GetInstance();
mTexture = renderer->LoadTexture("box.png");//方块图片 单面
mBgTex = renderer->LoadTexture("bg.png");//背景
mBg = new JQuad(mBgTex, 0, 0, 480, 272);//背景

mPlayer = new JMD2Model();
mPlayer->Load("tris.md2", "abarlith.png");   // load a MD2 character加载人物3D模型

mGun = new JMD2Model();
mGun->Load("weapon.md2", "weapon.png");    // load the character's weapon加载武器3D模型

if (mPlayer != NULL && mGun != NULL)
{
   mPlayer->SetState(mState);//设置状态
   mGun->SetState(mState);
}

renderer->SetFOV(60.0f);//设置视场

}


void GameApp::Destroy()
{

SAFE_DELETE(mTexture);
SAFE_DELETE(mBgTex);
SAFE_DELETE(mBg);
SAFE_DELETE(mPlayer);
SAFE_DELETE(mGun);

}


void GameApp::Update()
{

JGE* engine = JGE::GetInstance();

if (engine->GetButtonClick(PSP_CTRL_TRIANGLE))   // do a screen shot when the TRIANGLE button is pressed
{
   char s[80];
   sprintf(s, "ms0:/screenshot.png");
   JRenderer::GetInstance()->ScreenShot(s);
}

if (engine->GetButtonClick(PSP_CTRL_CROSS))    // exit when the CROSS button is pressed
{
   engine->End();
   return;
}


int mNewState = mState;//改变状态上和左键 状态-1
if (engine->GetButtonClick(PSP_CTRL_UP) || engine->GetButtonClick(PSP_CTRL_LEFT))
{
   mNewState--;
   if (mNewState < STATE_IDLE)
    mNewState = STATE_FALING_FORWARD_DYING;

}
//改变状态下和右键状态+1
else if (engine->GetButtonClick(PSP_CTRL_DOWN) || engine->GetButtonClick(PSP_CTRL_RIGHT))
{
   mNewState++;
   if (mNewState > STATE_FALING_FORWARD_DYING)
    mNewState = STATE_IDLE;

}

if (mNewState != mState && mPlayer != NULL && mGun != NULL)
{
   mState = mNewState;
   mPlayer->SetState(mState);//设置人物的状态
   mGun->SetState(mState);//设置枪的状态
   /* 人物的状态枚举 每个状态对应一个人物动作 比如跑步 走动 跳跃 下蹲 应该在如3DMax软件中定义 生成md2模型文件和切片图片
    STATE_IDLE,
    STATE_RUNNING,
    STATE_SHOT_NOT_FALLING_DOWN,
    STATE_SHOT_IN_SHOULDER,
    STATE_JUMP,
    STATE_IDLE2,
    STATE_SHOT_FALLING_DOWN,
    STATE_IDLE3,
    STATE_IDLE4,
    STATE_CROUCHING,
    STATE_CROUCHING_CRAWL,
    STATE_IDLE_CROUCHING,
    STATE_KNEELING_DYING,
    STATE_FALLING_BACK_DYING,
    STATE_FALING_FORWARD_DYING,
    STATE_FALLING_BACK_SLOWLY_DYING
   */
}

float dt = engine->GetDelta(); // get number of milliseconds passed since last frame

mAngle += 1.0f*dt;     // the frame rate is not fixed by default, so we use dt to do adjustments
if (mAngle > M_PI*2.0f)    // angles are in radian, so 2 PI is one full circle
   mAngle = 0.0f;
//angles是弧度 这个定义不明确 2Pi是一圈??数学基础= =!
mPlayer->Update(dt);    // update model animation
mGun->Update(dt);//更新人物的图片和枪的图片动画

}


void GameApp::Render()
{

JRenderer* renderer = JRenderer::GetInstance();

renderer->Enable2D();//开启2D功能
renderer->RenderQuad(mBg, 0, 0);//画背景

renderer->Enable3D();//开启3D模型功能

renderer->PushMatrix();//矩阵?
renderer->Translate(30.0f, 5.0f, -60.0f);//视角? 参数分别是 X,Y,Z坐标 从x,y,z点看模型的视角 z越大model越小x,y是横向的偏移
renderer->RotateX(-M_PI_2);//X轴旋转-Pi/2 应该是90度 默认的人物头朝向屏幕 90度后人物站起来了 adjust the model to the right orientation
renderer->RotateZ(mAngle);//Z轴旋转运行时的角度 让人物循环旋转

mPlayer->Render();//画人物和枪 render the model 内部实现略
mGun->Render();

   renderer->PopMatrix();//Pop矩阵?


Vertex3D tris[] =// define a 3D cube 定义3D方块的顶点Vertex矩阵 
{
   // float u, v; float x, y, z;
   //u?v? x,y,x后三个是矩阵的坐标
   // top
   { 0.0f, 0.0f, -5.0f, 5.0f, 5.0f },  
   { 1.0f, 0.0f, 5.0f, 5.0f, 5.0f },
   { 1.0f, 1.0f, 5.0f, 5.0f, -5.0f },

   { 1.0f, 1.0f, 5.0f, 5.0f, -5.0f },
   { 0.0f, 1.0f, -5.0f, 5.0f, -5.0f },
   { 0.0f, 0.0f, -5.0f, 5.0f, 5.0f },

   // front
   { 0.0f, 0.0f, 5.0f, -5.0f, 5.0f },  
   { 1.0f, 0.0f, 5.0f, 5.0f, 5.0f },
   { 1.0f, 1.0f, -5.0f, 5.0f, 5.0f },

   { 1.0f, 1.0f, -5.0f, 5.0f, 5.0f },
   { 0.0f, 1.0f, -5.0f, -5.0f, 5.0f },
   { 0.0f, 0.0f, 5.0f, -5.0f, 5.0f },

   // right
   { 0.0f, 0.0f, 5.0f, 5.0f, -5.0f },
   { 1.0f, 0.0f, 5.0f, 5.0f, 5.0f },
   { 1.0f, 1.0f, 5.0f, -5.0f, 5.0f },
  
   { 1.0f, 1.0f, 5.0f, -5.0f, 5.0f },
   { 0.0f, 1.0f, 5.0f, -5.0f, -5.0f },
   { 0.0f, 0.0f, 5.0f, 5.0f, -5.0f },

   // left
   { 0.0f, 0.0f, -5.0f, -5.0f, 5.0f },
   { 1.0f, 0.0f, -5.0f, 5.0f, 5.0f },
   { 1.0f, 1.0f, -5.0f, 5.0f, -5.0f },

   { 1.0f, 1.0f, -5.0f, 5.0f, -5.0f },
   { 0.0f, 1.0f, -5.0f, -5.0f, -5.0f },
   { 0.0f, 0.0f, -5.0f, -5.0f, 5.0f },

   // bottom
   { 0.0f, 0.0f, 5.0f, -5.0f, 5.0f },
   { 1.0f, 0.0f, -5.0f, -5.0f, 5.0f },
   { 1.0f, 1.0f, -5.0f, -5.0f, -5.0f },

   { 1.0f, 1.0f, -5.0f, -5.0f, -5.0f },
   { 0.0f, 1.0f, 5.0f, -5.0f, -5.0f },
   { 0.0f, 0.0f, 5.0f, -5.0f, 5.0f },
  
   // back
   { 0.0f, 0.0f, 5.0f, 5.0f, -5.0f },
   { 1.0f, 0.0f, 5.0f, -5.0f, -5.0f },
   { 1.0f, 1.0f, -5.0f, -5.0f, -5.0f },

   { 1.0f, 1.0f, -5.0f, -5.0f, -5.0f },
   { 0.0f, 1.0f, -5.0f, 5.0f, -5.0f },
   { 0.0f, 0.0f, 5.0f, 5.0f, -5.0f }
};

renderer->PushMatrix();//又矩阵?没找到API的说明 调用的win的底层函数= =!
renderer->Translate(-15.0f, 0.0f, -30.0f);
renderer->RotateX(mAngle);//跟随程序运行着的角度旋转XYZ轴
renderer->RotateY(-mAngle);
renderer->RotateZ(mAngle);
renderer->RenderTriangles(mTexture, tris, 0, 12);//绘制cube 第一个参数是单面图片 第二个参数各个点坐标 0?12? render the 3D cube
renderer->PopMatrix();//又Pop 干什么用的?

}


void GameApp::Pause()
{

}


void GameApp::Resume()
{

}

 

你可能感兴趣的:(UP)