Jump to content
  • Advertisement
Sign in to follow this  
ramy

getting pixel color from .X mesh

This topic is 4870 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, i have a problem. I have a mesh file, and i want to get a pixel color from its texture, when i click on it. i want the pixel color from the point where i clicked at. well i started off by projecting the mouse points from screen to world space, got a ray from it, and intersected the mesh using the ready functions, and noticed that i get information back from the function. i get the barycentric coordinates (the U and V) and i get the face index where i intersected. now how to get the pixel color of the texture? plz help.

Share this post


Link to post
Share on other sites
Advertisement
When you have the texture coordinates, you can easily convert them to texels ( X = U * texture_width; Y = V * texture_height).
Then you lock the texture surface(pseudo code):

D3DLOCKED_RECT LR;
RECT R;
Byte pxArr(3) ;

R.bottom = Y + 1;
R.left = X;
R.right = X + 1;
R.Top = Y;

m_ColTex->LockRect(0, LR, R, 0);
DXCopyMemory(pxArr, ByVal LR.pBits, 4);
m_ColTex->UnlockRect(0);

color = RGB(pxArr(2), pxArr(1), pxArr(0)) ;


I'm not sure about the function parameters, so maybe you have to play around with them a bit.

Share this post


Link to post
Share on other sites
i have barycentric texture coordinates UV's, is that wat u mean by "When you have the texture coordinates"?

Share this post


Link to post
Share on other sites
Quote:
Original post by ramy
i have barycentric texture coordinates UV's, is that wat u mean by "When you have the texture coordinates"?

Almost. Barycentric cooridnates are not the same thing as texture coordinates but you can use the barycentric coordinates of the intersection to figure out the exact texture coordinates you'll need to sample the texture.

Basically, barycentric coordinates tell you by how much the vertices of the triangle are weighted by to get to the point of intersection. The same holds true for the texture coordinates of the triangle. If you're interested in more info about how barycentric coordinates work and the kind of stuff you can do with them, do a google search and you'll find lots of data.

With barycentric coordinates, you can find the exact 3D point of intersection in your mesh by using the following equation:

(V0, V1, and V2 are the vertices of the triangle. a, and b are the barycentric coordinates of the intersection point)
P = V0 + a(V1-V0) + b(V2-V0)

You can use the same formula to find the texture coordinates of the intersection point:

(u0, v0 are the texture coordinates of V0, u1, v1 are the texture coordinates of V1, etc...)
u = u0 + a(u1-u0) + b(u2-u0)
v = v0 + a(v1-v0) + b(v2-v0)

Hope this helps,
neneboricua

Share this post


Link to post
Share on other sites
Quote:
Original post by ramy
i have barycentric texture coordinates UV's, is that wat u mean by "When you have the texture coordinates"?


Hi Ramy,
See my reply to you in this post

HTH,
Cambo_frog

Share this post


Link to post
Share on other sites
I have closed the other post, so here's Cambo_frog's reply for convenience:
Quote:
Original post by Cambo_frog
i Ramy,
The u, v barycentric coordinates refer to a point within a triangle your ray intesects (your "IntersectInformation").

Call your triangle vertices v0, v1 and V2.
Call the texture coordinates in your vertex structure, tu and tv.

You can derive a third barycentric coordinate w as:
float w = 1.0f - (u + v);

Your tu, tv values at the intersection point is:
float intersectionTu = v0.tu * u + v1.tu * v + v2.tu * w;
float intersectionTv = v0.tv * u + v1.tv * v + v2.tv * w;

HTH,
Cambo_frog

Share this post


Link to post
Share on other sites
Hey Cambo_frog i saw ur post.

first of all, i dont have access to the Vertices, or in other words from the Mesh object, i only have the VertexBuffer and VertexFormat, i dont know the structure of the vertex itself.

so that concludes that i cant get access to the "intersectionTu" and "intersectionTv" u were refering to, and also i cant get access to the vertices them selves.

if there is a way, plz point me to it.

Once again neneboricua19, i have no accessto the vertices, same as above.

NOTE: im loading the mesh from a .X file.

thx

Share this post


Link to post
Share on other sites
The vertex buffer is where the vertices are stored. You need to have access to the vertices. There is no way around that. If you performed a ray-triangle intersection in the first place, you needed to have access to the vertices. You can access the vertices by locking the vertex and index buffers (not something you want to do every frame). If you're going to be locking certain meshes over and over each frame to check their vertices, it might help you to have a copy of those vertices stored in system memory so you don't have to lock the ones in video memory all the time.

As far as not knowing the vertex structure, you don't need to know the exact vertex structure. You only need to know at what offsets the components you want are located. In your case, you're only interested in the texture coordinates. You need to figure out the offset within each vertex where the texture coordinates are located. If you're using the ID3DXMesh object, you can do this by calling ID3DXMesh::GetDeclaration and taking a look at the array that is returned. If you're using your own model format, then that's something else you need to take care of.

neneboricua

Share this post


Link to post
Share on other sites
well i took a look at the vertexformat of the mesh object, and i figured out the structure of the vertex, and extracted all of the vectices and all indices as well.

i have managed to get the faces that the intersection function told me about. the intesect method in the mesh object, returned the faceindex. i fetched in indices array with that faceindex, and added 1 and then 2 to get the 3 vectors.
those were my 3 vectors. but the original point, seems to be outside these three vectors.

something goes wrong while i try to compute the intersected texture U and V, i tried both of ur methods. maybe cause the vectors i get might be the wrong ones.

Share this post


Link to post
Share on other sites
The face index returned by the D3DXIntersect method (I'm assuming that's what you're using) indicates the triangle intersected by means of the index buffer. So you could get the vertices of the mesh like this:

V0 = pVertexBuffer[pIndexBuffer[faceIndex*3]];
V1 = pVertexBuffer[pIndexBuffer[faceIndex*3 + 1]];
V2 = pVertexBuffer[pIndexBuffer[faceIndex*3 + 2]];

You can then use the texture coordinates of these vertices in the equations supplied previously.

neneboricua

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!