Improved CTestSceneNode (texture mapping/lightning)

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Improved CTestSceneNode (texture mapping/lightning)

Post by jox »

In my opinion CTestSceneNode has two bugs. One in the texture mapping and one in the lightning.

This is a TestSceneNode with some texture and a light shining on it:

Image

Ugly, isn't it?

The texture mapping looks like this:

Image

It's destorted on most sides. Well, this is already an improvement by Dr Bragianos.

Before it looked like this (commented out in the source):

Image

Here 4 sides are not distorted, but 2 are extremly distorted.

The distortion is less noticeable in the first version, but it's still a compromise.

I think it should look like this:

Image

Some sides are still mirrored. But it doesn't matter if there is not text on the texture.

To achieve this I added just 4 vertices with proper uv coords to the existing 8 and reordered the triangles (which are still 12).

This fixed the texture mapping.

The lightning looks like this:

Image

It's ugly because the normal vectors of the vertices are all messed up.

I fixed them so it looks like this:

Image

And if you put it all together then it could look as beautyful as this: :)

Image

I think now it has deserved the title CubeSceneNode ;)

I'll post the source in a minute...
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Post by jox »

Changes have to be made to CTestSceneNode.cpp and CTestSceneNode.h.

(This is for Irrlich 0.6)

CTestSceneNode.cpp:

change:

Code: Select all

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,	3,5,2,	3,6,5,	0,1,4,	0,4,7};
to:

Code: Select all

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};
again CTestSceneNode.cpp:

change:

Code: Select all

// nicer texture mapping sent in by Dr Andros C Bragianos 
Vertices[0] = video::S3DVertex(0,0,0, 1,0,0, video::SColor(255,255,255,255), 0, 0);
Vertices[1] = video::S3DVertex(1,0,0, 0,1,0, video::SColor(255,255,255,255), 1, 0);
Vertices[2] = video::S3DVertex(1,1,0, 0,0,1, video::SColor(255,255,255,255), 1, 1);
Vertices[3] = video::S3DVertex(0,1,0, 1,1,0, video::SColor(255,255,255,255), 0, 1);

/*Vertices[0] = video::S3DVertex(0,0,0, 1,0,0, video::SColor(255,255,255,255), 0, 1);
Vertices[1] = video::S3DVertex(1,0,0, 0,1,0, video::SColor(255,255,255,255), 1, 1);
Vertices[2] = video::S3DVertex(1,1,0, 0,0,1, video::SColor(255,255,255,255), 1, 0);
Vertices[3] = video::S3DVertex(0,1,0, 1,1,0, video::SColor(255,255,255,255), 0, 0);*/
Vertices[4] = video::S3DVertex(1,0,1, 0,1,1, video::SColor(255,255,255,255), 0, 1);
Vertices[5] = video::S3DVertex(1,1,1, 1,0,1, video::SColor(255,255,255,255), 0, 0);
Vertices[6] = video::S3DVertex(0,1,1, 1,1,1, video::SColor(255,255,255,255), 1, 0);
Vertices[7] = video::S3DVertex(0,0,1, 0,0,1, video::SColor(255,255,255,255), 1, 1);

Box.reset(0,0,0);

for (i=0; i<8; ++i)
to

Code: Select all

Vertices[0]  = video::S3DVertex(0,0,0, -1,-1,-1, video::SColor(255,255,255,255), 0, 1);
Vertices[1]  = video::S3DVertex(1,0,0,  1,-1,-1, video::SColor(255,255,255,255), 1, 1);
Vertices[2]  = video::S3DVertex(1,1,0,  1, 1,-1, video::SColor(255,255,255,255), 1, 0);
Vertices[3]  = video::S3DVertex(0,1,0, -1, 1,-1, video::SColor(255,255,255,255), 0, 0);
Vertices[4]  = video::S3DVertex(1,0,1,  1,-1, 1, video::SColor(255,255,255,255), 0, 1);
Vertices[5]  = video::S3DVertex(1,1,1,  1, 1, 1, video::SColor(255,255,255,255), 0, 0);
Vertices[6]  = video::S3DVertex(0,1,1, -1, 1, 1, video::SColor(255,255,255,255), 1, 0);
Vertices[7]  = video::S3DVertex(0,0,1, -1,-1, 1, video::SColor(255,255,255,255), 1, 1);
Vertices[8]  = video::S3DVertex(0,1,1, -1, 1, 1, video::SColor(255,255,255,255), 0, 1);
Vertices[9]  = video::S3DVertex(0,1,0, -1, 1,-1, video::SColor(255,255,255,255), 1, 1);
Vertices[10] = video::S3DVertex(1,0,1,  1,-1, 1, video::SColor(255,255,255,255), 1, 0);
Vertices[11] = video::S3DVertex(1,0,0,  1,-1,-1, video::SColor(255,255,255,255), 0, 0);

Box.reset(0,0,0);

for (i=0; i<12; ++i)
again CTestSceneNode.cpp:

change:

Code: Select all

	driver->drawIndexedTriangleList(&Vertices[0], 8, &Indices[0], 12);
to

Code: Select all

	driver->drawIndexedTriangleList(&Vertices[0], 12, &Indices[0], 12);
now CTestSceneNode.h:

change:

Code: Select all

video::S3DVertex Vertices[8];
to

Code: Select all

video::S3DVertex Vertices[12];
That's it. (I hope) :)

Regards,
jox
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Making of...

Post by jox »

This is how I did it.

I started with the old version (4 sides non-distorted). The red ones are the 4 new vertices 8, 9, 10 and 11 with the same coordinates as 6, 3, 4 and 1. They build the top and bottom sides of the cube with vertices 2, 5, 0 and 7.

The green ones are obsolete. With them 4 sides would have been mirrored. With the red ones only two.

Image

I know, it's just a TestSceneNode. But it was a challenge! :)

jox
stampsm
Posts: 142
Joined: Mon Nov 10, 2003 5:52 pm
Location: Las Vegas

Post by stampsm »

i did the same sort of thing a few months ago. i modified the custom scenenode to display a well formed textured cube like you did. it wasn't that much to change the code, but to figure out by hand the polygon cordinates was a pain in the neck. it took me nearly a half hour to get the polygons right.
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Post by jox »

Cool. Did you also add 4 vertices? Couldn't think of a better way. Yea, figuring out the coords was the pain part. But doing it on paper helped a lot. I think I spend most time creating nice screeshots and posting everything here... :)
saigumi
Posts: 921
Joined: Fri Aug 22, 2003 11:31 am
Location: St. Louis, MO USA
Contact:

Post by saigumi »

Very nice
Crud, how do I do this again?
niko
Site Admin
Posts: 1759
Joined: Fri Aug 22, 2003 4:44 am
Location: Vienna, Austria
Contact:

Post by niko »

Hehe, cool, thanks for this posting. (added in next version) :) Now we have a nice cube in the engine. I did not care for that wrong normals and texture coords, because it was simply a test scene node, only to see if something could be rendered. And in the beginning, it wasn't even a cube, it was a single triangle. :)
stampsm
Posts: 142
Joined: Mon Nov 10, 2003 5:52 pm
Location: Las Vegas

Post by stampsm »

yea i ended up with something like 24 vertices.
Post Reply