Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7573933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:18:25+00:00 2026-05-30T16:18:25+00:00

I am trying to perform a realtime painting to the object texture. Using Irrlicht

  • 0

I am trying to perform a realtime painting to the object texture. Using Irrlicht for now, but that does not really matter.

So far, i’ve got the right UV coordinates using this algorithm:

  1. find out which object’s triangle user selected (raycasting, nothing
    really difficult)

  2. find out the UV (baricentric) coordinates of intersection point on
    that triangle

  3. find out the UV (texture) coordinates of each triangle vertex

  4. find out the UV (texture) coordinates of intersection point

  5. calculate the texture image coordinates for intersection point

But somehow, when i am drawing in the point i got in the 5th step on texture image, i get totally wrong results. So, when drawing a rectangle in cursor point, the X (or Z) coordinate of its is inverted:

enter image description here

enter image description here

Here’s the code i am using to fetch texture coordinates:

core::vector2df getPointUV(core::triangle3df tri, core::vector3df p)
{
    core::vector3df 
    v0 = tri.pointC - tri.pointA,
    v1 = tri.pointB - tri.pointA,
    v2 = p - tri.pointA;

    float dot00 = v0.dotProduct(v0),
    dot01 = v0.dotProduct(v1),
    dot02 = v0.dotProduct(v2),
    dot11 = v1.dotProduct(v1),
    dot12 = v1.dotProduct(v2);

    float invDenom = 1.f / ((dot00 * dot11) - (dot01 * dot01)),
    u = (dot11 * dot02 - dot01 * dot12) * invDenom,
    v = (dot00 * dot12 - dot01 * dot02) * invDenom;

    scene::IMesh* m = Mesh->getMesh(((scene::IAnimatedMeshSceneNode*)Model)->getFrameNr());

    core::array<video::S3DVertex> VA, VB, VC;
    video::SMaterial Material;

    for (unsigned int i = 0; i < m->getMeshBufferCount(); i++)
    {
    scene::IMeshBuffer* mb = m->getMeshBuffer(i);
    video::S3DVertex* vertices = (video::S3DVertex*) mb->getVertices();

    for (unsigned long long v = 0; v < mb->getVertexCount(); v++)
    {
        if (vertices[v].Pos == tri.pointA)
        VA.push_back(vertices[v]); else
        if (vertices[v].Pos == tri.pointB)
        VB.push_back(vertices[v]); else
        if (vertices[v].Pos == tri.pointC)
        VC.push_back(vertices[v]);

        if (vertices[v].Pos == tri.pointA || vertices[v].Pos == tri.pointB || vertices[v].Pos == tri.pointC)
        Material = mb->getMaterial();

        if (VA.size() > 0 && VB.size() > 0 && VC.size() > 0)
        break;
    }

    if (VA.size() > 0 && VB.size() > 0 && VC.size() > 0)
        break;
    }

    core::vector2df 
    A = VA[0].TCoords,
    B = VB[0].TCoords,
    C = VC[0].TCoords;

    core::vector2df P(A + (u * (C - A)) + (v * (B - A)));
    core::dimension2du Size = Material.getTexture(0)->getSize();
    CursorOnModel = core::vector2di(Size.Width * P.X, Size.Height * P.Y);
    int X = Size.Width * P.X, Y = Size.Height * P.Y;

    // DRAWING SOME RECTANGLE    
    Material.getTexture(0)->lock(true);
    Device->getVideoDriver()->setRenderTarget(Material.getTexture(0), true, true, 0);
        Device->getVideoDriver()->draw2DRectangle(video::SColor(255, 0, 100, 75), core::rect<s32>((X - 10), (Y - 10), 
            (X + 10), (Y + 10)));
    Device->getVideoDriver()->setRenderTarget(0, true, true, 0);
    Material.getTexture(0)->unlock();

    return core::vector2df(X, Y);
}

I just wanna make my object paintable in realtime. My current problems are: wrong texture coordinate calculation and non-unique vertex UV coordinates (so, drawing something on the one side of the dwarfe’s axe would draw the same on the other side of that axe).

