Help, 3D Cube isn't drawn

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
odenter
Posts: 48
Joined: Thu Oct 21, 2004 10:35 am
Location: Bremen (Germany)

Help, 3D Cube isn't drawn

Post by odenter »

First, my english in writing isn't perfect.

I've a plane, with one texture and 3 cube boxes on it, but the third box (cube3) isn't drawn.

Here's my code. Any advice?

Code: Select all

#include "Application.h"

Application::Application(void) {
  _zoomFactor = 400;
}

Application::~Application(void) {
}

void Application::initGfx() {
  _device = createDevice(video::EDT_OPENGL, dimension2d<u32>(1024, 768), 16, false, false, false, this);
  _device->setWindowCaption(L"Isometric Projection");
  _driver = _device->getVideoDriver();
  _smgr = _device->getSceneManager();

}

void Application::Run() {
  this->initGfx();

  this->getTestTerrainMesh();

  //_camera = _smgr->addCameraSceneNodeFPS();
  _camera = _smgr->addCameraSceneNode();


  _testTexture = _driver->getTexture("cube.jpg");
  IMeshSceneNode *cube1 = _smgr->addCubeSceneNode(10, 0, -1, vector3df(0, 0, 0), vector3df(0, 0, 0), vector3df(1,1,1));
  cube1->setMaterialTexture(0, _testTexture);
  cube1->setMaterialFlag(video::EMF_LIGHTING, false);

  IMeshSceneNode *cube2 = _smgr->addCubeSceneNode(10, 0, -1, vector3df(5, 5, 0), vector3df(0, 0, 0), vector3df(1,1,1));
  cube2->setMaterialTexture(0, _testTexture);
  cube2->setMaterialFlag(video::EMF_LIGHTING, false);

  IMeshSceneNode *cube3 = _smgr->addCubeSceneNode(10, 0, -1, vector3df(10, 10, 0), vector3df(0, 0, 0), vector3df(1,1,1));
  cube3->setMaterialTexture(0, _testTexture);
  cube3->setMaterialFlag(video::EMF_LIGHTING, false);


  while(_device->run()) {
    this->Render();
  }
  _device->drop();
}

bool Application::OnEvent(const irr::SEvent &event) {
  if (event.EventType == irr::EET_MOUSE_INPUT_EVENT) {
    if (event.MouseInput.Event == irr::EMOUSE_INPUT_EVENT::EMIE_MOUSE_WHEEL) {
      return this->OnMouseWheelEvent(event);
    }
  }
  return false;
}

bool Application::OnMouseWheelEvent(const irr::SEvent &event) {
  
  if (event.MouseInput.Wheel > 0) {
    _zoomFactor -= event.MouseInput.Wheel * 10;
  } else {
    _zoomFactor += (event.MouseInput.Wheel * -1) * 10;
  }
  
  return true;
}


void Application::Render() {
  _driver->beginScene(true, true, SColor(255, 0, 0, 255));
  _smgr->drawAll();

  matrix4 projMatrix;
  projMatrix.makeIdentity();
  projMatrix[0] = 1.0f;
  projMatrix[8] = -1.0f;
  projMatrix[1] = 0.5f;
  projMatrix[5] = 1.0f;
  projMatrix[9] = 0.5f;
  projMatrix[10] = 0.0;

  f32 scale = _zoomFactor;
  matrix4 orthoMatrix;
  orthoMatrix.buildProjectionMatrixOrthoLH(2 * scale, 2 * scale ,-1 * scale, 1 * scale);
  orthoMatrix = orthoMatrix * projMatrix;
  _camera->setProjectionMatrix(orthoMatrix, true);

  _driver->endScene();
}

IAnimatedMeshSceneNode* Application::getTestTerrainMesh() {
  IAnimatedMesh* terrain = _smgr->addHillPlaneMesh("ground_plane",
                                                   dimension2d<f32>( 100.0f, 100.0f ),   // tile size
                                                   dimension2d<u32>( 128, 128 ),         // tile count
                                                   0, 
                                                   0.0f,
                                                   dimension2d<f32>( 0.0f, 0.0f ),
                                                   dimension2d<f32>( 64.0f, 64.0f )      // texture repeats
                                                   );
  ITexture* texture1 = _driver->getTexture("terrain_grass.jpg"); 
  IAnimatedMeshSceneNode* terrain_node = _smgr->addAnimatedMeshSceneNode(terrain);
  terrain_node->setPosition( vector3df( 0.0f, 0.0f, 0.0f ) );
  terrain_node->setMaterialTexture( 0, texture1 );
  terrain_node->setMaterialFlag( video::EMF_LIGHTING, false );
  terrain_node->setMaterialFlag( video::EMF_ANISOTROPIC_FILTER, true ); 
  return terrain_node;
}
Last edited by odenter on Fri Jan 07, 2011 4:54 pm, edited 1 time in total.
Alpha Omega
Posts: 288
Joined: Wed Oct 29, 2008 12:07 pm

