Transformation functions in vulkan - graphics

Can anyone help me find resources to study about how to implement transformation functions (Translation, rotation & scaling) using Vulkan API on windows platform?
There are very limited sources on Vulkan as far as I know and I couldn't find a single material about transformations in Vulkan.

If You are looking for a source code, Vulkan Cookbook has code samples implementing each of these transformations (translation, rotation and scaling).
If You are looking for an explanation why they are implemented in such a way, You can look for any resources describing these transformations. Translation, rotation and scaling behave exactly the same, no matter what API You are using. This is pure linear algebra. There are differences in projection matrices due to different coordinate systems being used (Vulkan has depth in a [0; 1] range while OpenGL in a [-1; 1] range; and framebuffer space is inverted along Y axis). But the same methodologies apply when You are constructing them by Yourself, You just need to take into account these different coord systems.
The best resource I have encountered describing why matrices are created in such a way, was Frank D. Luna's Introduction to 3D Game Programming with DirectX.

If you're looking for something like the equivalent of glTranslatef, glScalef, and glRotatef, you're out of luck. Nothing like that exists in Vulkan (or in modern OpenGL, for that matter). With modern rendering API's you manage transforms (view, model, projection) on the CPU side using your own preferred library (I use and recommend GLM). Transforms are passed to the shaders using uniform blocks, typically as mat4 values where they can then be used to transform input vertices.

If you are asking us how to implement these by yourself, you have to study some linear algebra to do this. Previous answers answered this.
If not, then I suggest using GLM. It is a library originally written for OpenGL, so you have to be careful while using it. Axes are flipped, keep that in mind.

Related

glTF: how can we store basic geometry primitives?

