POV-Ray : Newsgroups : povray.newusers : Image mapping on triangles Server Time
31 Oct 2024 06:00:11 EDT (-0400)
  Image mapping on triangles (Message 1 to 9 of 9)  
From: solanb
Subject: Image mapping on triangles
Date: 3 Nov 2009 08:20:00
Message: <web.4af02da516d6504fcf0bfa9c0@news.povray.org>
Hi,

I'm working with 3d scenes of plants. Each plant is composed of leaves and each
leaved is composed of triangles. The whole scene contains thousands of
triangles.

I'd like to add a texture from an image file on the leaves and I tried several
techniques but I'm not sure of the best way to do.
My constraint is that the image's projection on a given triangle must be in the
same plane of the triangle.
Using image_map, I have to rotate the texture in the triangle's plane: is there
a way to calculate rotation parameters from triangle's coordinates?

I there an easier way to do this?

Thanks for your help,
Benoit


Post a reply to this message

From: Mike Williams
Subject: Re: Image mapping on triangles
Date: 3 Nov 2009 08:43:26
Message: <JnhwnfAPND8KFwgq@econym.demon.co.uk>
Wasn't it solanb who wrote:
>Hi,
>
>I'm working with 3d scenes of plants. Each plant is composed of leaves and each
>leaved is composed of triangles. The whole scene contains thousands of
>triangles.
>
>I'd like to add a texture from an image file on the leaves and I tried several
>techniques but I'm not sure of the best way to do.
>My constraint is that the image's projection on a given triangle must be in the
>same plane of the triangle.
>Using image_map, I have to rotate the texture in the triangle's plane: is there
>a way to calculate rotation parameters from triangle's coordinates?

If you collect your triangles into a mesh or mesh2, you can use 
uv_mapping.

-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

From: solanb
Subject: Re: Image mapping on triangles
Date: 3 Nov 2009 10:30:01
Message: <web.4af04b4ba22773f2cf0bfa9c0@news.povray.org>
Mike Williams <nos### [at] econymdemoncouk> wrote:
> Wasn't it solanb who wrote:
> >Hi,
> >
> >I'm working with 3d scenes of plants. Each plant is composed of leaves and each
> >leaved is composed of triangles. The whole scene contains thousands of
> >triangles.
> >
> >I'd like to add a texture from an image file on the leaves and I tried several
> >techniques but I'm not sure of the best way to do.
> >My constraint is that the image's projection on a given triangle must be in the
> >same plane of the triangle.
> >Using image_map, I have to rotate the texture in the triangle's plane: is there
> >a way to calculate rotation parameters from triangle's coordinates?
>
> If you collect your triangles into a mesh or mesh2, you can use
> uv_mapping.
>
> --
> Mike Williams
> Gentleman of Leisure

Ok, but then how to define the uv_vector corresponding to each triangle?

My image to be mapped consists of spots of a given dimension (eg. 1cm diameter),

also in cm. And I want to keep the scale of my image when I project it on the
scene's triangles.
Using uv_mapping without uv_vector doesn't produce what I expect but the
definition uv_vector is not clear for me.

Thanks in advance!
Benoit


Post a reply to this message

From: solanb
Subject: Re: Image mapping on triangles
Date: 3 Nov 2009 11:15:01
Message: <web.4af0563fa22773f2cf0bfa9c0@news.povray.org>
> If you collect your triangles into a mesh or mesh2, you can use
> uv_mapping.
>
> --
> Mike Williams
> Gentleman of Leisure

Ok, but then how to define the uv_vector corresponding to each triangle?

My image to be mapped consists of spots of a given dimension (eg. 1cm diameter),

also in cm. And I want to keep the scale of my image when I project it on the
scene's triangles.
Using uv_mapping without uv_vector doesn't produce what I expect but the
definition uv_vector is not clear for me.

Thanks in advance!
Benoit


Post a reply to this message

From: Captain Jack
Subject: Re: Image mapping on triangles
Date: 3 Nov 2009 11:41:56
Message: <4af05d54@news.povray.org>
"solanb" <nomail@nomail> wrote in message 
news:web.4af04b4ba22773f2cf0bfa9c0@news.povray.org...
 > Ok, but then how to define the uv_vector corresponding to each triangle?
>
> My image to be mapped consists of spots of a given dimension (eg. 1cm 
> diameter),

> are
> also in cm. And I want to keep the scale of my image when I project it on 
> the
> scene's triangles.
> Using uv_mapping without uv_vector doesn't produce what I expect but the
> definition uv_vector is not clear for me.
>
> Thanks in advance!
> Benoit

