XNA - Textures and Indices: Shared Vertices

Started by
10 comments, last by Evil Steve 15 years, 3 months ago
Hi! I am learning XNA & after succeeding in creating a cube using indices & some other stuff, I got stuck with one thing: When setting the Textures Coords I found that each vertex is shared... so the textures coords get overwritten: 6 7 8 _______ |\ |\ | |_\_|_\_| 3 4 5 The numbers are the vertices |\ |\ | |_\_|_\_| 0 1 2 for example, I have triangle (3,1,0) and triangle (3,4,1) forming one square. then triangle (4,2,1) and (4,5,2) forming a second square. First Square: ------------ First Triang: vertex 3 -> Texture Coord X=0 Y=0 vertex 1 -> Texture Coord X=1 Y=1 vertex 0 -> Texture Coord X=0 Y=1 Second Triang: vertex 3 -> Texture Coord X=0 Y=0 vertex 4 -> Texture Coord X=1 Y=0 vertex 1 -> Texture Coord X=1 Y=1 Second Square: ------------- First Triang: vertex 4 -> Texture Coord X=0 Y=0 ---OOPS!!!! ... There I've just overwritten the texture coords of this vertex!!! So I thought about indices. I read in Riemer's tutorial that it's possible, but I found nowhere how to do it... How can I do it guys? (please, be detailed if possible as I find this theme rather complicated & it takes me loads of time to understand it). THANK YOU VERY MUCH!!!
Advertisement
You need to create an IndexBuffer, but that wont solve your texture coord issue.

All indices do is allow you to reuse a vertex in another triangle without sending 3 sets of coordinates again, exactly the same way you explained it to us. So you might store vertices 0,1,2,3,4,5 and then you tell it (with an index buffer) that triangle 0 is made up of vertices 0,1,3 and triangle 1 is made from vertices 3,1,4. Given that a vertex is 12 bytes (with only position info), and a index can be either 2 or 4 bytes, it can save quite a lot of vertex buffer memory.

Unfortunately though, that doesn't help your texture coords, as the vertices still have the same texture coords. What you will need to do is either UV unwrapping (where you put all the textures for a mesh in one image) or double up on vertices (so triangles 0 and 1 take 6 vertices instead of 4). The method you are currently using is called a triangle strip, which is more suited to all the textures being in one image.

I hope that helps a bit, I'm no expert on graphics, just interested.
Hi Blitzwing!

Thanks a lot 4 ur quick answer.
Well, u've just introduced me to some unknown topics for me, so that's great as I may find there some light!
I searched a bit about triangles strip & yes, u are right. I did something like this even not knowing it, hehe.

I just wanted to ask you: what's UV?

I found I can probably mirror the texture; so that may be an option... (don't know how to do it yet, but I guess that playing with the coords somehow). I can't understand why XNA doesn't let u do the simple task I need... I also can't understand why the culling DOESN'T work correctly (default is clockwise, but if I don't specify it: "CLOCKWISE", it does whatever it wants)... but well...

So well, once again, thanks a lot for the tips, & if u have a bit more of info it will be very welcome :)

Cheers!!
I'm glad I could help somewhat!

UV (or more preciesly, u,v,w) are coordinates used to refer to a point on (or in) texture space, and named UVW so as easily distinguishable to XYZ (considering they are used together like nearly always). So simply put, U = X, V = Y, W = Z. If you wanted the pixel colour at x = 2, y = 5 of a 2D texture, you'd actually be asking for the colour at u = 2, v = 5. Simple as that. I hope I haven't successfully made it sound overly complicated, because it's not, its just a naming scheme.

The easiest way to mirror your texture would be to set the coords further round to 2.0 then 3.0 (as numbers over 1 typically wrap, though I think there is a setting for that too). This will of course cause you problems on the top and bottom of your cube.

I'm not quite sure how you've come to confuse culling modes with texture displays, that's an odd one. Culling is all about how to hide polygons that are behind others.

Oh, to be honest, don't stress about finding a way to effeciently make your cube unless you plan on rendering thousands and thousands of them on screen. Your best bet is to simply make 16 complete triangles worth of vertices (48 vertices). You wont get any speed increase over such a small number of vertices, but you gain the advantage of being able to have 2 sets of texture coordinates at each cube corner, and being able to specify a different texture for every side.
Hello again!

Thanks one more time for your answer!
We have a situation here:

I'm gonna tell u the whole story. I developed an algorithm which creates "rooms". You tell the method the width, the height & the thick & it builds a cube (taking care of clockwise stuff so u will always see inner walls as if u were inside the room). The width, height & thick defines the number of squares that a face will have. For example: width=2, height=2, thick=2 -> it will build a cube with 4 squares per face.
AND it doesn't draw inner faces nor vertices: just the walls! So a 2,2,2 cube will have 26 vertices!! (please if u don't understand what i'm saying just tell me & i explain it better).
Evrth is made with indices (so i saved many vertices).
It took me like 2 or 3 weeks to create this algorithm... Finally i understood it was not so difficult, but well.

When I wanted to texture the cube, I faced the problem that I already explained.

I know I can destroy evrth & forget about indices, but searching the themes u adviced me I found about mirroring, so I gave it a try.

It worked well for 2 walls (I was super happy!), but when I added a 3rd one, evrth messed up. It's not logical because I checked that if it works for 1 & 2 walls, it would work for 3 & 6 as well including floor & ceiling. But well...

I would like to share my code, I don't want to complicate ur life or anything, but I feel that it must be a silly mistake & that i'm very close...!
Is it there any way to post my code? HOW? Is it ok for you if I post it? (please don't get me wrong, just in case u have any free time & feel like taking a look at it). (Anyway I will follow your advice in the end & work with vertices only to be able to apply a full texture non sharing vertices. But I'd like to solve this issue first so I can offer both options: using indices with mirroring and using vertices with a full texture).

Well, thanks a lot for all your help & for the UVW explanation, it was brilliant :)
HI!!!
Sorry for the loooong text! I THINK I've just found out where the mistake is: it may be a silly coords problem, as some walls (opposed) are drawn in the other way (because of the culling). I have came to this conclution while traveling to work, so I'll try it as far as I get back home!

Thanks thanks thanks a lot, I'll tell u the results as soon as possible :)
U were right, the problem is the floor & ceiling.
the middle top & bottom vertices of the walls complicate evrth as they are shared in the floor & roof... Cheese!

No way out? No exit????! Any workaround?!!
Thanks!

[Edited by - Synthesizer on December 18, 2008 6:57:58 PM]
Sure there is; create more vertices :)
Rats! :P

Ok, I'll dump the dang index buffer (thanks Riemer for the waste of WEEKS).

Now come new questions: For rooms; is it good to make a cube with many vertices per wall (let's say for example 4x4) or it's better just to always use 4 vertices (corners) per wall regardless of the width, height & thick?

If i use only vertices (no indices) & I plot many vertices per wall & all the stuff: Am I gonna face critical problems with textures or anything again? (just i don't really want to face a dead end again...)

U have a lot more of experience: any advice? anything I should be aware of?

My idea is to be able to build a room that I can easily texture with 1 or more textures per wall. Then to put meshes from 3d max & see how evrth goes... Then to add a bigger "room" (world, outdoors) which will contain the first little room. Am I in the good path?
Thanks!
Guys, I succeeded in doing what I was needing :)

In a few words: I didn't use index buffer, so no shared vertices. I think that's the only way if you need to create a texturized cube.

Now I'll go to normals & lighting...!

Thanks for your help, my problem was solved.

This topic is closed to new replies.

Advertisement