[Solved] polygon faces;single drawvertexprim call

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

[Solved] polygon faces;single drawvertexprim call

Post by pandoragami »

I have the following code which initializes about 10k randomly placed polygons (square shaped faces) into a volume of space 200 irrlicht units cubed. What I was hoping to do is to try attaching various textures randomly (one texture per polygon face) and using a single drawvertexprimitivelist call to display them all efficiently. I know the SMaterial object can be loaded with a texture although that only allows a single texture per call to drawvertexprimitivelist; is there another to load a texture per polygon and still use one call to drawvertexprimitivelist?

Here's the code,

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;
    }    
     
 
Last edited by pandoragami on Sat Nov 22, 2014 1:58 am, edited 2 times in total.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Add textures to polygon faces;single drawvertexprim call

Post by hendu »

You will have to map such in the shader if you want one draw call with many different textures.
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: Add textures to polygon faces;single drawvertexprim call

Post by pandoragami »

hendu wrote:You will have to map such in the shader if you want one draw call with many different textures.
Using glsl I assume; I thought that was only for shading but I guess textures fall into that category. Do you have any examples ( I have looked at the shading tutorial, but that doesn't address custom primitives at all) or links maybe to save me a few hours of steps... maybe specific functions to look at in the Irrlicht documentation. I actually have no background with glsl but have been looking at a tutorial here http://3dgep.com/introduction-to-opengl-and-glsl/ to get started.

thanks.
hendu
Posts: 2600
Joined: Sat Dec 18, 2010 12:53 pm

Re: Add textures to polygon faces;single drawvertexprim call

Post by hendu »

Custom primitive doesn't matter, the way you apply a shader is the same.

Pseudocode:

Code: Select all

if (a) color = texture1; else color = texture2
Then you need to figure out how to pass the variable. I recommend the vertex alpha color, which you don't seem to use for anything else. Can't really give more specific advice without writing the shader for you.
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: Add textures to polygon faces;single drawvertexprim call

Post by pandoragami »

hendu wrote:Custom primitive doesn't matter, the way you apply a shader is the same.

Pseudocode:

Code: Select all

if (a) color = texture1; else color = texture2
Then you need to figure out how to pass the variable. I recommend the vertex alpha color, which you don't seem to use for anything else. Can't really give more specific advice without writing the shader for you.
If you wouldn't mind writing it for me that would be nice (I'm assuming it's nothing but 20 lines of code), as I said I have zero experience with glsl and I'm already looking everywhere trying to tie the pieces together. Thanks for your help so far.
chronologicaldot
Competition winner
Posts: 688
Joined: Mon Sep 10, 2012 8:51 am

Re: Add textures to polygon faces;single drawvertexprim call

Post by chronologicaldot »

Or you could learn to work with OpenGL, which will be useful to you in the future. It's like C.
Here's something to get you started:
https://www.opengl.org/sdk/docs/tutoria ... pter_1.pdf
pandoragami
Posts: 226
Joined: Wed Jan 26, 2011 5:37 pm
Contact:

Re: Add textures to polygon faces;single drawvertexprim call

Post by pandoragami »

No problem solved it.
Last edited by pandoragami on Sun Nov 23, 2014 12:30 am, edited 1 time in total.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Add textures to polygon faces;single drawvertexprim call

Post by mongoose7 »

Hendu already told you. You put four textures on the material. You read the shader example. In the vertex shader you pass the vertex colour to the pixel shader. In the pixel shader you have four textures and the vertex colour so you use the vertex colour to select the texture. This gives you a choice of four textures for any polygon, though you could also blend them. I don't think hendu wants to write your shaders.
Post Reply