Post by Alpha Omega »

How far away is vector3df(15,15,0) away from your plane and the rest of the cubes?
odenter
Posts: 48
Joined: Thu Oct 21, 2004 10:35 am
Location: Bremen (Germany)

Post by odenter »

I've uploaded a few pics, each 160k.

http://www.bilderload.com/bild/78404/1cubePDCS5.jpg
http://www.bilderload.com/bild/78405/2cubeYVRB9.jpg
http://www.bilderload.com/bild/78406/3cubeCGNA3.jpg

The second cube is correct behind the first. The third should be behind the second.

Plane is at 0,0,0
1st cube is at 0,0,0
2nd cube is at 5, 5, 0
3rd cube is at 10, 10, 0
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Why do you play around with the projection matrices, and why would you do that inside the render loop?
odenter
Posts: 48
Joined: Thu Oct 21, 2004 10:35 am
Location: Bremen (Germany)

Post by odenter »

I love games like Jagged Alliance, so the idea is first to create a map/terrain and render it. Did this with SDL and it works fine, with correct sprites, but I'm not an artist so get a free Model is easier (for me) than to create a sprite.
Now I tried this with real 3D graphics, to zoom, rotate etc. without the need for special sprites.

And I do this in my render loop, because it's just a test, could simple be switched.
http://www.irrlicht3d.org/wiki/index.ph ... ohnFredCee
odenter
Posts: 48
Joined: Thu Oct 21, 2004 10:35 am
Location: Bremen (Germany)

Post by odenter »

I think I knew what i've been doing wrong.

I've written a small test. I took sprite class from here http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=27015.

Current screenshot
http://www.bilder-space.de/show_img.php ... e=original

But I think now I've to write my own camera, which scrolls (left,right, up, down) and zooms in/out. To get an "isometric" 2d game.

Current testcode

Code: Select all

#pragma comment(lib, "irrlicht.lib")

#include "global.h"
#include "irrlichthelper.h"
#include "spritescenenode.h"
#include <map>

void render() {
  IrrlichtHelper::Driver()->beginScene(true, true, SColor(255, 0, 0, 255));
  IrrlichtHelper::Smgr()->drawAll();
  IrrlichtHelper::Driver()->endScene();
}


int main() {
  if (!IrrlichtHelper::Init(1024, 768, 16, false, 0)) {
    return -1;
  }

  ICameraSceneNode *_camera = IrrlichtHelper::Smgr()->addCameraSceneNodeFPS();
  _camera->setPosition(vector3df(0.f, 0.f, 500.f));
  _camera->setTarget(vector3df(0,0,0));

  s32 sizeX = 4;
  s32 sizeY = 4;
  std::map<s32, std::map<s32, SpriteSceneNode*>> _map;
  for(int x = 0; x < sizeX; x++) {
    for (int y = 0; y < sizeY; y++) {
      SpriteSceneNode *node = new SpriteSceneNode(dimension2df(64.f, 32.f), "./data/terrain1.bmp",
                                     EMT_TRANSPARENT_ALPHA_CHANNEL_REF, true,
                                     IrrlichtHelper::Driver(),
                                     IrrlichtHelper::Smgr()->getRootSceneNode(), IrrlichtHelper::Smgr());
      int plotX = 0;
      int plotY = 0;

      if ((y % 2) == 0) {
        plotX = x * 64;
        plotY = y * 15;
      } else {
        plotX = x * 64 + (64 / 2);
        plotY = y * 15;
      }
 
      node->setPosition(vector3df(plotX, plotY, 0));
      _map[x][y] = node;
    }
  }

  u32 frames=0;
  while(IrrlichtHelper::Device()->run()) {
    vector3df pos = _camera->getPosition();
    irr::core::stringw str = "X:";
    str += pos.X;
    str += " Y:";
    str += pos.Y;
    str += " Z:";
    str += pos.Z;
    IrrlichtHelper::Device()->setWindowCaption(str.c_str());
    
    render();
  }

  IrrlichtHelper::Device()->drop();
 
  return 0;
} 


Post Reply