I declare incompetence! (so.. uh please help, I'm lost)

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
Cube_
Posts: 1010
Joined: Mon Oct 24, 2011 10:03 pm
Location: 0x45 61 72 74 68 2c 20 69 6e 20 74 68 65 20 73 6f 6c 20 73 79 73 74 65 6d

I declare incompetence! (so.. uh please help, I'm lost)

Post by Cube_ »

So, I took hendu's advice and sent the SMesh pointer to my main thread and created a scene node, now this is all fine and well, at first I had some issues with how I handled it causing the game to not render at all but it was quickly fixed.
Anyway, the more pressing issue now is... well my math is clearly wrong.
Image

Code: Select all

SMesh * cubeSpawner(unsigned int typeID, bool artificial)
{
    cout<<"time to make a cube\n";
    f32 cubeSize = 5.0f;
    video::SColor cubeColour(255,255,255,255);
    SMeshBuffer * buffer = new SMeshBuffer();
 
    u16 u[36] = { 0,2,1, 0,3,2, 1,5,4, 1,2,5, 4,6,7, 4,5,6,
    7,3,0, 7,6,3, 9,5,2, 9,8,5, 0,11,10, 0,10,7};
 
    int vertcount       = 0;
    int indicescount    = 0;
 
    cout<<"starting cube creating loop\n";
    for (int x = 0; x < 10; x ++ ){
        for (int y = 0; y < 10; y ++ ){
            buffer->Vertices.set_used(vertcount + 12);
            buffer->Vertices[vertcount++]    = video::S3DVertex(0,0,0, -1,-1,-1, cubeColour, 0, 1);
            buffer->Vertices[vertcount++]    = video::S3DVertex(1,0,0,  1,-1,-1, cubeColour, 1, 1);
            buffer->Vertices[vertcount++]    = video::S3DVertex(1,1,0,  1, 1,-1, cubeColour, 1, 0);
            buffer->Vertices[vertcount++]    = video::S3DVertex(0,1,0, -1, 1,-1, cubeColour, 0, 0);
            buffer->Vertices[vertcount++]    = video::S3DVertex(1,0,1,  1,-1, 1, cubeColour, 0, 1);
            buffer->Vertices[vertcount++]    = video::S3DVertex(1,1,1,  1, 1, 1, cubeColour, 0, 0);
            buffer->Vertices[vertcount++]    = video::S3DVertex(0,1,1, -1, 1, 1, cubeColour, 1, 0);
            buffer->Vertices[vertcount++]    = video::S3DVertex(0,0,1, -1,-1, 1, cubeColour, 1, 1);
            buffer->Vertices[vertcount++]    = video::S3DVertex(0,1,1, -1, 1, 1, cubeColour, 0, 1);
            buffer->Vertices[vertcount++]    = video::S3DVertex(0,1,0, -1, 1,-1, cubeColour, 1, 1);
            buffer->Vertices[vertcount++]    = video::S3DVertex(1,0,1,  1,-1, 1, cubeColour, 1, 0);
            buffer->Vertices[vertcount++]    = video::S3DVertex(1,0,0,  1,-1,-1, cubeColour, 0, 0);
 
            //buffer->BoundingBox.reset(0,0,0);
 
            for (int i = vertcount - 12; i < vertcount; ++i) {
                buffer->Vertices[i].Pos -= vector3df(0.5f, 0.5f, 0.5f);
                buffer->Vertices[i].Pos *= cubeSize;
                buffer->Vertices[i].Pos += vector3df(x * cubeSize, 0, y * cubeSize);
            //    buffer->BoundingBox.addInternalPoint(buffer->Vertices[i].Pos);
            }
 
            indicescount += 36;
            buffer->Indices.set_used(indicescount);
            for (s32 i = indicescount - 36; i < indicescount; i+=3) {
                buffer->Indices[i]      = u[(i)     % 36] += x * cubeSize;
                buffer->Indices[i + 1]  = u[(i + 1) % 36] = 0; //cubeSize;
                buffer->Indices[i + 2]  = u[(i + 2) % 36] += y * cubeSize;
            }
 
        }
    }
    cout<<"Creating mesh\n";
    SMesh * cubeMesh = new SMesh();
    cubeMesh->addMeshBuffer(buffer);
    cout<<"sending mesh to main function\n";
    return cubeMesh;
The code is an inefficient placeholder that I intend to do away with as I just nabbed it from this forum (can't remember the exact post though), but first I need to understand the essence of making a cube, I thought the math looked right but apparently not.
Or perhaps I just don't understand how Irrlicht handles vertices (I ussually create my meshes by hand, this is more or less my first serious dip into procedural generation as my previous attempts have used the built in cubemesh type which is fine for some things but I intend to have more than cubes so having a solid mesh generator is something I'll have to do)

Now since I only know one cure for incompetence, namely education I would humbly ask for someone to explain why this is happening (and I suspect this is all the code you need as all that happens on the other side is that it dereferences the pointer returned to get the mesh data (the function type is SMesh) and assigns it to a scenenode)
"this is not the bottleneck you are looking for"
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: I declare incompetence! (so.. uh please help, I'm lost)

Post by mongoose7 »

Don't know what's going on with your indices. You see the u[] array? What you need to do is add on the current number of vertices so that the indices relate to them. So

Code: Select all

for (i = 0; i < 36; ++i)
indices[indexcount + i] = u[i] + vertexcount;
indexcount += 36;
vertexcount += 12;
(I think you should increment indexcount, vertexcount at the end. Negative indexing? Weird.)
Cube_
Posts: 1010
Joined: Mon Oct 24, 2011 10:03 pm
Location: 0x45 61 72 74 68 2c 20 69 6e 20 74 68 65 20 73 6f 6c 20 73 79 73 74 65 6d

Re: I declare incompetence! (so.. uh please help, I'm lost)

Post by Cube_ »

Terribly sorry about taking so long, I've been a tad busy with trying to get my code to work and whatnot.
Anyway, the code you suggested did not work and I kept getting a crash due to what() bad_alloc.
Reverting the change fixes the problem

I'll keep hacking at my code to see if I can solve it, if not I'll just have to scrap it and start over trying some other method of generating a cubemesh without any additional overhead (scene nodes have overhead and I need a lot of cubes, in fact I had to start optimizing out single bytes from the data that will be attached to each cube to get it within anything reasonable (7gb isn't reasonable, neither is 800mb, I got it sub 500mb worst case which is reasonable (rather in theory I got it to sub 500, I can't actually apply the patches to my data structures until I have my cubes generating again, irrlicht's built in cube scene node isn't really feasible for what I need unless there's some way to just generate the cube mesh without overhead (and even so I'd like finer control since I intend to use non cube shapes later as well))
"this is not the bottleneck you are looking for"
Post Reply