2016-04-27 39 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以及所有這些東西。一旦我能看到對象,然後我將添加動態燈光等,但對於初學者來說,開始簡單。

問候。