Search Home Members Contacts
About Us
Products
Downloads
Community
Support
Pages: [1]
  Print  
Author Topic: Add Triangle with texture mapping  (Read 1632 times)
harikumar001
Customers
Community Member
*****
Posts: 65


« on: June 20, 2010, 07:59:31 AM »

Hey guys,

Been working on a program and ran into a new problem.

What I am doing is creating a program that allows the user to draw a 2D shape using lines and then the program draws the same in 3d. So basically the user can create any shape using lines.

Earlier, I could draw only walls without any floor for the room. With help from mithrandir I was able to learn more about and use triangulation process for converting the polygon(shape of the room) into triangles to draw a floor for my room.

As of now, my program is able to draw the floor with textures applied on each of the triangles, which makes it look weird. I want the texture applied to the whole polygon, not to individual triangles.

I read numerous posts on this forum regarding this, but for most of the cases they know which shape they will be applying texture to in advance. In my case, I do not know beforehand which shape I will be applying texture to. I believe it is something to do with UV co-ordinates. FYI I am using the AddTriangle method to draw the triangles.

Been learning a lot of new things here and you guys have been really helpful. Have a great time guys and keep up the excellent work.

Regards
Logged
asia
Customers
Community Member
*****
Posts: 183


« Reply #1 on: June 20, 2010, 02:14:57 PM »

I suggest you should look to getvertex and setvertex methods of the mesh class.
Using them you can set the U V coordinates.
I also suggest to find the minimum and maximum x and z for your floor. Then compute the u,v coordinates using them like this pseudo code:
 
Code:
u = (x-xmin)/(xmax-xmin)
v = (z-zmin)/(zmax-zmin)

Iterate on all the verteces setting the appropriate u v.
In this manner at the minimum the texture coordinate is zero, at the maximum is one.
The texture could be any classical texture for floors.
Hope this will help you to find a way...
Ciao

Logged

Fabio Musmeci
ENEA
CR Casaccia
Via Anguillarese 301
00060 Rome
Italy
musmeci@enea.it
+39 3333934898
Learning to live better on a smaller footprint..
Mithrandir
Community Member
*
Posts: 325


« Reply #2 on: June 20, 2010, 04:55:45 PM »

If you want to repeat the texture on the floor (create tiles) instead of stretching it across the whole shape, you can do it right away when adding vertices. Just set the UV coordinates like this:
Code:
u = (x+TileOffsetX)/(TileSizeX)
v = (z+TileOffsetZ)/(TileSizeY)

x,y are vertex positions.
Sizes and offsets can be arbitrary.

You can also add rotation but for that you'll have to use transformation matrix instead of above 2 lines of code.
Logged
Lenn
Customers
Community Member
*****
Posts: 876

+/-


« Reply #3 on: June 20, 2010, 05:09:43 PM »

asia and Mith are right, but you dont get to set the UV of verts using AddTriangle.

What you can do is this:

Code:

        Dim Mesh As TVMesh

        Dim MeshWidth As Integer 'you get this from your mesh by yourself
        Dim MeshHeight As Integer

        Dim VertCount As Integer = Mesh.GetVertexCount

        Dim x, y, z, nx, ny, nz As Single
        Dim ru1, rv1, ru2, rv2 As Single
        Dim col As Integer

        Dim u, v As Single

        For CurVert As Integer = 0 To VertCount - 1

            Mesh.GetVertex(CurVert, x, y, z, nx, ny, nz, ru1, rv1, ru2, rv2, col)

            u = x / MeshWidth
            v = z / MeshHeight 'this is if the mesh is on the XZ axis. If it is on XY (upright) then use Y not Z

            Mesh.SetVertex(CurVert, x, y, z, nx, ny, nz, u, v, ru2, rv2, col)

        Next


Next



I think Mesh.GetVertex will return the vertex position relative to the mesh origin point, and not relative to world origin point. If it's mesh-relative then just substract the XZ of the position of the mesh from the xz value in the calculation.

Logged

TV3D 6.5 Community Docs - Read, use and please contribute!
Mithrandir
Community Member
*
Posts: 325


« Reply #4 on: June 20, 2010, 05:24:35 PM »

asia and Mith are right, but you dont get to set the UV of verts using AddTriangle.

True.

But you can also use AddVertex or AddVertexEx and AddFace instead of AddTriangle. Then you can specify UV right away.
Logged
Lenn
Customers
Community Member
*****
Posts: 876

+/-


« Reply #5 on: June 20, 2010, 05:31:43 PM »

You can also use SetGeometry and SetGeometryEx.  Grin That would really be the most efficient/pro way... But the fact is he only knows how to use AddTriangle.
Logged

TV3D 6.5 Community Docs - Read, use and please contribute!
harikumar001
Customers
Community Member
*****
Posts: 65


« Reply #6 on: June 21, 2010, 03:43:55 AM »

Thank you so much Mithrandir, Lenn and asia Smiley
Really appreciate that. Helped me solve my problem, yet again.

That piece of code by Lenn was really helpful. Might help others who face the same problem in the future.

Keep up the good work guys. Have a good day.

Regards


Logged
asia
Customers
Community Member
*****
Posts: 183


« Reply #7 on: June 21, 2010, 09:08:52 AM »

For Lenn: to be precise MeshWidth and  MeshHeight should be single NOT integers! Smiley
Logged

Fabio Musmeci
ENEA
CR Casaccia
Via Anguillarese 301
00060 Rome
Italy
musmeci@enea.it
+39 3333934898
Learning to live better on a smaller footprint..
Lenn
Customers
Community Member
*****
Posts: 876

+/-


« Reply #8 on: June 21, 2010, 12:54:13 PM »

Youre welcome. Smiley

asia, he is drawing in 2d so i assumed it would be in pixels, ie, his verts would be locked on unitary lengths.
Logged

TV3D 6.5 Community Docs - Read, use and please contribute!
asia
Customers
Community Member
*****
Posts: 183


« Reply #9 on: June 22, 2010, 04:20:02 AM »

Lenn you know in this case you are wrong:
Code:
u = x / MeshWidth
 v = z / MeshHeight 
will require type casting!
For this time you should agree!

PS
Always use OPTION STRICT ON!

PPS
Of course i am kidding... Lips sealed
« Last Edit: June 22, 2010, 04:23:33 AM by asia » Logged

Fabio Musmeci
ENEA
CR Casaccia
Via Anguillarese 301
00060 Rome
Italy
musmeci@enea.it
+39 3333934898
Learning to live better on a smaller footprint..
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.3 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks