specifying UVs in code
-
- 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
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
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.
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
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"
Re: specifying UVs in code
You need 12 vertices. With only 8 you will have a line of pixels smeared across a face.
-
- 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
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.
nor am I particularly keen about adding any extra memory overhead.
"this is not the bottleneck you are looking for"
-
- 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
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"
Re: specifying UVs in code
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 ().
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
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
-
- 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
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)
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)
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"
Re: specifying UVs in code
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
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
-
- 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
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"
Re: specifying UVs in code
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.
-
- 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
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"
-
- 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
well, added four more vertices, calculated the normals (nothing a pad of paper and a sharpie couldn't do) and voila
this
became this:
this
became this:
"this is not the bottleneck you are looking for"
Re: specifying UVs in code
You could texture the cubes with a tree or an up arrow
Re: specifying UVs in code
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
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
-
- 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
I just added these verts
and changed the indices to match (which, if you're just copy pasting my cube code, would be)
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),
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"