ralian wrote:Honestly, if you could comment your code a bit better, that would be great
However, here were some things I saw:
* Camera position and target both set to 0? That's undefined behavior, I think. If you're not using the camera, just declare it with the default values.
* Use the irrlicht logger, or at least printf, instead of cout. I mean, that's more of a recommendation, but that would make your life easier I think
* Using the irrlicht randomizer will also make your life easier
* You've also got a lot of magic numbers and things that just don't make sense to me: Why is x, y, and z declared to each other then immediately set to a random number? Comments would help clear up that sort of thing.
The actual problem is a bad reference to, seemingly, all of the irrlicht engine (the device, driver, and scene manager) that was somehow changed in the middle of the code? How was that even managed? I'll look at this more later, but I think the code needs some revising either way. I'll be happy to answer further questions, but could you please take solved out of the title while you continue to ask? You get more help if people know you aren't finished with questions
Best luck, ralian
Here's the newer code.
Things considered,
*The camera position is now -10 on the z axis and looks (somewhat) at the center of field of planes.
*I kept the std::cout due to preference as this is C++, would it be better to use printf ( I never use it anywhere, std::cout has never failed me)? Never used Irrlicht logger by the way, don't even know the codes for it.
*Irrlicht randomizer is something I'll lookup but rand() should be fine for now.
*I added comments to the most important parts of the code, ask for a specific line if you're still unsure.
The bad reference was probably due to the overflow in the indices from the code where it's too small I guess. The memory segment around the indices was probably overwritten and became corrupt... maybe.
thanks.
Code: Select all
#include <irrlicht.h>
#include <iostream>
using namespace irr;
int main()
{
IrrlichtDevice* device = 0;
device = createDevice( video::EDT_OPENGL, core::dimension2d<u32>(1280, 960));
if (device == 0)
return 1;
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);
scene::ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS( 0, 50.0f, 0.01f, -1, 0, 0, false, 0.0f, false, true);
cam->setPosition(core::vector3df(0,0,-10));
cam->setTarget(core::vector3df(100,100,0));
device->getCursorControl()->setVisible(false);
irr::video::SMaterial cube_material;
irr::video::S3DVertex *Vertices = (irr::video::S3DVertex*) malloc(sizeof(irr::video::S3DVertex) * 65535);
irr::u16 indices[310000];
int x, y, z, l;//this initializes the positions
x = y = z = 0;
l = 1;
//Bottom
int R, G, B;//used for coloring vertices
R = rand()%255;
G = rand()%255;
B = rand()%255;
x = rand()%200;
y = rand()%200;
z = rand()%200;
int vertex_count = 0;//the counter for the vertices, 4 per face/plane
int vertex_limit = 65500;//the total number of vertices
while(vertex_count < vertex_limit)
{
Vertices[vertex_count] = irr::video::S3DVertex(
x, y, z, 1,1,0,
irr::video::SColor(255, R, G, B), 0, 1);
vertex_count++;
Vertices[vertex_count] = irr::video::S3DVertex(
x+l, y, z, 1,0,0,
irr::video::SColor(255, R, G, B), 1, 1);
vertex_count++;
Vertices[vertex_count] = irr::video::S3DVertex(
x+l, y, z+l, 0,1,1,
irr::video::SColor(255, R, G, B), 1, 0);
vertex_count++;
Vertices[vertex_count] = irr::video::S3DVertex(
x, y, z+l, 0,0,1,
irr::video::SColor(255, R, G, B), 0, 0);
vertex_count++;
x = rand()%200;//this reinitializes the positions (within a box of 200 irrlicht units of distance) and colors
y = rand()%200;
z = rand()%200;
R = rand()%255;
G = rand()%255;
B = rand()%255;
//std::cout<<x<<","<<y<<","<<z<<" vertex_count: "<<vertex_count<<std::endl;
};
std::cout<<" vertex_count: "<<vertex_count<<std::endl;
int index_count = 0;//the counter for the indices
int index_offset = 0;//this is the offset used to keep the indices separate in the array when rendering
while(index_offset < vertex_limit)
{
indices[index_count] = 0 + index_offset;
index_count++;
indices[index_count] = 1 + index_offset;
index_count++;
indices[index_count] = 2 + index_offset;
index_count++;
indices[index_count] = 0 + index_offset;
index_count++;
indices[index_count] = 2 + index_offset;
index_count++;
indices[index_count] = 3 + index_offset;
index_count++;
index_offset+=4;
}
std::cout<<"index_offset" <<index_offset<<" vertex_count: "<<vertex_count<<std::endl;
while(device->run())
{
driver->beginScene(true, true, video::SColor(255,0,0,0));
smgr->drawAll();
cube_material.BackfaceCulling = false;
cube_material.Wireframe = false;
cube_material.Lighting = false;
driver->setMaterial(cube_material);
driver->setTransform(irr::video::ETS_WORLD, irr::core::matrix4());
driver->drawVertexPrimitiveList( &Vertices[0], vertex_limit, &indices[0], index_offset/2, irr::video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
driver->endScene();
device->sleep(50);
}
device->drop();
return 0;
}