I'm new to glTF and I would have a very basic, and maybe naive, question. Sorry, and thanks for your understanding and your help.
We have a C++ application where we handle geometry primitive entities, like boxes, cones, cylinders, and so forth.
For visualizing the geometry entities we currently use Coin3D, which have corresponding geometry shapes: Box, Cone, ...
We now would like to add a glTF exporter too, and I have started to explore the glTF specs.
I must say, in the official documentation, and on the web, I could not find any support in glTF for basic geometry shapes.
Therefore, my questions are:
is that true, that glTF has no notion of, let's say, a Box, or a Cone? Or did I missed something obvious?
If the answer to 1) is "NO", are there tested/supported/suggested implementations for basic shapes? I have only found some "example" shapes, like the Box here; but I could not find any collection of implementations of basic shapes. Again, did I miss something?
Are there any best practices, or documentation, on how to implement basic geometry shapes in glTF?
The short answer is you're correct, glTF does not currently store basic geometric shapes directly as a box, cone, cylinder, etc. The format is intended to be a runtime delivery format, not an asset interchange format.
As such, the internal data structures within glTF are designed to mimic the raw data that would typically be fed into a GPU using a graphics API such as OpenGL, WebGL, etc. Entire blocks of glTF data can often be pulled off a disk or network and handed over directly to a graphics API for rendering, with minimal pre-processing.
This means that all of your basic shapes must arrive as the GPU expects to find them: triangulated. Even a simple box is made up of twelve triangles, and because the sides don't share normal vectors, the normal "vertex attributes" are different, hence triangles from different sides of the box don't share vertices (again, because the GPU wouldn't accept that as a raw input). The benefit is that a WebGL client doesn't have to think very hard about what to do when it receives a glTF, it can just start cramming data into the graphics pipeline to get things moving.
For a broader overview, the ever-popular glTF - What the Duck diagram is widely considered an excellent starting point, and the glTF Tutorials are a good follow-up to that.

Which CAD / geometry library should I use: CGAL Open CASCADE or Boost::Geometry something else?

For a customizable laser-cut drawers project I would like to be able to create the connection between any two intersecting perpendicular "2D" objects (with width) programmatically. For this I need to:
load and save 2D objects in some standard format.
extrude 2D object, and make the standard manipulations on 2D objects (specifically subtraction of objects.)
intersect 2 object, and be able to determine the "line" of intersection.
Which library/tool you would suggest for that? Can you estimate how difficult it would be to master it to do the above tasks?
Thanks,
Ronen
I'm biased ( PythonOCC dev ), but I'd say OpenCasCade would best fit your needs. Its comes wit Step and Iges importers out of the box ( more formats are supported commercially ), where you'd have to parse and reconstruct the geometry from a file with Boost and CGAL. Finally, neither of those packages deal with nurbs / CAD geometry ( BRep, boundary representation ) but merely triangles ( polygon soup ). So, OCC fits the scope best IMHO.
I have experience with OPENCASCADE and CGAL . Boost Geometry is very limited / simple and does not provider support for topological structures , solids , brep etc. The purpose has been quite different from the rest. Certainly, its quality is higher than any of the other two.
Opencascade among the three is least preferable choice in terms of quality. For example, everything is defined in global namespace, there are multiple macro definitions on header files, and has bloated classes. It has quite some support for various algorithms and constructs but only up to some point; for the rest you have to pay. These are somehow understandable, since it has been developed as an in-house library not for public access, and therefore they did not care about such things. The community is quite small, therefore you will have to go searching through the documentation a lot and experiment on how to do things. Usually, there are more than one way to work, and it is common to write many adaptors in your code to interface between different algorithms.
On the other hand CGAL is quite the opposite, has support for almost anything you can imagine, it is quite modern, and there is dedicated community along with very good documentation and examples for most use cases. There are different classes and algorithms depending on trade-off of the problem at hand. There is support for different UI (WIN/QT) and it interfaces well with STL / BOOST containers structures. Comparing to opencascade that does not even have a proper STL compatible iterator class, that is a significant difference.
Therefore, I would strongly suggest to work with CGAL.
In case you are forced to work with OPENCASCADE and want to use CGAL at the same time you will probably have to include header files such that Handle macro is undefed e.g.
#include <TopoDS_Shape.h>
#undef Handle
#include <CGAL/Alpha_shape_3.h>
#define Handle(ClassName) Handle_##ClassName

Texture coordinates: how to calculate them?

Is there a formula that i can use to calculate texture coordinates for a complex object not something like cube or sphere?
The texture coordinates are usually set manually by whoever creates the model, using the modelling package.
There are ways of automating the whole process, to a great extent. The results may not be much use if somebody is going to draw the texture based on the UV coordinates, and if you ask the impossible (e.g., mapping a sphere exactly, with no distortion and no seams) then you may not get perfect results -- but for processes such as light mapping this is a common approach.
Levy's LSCM is one approach, as used in Blender, for example. See http://alice.loria.fr/index.php/publications.html?Paper=lscm#2002
Direct3D9 has a UV unwrap tool in its D3DX library; I'm not sure what algorithm it uses, and the documentation isn't amazing, but it does work. See
http://msdn.microsoft.com/en-us/library/bb206321(VS.85).aspx
(Most 3D modelling packages have some kind of automated UV unwrap, too, but in general they never seem to have had too much time spent on them. Presumably the expectation is that somebody will want to go through and fix it up by hand afterwards anyway.)

Collision detection with hardware generated primitives

There's a lot of literature on collision detection, and I've read at least a big enough portion of it to be fairly familiar with most techniques. However, there's something that has eluded me for a while, and I figured, since StackOverflow provides access to a large group of brilliant minds at once, I'd ask here first before digging around in the bookshelf.
In this day and age, more and more work is being delegated to GPU rather than CPU, and in a lot of cases this is a good thing. For example, there are geometry shaders to create new geometry, or (slightly less new, but still quite fascinating) vertex shaders to which you can through a bunch of vertexes at, and something elegant will come out of it. What I was considering though, as these primitives exists only on the graphics hardware, how would you perform reliable collision detection with these primitives? Let's assume I have some kind of extremely simplified mesh which is displaced in a vertex shader (I don't have a concrete problem, I'm more playing with the idea, and I haven't gotten very deep into geometry shaders yet).
What I've considered so far is separate 'rendering' passes from suitable angles with shading more or less turned off, and perhaps lower resolution mesh, rendering the inside (with faces flipped inward) of my second primitive along with the mesh I want to test against, and executing an occlusion query for the mesh. If the mesh is completely occluded there'd be no intersection. This would of course require that my second primitive is convex.
Somehow I get the feeling that this kind of test will be extremely expensive as the number of primitives increase (even if a large portion can be culled directly). Does anyone else have another idea or technique? I'm more familiar with opengl and cg than directx, but if you have some examples or so in directx, I guess I'll be able to figure out the opengl counterparts.
All ideas are appreciated, so please brainstorm. :)
It sounds like Dan Horn's article “Stream Reduction Operations for GPGPU Applications” in GPU Gems 2 is exactly what you want. Like all chapters, it's freely available online.

