newbie questions about triangle mode

53 views
Skip to first unread message

Simon Haegler

unread,
Jul 14, 2015, 6:25:52 PM7/14/15
to pt...@googlegroups.com
hi ptex users

i am trying to convert a traditional triangular mesh with UV mapped textures to ptex (triangle mode).
conceptually, here is what i am trying to do:

1. for each triangle, look at its texture and determine the necessary face resolution to store it without loss in ptex
2. in UV space, uniformly subdivide the triangle down to the desired resolution
3. sample the original texture at the subdivided triangles UV centers
4. store the new value in the ptex buffer at index i, as specified in http://ptex.us/tritex.html

i am testing this with a single triangle and all i see in maya (or the ptex viewer) is a distorted texture.
is this just a bug in my code somewhere or did i make a conceptual mistake?

grateful for any pointers & best,
simon

Brent Burley

unread,
Jul 14, 2015, 6:35:02 PM7/14/15
to pt...@googlegroups.com, simon....@gmail.com
Without more info it would be hard to guess what might have gone wrong.  Can you provide an image of the distortion problem you're seeing?  It might also help to see the ptex file and the relevant snippet of your code.

Simon Haegler

unread,
Jul 15, 2015, 3:42:16 AM7/15/15
to pt...@googlegroups.com, simon....@gmail.com
On Wednesday, 15 July 2015 00:35:02 UTC+2, Brent Burley wrote:
Without more info it would be hard to guess what might have gone wrong.  Can you provide an image of the distortion problem you're seeing?  It might also help to see the ptex file and the relevant snippet of your code.
 
thanks for your time, brent.

i've attached the ptex file. below is the input (left) and resulting import into maya (right). 





best,
simon
test_triangle.ptx

Brent Burley

unread,
Jul 15, 2015, 11:17:00 AM7/15/15
to pt...@googlegroups.com, simon....@gmail.com
I see the same distortion in the ptex file that you are seeing in maya so I'm guessing the problem is in your conversion.  Care to post a code snippet?

--

---
You received this message because you are subscribed to the Google Groups "ptex" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ptex+uns...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Simon Haegler

unread,
Jul 15, 2015, 12:01:37 PM7/15/15
to pt...@googlegroups.com, simon....@gmail.com
On Wednesday, 15 July 2015 17:17:00 UTC+2, Brent Burley wrote:
I see the same distortion in the ptex file that you are seeing in maya so I'm guessing the problem is in your conversion.  Care to post a code snippet?

sure. here is the method to convert our internal texture buffers into triangular ptex buffers:

void sampleTexture(...) {
// source texture (let's assume it is also quadratic)
const uint8_t* srcTexBuf = ...
size_t srcTexBufSize = ...
uint32_t srcTexWidth = ...
uint32_t srcTexHeight = ...
uint8_t srcBPP = ... // bytes per pixel

// source triangle in uv space
Vector2d A = ..., B = ..., C = ... // eg. 0/0, 1/1, 0/1

// determine optimal face resolution (power-of-two)
uint32_t potRes = ...
int8_t logDim = log2(potRes);

// allocate the target buffer for ptex
const size_t ptexBufSize = potRes*potRes*bpp;
uint8_t* ptexBuf = ...

// helper function (see below) to recursively generate potRes^2 uniformly subdivided triangle middle points
// (= the sampling points for the ptex texels)
std::vector<Vector2d> samplePoints;
generateSamplePoints(logDim, 0, A, B, C, samplePoints);

// sample the source texture and copy the texels into the ptex buffer
// using the indexing schema described in http://ptex.us/tritex.html
for (Vector2d p : samplePoints) {

// wrap UVs into to 0..1
if (p.x > 1.0 || p.x < 0.0) p.x -= std::floor(p.x);
if (p.y > 1.0 || p.y < 0.0) p.y -= std::floor(p.y);

// lookup source pixel
Vector2d uvt(p.x * srcTexWidth, p.y * srcTexHeight);
Vector2<uint32_t> uvi(std::floor(uvt.x), std::floor(uvt.y));
size_t srcTexIdx = bpp * (uvi.y*texWidth + uvi.x);
const uint8_t* srcTexData = &srcTexBuf[srcTexIdx];

// compute triangular texture index
Vector2d ptexUVT(p.x * potRes, p.y * potRes);
Vector2<uint32_t> ptexUVI(std::floor(ptexUVT.x), std::floor(ptexUVT.y));
Vector2d ptexDUV(ptexUVT.x - ptexUVI.x, ptexUVT.y - ptexUVI.y);
size_t ptxIdx = 0;
if (ptexDUV.x + ptexDUV.y <= 1.0) {
ptxIdx = ptexUVI.x + ptexUVI.y*potRes;
}
else {
ptxIdx = (potRes*potRes-1) - (ptexUVI.y+ptexUVI.x*potRes);
}
ptxIdx *= bpp;
// copy pixel
std::memcpy(&ptexBuf[ptxIdx], srcTexData, bpp);
}

// ... ptex writeFace ...
}


void generateSamplePoints(uint8_t N, uint8_t n, Vector2d T1, Vector2d T2, Vector2d T3, std::vector<Vector2d>& s) {
if (n == N) {
s.push_back(Vector2d((T1+T2+T3)/3.0);
return;
}

util::Vector2d A = 0.5 * (T1 + T2);
util::Vector2d B = 0.5 * (T2 + T3);
util::Vector2d C = 0.5 * (T1 + T3);

generateSamplePoints(N, n+1, T1, A, C, s);
generateSamplePoints(N, n+1, A, T2, B, s);
generateSamplePoints(N, n+1, B, T3, C, s);
generateSamplePoints(N, n+1, A, B, C, s);
}




here is what i get with the above code. on the upper right you see the input triangle with a 2x2 texture (turned off filtering in maya). lower right shows the alembic triangle with the ptex file assigned. on the left is the ptex file viewer. the ptex file is attached.


 
mesh_CityEngineMaterial.ptx

Brent Burley

unread,
Jul 15, 2015, 2:29:08 PM7/15/15
to pt...@googlegroups.com, simon....@gmail.com
It looks like you are using the same UV values for accessing the source texture and the target ptex texture though I don't imagine this is the mapping you want.  You could instead call generateSamplePoints twice, once with the assigned UVs and once with the ptex UVs (0 0), (1 0), (0 1), or you could generate the points in tandem with a 4d vector.

Alternately, you could just enumerate the ptex UVs directly (for i from 0 to res*res-1) using the following mapping (the inverse of the mapping shown on http://ptex.us/tritex.html):

    int ui = i % res, vi = i / res;

    u = (ui + 0.5 - 1.0/6) / res;
    v = (vi + 0.5 - 1.0/6) / res;
    if (u + v > 1) {
        float utmp = u;
        u = 1 - v;
        v = 1 - utmp;
    }

Then, given assigned UVs A, B, and C that correspond to ptex UVs (0 0), (1 0), (0 1), the source UV can be computed using barycentric interpolation: A*(1-u-v) + B*u + C*v.


--

Simon Haegler

unread,
Jul 16, 2015, 3:14:01 AM7/16/15
to pt...@googlegroups.com, simon....@gmail.com
doh! thanks for pointing this out! things are looking good now.

best,
simon
Reply all
Reply to author
Forward
0 new messages