specifying UVs in code

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Cube_
Posts: 1010
Joined: Mon Oct 24, 2011 10:03 pm
Location: 0x45 61 72 74 68 2c 20 69 6e 20 74 68 65 20 73 6f 6c 20 73 79 73 74 65 6d

specifying UVs in code

Post by Cube_ »

How would I specify the UVs in code? (I did say I wasn't going to use UVs but I managed to optimize out a huge chunk of memory and can safely say that I could easily use textures which could be neat).

Anyway, given that I use 8 verts per cube my UV mapping capabilities will be a tad limited (I can't do separate islands per cube, even if I could I wouldn't know how), but I should be able to do overlapping UVs and specify the folding in such a way that they end up like this
Image

Which is of course

TOP - SIDES (folded over each other) - BOTTOM

I'd like to map this out, if at all possible, using code (if there's a memory efficient way to do this, if not I'll just not use textures) but I've got literally no clue how I'd even do that.
"this is not the bottleneck you are looking for"
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: specifying UVs in code

Post by mongoose7 »

You need 12 vertices. With only 8 you will have a line of pixels smeared across a face.
Cube_
Posts: 1010
Joined: Mon Oct 24, 2011 10:03 pm
Location: 0x45 61 72 74 68 2c 20 69 6e 20 74 68 65 20 73 6f 6c 20 73 79 73 74 65 6d

Re: specifying UVs in code

Post by Cube_ »

I wouldn't know how to do the math for a cube with 12 verts so back to my original plan of using just the mesh colors then.
nor am I particularly keen about adding any extra memory overhead.
"this is not the bottleneck you are looking for"
Cube_
Posts: 1010
Joined: Mon Oct 24, 2011 10:03 pm
Location: 0x45 61 72 74 68 2c 20 69 6e 20 74 68 65 20 73 6f 6c 20 73 79 73 74 65 6d

Re: specifying UVs in code

Post by Cube_ »

Would it be possible to just use the same texture over every face using an 8 vertex cube then?
"this is not the bottleneck you are looking for"
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: specifying UVs in code

Post by CuteAlien »

The math is the same as with 8 vertices as with 12. You just put 2 vertices at the same corner sometimes. The reason is pretty trivial - you can give each vertex only 1 uv coordinate. So if you want for example x=0 for one side and x=1 for the connected side then you need 2 vertices at that corner so you can use 2 different uv's.
Sometimes easiest way to do such stuff is to build such a cube quickly with paper and scissors and then just write down the numbers in the corners.

Setting the uv's is done in S3DVertex::TCoords which you get from IMeshBuffer::getVertices () and have to cast then to the correct type (like S3DVertex* or S3DVertex2TCoords*) depending on the values of IMeshBuffer::getVertexType ().
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Cube_
Posts: 1010
Joined: Mon Oct 24, 2011 10:03 pm
Location: 0x45 61 72 74 68 2c 20 69 6e 20 74 68 65 20 73 6f 6c 20 73 79 73 74 65 6d

Re: specifying UVs in code

Post by Cube_ »

so... it should work with just one texture.
let's assume the top right corner of the texture is x=0 and the bottom right is y=1, then it follows that... I don't understand how to assign the numbers but it should work if I only assign one texture per block? maybe? (would this work for any relative position on a texture atlas? like x=46, y=12? If so does this only work on fixed width/height atlases or does it work on any random size one? I don't know how to do texture atlases)
Image

Code: Select all

video::S3DVertex(x-0,y+1,z-0, 0,0,0, c, 1,0), //0
        video::S3DVertex(x+1,y+1,z-0, 0,0,0, c, 0,1), //1
        video::S3DVertex(x+1,y-0,z-0, 0,0,0, c, 1,1), //2
        video::S3DVertex(x-0,y-0,z-0, 0,0,0, c, 0,0), //3
        video::S3DVertex(x-0,y+1,z+1, 0,0,0, c, 1,0), //4
        video::S3DVertex(x+1,y+1,z+1, 0,0,0, c, 0,1), //5
        video::S3DVertex(x+1,y-0,z+1, 0,0,0, c, 1,1), //6
        video::S3DVertex(x-0,y-0,z+1, 0,0,0, c, 0,0), //7
"this is not the bottleneck you are looking for"
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: specifying UVs in code

Post by CuteAlien »

Draw the uv-values also into your image - otherwise you can't see if they make sense. The uv's 0,0 are the top-left corner of your texture and 1,1 the bottom-right corner. So even ignoring top-and bottom polygons of your cube and just looking at the 4 side-rectangles you run already into problems with 8 vertices. The x (or u) values of the top-row could then be for example 0,1,0,1. That allows you to have the full texture on all 4 sides - but there is a problem. The first and third side go from 0-1, the second and forth now go from 1 to 0. So half the sides have an inversed texture. You can only avoid that by giving the corners more vertices so each side can have it's own uv's. So 24 will work definitely - each side just has it's own 4 vertices then. Not sure how to get down to 12... I suppose a few uv's can be cleverly re-used, but I would have to draw that down myself on paper (not doing that now).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Cube_
Posts: 1010
Joined: Mon Oct 24, 2011 10:03 pm
Location: 0x45 61 72 74 68 2c 20 69 6e 20 74 68 65 20 73 6f 6c 20 73 79 73 74 65 6d

Re: specifying UVs in code

Post by Cube_ »

I don't mind the texture being inversed on half the faces, I already realized that much (in fact, that makes the texture less likely to seem like it's repeating due to having different orientations... it might be a tad harder to make it seamless but I'll deal with that)
"this is not the bottleneck you are looking for"
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: specifying UVs in code

Post by mongoose7 »

That's not the problem! The problem is that u will be 0 on *both* sides of the square, so you get a vertical row of pixels smeared across the face. (Or with v insead of u, or 1 instead of 0.) Or u goes from 0 to 1 on the top and from 1 to 0 on the bottom.
Cube_
Posts: 1010
Joined: Mon Oct 24, 2011 10:03 pm
Location: 0x45 61 72 74 68 2c 20 69 6e 20 74 68 65 20 73 6f 6c 20 73 79 73 74 65 6d

Re: specifying UVs in code

Post by Cube_ »

Image
as you predicted this seems to cause UVs behaving weirdly on the top to bottom, from what I can tell using paper math the top has half the UVS wanting to go from top to bottom and the other half from bottom to top (inverted) and the bottom has the UVs pulling away from each other.
specifically
top face
1,0
0,0
1,0
0,0
bottom face
0,1
1,1
1,1
0,1

and I can't see any way to solve this, at all... with two more verts on the top I could fix that, ditto for two more verts on the bottom so I could do this with 12 verts.
I mean that's "only" 16384 more verts per chunk (unoptimized) or a whopping 768kb!

It doesn't seem I have any choice though.
"this is not the bottleneck you are looking for"
Cube_
Posts: 1010
Joined: Mon Oct 24, 2011 10:03 pm
Location: 0x45 61 72 74 68 2c 20 69 6e 20 74 68 65 20 73 6f 6c 20 73 79 73 74 65 6d

Re: specifying UVs in code

Post by Cube_ »

well, added four more vertices, calculated the normals (nothing a pad of paper and a sharpie couldn't do) and voila
this
Image
became this:
Image
"this is not the bottleneck you are looking for"
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: specifying UVs in code

Post by mongoose7 »

You could texture the cubes with a tree or an up arrow :wink:
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: specifying UVs in code

Post by thanhle »

In the end you add a few more vertices.
How is the code to create a proper cube with more vertices now?
It would be useful for newbie if you post that code.

Regards
thanh
Cube_
Posts: 1010
Joined: Mon Oct 24, 2011 10:03 pm
Location: 0x45 61 72 74 68 2c 20 69 6e 20 74 68 65 20 73 6f 6c 20 73 79 73 74 65 6d

Re: specifying UVs in code

Post by Cube_ »

I just added these verts

Code: Select all

        video::S3DVertex(x-0,y+1,z-0, -2,2,-2, c, 1,1),
        video::S3DVertex(x+1,y+1,z-0, 2,2,-2, c, 0,1),
        video::S3DVertex(x+1,y-0,z-0, 2,-2,-2, c, 0,0),
        video::S3DVertex(x-0,y-0,z-0, -2,-2,-2, c, 1,0),
and changed the indices to match (which, if you're just copy pasting my cube code, would be)

Code: Select all

                                //Top
                        9, 8, 4,
                        4, 5, 9,
                                //Bottom
                        11, 10, 6,
                        6, 7, 11};
"this is not the bottleneck you are looking for"
Post Reply