Thoughts about UV mapping in general:

If you have a triangle defined in 3D space by these points:
    A = <x1, y1, z1>
    B = <x2, y2, z2>
    C = <x3, y3, z3>

you'll define a corresponding triangle in your image map, in two dimensions:
    a = <u1, v1>
    b = <u2, v2>
    c = <u3, v3>

Traditionally, X/Y/Z are used for coordinates in 3D space, and U/V are used 
for the coordinates in the mapped 2D space (W is used in matrix 
representations of 3D points, so U & V are next in line from that end of the 
Latin alphabet).

When a program like POV goes to figure out what color your 3D triangle is, 
it takes the point in the plane of the triangle that it's looking at, and 
figures out how close it is relatively to points A, B, & C in 3D space, then 
uses the same relationship to find a corresponding pixel in an image map 
that is that (relative) distance from points a, b, & c.

The values for U & V are normally expressed as values from zero to one, 
reading from left to right and bottom to top. When you specify the triangle, 
you say both which 3D points define the shape in space, and which 2D points 
map to a flat picture, and in what order.

So, the program figures out the distance from each ABC, determines the same 
relative distances from abc, and comes up with a point on the 2D plane that 
equals 0.300 by 0.250. It then multiplies those numbers by the number of 
pixels in each direction, finds the pixel in the image map at that point, 
then uses the color of that pixel for the color of the triangle in the 
rendered image.

The mapping is always done in the plane of the triangle to the plane of the 
image, so you should get what you're looking for there. You can use the same 
UV coordinates for every leaf in the mesh, if you want, and they'll all have 
the same color (before lighting, fog, whatever, etc.)

Here's an example of a shape made from two triangles in a POV mesh that form 
a diamond shape, each triangle using the same part of an image (which might 
look a little weird if you actually rendered it):

// The image map color
#declare Image_Based_Texture =
    texture {
        pigment {
            uv_mapping
            image_map {
                jpeg "Leaf.jpg"
                }
            }
        }

// The "leaf"
mesh2 {
    // Physical points in 3D space that define
    // the shape
    vertex_vectors {
        4,
        < 0.000, 0.000, 0.000>,
        <-1.000, 1.000, 0.500>,
        < 0.750, 1.100, 0.250>,
        < 0.100, 2.000, 0.000>
        }
    // Points inside the image file
    uv_vectors {
        3,
        <0.000, 0.000>,
        <1.000, 0.000>,
        <0.500, 1.000>
        }
    // List of textures that are used for the shape
    texture_list {
        1,
        texture { Image_Based_Texture }
        }
    // Build the actual triangles of the shape from
    // the vertex_vectors list plus textures
    face_indices {
        2,
        <0, 1, 2>, 0,
        <1, 2, 3>, 0
        }
    // For each face, show the points in the image
    // plane that create its color
    uv_indices {
        2,
        <0, 1, 2>,
        <0, 1, 2>
        }
    }


Post a reply to this message

From: Chris B
Subject: Re: Image mapping on triangles
Date: 3 Nov 2009 11:48:48
Message: <4af05ef0$1@news.povray.org>
"solanb" <nomail@nomail> wrote in message 
news:web.4af0563fa22773f2cf0bfa9c0@news.povray.org...
>
>> If you collect your triangles into a mesh or mesh2, you can use
>> uv_mapping.
>>
>> --
>> Mike Williams
>> Gentleman of Leisure
>
> Ok, but then how to define the uv_vector corresponding to each triangle?
>
> My image to be mapped consists of spots of a given dimension (eg. 1cm 
> diameter),

> are
> also in cm. And I want to keep the scale of my image when I project it on 
> the
> scene's triangles.
> Using uv_mapping without uv_vector doesn't produce what I expect but the
> definition uv_vector is not clear for me.
>
> Thanks in advance!
> Benoit
>


If all of your leaves are the same basic shape then it's relatively easy to 
work out uv_vectors and reuse them on all of your leaves.

In the example below I've created a simple shape using 6 vertices to create 
4 triangular faces aligned roughly to the plane y=0. I've added uv_mapping 
coordinates that  map the brick pigment using the x and z coordinates of the 
vertices. Because the shape is not totally flat you can see (when you render 
it) that there's some stretching of the pigment, but, in this position you 
can fairly easily adjust the uv_mapping until you've taken account of that.

