Morph surface of model

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
Nerexis
Posts: 27
Joined: Sun Dec 09, 2007 4:18 pm

Morph surface of model

Post by Nerexis »

How i can transform surface of model?

I want to create randomly generated terrain or maybe planets.
Nerexis
Sorry for bad English.
Frank Dodd
Posts: 208
Joined: Sun Apr 02, 2006 9:20 pm

Post by Frank Dodd »

I am quite not sure what you want. You used the word Morph in the title which suggests that you were going to want to animate a mesh. But if you are after a terrain or planet I assume you will want a static mesh.

I created a command for my FreeBasic - Irrlicht Wrapper that enabled a user to create a mesh with a simple call using the equally simple Irrlicht commands, the code was as follows: -

Code: Select all

/* ----------------------------------------------------------------------------
create a mesh from an array of verticies, an array of incidies to those
verticies that connect them to form triangles and finally texture co-ordinates
to map the verticies against a texture plane
*/
void * DLL_EXPORT IrrCreateMesh(
                                char *cptrMeshName,
                                int iVertexCount,
                                IRR_VERT *vVertices,
                                int iIndicesCount,
                                unsigned short *usIndices )
{
    int iLoop;

    /* get the mesh cache for the scene */
    IMeshCache *mcache = smgr->getMeshCache();

    SMeshBuffer *smeshbuffer = new SMeshBuffer;
    SMesh *smesh = new SMesh;
    SAnimatedMesh * sanimmesh = new SAnimatedMesh;

    // add all of the verts
    printf( "Adding %d verts\n", iVertexCount );
    smeshbuffer->Vertices.set_used(iVertexCount);
    for ( iLoop = 0; iLoop < iVertexCount; iLoop++ )
    {
        smeshbuffer->Vertices[iLoop].Pos.set(
                                            vVertices[iLoop].x,
                                            vVertices[iLoop].y,
                                            vVertices[iLoop].z );
        smeshbuffer->Vertices[iLoop].Normal.set(
                                            vVertices[iLoop].normal_x,
                                            vVertices[iLoop].normal_y,
                                            vVertices[iLoop].normal_z );
        smeshbuffer->Vertices[iLoop].Color.color = vVertices[iLoop].vcolor;
        smeshbuffer->Vertices[iLoop].TCoords.set(
                                            vVertices[iLoop].texture_x,
                                            vVertices[iLoop].texture_y );
    }

    // add all of the indices
    printf( "Adding %d indicies\n", iIndicesCount );
    smeshbuffer->Indices.set_used(iIndicesCount);
    for ( iLoop = 0; iLoop < iIndicesCount; iLoop++ )
    {
        smeshbuffer->Indices[iLoop] = usIndices[iLoop];
    }

    // assemble the mesh objects
    smesh->addMeshBuffer( smeshbuffer );
    sanimmesh->addMesh( smesh );
    mcache->addMesh( cptrMeshName, sanimmesh );

    // recalculate the bounding box around the object for clipping purposes
    sanimmesh->recalculateBoundingBox();

	return (void *)sanimmesh;
}
Another way would be to create your own scene node, there is an Irrlicht example of this in examples/03.CustomSceneNode[/code]
Nerexis
Posts: 27
Joined: Sun Dec 09, 2007 4:18 pm

Post by Nerexis »

Thx for the function but i made this:

Code: Select all

IAnimatedMesh *Obiekt_mesh =     
    smgr->addHillPlaneMesh("hill",
		dimension2d< f32 >(10,10),
		dimension2d< u32 >(100,100),
		&material,
		0.0f,
		dimension2d< f32 >(0.0f, 0.0f),
		dimension2d< f32 >(1.0f, 1.0f)
	);

    IAnimatedMeshSceneNode *Obiekt = smgr->addAnimatedMeshSceneNode(Obiekt_mesh);
    IMeshBuffer * buffer = Obiekt->getMesh()->getMesh(0)->getMeshBuffer(0);
    S3DVertex * vertices = static_cast<video::S3DVertex *>(buffer->getVertices());
    Obiekt->setMaterialTexture(0,driver->getTexture("meshes/OakBark.png"));
    Obiekt->setMaterialTexture(1,driver->getTexture("meshes/detailmap.jpg")); 
    Obiekt->getMaterial(0).getTextureMatrix(0).setTextureScale(100,100);   
    
  

    printf("Generating random terrain mesh...\n");
    int all = Obiekt->getMesh()->getMesh(0)->getMeshBuffer(0)->getVertexCount();
    for(int i = 0; i < all; i+=3)
    {
      vector3df oldpos;
      oldpos = vertices[i].Pos;
    
      vertices[i].Pos += vector3df(0,rand()%5,0);
      for(int b = 0; b<all; b++)
      {
              if(vertices[b].Pos==oldpos) vertices[b].Pos = vertices[i].Pos;
      }
      printf("Vertex: %i%%\r",i*100/all);
    } 
    printf("Done.\n");

Image

Terrain made by this function is ugly.. who knows better way to do it?
and how i can subdivide vertices in this mesh to make terrain smooth?
Nerexis
Sorry for bad English.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

look in a app like photoshop, gimp, milkshape3d or blender and you can render a heightmap/'clouds' which is just a grayscale texture created using perlin noise (look it up) which randomises the image. Then you can load that image as a heightmap in irrlicht (have you looked at the terrain example?), et voila, lovely terrain.
Image Image Image
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

The random terrain is also possible, but you might need to smooth the hills a little bit. And you also have to adjust texture coords, as it seems to stretch too much.
Nerexis
Posts: 27
Joined: Sun Dec 09, 2007 4:18 pm

Post by Nerexis »

I read about perlin noise and i editted my code:

Code: Select all

    all = Obiekt->getMesh()->getMesh(0)->getMeshBuffer(0)->getVertexCount();
    for(int c = 0; c<all; c++)
    {
      vector3df oldpos;
      oldpos = vertices[c].Pos;
      float perlin_noise;
      for(int d = 0; d<5; d++) 
          perlin_noise+=NoiseFunction(rand()%10000);   
             
      vertices[c].Pos.Y = perlin_noise;

      for(int b = 0; b<all; b++)
      {
              if(vertices[b].Pos==oldpos) vertices[b].Pos = vertices[c].Pos;
      }
      printf("Vertex: %i%%\r",c*100/all);
    }
NoiseFunction:

Code: Select all

float NoiseFunction(int x)
{
    x = (x<<13) ^ x;
    return ( 1.0 - ( (x * (x * x * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0);     
}

Generated terrain is almost good but i dont why it looks like that:
Image

Whats wrong??
Nerexis
Sorry for bad English.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

What do you mean what's wrong? What should it look like? It looks fairly sensible output from a perlin noise function to me....
Image Image Image
Darktib
Posts: 167
Joined: Sun Mar 23, 2008 8:25 pm
Location: France

Post by Darktib »

use Arras's terrain scene node (New Tiled Terrain Scene Node)
Nerexis
Posts: 27
Joined: Sun Dec 09, 2007 4:18 pm

Post by Nerexis »

I want to make my own terrain...

Terrain is not realistic. Why mountains are in one axis??

I dont know if my perlin noise function is correct and how to exactly do that in irrlicht.
Nerexis
Sorry for bad English.
3DModelerMan
Posts: 1691
Joined: Sun May 18, 2008 9:42 pm

ummm

Post by 3DModelerMan »

You could just have like ten different hightmaps generated in photoshop, with a cloud filter, and load them based on a randomly generated number,
like "if integer = 1 load heightmap 1,
or else load heightmap 2".
That would be illogical captain...

My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
Post Reply