Texture Mapping

Skip to first unread message


Jul 18, 2010, 6:23:13 AM7/18/10
to pre3d
Hi, I have been playing around with your library and have found it
I was kinda curious about texture mapping and have discovered quad
callbak selectTexture implementation from one of the examples.
I was kinda hoping if you could shed some light on "Pre3d.TextureInfo"
and how this could be used to texture paint some of the complex shapes
like an 'Sphere'. you could take example shape as " var shape =

So I guess the question is about mapping a jpeg image onto 16*16 = 256



Jul 18, 2010, 3:16:08 PM7/18/10
to pr...@googlegroups.com

Because the texture is sent on the renderer, you need a chance to
change the texture as pre3d is drawing. This is what the
quad_callback allows you to do. It's a little bit clumsy, but it
gives your application a bit more power.

Basically the callback is called before a quad is rendered, and you
have a chance to modify any render state, like the texture. This is
just done by setting renderer.texture, which is a TexureInfo class.
This class specifies the classic u/v texture coordinates that map to
the vertices of that quad. So u0/v0 is maps to the first vertex of
the QuadFace. The u/v coordinates are pixel coordinates into the

The tricky bit when using something like a sphere tessellation code,
is now you need to somehow make between the quad face and how it
should be textured. You could try doing something like a cube map,
and/or just looking at the centroid/normal of the quad face to select
which part of the image to texture from.

Here are some nice textures that will work well for a sphere:


It shouldn't be so bad to map these uv's in the quad callback.

Let me know if that didn't help.

-- dean

> --
> You received this message because you are subscribed to the Google Groups "pre3d" group.
> To post to this group, send email to pr...@googlegroups.com.
> To unsubscribe from this group, send email to pre3d+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pre3d?hl=en.


Jul 18, 2010, 3:13:14 PM7/18/10
to pr...@googlegroups.com
Don't know if anything changed in the actual pre3d library for spheres, but I hacked together spherical mapping of jpegs to the pre3d created spheres here:


Jul 19, 2010, 12:40:19 AM7/19/10
to pre3d


Jul 19, 2010, 12:35:51 AM7/19/10
to pre3d
Thanks for a quick response Dean.

I actually did manage to texture map the sphere, after few hours of

I have few questions though

- Is it normal for texture image to appear inverted/rotated ?, I had
to rotate my base image by 180 degs to finally make it look right.
I tried changing texture mapping cordiantes, assuming the top left
point as the start (0,0), even after changing all chords, i either
still had the texture applied inverted or deformed.

- My other question is perhaps more of a sphere geometry related
doubt, for some very strange reason the top-most part and bottom-most
part of the sphere are not being mapped correctly( imagine north/south
poles in google earth / Bing earth), I had to skip them while texture
mapping(by returning True) in callback method, do you possibly have
any clue as to why that is happening ?

Once again Thank You for publishing such a wonderful lib.



Jul 19, 2010, 6:09:39 AM7/19/10
to pr...@googlegroups.com
The way the sphere is tessellated, it creates a quad grid for
everything except the top and bottom, which need to be triangles that
all share a single vertex in the middle. So in this way it's special
cased, and they are triangles and not quads. It could be that the
vertices are just ordered inconsistently, if that's the case it's an
easy change. I'll look into it.

-- dean


Jul 23, 2010, 4:20:25 AM7/23/10
to pre3d
I supose that could be an issue, you might wannna look into the order
in which the quads are being called in callbackfunction, I guess first
half set of triangles are called for northpole, and before completing
taht row, vertices are then being called for the last row.

Hope this could shed some light.

Thank You.



Jul 30, 2010, 8:22:40 AM7/30/10
to pr...@googlegroups.com

Sorry for the slow response. I sat down and tried this today, and
realized something I should have realized a lot earlier. Hopefully
the attached picture clearly illustrates the problem.

This is one of many problems when you're working in higher level
primitives (in this case triangles and quads on a <canvas>) instead of
working per-pixel. In general cube mapping is a per-pixel concept.
If you try to just map them to triangles, you end up with holes along
the edges of the cubes (illustrated in the attached picture as the
large triangles spanning over the black regions). You must also
support triangles that wrap around the edges of the image, etc. So
unless I'm missing something clever, I don't see an obvious way of
directly mapping a cube-map image to vertex triangles.

I could think of a few ways of still making this work. One would be
to tesselate based on the cubemap image, so triangles never span
different faces of the cubemap. Another would be to walk the quads
manually, and do a scanline per-pixel texture fetch, creating a new
<canvas> with the faces drawn there. This would be a less compact
image, since the texture will likely not be packed very well.
Additionally as an optimization you could do a hybrid, only do this
for triangles that span more than one face. I think this could
definitely be doable. You could also pre-compute this if your
geometry is static.

Things are never as easy as they seem. Thanks,
-- dean



Jul 30, 2010, 8:58:10 AM7/30/10
to pr...@googlegroups.com
It's also worth noting that when I started pre3d, per-pixel <canvas>
support was still a new thing, and not really usable or fast. Things
have changed a lot since, including some impressive uses of per-pixel


I've been tempted to make pre3d do things like the above, but in the
bigger picture WebGL is coming, and pre3d is not meant to be a
replacement. I'm trying to concentrate on doing things in pre3d that
are unique (like vector graphics), and it doesn't really make sense to
keep pushing the classic 3d concepts. I think there is still an
interesting future for applications like this:


-- dean

Reply all
Reply to author
0 new messages