Textured Brush

Topics: Developer Forum, User Forum
Sep 22, 2009 at 7:07 AM

Hi, it's me again,

This time I'm trying to figure out how to texture a polygon created from a texture. I have used VertexPositionColorTexture as vertices for the brush as well as constructing one with Texture2D passed to the BasicEffect, but somewhy texturing seems odd and off-place. This is the algorithm, vertices are passed after extraction from the Vertices.CreatePolygon:

public Vertices Texturize(Vertices vertices, Texture2D texture)
  {
  //Make sure that the vertices are in counter clockwise order.
  vertices.ForceCounterClockWiseOrder();

  // Adjust vertices so that AABB will be starting from 0, 0
  // AABB aabb = new AABB(vertices);
  // Vector2 TopLeft = aabb.Min;
  // vertices.Translate(ref TopLeft);

  Vertices texcoords = new Vertices();
  foreach(Vector2 Vertex in vertices)
  texcoords.Add(new Vector2((float)Vertex.X / (float)texture.Width, (float)Vertex.Y / (float)texture.Height));

  return texcoords;
  }

What am I doing wrong here? Why mapping is  offset so much? At first I thought it was because I didn't generate mipmaps, but then figured it wasn't them.The original and resultant pictures can be seen here: http://img23.imageshack.us/img23/6872/testdz.png

Sep 23, 2009 at 5:17 AM
Edited Sep 23, 2009 at 5:45 AM

Come on guys, anybody? Is the only solution to draw it via SpriteBatch as per example? One on the left is original and it looks close to the one on the right (polygonized), but not quite due to distortion of triangle list that sometimes overlaps.

Sep 23, 2009 at 5:34 AM

Im confused at what you are trying to do. Are you trying to make a brush that paints with a tileable texture instead of just a color?
If so that would be amazing!!! I would love to know how to do that.

Sep 23, 2009 at 5:46 AM
Edited Sep 25, 2009 at 7:14 AM

I am trying to figure out how to generate proper texture mapping coordinates for a polygon that was created from that texture via Vertices.CreatePolygon, i.e:

Texture2D Texture = ScreenManager.ContentManager.Load<Texture2D>(@"Content/Textures/Nuggets/Nugget05");

uint[] data = new uint[Texture.Width * Texture.Height]; Texture.GetData(data);

Vertices verts = Vertices.CreatePolygon(data, Texture.Width, Texture.Height);

Vertices texcoords = Texturize(verts, Texture);

Object Obj = AddPolygon(verts);

Obj.TextureCoordinates = texcoords;

Sep 25, 2009 at 4:12 AM
Edited Sep 25, 2009 at 7:11 AM

I decided to draw sprite instead of textured triangles. By the way, what do I need to do to adjust sprite position with vertices as on example image?

 ** SOLVED**

This one was solved by adding offset of centroid to body position (as per Demo3 in new Farseer source distribution).

 

Now I am back trying to figure out original problem of generating proper texture coordinates for the polygon. Anyone has thoughts as to what  I am doing wrong?

 

Sep 25, 2009 at 8:53 PM

So are you trying to transform Vector2 coordinates from world space onto texture space? I would imagine it would just be texturevertex = (worldvertex - textureoffset) * texturescale. If things can rotate you'd have to deal with that as well.

Sep 26, 2009 at 3:27 AM
Cowdozer wrote:

So are you trying to transform Vector2 coordinates from world space onto texture space? I would imagine it would just be texturevertex = (worldvertex - textureoffset) * texturescale. If things can rotate you'd have to deal with that as well.

 

I am trying to automatically map a polygon created from texture to that texture, but the result appears to have triangle seams visible. In general, it slightly resembles the texure if it is rendered through SpriteBatch, but most of times it doesn't resemble anything close to it. The algorithm of generating texture coordinates is in Texturize function above andit doesn't seem to be doing right job. Any clues as to why?

Developer
Sep 26, 2009 at 4:58 PM

dpr: You should check you the Nuclex framework if you want to draw textured primitives. It simplifies the process quite well while still allowing any effects and offers better performance then SpriteBatch. http://nuclexframework.codeplex.com/ - check out the PrimitiveBatch.

Sep 27, 2009 at 2:45 AM

Thanks, mattbetcher, I will consider using Nuclex, but have you noticed anything that I am doing wrong in Texturize function?

Developer
Sep 28, 2009 at 12:00 AM

dpr: Texture coordinates are from 0-1, your code seems to produce the correct output except for vertices that are negative. What Cowdozer said above is correct - you need to transform world coordinates into texture coordinates. If you can send me a demo showing everything involved with how you are rendering your polygons I can help you find your problem.  My email is mattbettcher@gmail.com.  Also may I ask why you are trying to render with polygons?

 

Oct 1, 2009 at 4:42 AM
Edited Oct 1, 2009 at 4:45 AM
mattbettcher wrote:

dpr: Texture coordinates are from 0-1, your code seems to produce the correct output except for vertices that are negative. What Cowdozer said above is correct - you need to transform world coordinates into texture coordinates. If you can send me a demo showing everything involved with how you are rendering your polygons I can help you find your problem.  My email is mattbettcher@gmail.com.  Also may I ask why you are trying to render with polygons?

 

 Yes, I figured that one out too, not too long ago, but the main problem was with the centroid that offsetted original Vertices. You see, after creating Vertices with CreatePolygon from bitmap data, I fed it into Geom constructor and used Geom' Vertices to produce texture coordinates. But the problem is that as soon as you feed Vertices into Geom, they are translated by centroid. Now I use Vertices produced by CreatePolygon as they are returned with 0,0 as origin, whether Vertices of Geom have origin at centroid. And I am rendering objects as polygons because I am peforming union/subtract/intersect on them, but there will be no problem with texturing them as texture will be repeated at certain scale level.

 

Cheers!

Nov 9, 2010 at 12:51 PM

Any news here?

Is possible to add TextureCoordinate of Vertices when they are "triangulated" ?

 

Thanks