Get the Color of a triangle?

Blitz3D Forums/Blitz3D Programming/Get the Color of a triangle?

Chroma(Posted 2006) [#1]
Here's what I'm trying to do. I have an object that has a regular texture applied. The texture has red and blue parts to it. If my laser hits the red part of the texture I want "this" to happen. If it hits the blue part, I want "that" to happen.


Stevie G(Posted 2006) [#2]
Look for the pickedu,v code in the archives. Once you have the u & v coords simply get the pixel in question based on u*texturewidth and v * textureheight.

Stevie


kevin8084(Posted 2006) [#3]
just use CollisionTriangle, then use TriangleVertex to get one of the triangle's vertices...next just use VertexBlue and VertexRed to return the vertex's color to you.
***EDIT***
If you are using a pick function, then you can use PickedTriangle to get you the triangle's index.


Chroma(Posted 2006) [#4]
Now would the vertex colors correlate to the texture on the triangle? But yes maybe I didn't specify enough. I need to know the texture/brush color of a triangle that was involved in a collision.

Gonna try Stevie's and your methods...brb.


Chroma(Posted 2006) [#5]
Ok...what am I doing wrong?

num = CountCollisions(mesh)
surf = CollisionSurface(surf,num)
tri = CollisionTriangle(surf,num)
red# = VertexRed(???)


EDIT: Oooh....getting massive MAVs. :(


kevin8084(Posted 2006) [#6]
num=CountCollisions(mesh)
surf=CollisionSurface(surf,num)
tri=PickedTriangle() ; or tri=CollisionTriangle(surf,num)
vIndex=TriangleVertex(surf,tri,0)
red#=VertexRed(surf,vIndex)



Chroma(Posted 2006) [#7]
Ok this is working...but it's returning 255s. Which yeah is the vertex color duh lol.

What I need to actually do is return the average color of the triangle texture/brush that was involved in the collision.

EDIT: lol I posted this at the same time you did Kev.

colnum = CountCollisions(b\mesh)
surf = CollisionSurface(b\mesh,colnum)
tri = CollisionTriangle(b\mesh,colnum)
temp = TriangleVertex(surf,tri,0)
vert_red = VertexRed(surf,temp)
temp = TriangleVertex(surf,tri,1)
vert_green = VertexGreen(surf,temp)
temp = TriangleVertex(surf,tri,2)
vert_blue = VertexBlue(surf,temp)



kevin8084(Posted 2006) [#8]
To get the U/V at that point you can use the VertexU and VertexV...according to Stevie G you then multiply U by TextureWidth and V by TextureHeight...how about trying that.


Chroma(Posted 2006) [#9]
Ok I just saw the VertexU and VertexV commands.

If I have to do a readpixelfast to do this then I might have to find another method. I hear that has some hefty cost.


kevin8084(Posted 2006) [#10]
You could also just use GetColor to return the color components at x,y coordinate


Chroma(Posted 2006) [#11]
Looks like I can just put the texture in a texture buffer, then read the pixel color directly. Pretty sure I got this worked out now...thanks for the input...

EDIT: Yeah I saw that too. We keep hitting enter at the same time. :-)


kevin8084(Posted 2006) [#12]
sorry I couldn't help you more...another thing that you could do, though, is use CameraProject on the location of the vertex and then pass ProjectedX and ProjectedY to GetColor


Chroma(Posted 2006) [#13]
It's too bad that there's not a CollisionU() and CollisionV() command.

Now all I have to do is calculate the actual collision point ON the triangle. Using the texture color of the 3 vertices isn't accurate enough.


kevin8084(Posted 2006) [#14]
If it was me, I'd look for algorithms used for color-picker function in paint programs and see how they do it. Then see if it can be ported/modified for blitz. Good luck on your program!


b32(Posted 2006) [#15]
Maybe it is better to use a pick on the object, because it could be more accurate than a collision. Then you could do a LinePick from the collided object's centerpoint to the collisionobject's centerpoint. Else, you could find the PickedUV from the code vault. I believe it uses PickedTriangle, that can be replaced with CollisionTriangle for your program.
(edit, read kevins reply)
If the collision point in object is in view, you could use GetColor x, y. Or CameraPick camera, x, y.


Chroma(Posted 2006) [#16]
I think you're right bram. The easiest way to get accurate uv coords for the collision would be to linepick from the bullet to the center of the object upon collision and record the linepick uv stuff. Thanks man.


Chroma(Posted 2006) [#17]
Ok...the final solution!

Fredborg's UV linepick code from the archives. Works perfect and was exactly what I was looking for.


big10p(Posted 2006) [#18]
Ok...the final solution!

Fredborg's UV linepick code from the archives. Works perfect and was exactly what I was looking for.
Which is what Stevie G pointed you to in the very first reply! ;)


Chroma(Posted 2006) [#19]
Well you know I like to pursue all avenues and select the best one. kev8084's method seemed easier to implement but I didn't realize it wasn't what I needed.

My thread title was very misleading. Getting the 'color' of a triangle is simple. So I think a couple of people misunderstood ( kev8084). What I was actually looking for was the UV collision coordinates so I could get the texture pixel color at the exact point of impact. That was my bad for not being specific enough.

Ok so I lied. Fred's code wasn't EXACTLY what I was looking for but it's 99% of it. I just need it to work on collision, which isn't hard to implement...so...I'm adapting Fred's PickedUVW code to use collision. Here's what I have so far. It is working but there's a gap where no UVs are ready....it's still WIP.

I'm no math guru myself, but I do understand that you find the triangle that was involved in the collision. Find the 3 vertices and they're coordinates. Then find the exact collision coordinates and plot that on the triangle. Then find the vertice UV coordinates and use that to find the collision point UV coordinates. It just takes me longer to figure that stuff out than Fred. :p




Stevie G(Posted 2006) [#20]
@ Big10p, sometimes I feel invisible around here!!

@ Chroma, Why not post the function in the archives to compliment Freds. Code posted in the forums tends to get lost forever.

Stevie


Chroma(Posted 2006) [#21]
@ Stevie, thanks for pointing me to the archives lol. :p

Nothing should be posted in the archives unless it works 100%. And that function doesn't, so...

For some reason, when the bullet collides with a sphere, there's a dead section on the sphere where no UVs are recorded from CollisionU etc. I'm still investigating.