How should i do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:18:27+00:00Added an answer on May 30, 2026 at 4:18 pm

    I was able to use your codebase and get it to work for me.

    Re your second problem “non-unique vertex UV coordinates”:
    Well you are absolutely right, you need unique vertexUVs to get this working, which means that you have to unwrap you models and don’t make use of shared uv-space for e.g. mirrored elements and stuff. (e.g. left/right boot – if they use the same uv-space, you’ll paint automatically on both, where you want the one to be red and the other to be green). You can check out “uvlayout” (tool) or the uv-unwrap modifier ind 3ds max.

    Re the first and more important problem: “**wrong texture coordinate calculation”:
    the calculation of your baycentric coordinates is correct, but as i suppose your input-data is wrong. I assume you get the triangle and the collisionPoint by using irrlicht’s CollisionManager and TriangleSelector. The problem is, that the positions of the triangle’s vertices (which you get as returnvalue from the collisionTest) is in WorldCoordiates. But you’ll need them in ModelCoordinates for the calculation, so here’s what you need to do:

    pseudocode:

    1. add the node which contains the mesh of the hit triangle as parameter to getPointUV()
    2. get the inverse absoluteTransformation-Matrix by calling node->getAbsoluteTransformation() [inverse]
    3. transform the vertices of the triangle by this inverse Matrix and use those values for the rest of the method.

    Below you’ll find my optimized method wich does it for a very simple mesh (one mesh, only one meshbuffer).

    Code:

    irr::core::vector2df getPointUV(irr::core::triangle3df tri, irr::core::vector3df p, irr::scene::IMeshSceneNode* pMeshNode, irr::video::IVideoDriver* pDriver)
    {
        irr::core::matrix4 inverseTransform(
        pMeshNode->getAbsoluteTransformation(),
          irr::core::matrix4::EM4CONST_INVERSE);
    
        inverseTransform.transformVect(tri.pointA);
        inverseTransform.transformVect(tri.pointB);
        inverseTransform.transformVect(tri.pointC);
    
        irr::core::vector3df 
        v0 = tri.pointC - tri.pointA,
        v1 = tri.pointB - tri.pointA,
        v2 = p - tri.pointA;
    
        float dot00 = v0.dotProduct(v0),
        dot01 = v0.dotProduct(v1),
        dot02 = v0.dotProduct(v2),
        dot11 = v1.dotProduct(v1),
        dot12 = v1.dotProduct(v2);
    
        float invDenom = 1.f / ((dot00 * dot11) - (dot01 * dot01)),
        u = (dot11 * dot02 - dot01 * dot12) * invDenom,
        v = (dot00 * dot12 - dot01 * dot02) * invDenom;
    
        irr::video::S3DVertex A, B, C;
        irr::video::S3DVertex* vertices = static_cast<irr::video::S3DVertex*>(
          pMeshNode->getMesh()->getMeshBuffer(0)->getVertices());
    
        for(unsigned int i=0; i < pMeshNode->getMesh()->getMeshBuffer(0)->getVertexCount(); ++i)
        {
          if( vertices[i].Pos == tri.pointA)
          {
            A = vertices[i];
          }
          else if( vertices[i].Pos == tri.pointB)
          {
            B = vertices[i];
          }
          else if( vertices[i].Pos == tri.pointC)
          {
            C = vertices[i];
          }
        }
    
        irr::core::vector2df t2 = B.TCoords - A.TCoords;
        irr::core::vector2df t1 = C.TCoords - A.TCoords;
    
        irr::core::vector2df uvCoords = A.TCoords + t1*u + t2*v;
    
        return uvCoords;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to perform a single boolean NOT operation, it appears that under MS SQL
Trying to perform a nested object form. The page loads with no errors, but
I'm trying to perform some realtime phone-number validation using javascript. I don't need help
I am trying to perform an ajax.get using jquery within document.ready . But the
I'm trying perform a relatively basic TCP socket send/receive operation on Windows CE using
I'm trying to perform a LINQ query on a DataTable object and bizarrely I
I'm trying to perform a bitwise NOT in SQL Server. I'd like to do
I am trying to perform some unit testing on the iphone but for some
I am trying to perform a Calculus Cross Product calculation using Linq and trying
I was trying to perform subsequent calls verification and I found that moq supports

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.