Ring mesh buffer

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
pera
Posts: 460
Joined: Wed May 14, 2008 1:05 pm
Location: Novi Sad, Serbia
Contact:

Ring mesh buffer

Post by pera »

I would like to draw 2d circle with thicker line, kind of flat ring in 3d space.
Anyone knows if that as been done?

I've seen this topic http://irrlicht.sourceforge.net/phpBB2/ ... 032#211032 but its just 1 pixel line.

And triangle fans draw full circle, not just outline that i need..
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

Since I've been away from Irrlicht for a while and haven't done anything in ages, so I thought I'd have a pop at this.

Here's a screenshot:

Image

The function to create it:

Code: Select all

IMeshBuffer* createRingMesh(f32 radius, f32 width, u32 sections)
{
    CMeshBuffer<S3DVertex>* mb = new CMeshBuffer<S3DVertex>();
    mb->Vertices.set_used(sections*2 + 2); // 2 new verts per section, plus dupe at the start/end
    mb->Indices.set_used(sections*6); // two triangles per section

    // radians per segment
    f32 segRadians = (core::PI*2) / f32(sections);
    f32 segTCoord = 1.0 / f32(sections);

    // position vertices
    for (u32 i=0; i < sections; ++i)
    {
        const vector2df outer = vector2df(sin(segRadians*f32(i)), cos(segRadians*f32(i))) * radius;
        const vector2df inner = vector2df(sin(segRadians*f32(i)), cos(segRadians*f32(i))) * (radius-width);

        S3DVertex &v1 = mb->Vertices[i*2 +0];
        S3DVertex &v2 = mb->Vertices[i*2 +1];
        v1.Color.set(255, 255, 255, 255);
        v1.Normal.set(0, 1.0, 0);
        v1.Pos.set(outer.X, 0, outer.Y);
        v1.TCoords.set(1.0, segTCoord * f32(i));

        v2.Color.set(255,255,255,255);
        v2.Normal.set(0, 1.0, 0);
        v2.Pos.set(inner.X, 0,inner.Y);
        v2.TCoords.set(0.0, segTCoord * f32(i));
    }

    // dupe vertices at the end
    mb->Vertices[sections*2 +0] = mb->Vertices[0];
    mb->Vertices[sections*2 +0].TCoords.set(1.0, 1.0);
    mb->Vertices[sections*2 +1] = mb->Vertices[1];
    mb->Vertices[sections*2 +1].TCoords.set(0.0, 1.0);

    // stitch indices
    u32 v=0;
    for (u32 i=0; i < sections*6; i+=6, v+=2)
    {
        mb->Indices[i+0] = (v+0);
        mb->Indices[i+1] = (v+2);
        mb->Indices[i+2] = (v+3);
        mb->Indices[i+3] = (v+0);
        mb->Indices[i+4] = (v+3);
        mb->Indices[i+5] = (v+1);
    }

    mb->recalculateBoundingBox();
    return mb;
}
And an example of how to use it:

Code: Select all

int main()
{
	IrrlichtDevice *device =
		createDevice( video::EDT_OPENGL, dimension2d<u32>(640, 480), 16,
			false, false, false, 0);

	if (!device)
		return 1;

	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();
	IGUIEnvironment* guienv = device->getGUIEnvironment();

    // create ring
    IMeshBuffer* mb = createRingMesh(100.f, 25.f, 15);

    // add it to a mesh and recalculate the bounding box
    SMesh *mesh = new SMesh();
    mesh->addMeshBuffer(mb);
    mesh->recalculateBoundingBox();
    mb->drop();

	IMeshSceneNode* node = smgr->addMeshSceneNode(mesh);

	if (node)
	{
		node->setMaterialFlag(EMF_LIGHTING, false);
		// johnny cash
		node->setMaterialTexture( 0, driver->getTexture("../../media/fire.bmp") );
	}

	smgr->addCameraSceneNodeFPS();

	while(device->run())
	{
		driver->beginScene(true, true, SColor(255,100,101,140));
		smgr->drawAll();
		driver->endScene();
	}

	device->drop();

	return 0;
}
I've moved the topic to the code snippets forum :)
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Post by ent1ty »

Just curious: Why are you mixing johnny cash and C++ coding? :)
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

Obviously because it's a Ring of Fire.bmp :lol:
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Hey bitplane, I guess it's time for a new StandardGeometryMeshFactory :wink: Nice to see you here again :D
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

Yeah thats a good idea, but how would it deal with serialization and caching? I have an idea but I'm not sure if you'll like it. I'll start another thread in open discussion about this!
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Post by ent1ty »

bitplane wrote:Obviously because it's a Ring of Fire.bmp :lol:
Ah, didn't realize that, I'm more like rock/metal guy. :wink:
Last edited by ent1ty on Mon May 17, 2010 9:56 am, edited 3 times in total.
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

ent1ty wrote:Just curious: Why are you mixing johnny cash and C++ coding? :)
I still think this is dangerous. You should be careful when mixing Johnny Cash and C++. :P
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
pera
Posts: 460
Joined: Wed May 14, 2008 1:05 pm
Location: Novi Sad, Serbia
Contact:

Post by pera »

Thank you! I owe you one.

This is just what I was looking for. And that bitmap shadow make it look like torus, very nice.

Now let me see how that code works... thats some serious geometry there; when I grow up Im gonna be smart just like you! :)
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

ent1ty wrote:Just curious: Why are you mixing johnny cash and C++ coding? :)
Why the hell wouldn't you?
slavik262
Posts: 753
Joined: Sun Nov 22, 2009 9:25 pm
Location: Wisconsin, USA

Post by slavik262 »

Welcome back Bitplane... us newer members have heard stories about you... :P

Nice to see you active again.
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

are you the same slavik that plays savage by chance?

Here's some johnny to program to, on the house

oh and don't suck bitplane off please. just cause he mobs with the nublets.
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

So glad you came back, Midnight, we really missed you.
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

Dorth wrote:So glad you came back, Midnight, we really missed you.
Be nice, play fair.
pera
Posts: 460
Joined: Wed May 14, 2008 1:05 pm
Location: Novi Sad, Serbia
Contact:

Post by pera »

Post Reply