Public Group

# is there a way to pic a random position and normal in a mesh?

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

## Recommended Posts

once a mesh has been loaded in , you are given its position and normals, i was wondering without using a shader , can you randomly get a position and it will give you its normal for that position

in doing a particle system , and would like to use a mesh for the shape of the emiotter , and would like a , my program to get a random position and normal , for the particls start point and verlosity?

##### Share on other sites
Hey Anthony ;) I would also like to know this xD

##### Share on other sites
You can make pick a random position by first picking a random triangle in the mesh, where the probability of each triangle is proportional to its area. Now you pick a random point in the triangle. Interpolate -say, using barycentric coordinates- between the normals at the vertices of the triangle to obtain the normal at your point.

Is that what you were looking for?

##### Share on other sites

You can make pick a random position by first picking a random triangle in the mesh, where the probability of each triangle is proportional to its area. Now you pick a random point in the triangle. Interpolate -say, using barycentric coordinates- between the normals at the vertices of the triangle to obtain the normal at your point.

Is that what you were looking for?

thats what i need , just cant figure out how to do it, ie access the mesh data to get at it. there put into the vertex buffer, and can get the number of vertx but how do you pull out the information form there, in c++

##### Share on other sites
When the mesh is loaded from a file, you'll see it contains vertex and index data.
It's most likely that the index data is formatted as a triangle strip.
i.e: 0,1,2 makes a triangle (0,1,2), then 3, makes the triangle (1,2,3) and so on...

With this, you'll be able to work out which vertices are used to make which triangles, by accessing the vertex list using the index values...

If you want to choose random triangles, you can select random indices, and just use the previous (or next) 2 indices in the index array to form a triangle of 3 vertices.

With this triangle you can calculate the position by adding the positions of the vertices together and dividing by three.
(Note, this will be in local space - instead of world space - you'll need to multiply this by the worldmatrix to make it world space)

To get the normal, assuming you want the face normal, you can calculate this by taking the normalized vector from vertex A -> vertex B, and performing the cross product of this with the normalized vector from vertex B -> vertex C. (Note, you may need to flip the order for the cross product to make sure it faces the right way. )
You'll also need to multiply this normal vector by the world matrix to transform it into worldspace.

This should give you a random triangle's position and normal.

Hope this helps.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 9
• 11
• 15
• 21
• 26