Once you've got this uv-mapping you can use it for any other mesh2 object of 
the same shape, although the vertex coordinates may be different because the 
leaf would be at a different position and orientation in 3D space. The 
uv_mapping takes the part of the pigment that crosses the plane z=0 and map 
it to the vertices as specified.


camera {location <0.4,1,0.4> look_at <0.5,0,0.5>}
light_source {<-3, 16,-10> color rgb 1}

mesh2 {
  vertex_vectors{
    6
    <0.5 ,-0.1,0  >
    <0.1 ,-0.1,0.5>
    <0.35, 0  ,0.4>
    <0.5 , 0  ,1  >
    <0.65, 0  ,0.4>
    <0.9 ,-0.1,0.5>
  }
  uv_vectors{
    6
    <0.5 ,0  >
    <0.1 ,0.5>
    <0.35,0.4>
    <0.5 ,1  >
    <0.65,0.4>
    <0.9 ,0.5>
  }
  face_indices{
    4
    <0,1,2>
    <0,2,3>
    <0,3,4>
    <0,4,5>
  }
  pigment {uv_mapping brick scale 0.01 }
}

Hope this helps,

Regards,
Chris B.


Post a reply to this message

From: clipka
Subject: Re: Image mapping on triangles
Date: 3 Nov 2009 19:44:42
Message: <4af0ce7a@news.povray.org>
solanb schrieb:

> My constraint is that the image's projection on a given triangle must be in the
> same plane of the triangle.
> Using image_map, I have to rotate the texture in the triangle's plane: is there
> a way to calculate rotation parameters from triangle's coordinates?
> 
> I there an easier way to do this?

Yes: UV mapping.

Note that for this you will need a mesh (or mesh2) object (which is 
actually just a memory- and performance-efficient collection of 
triangles, so you should use this object type anyway).

Essentially, what this does is that you specify which points of the 
texture you want to end up at the individual vertices, and POV-Ray will 
automatically rotate and scale and skew the texture - and even 
nonlinearily distort it - as needed. For each triangle individually.


Post a reply to this message

From: clipka
Subject: Re: Image mapping on triangles
Date: 3 Nov 2009 19:53:39
Message: <4af0d093$1@news.povray.org>
solanb schrieb:

> Ok, but then how to define the uv_vector corresponding to each triangle?
> 
> My image to be mapped consists of spots of a given dimension (eg. 1cm diameter),

> also in cm. And I want to keep the scale of my image when I project it on the
> scene's triangles.

Like with all image-backed texture stuff in POV-Ray, coordinate values 
<0,0>, <0,1>, <1,0> and <1,1> correspond to the corners of the texture 
image.

If you want the image to repeat, just use larger coordinate values; e.g. 
if you use <0,0>, <0,100>, <100,0> for some triangle's UV coordinates, 
the image will repeat 100 times along the edges.

Provided that you know the shape of your triangles, you can compute 
suitable UV coordinates from the length of any single of the triangle's 
edges (using vlength(P1-P2) for instance, or using the same base values 
you use to generate the triangle XYZ coordinates in the first place).


Post a reply to this message

From: solanb
Subject: Re: Image mapping on triangles
Date: 5 Nov 2009 03:55:01
Message: <web.4af292dba22773f2cf0bfa9c0@news.povray.org>
Thanks everybody for your help.

uv_mapping is indeed the right way to solve my problem.

I define first the texture from my image file.
Then I create the mesh with the triangles' coordinates and calculate the
uv_vectors coordinates from the distances between the triangles coordinates.

Have a nice day!
Benoit

clipka <ano### [at] anonymousorg> wrote:
> solanb schrieb:
>
> > Ok, but then how to define the uv_vector corresponding to each triangle?
> >
> > My image to be mapped consists of spots of a given dimension (eg. 1cm diameter),

> > also in cm. And I want to keep the scale of my image when I project it on the
> > scene's triangles.
>
> Like with all image-backed texture stuff in POV-Ray, coordinate values
> <0,0>, <0,1>, <1,0> and <1,1> correspond to the corners of the texture
> image.
>
> If you want the image to repeat, just use larger coordinate values; e.g.
> if you use <0,0>, <0,100>, <100,0> for some triangle's UV coordinates,
> the image will repeat 100 times along the edges.
>
> Provided that you know the shape of your triangles, you can compute
> suitable UV coordinates from the length of any single of the triangle's
> edges (using vlength(P1-P2) for instance, or using the same base values
> you use to generate the triangle XYZ coordinates in the first place).


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.