Polygon Triangulation with Holes

I am looking for an algorithm or library (better) to break down a polygon into triangles. I will be using these triangles in a Direct3D application. What are the best available options?
Here is what I have found so far:
Ben Discoe's notes
FIST: Fast Industrial-Strength Triangulation of Polygons
I know that CGAL provides triangulation but am not sure if it supports holes.
I would really appreciate some opinions from people with prior experience in this area.
Edit: This is a 2D polygon.
To give you some more choices of libraries out there:
Polyboolean. I never tried this one, but it looks promising: http://www.complex-a5.ru/polyboolean/index.html
General Polygon Clipper. This one works very well in practice and does triangulation as well as clipping and holes holes: http://www.cs.man.ac.uk/~toby/alan/software/
My personal recommendation: Use the tesselation from the GLU (OpenGL Utility Library). The code is rock solid, faster than GPC and generates less triangles. You don't need an initialized OpenGL-Handle or anything like this to use the lib.
If you don't like the idea to include OpenGL system libs in a DirectX application there is a solution as well: Just download the SGI OpenGL reference implementation code and lift the triangulator from it. It just uses the OpenGL-Typedef names and a hand full of enums. That's it. You can extract the code and make a stand alone lib in an hour or two.
In general my advice would be to use something that alreay works and don't start to write your own triangulation.
It is tempting to roll your own if you have read about the ear-clipping or sweep-line algorithm, but fact is that computational geometry algorithms are incredible hard to write in a way that they work stable, never crash and always return a meaningful result. Numerical roundoff errors will accumulate and kill you in the end.
I wrote a triangulation algorithm in C for the company I work with. Getting the core algorithm working took two days. Getting it working with all kinds of degenerated inputs took another two years (I wasn't working fulltime on it, but trust me - I spent more time on it than I should have).
Jonathan Shewchuk's Triangle library is phenomenal; I've used it for automating triangulation in the past. You can ask it to attempt to avoid small/narrow triangles, etc., so you come up with "good" triangulations instead of just any triangulation.
CGAL has the tool you need:
Constrained Triangulations
You can simply provide boundaries of your polygon (incuding the boundaries of the holes) as constraints (the best would be that you insert all vertices, and then specify the constraints as pairs of Vertex_handles).
You can then tag the triangles of the triangulation by any traversal algorithm: start with a triangle incident to the infinite vertex and tag it as being outside, and each time you cross a constraint, switch to the opposite tag (inside if you were previously tagging the triangles as outsider, outside if you were tagging triangles as insider before).
I have found the poly2tri library to be exactly what I needed for triangulation. It produces a much cleaner mesh than other libraries I've tried (including libtess), and it does support holes as well. It's been converted to a bunch of languages. The license is New BSD, so you can use it in any project.
Poly2tri library on Google Code
try libtess2
https://code.google.com/p/libtess2/downloads/list
based on the original SGI GLU tesselator (with liberal licensing). Solves some memory management issues around lots of small mallocs.
You can add the holes relatively easily yourself. Basically triangulate to the convex hull of the input points, as per CGAL, and then delete any triangle whose incentre lies inside any of the hole polygons (or outside any of the external boundaries). When dealing with lots of holes in a large dataset, masking techniques may be used to significantly speed this process up.
edit: A common extension to this technique is to weed weak triangles on the hull, where the longest edge or smallest internal angle exceeds a given value. This will form a better concave hull.
I have implemented a 3D polygon triangulator in C# using the ear clipping method. It is easy to use, supports holes, is numerically robust, and supports aribtrary (not self-intersecting) convex/non-convex polygons.
This is a common problem in finite element analysis. It's called "automatic mesh generation". Google found this site with links to commercial and open source software. They usually presume some kind of CAD representation of the geometry to start.
Another option (with a very flexible license) is to port the algorithm from VTK:
vtkDelaunay2D
This algorithm works fairly well. Using it directly is possible, but requires links to VTK, which may have more overhead than you want (although it has many other nice features, as well).
It supports constraints (holes/boundaries/etc), as well as triangulating a surface that isn't necessarily in the XY plane. It also supports some features I haven't seen elsewhere (see the notes on Alpha values).

Resources