library for beam tracing (beam intersection) on a 3D Polygon model - opengl

I want to simulate a laser scanner which emits laser beam onto a 3D model to measure distance or other features from the model. The 3D model consists of vertices in xyz coordinate and faces; each vertex has also some user defined features.
The method should be simple. I define a view point and view vector (i.e. laser beam); what I need to do is checking the first vertex or the first face which is intersected with the view vector, then I can measure the distance and evaluate feature from the nearest vertices.
Is there any available library or tools to do that?

What you are talking about is, in a very literal sense, ray tracing. The maths and code behind doing this is not particularly complicated, especially if you don't have to consider reflections. There's a tutorial for doing exactly this in C++ here; triangle intersection is almost as simple as sphere intersection, and you can completely ignore the surface properties. If you don't want to write your own code (but seriously, it's maybe a hundred lines to do what you're looking for), there's a hint as to how to get Povray to do what you're after here.
EDIT: More maths, including triangle intersection, is here.

Related

Collision Detection between quads OpenGL

I am making a simple 3D OpenGL game. At the moment I have four bounding walls, a random distribution of internal walls and a simple quad cube for my player.
I want to set up collision detection between the player and all of the walls. This is easy with the bounding walls as i can just check if the x or z coordinate is less than or greater than a value. The problem is with the interior walls. I have a glGenList which holds small rectangular wall segments, at the initial setup i randomly generate an array of x,z co ordinates and translate these wall segments to this position in the draw scene. I have also added a degree of rotation, either 45 or 90 which complicates the collision detection.
Could anyone assist me with how I might go about detecting collisions here. I have the co ordinates for each wall section, the size of each wall section and also the co ordinates of the player.
Would i be looking at a bounded box around the player and walls or is there a better alternative?
I think your question is largely about detecting collision with a wall at an angle, which is essentially the same as "detecting if a point matches a line", which there is an answer for how you do here:
How can I tell if a point belongs to a certain line?
(The code may be C#, but the math behind it applies in any language). You just have to replace the Y in those formulas for Z, since Y appears to not be a factor in your current design.
There has been MANY articles and even books written on how to do "good" collision detection. Part of this, of course, comes down to a "do you want very accurate or very fast code" - for "perfect" simulations, you may sacrifice speed for accuarcy. In most games, of the players body "dents" the wall just a little bit because the player has gone past the wall intersection, that's perhaps acceptable.
It is also useful to "partition the space". The common way for this is "Binary space partitioning", which is nicely described and illustrated here:
http://en.wikipedia.org/wiki/Binary_space_partition
Books on game programming should cover basic principles of collision detection. There is also PLENTY of articles on the web about it, including an entry in wikipedia: http://en.wikipedia.org/wiki/Collision_detection
Short of making a rigid body physics engine, one could use point to plane distance to see if any of the cubes corner points are less than 0.0f away from the plane (I would use FLT_MIN so the points have a little radius to them). You will need to store a normalized 3d vector (vector of length 1.0f) to represent the normal of the plane. If the dot product between the vector from the center of the plane to the point and the plain normal is less than the radius you have a collision. After that, you can take the velocity (the length of the vector) of the cube, multiply it by 0.7f for some energy absorption and store this as the cubes new velocity. Then reflect the normalized velocity vector of the cube over the normal of the plane, then multiply that by the previously calculated new velocity of the cube.
If you really want to get into game physics, grab a pull from this guys github. I've used his book for a Physics for games class. There are some mistakes in the book so be sure to get all code samples from github. He goes through making a mass aggregate physics engine and a rigid body one. I would also brush up on matrices and tensors.

Creating and implementing a metaball skin system in OpenGL

I'd like to create or find an open source metaball skin system, which uses metaballs, voxelization to create meshes. I have 3 years experience in C\C++ and OpenGL. Yet I don't have an idea on how to tackle this system. What I want is a way to implement or a way to structure a system like this. Any ideas/thoughts?
Here's a link of liner note of someone who was working on an engine that uses this system:
http://chrishecker.com/My_liner_notes_for_spore
I have 3 years experience in C\C++ and OpenGL. Yet I don't have an idea on how to tackle this system.
I find this very hard to believe.
What you need:
Marching cubes algorithm. Or marching tetrahedra, if you worry about patents.
Meta balls formula
Meta particle system consist of several "meta balls", which "emit" "energy".
"Energy" emmited by meteball can be positive/negative, and normally fall off with a distance from metaball. Falloff is kinda similar to point light, except that instead of point any mathematcial body can be used (as long as you can calculate distance to it).
For any given point in space "energy" of metaball system can be, for example, sum of all energies (they fall off with distance, remember?) of all meta bodies in the system.
Now, given this "energy field", you visualize metabody system as if you would visualize iso surface (or any other procedural surface).
For any point of space, if "energy" is below certain thresold, this point is outside of "mesh" formed by meta particle.
If "energy" is above that thresold, point is inside the "mesh" formed by meta particles.
Two points give you segment, and knowing line segment, you can calculate point at which segment crosses "surface" of the mesh.
Now, just walk through certain region of space using "marching cubes" algorithm, and you'll get a mesh.
Normals are calculated by sampling energy levels at small offsets (x/y/z +- 0.01, for example) of the point that lies on the surface of mesh.
If this explanation is hard, then I think nvidia sdk had examples for marching cubes algorithm. Read example, apply same ideas to meta particles.
Here's what you can get with this algorithm:

Detecting surface intersection in lispbuilder-sdl

I am working on making a game in Common Lisp, using Lispbuilder-SDL. I am currently writing a function to check for collision between two surfaces. I need to find out if a surface A intersects with another surface B. I haven't yet seen anything that fits the bill on https://lispbuilder.googlecode.com/svn/trunk/documentation/lispbuilder-sdl.html - is anyone aware of a simple, built-in way of doing this?
The math for the line that forms when two planes/surfaces intersect is not difficult. See Intersecting Planes, Plane Geometry or generally Googling. If there is no intersection, then the planes/surfaces are parallel - there are simple tests for that.
A plane is defined by a point and a normal. If the dot-product of two plane normals is one, then the planes are parallel. In your game, if the dot-product is close enough to one, then the intersection is outside of the play area.

Finding object under mouse

I'm developing a game that basically has its entire terrain made out of AABB boxes. I know the verticies, minimum, and maximum of each box. I also set up my camera like this:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(Camera.rotx,1,0,0);
glRotatef(Camera.roty,0,1,0);
glRotatef(Camera.rotz,0,0,1);
glTranslatef(-Camera.x,-Camera.y,-Camera.z);
What I'm trying to do is basically find the cube the mouse is on. I thought about giving the mouse position a forward directional vector and simply iterating through until the 'mouse bullet' hits something. However this envolves interating through all objects several times. Is there a way I could do it by only iterating through all the objects once?
Thanks
This is usually referred to as 'picking' This here looks like a good gl based link
If that is tldr, then a basic algorithm you could use
sort objects by z (or keep them sorted by z, or depth buffer tricks etc)
iterate and do a bounds test, stopping when you hit the first one.
This is called Ray Tracing (oops, my mistake, it's actually Ray Casting). Every Physics engine has this functionality. You can look at one of the simplest - ODE, or it's derivative - Bullet. They are open-source so you can take out what you don't need. They both have a handy math library that handles all oftenly needed matrix and vertex operations.
They all have demos on how to do exactly this task.
I suggest you consider looking at this issue from a bigger perspective.
The boxes are just points at a lower resolution. The trick is to reduce the resolution of the mouse to figure out which box it is on.
You may have to perform a 2d to 3d conversion (or vice versa). In most games, the mouse lives in a 2d coordinate world. The stuff "under" the mouse is a 2d projection of a 3d universe.
You want to use a 3D picking algorithm. The idea is that you draw a ray from the user's position in the virtual world in the direction of the click. This blog post explains very clearly how to implement such an algorithm. Essentially your screen coordinates need to be transformed from the screen space to the virtual world space. There's a website that has a very good description about the various transformations involved and I can't post the link due to my rank. Search for book of hook's mouse picking algorithm [I do not own the site and I haven't authored the document].
Once you get a ray in the desired direction, you need to perform tests for intersection with the geometries in the real world. Since you have AABB boxes entirely, you can use simple vector equations to check which geometry intersects the ray. I would say that approximating your boxes as a sphere would make life very easy since there is a very simple sphere-ray intersection test. So, your ray would be described by what you obtain from the first step (the ray drawn in the first step) and then you would need to use an intersection test. If you're ok with using spheres, the center of the sphere would be the point you draw your box and the diameter would be the width of your box.
Good Luck!

Implementing Marching Cube Algorithm?

From My last question: Marching Cube Question
However, i am still unclear as in:
how to create imaginary cube/voxel to check if a vertex is below the isosurface?
how do i know which vertex is below the isosurface?
how does each cube/voxel determines which cubeindex/surface to use?
how draw surface using the data in triTable?
Let's say i have a point cloud data of an apple.
how do i proceed?
can anybody that are familiar with Marching Cube help me?
i only know C++ and opengl.(c is a little bit out of my hand)
First of all, the isosurface can be represented in two ways. One way is to have the isovalue and per-point scalars as a dataset from an external source. That's how MRI scans work. The second approach is to make an implicit function F() which takes a point/vertex as its parameter and returns a new scalar. Consider this function:
float computeScalar(const Vector3<float>& v)
{
return std::sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
}
Which would compute the distance from the point and to the origin for every point in your scalar field. If the isovalue is the radius, you just figured a way to represent a sphere.
This is because |v| <= R is true for all points inside a sphere, or which lives on its interior. Just figure out which vertices are inside the sphere and which ones are on the outside. You want to use the less or greater-than operators because a volume divides the space in two. When you know which points in your cube are classified as inside and outside, you also know which edges the isosurface intersects. You can end up with everything from no triangles to five triangles. The position of the mesh vertices can be computed by interpolating across the intersected edges to find the actual intersection point.
If you want to represent say an apple with scalar fields, you would either need to get the source data set to plug in to your application, or use a pretty complex implicit function. I recommend getting simple geometric primitives like spheres and tori to work first, and then expand from there.
1) It depends on yoru implementation. You'll need to have a data structure where you can lookup the values at each corner (vertex) of the voxel or cube. This can be a 3d image (ie: an 3D texture in OpenGL), or it can be a customized array data structure, or any other format you wish.
2) You need to check the vertices of the cube. There are different optimizations on this, but in general, start with the first corner, and just check the values of all 8 corners of the cube.
3) Most (fast) algorithms create a bitmask to use as a lookup table into a static array of options. There are only so many possible options for this.
4) Once you've made the triangles from the triTable, you can use OpenGL to render them.
Let's say i have a point cloud data of an apple. how do i proceed?
This isn't going to work with marching cubes. Marching cubes requires voxel data, so you'd need to use some algorithm to put the point cloud of data into a cubic volume. Gaussian Splatting is an option here.
Normally, if you are working from a point cloud, and want to see the surface, you should look at surface reconstruction algorithms instead of marching cubes.
If you want to learn more, I'd highly recommend reading some books on visualization techniques. A good one is from the Kitware folks - The Visualization Toolkit.
You might want to take a look at VTK. It has a C++ implementation of Marching Cubes, and is fully open sourced.
As requested, here is some sample code implementing the Marching Cubes algorithm (using JavaScript/Three.js for the graphics):
http://stemkoski.github.com/Three.js/Marching-Cubes.html
For more details on the theory, you should check out the article at
http://paulbourke.net/geometry/polygonise/