Painting a circle on top of a terrain

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
suliman
Posts: 379
Joined: Sat Sep 23, 2006 2:06 pm

Painting a circle on top of a terrain

Post by suliman »

Hi

This drawn a circle on top of the terrain, it follows the topography of the terrain.

Code: Select all

void gameGFX_irr::circleOnTerrain(float x, float z, float Radius, float heightOverTerrain,int Segments, SColor color,scene::ITerrainSceneNode* terr) 
{ 
        float EachAngle; 
        float a; 
        float x1; 
        float x2; 
        float z1; 
        float z2; 
 
        EachAngle = 2.0 * PI_CONSTANT / (float)Segments; 
 
        x2 = Radius; 
        z2 = 0.0; 
 
        for(a=0.0; a<= (2.0*PI_CONSTANT + EachAngle); a+=EachAngle) { 
                x1 = x2; 
                z1 = z2; 
                x2 = Radius * cos(a); 
                z2 = Radius * sin(a); 
                //line(x1+x, y1+y, x2+x, y2+y, color); 
                f32 h=terr->getHeight((x1+x)*MAP_SCALE,(z1+z)*MAP_SCALE);
                f32 h2=terr->getHeight((x2+x)*MAP_SCALE,(z2+z)*MAP_SCALE);
                line3d(vector3df((x1+x)*MAP_SCALE,h+heightOverTerrain,(z1+z)*MAP_SCALE),vector3df((x2+x)*MAP_SCALE,h2+heightOverTerrain,(z2+z)*MAP_SCALE),color);
        } 
} 
I use my own wrapper for a 3d line

Code: Select all

void gameGFX_irr::line3d( vector3df start,vector3df end,SColor color )
{
        SMaterial m; 
        m.Lighting=false;
        driver->setMaterial(m);
        driver->setTransform(video::ETS_WORLD, core::matrix4());
        driver->draw3DLine(start,end,color);
}
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: Painting a circle on top of a terrain

Post by randomMesh »

It would be wise to set the transformation and material only once, since they don't change while drawing all the lines. Would be a lot faster.

Code: Select all

 
void gameGFX_irr::circleOnTerrain(float x, float z, float Radius, float heightOverTerrain,int Segments, SColor color,scene::ITerrainSceneNode* terr) 
{ 
        float EachAngle; 
        float a; 
        float x1; 
        float x2; 
        float z1; 
        float z2; 
 
        EachAngle = 2.0 * PI_CONSTANT / (float)Segments; 
 
        x2 = Radius; 
        z2 = 0.0; 
 
        SMaterial m; 
        m.Lighting=false;
        driver->setMaterial(m);
        driver->setTransform(video::ETS_WORLD, core::matrix4());
 
        for(a=0.0; a<= (2.0*PI_CONSTANT + EachAngle); a+=EachAngle) { 
                x1 = x2; 
                z1 = z2; 
                x2 = Radius * cos(a); 
                z2 = Radius * sin(a); 
                //line(x1+x, y1+y, x2+x, y2+y, color); 
                f32 h=terr->getHeight((x1+x)*MAP_SCALE,(z1+z)*MAP_SCALE);
                f32 h2=terr->getHeight((x2+x)*MAP_SCALE,(z2+z)*MAP_SCALE);
                line3d(vector3df((x1+x)*MAP_SCALE,h+heightOverTerrain,(z1+z)*MAP_SCALE),vector3df((x2+x)*MAP_SCALE,h2+heightOverTerrain,(z2+z)*MAP_SCALE),color);
        } 
}
 
void gameGFX_irr::line3d( vector3df start,vector3df end,SColor color )
{
        driver->draw3DLine(start,end,color);
}
 
This means that you can get rid off the gameGFX_irr::line3d and draw the lines with Irrlichts native draw3DLine method. It's senseless overhead otherwise.
At least make it const-correct.

Code: Select all

 
void gameGFX_irr::line3d(const vector3df& start, const vector3df& end, const SColor& color) const
{
        driver->draw3DLine(start,end,color);
}
 
"Whoops..."
Post Reply