2016-04-27 15 views
0

我是一个带有gameplay3d的新手,并且经历了所有教程,但是我无法设法显示从Fbx编码的这个简单(不是多边多边形和材质)模型。我用unity3D检查了这个模型,而使用gameplay3d的封闭源代码软件似乎都很好。我想我错过了一些加载场景的细节。
这是包含原始fbx文件的模型文件。我怀疑它是否与光线有关 https://www.dropbox.com/sh/ohgpsfnkm3iv24s/AACApRcxwtbmpKu4_5nnp8rZa?dl=0 这是加载场景的类。GamePlay3d引擎不会显示从fbx导入的模型

#include "Demo.h" 

// Declare our game instance 
Demo game; 

Demo::Demo() 
    : _scene(NULL), _wireframe(false) 
{ 
} 

void Demo::initialize() 
{ 
    // Load game scene from file 
    Bundle* bundle = Bundle::create("KGN56AI30N.gpb"); 
    _scene = bundle->loadScene(); 
    SAFE_RELEASE(bundle); 
    // Get the box model and initialize its material parameter values and bindings 

    Camera* camera = Camera::createPerspective(45.0f, getAspectRatio(), 1.0f, 20.0f); 
    Node* cameraNode = _scene->addNode("camera"); 

    // Attach the camera to a node. This determines the position of the camera. 
    cameraNode->setCamera(camera); 

    // Make this the active camera of the scene. 
    _scene->setActiveCamera(camera); 
    SAFE_RELEASE(camera); 
    // Move the camera to look at the origin. 
    cameraNode->translate(0,0, 10); 
    cameraNode->rotateX(MATH_DEG_TO_RAD(0.25f)); 
    // Update the aspect ratio for our scene's camera to match the current device resolution 
    _scene->getActiveCamera()->setAspectRatio(getAspectRatio()); 
    // Set the aspect ratio for the scene's camera to match the current resolution 
    _scene->getActiveCamera()->setAspectRatio(getAspectRatio()); 

    Light* directionalLight = Light::createDirectional(Vector3::one()); 
    _directionalLightNode = Node::create("directionalLight"); 
    _directionalLightNode->setLight(directionalLight); 
    SAFE_RELEASE(directionalLight); 
    _scene->addNode(_directionalLightNode); 
    _scene->setAmbientColor(1.0, 1.0, 1.0); 
    _scene->visit(this, &Demo::initializeMaterials); 

} 


bool Demo::initializeMaterials(Node* node) 
{ 
     Model* model = dynamic_cast<Model*>(node->getDrawable()); 

     if (model) 
     { 
      for(int i=0;i<model->getMeshPartCount();i++) 
      { 
      Material* material = model->getMaterial(i); 
      if(material) 
      { 
      // For this sample we will only bind a single light to each object in the scene. 
      MaterialParameter* colorParam = material->getParameter("u_directionalLightColor[0]"); 
      colorParam->setValue(Vector3(0.75f, 0.75f, 0.75f)); 
      MaterialParameter* directionParam = material->getParameter("u_directionalLightDirection[0]"); 
      directionParam->setValue(Vector3(1, 1, 1)); 
      } 
      } 
     } 
     return true; 
} 

void Demo::finalize() 
{ 
    SAFE_RELEASE(_scene); 
} 

void Demo::update(float elapsedTime) 
{ 
    // Rotate model 
    //_scene->findNode("box")->rotateY(MATH_DEG_TO_RAD((float)elapsedTime/1000.0f * 180.0f)); 
} 

void Demo::render(float elapsedTime) 
{ 
    // Clear the color and depth buffers 
    clear(CLEAR_COLOR_DEPTH, Vector4::zero(), 1.0f, 0); 

    // Visit all the nodes in the scene for drawing 
    _scene->visit(this, &Demo::drawScene); 
} 

bool Demo::drawScene(Node* node) 
{ 
    // If the node visited contains a drawable object, draw it 
    Drawable* drawable = node->getDrawable(); 
    if (drawable) 
     drawable->draw(_wireframe); 

    return true; 
} 

void Demo::keyEvent(Keyboard::KeyEvent evt, int key) 
{ 
    if (evt == Keyboard::KEY_PRESS) 
    { 
     switch (key) 
     { 
     case Keyboard::KEY_ESCAPE: 
      exit(); 
      break; 
     } 
    } 
} 

