How to slice a geometry? - opengl

Let's say I have this geometry:
glutSolidTeapot(1);
I want to slice it in 8 cubes, for example across the 3 planes (xy), (yz), (xz), to make a 3D puzzle.
How can I clip a geometry?

There is 2 ways of doing this. I'm going to assume you want to slice your geometry into cubes, but other shapes can be done similarly.
1. Slice your triangle mesh
Here you just loop through all your triangles and check in which cube the triangle belongs to. If the triangle intersects multiple cubes you need to split it in multiple triangles. You'll need to do some math for line-plane intersection to get the splits right, but its not very hard.
2. Use opengl clip planes
You can also render your geometry multiple times, but clip only the part you want to be shown on screen. This can be done using glClipPlane (see http://www.opengl.org/sdk/docs/man2/xhtml/glClipPlane.xml). For each cube you'll need 6 clip planes. This method will be slower than the first as gpu needs to consider each triangle for every cube.

Related

Dynamically create complementary triangles of a regular grid in OpenGL

I have created a regular grid which originates from a 2D image, i.e. each pixels has a vertex. There are two triangles per four pixels so that I have a triangle in the top right and in the bottom left. I use vertex and index buffers for that.
Now I dynamically remove triangles / faces at the border of two different kinds of vertices (according to my application) because else there would be distortions. I wrote a geometry shader which takes a triangle and outputs the triangle or nothing (see first picture). The shader recognizes if a triangle is "faulty" (has orange edges) and omits it.
Now this works fine, but I may lose some details because of my vertex geometry. I can add complementary triangles to the mesh (see second picture, new triangles with dashed orange line).
How do I accomplish this in OpenGL?
My first idea is to create one quad instead of two triangles, check for the four possible triangles cases and create those triangles dynamically in the geometry shader. But this might be slow; GL_QUADs are deprecated and alternatives might be slow too. What do you have in mind?
Here's my idea:
Put the whole grid in a buffer/texture.
Build four triangles for each four pixels. They cross each other, yes.
In the geometry shader you can tell if a triangle is "faulty" because it connects two wrong regions. Or, sampling form the texture, because the crossing triangle is valid, so this new one can be discarded.
EDIT: Another approach
Use the texture. Draw instanced with GL_POINTS. With some order and the help of the instanceID the shader knows where the point is.
For this point test the four possible triangles. If you instance top to down and left to right, only a point to the right and the two below are used for the four triangles. And you avoid repeating tests.
Emit only those you choose.

polygon to triangle convertion effect visibility of the mesh

I am using OpenGl ES to visualize a mesh which has polygons with more than 3 vertexes. I wanted to convert these polygons to triangles using following loop. In the loop I created polygonVertexSize-2 number of triangles just by filling an OpenGL index array which refers to same vertexes in a different order and times.
for(int j=0;j<polygonVertexSize-2;j++) //number of triangles
{
//GetPolygonVertex returns the index of a polygon Vertex
indices[indp+0]=Polygon->GetPolygonVertex(0);
indices[indp+1]=Polygon->GetPolygonVertex(1+j);
indices[indp+2]=Polygon->GetPolygonVertex(2+j);
indp+=3;
}
Problem with this conversion is, unless I apply glDisable(GL_CULL_FACE) some parts of the meshes are not visible. Which probably means my triangulation cause surface normals to be wrong. Another thing to note is, I average a normal for a vertex using the normals of the same vertex in different triangles.
How may I solve this problem? is it a bad idea to disable culling to solve this problem?
Here are the results with culling and without
The problem is with back-face culling.
Part of the mesh are invisible because they are facing away from camera. glDisable(GL_CULL_FACE) is the simplest way to solve this problem but this can cause performance problems (every triangle is processed twice). But it shouldn't affect your scene.
If you want to do it "right" you have to change the winding for invisible triangles. Just swap two vertices.
//only for invisible triangles
indices[indp+0]=Polygon->GetPolygonVertex(0);
indices[indp+1]=Polygon->GetPolygonVertex(2+j);
indices[indp+2]=Polygon->GetPolygonVertex(1+j);
Your triangulation is right if your polygon is planar and convex. You can simply check if your polygon is convex using gift wrapping algorithm or just walk through vertices and compute dot products, if the sign of dot product changes, polygon is not convex

Render decal on additional polygon

I want to export difficult model to OBJ format and render it on iPad. The most of the work performed. But there is the following problem.
For render decals I need to use additional polygon. For calculate UV coordinates I use Exponential Mapping algorithm. after applying this algorithm I get mesh with texture coordinates, but this algorithm use big radious(a lot of triangles). And after all I get something like this:
A lot of triangles but moste of them is not need.How can I remove additional tiangles and moove points to get correctsize of decal.
Or more simle example.
If I has a rectungle (2 triangles and 4 points) with texture (not in 0..1 range). And I want to find another 4 point (with texture coordinates in 0..1 range). Ok, if you see the image I has black triangles and I want to get red triangles.
How can i get positions of red triangles point (x, y ,z coordinates of all 4 point).

calculating normals for quad mesh

I have a struct QUAD that stores 4 pointers to 4 VECTOR3D (which contains 3 floats) so that I can draw the quad mesh.
From what I understand is whenever I draw a mesh, I need normal as well to properly light/shade a mesh and it's relatively easy when it's a mesh laying on a plain, using normal per face.
When I have 2 by 2 quad meshes laying on XZ coordinate and tried to raise it's centre (0,0,0) by a certain point, say (0, 4, 0) it would start to form real 3D shapes, then I need to calculate normals again. I'm having hard time understanding how and what is to be to calculated normals. As expected, the 3D shape shades like it's still a flat mesh, so it does not represent real shape. One of the explanation says I need to calculate normals per vertex instead of per face.
Does it mean I need to calculate normals for all corners of mesh? once i have normals what would i do? I was still using old glBegin glEnd methods but now I feel like i need to use DrawArray method. I'm deeply confused and I'm pretty sure I don't make much sound but i'd much appreciate your help.
If you need flat looking surface then your normals will be normals to the quad plane. If you need "soft looking" surface you need to blend(read this and watch this cool simple video) normals - that will add sort of gradient.

OpenGL quads overlapping

Say I have two quads which are overlapping, forming an X.
Is it possible to disable this from happening? So that the GPU removes (one of) these overlapping quads?
The case is about billboards, which shine through the static walls. In a ideal case, I would wan't to have the billboard removed when it overlaps with the wall, without spending a lot of CPU utilisation on it (I have about 10.000 billboards and walls).
Currently my billboards are implemented by calculating the angle between object and camera.
Currently my billboards are implemented by calculating the angle between object and camera.
Totally wrong approach. The usual OpenGL perspective projection is a plane parallel one. So this means that you normally want your billboard to be coplanar to your near/far cliping plane.
This is most easily done by taking the inverse of the upper left 3×3 part of the world- to eyespace transformation matrix, and use the X any Y columns of this as base vectors for your billboard quads.
As it happens, rotation matrices inverse is the transpose. So just take the first and second row vectors of the camera transformation matrix (only the first 3 elements each) and use them as base for your billboards. Don't mess with triginometry or angles here, you're barking up the wrong tree then.
The overlap is usually taken care of by depth testing. For translucent geometry disable depth writes and pre-sort your geometry far-to-near.