void Demo::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex) 
{ 
    switch (evt) 
    { 
    case Touch::TOUCH_PRESS: 
     _wireframe = !_wireframe; 
     break; 
    case Touch::TOUCH_RELEASE: 
     break; 
    case Touch::TOUCH_MOVE: 
     break; 
    }; 
} 

回答

0

我无法下载您的dropbox .fbx文件。你有多少模特在现场?这里有一个简单的方法来做你想做的事 - 不是最优的,但它会让你开始...

所以首先,我看不到在你的代码中你实际分配了一个着色器与材料一起使用。我用的是这样的:

material = model->setMaterial("Shaders/Animation/ADSVertexViewAnim.vsh", "Shaders/Animation/ADSVertexViewAnim.fsh"); 

您需要分配着色器,和上面的代码将采取顶点和片段着色器和使用时需要绘制的对象。

我对它做了一些略微不同的处理,不自动加载场景文件,而是创建一个空场景,然后从包中提取模型并手动添加到场景中。这样,我可以确切地看到发生了什么,并且我掌控着每一步。 GamePlay3D有一些奇特的属性文件,但只有在知道该过程如何手动运行时才使用它们。

最初,我在场景中创建了一个简单的多维数据集,并手动创建了一个场景,并将猴子添加到节点图,如下所示:

void GameMain::ExtractFromBundle() 
{ 
    /// Create a new empty scene. 
    _scene = Scene::create(); 

    // Create the Model and its Node 
    Bundle* bundle = Bundle::create("res/monkey.gpb");   // Create the bundle from GPB file 

    /// Create the Cube 
    { 
     Mesh* meshMonkey = bundle->loadMesh("Character_Mesh");    // Load the mesh from the bundle 
     Model* modelMonkey = Model::create(meshMonkey); 
     Node* nodeMonkey = _scene->addNode("Monkey"); 
     nodeMonkey->setTranslation(0,0,0); 
     nodeMonkey->setDrawable(modelMonkey); 
    } 
} 

然后我想要搜索场景图并只给我要绘制的对象(猴子)分配一个材质。如果你想指定不同的材质手工不同的对象使用此...

bool GameMain::initializeScene(Node* node) 
{ 
    Material* material; 

    std::cout << node->getId() << std::endl; 

    // find the node in the scene 
    if (strcmp(node->getId(), "Monkey") != 0) 
     return false; 

    Model* model = dynamic_cast<Model*>(node->getDrawable()); 
    if(!model) 
     return false; 

    material = model->setMaterial("Shaders/Animation/ADSVertexViewAnim.vsh", "Shaders/Animation/ADSVertexViewAnim.fsh"); 

    material->getStateBlock()->setCullFace(true); 
    material->getStateBlock()->setDepthTest(true); 
    material->getStateBlock()->setDepthWrite(true); 

    // The World-View-Projection Matrix is needed to be able to see view the 3D world thru the camera 
    material->setParameterAutoBinding("u_worldViewProjectionMatrix", "WORLD_VIEW_PROJECTION_MATRIX"); 

    // This matrix is necessary to calculate normals properly, but the WORLD_MATRIX would also work 
    material->setParameterAutoBinding("u_worldViewMatrix", "WORLD_VIEW_MATRIX"); 
    material->setParameterAutoBinding("u_viewMatrix", "VIEW_MATRIX"); 

    return true; 
} 

现在的对象是准备要绘制....所以我使用这些功能:

void GameMain::render(float elapsedTime) 
{ 
    // Clear the color and depth buffers 
    clear(CLEAR_COLOR_DEPTH, Vector4(0.0, 0.0, 0.0, 0.0), 1.0f, 0); 

    // Visit all the nodes in the scene for drawing 
    _scene->visit(this, &GameMain::drawScene); 
} 

bool GameMain::drawScene(Node* node) 
{ 
    // If the node visited contains a drawable object, draw it 
    Drawable* drawable = node->getDrawable(); 
    if (drawable) 
     drawable->draw(_wireframe); 

    return true; 
} 

我用我自己的着色器,所以我不必担心Light and DirectionalLight以及所有这些东西。一旦我能看到对象,然后我将添加动态灯光等,但对于初学者来说,开始简单。

问候。