How does OpenGL actually read indices? - c++

I trying to figure out how to draw a flat terrain with triangle_strips, and while I was writing the loop for creating indices array I thought how does OpenGL know to parse the indices because so far I have seen everybody when creating indices write them like that:
0 3 1 4 2 5 0xFFFF 3 6 4 7 5 8
(here being 0xFFFF primitive restart that marks the end of the strip)
and so technically as I understand this should be parsed with offset +1 for every triangle...so the first triangle would use first three indices (0 3 1) and the next with offset +1 (3 1 4)...next (1 4 2) and so on. Here's the picture in case I didn't explain well:
But other way that I have seen is creating indices for every triangle separately .... so:
0 3 1 3 1 4 1 4 2 4 2 5
So my question is how to specify the layout...does OpenGL do this automatically as I set draw call to GL_TRIANGLE_STRIP or else?

There are three kinds of triangle primitives:
GL_TRIANGLES: Vertices 0, 1, and 2 form a triangle. Vertices 3, 4, and 5 form a triangle. And so on.
GL_TRIANGLE_STRIP: Every group of 3 adjacent vertices forms a triangle. A vertex stream of n length will generate n-2 triangles.
GL_TRIANGLE_FAN: The first vertex is always held fixed. From there on, every group of 2 adjacent vertices form a triangle with the first. So with a vertex stream, you get a list of triangles like so: (0, 1, 2) (0, 2, 3), (0, 3, 4), etc. A vertex stream of n length will generate n-2 triangles.
Your first scenario describes a GL_TRIANGLE_STRIP, while your second scenario describes GL_TRIANGLES.

Related

CGAL read_OFF discards face depending on vertex order

When reading an off-file with cgal it appears that the vertex order of a face decides whether or not it is read in by read_OFF. But the off-file definition does not say anything about the vertex order of a face.
I am reading in self generated off-files using the read_OFF method of cgal:
using Kernel = CGAL::Exact_predicates_inexact_constructions_kernel;
using Point_3 = typename Kernel::Point_3;
...
CGAL::Surface_mesh<Point_3> test_mash;
CGAL::IO::read_OFF(file_name, test_mash);
std::cout << "Number of vertices: " << test_mash.vertices().size()
<< ", Number of faces: " << test_mash.faces().size() << std::endl;
two_faces_read.off:
OFF
4 2 0
1 1 1
2 -2 2
3 3 -3
-4 4 4
3 0 1 2
3 0 3 1
one_face_read.off:
OFF
4 2 0
1 1 1
2 -2 2
3 3 -3
-4 4 4
3 0 1 2
3 0 1 3
Reading two_faces_read.off works as expected, printing:
Number of vertices: 4, Number of faces: 2.
But when i read one_face_read.off i get Number of vertices: 4, Number of faces: 1. The only difference between these two files is the last line, the vertex order of the second face is different. After trying all possible combinations it seems that with 031, 103, 310 2 faces are read in, while with 013, 130, 301 only 1 face is read in.
The off-file specification referenced by cgal does not mention any rules concerning the vertex order of a face.
Why does this happen and how can i ensure that all faces are read in?
one_face_read.off does not define a valid surface mesh has the orientation of the two faces are not compatible. You can use the following function to read points and faces and call CGAL::Polygon_mesh_processing::is_polygon_soup_a_polygon_mesh() to check if the input is a valid surface mesh. The function CGAL::Polygon_mesh_processing::orient_polygon_soup() can be used to fix orientations. CGAL::Polygon_mesh_processing::polygon_soup_to_polygon_mesh() can be used to create the mesh.

How do I wrap a sprite around a cube without GL_REPEAT? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm using OpenGL 3.2+. I created a cube VBO of 8 verticies (each vertex consists of 8 floats: x,y,z,r,g,b,s,t), an EBO to create the faces for that cube, and a VAO to tie it all together.
I have a sprite atlas consisting of hundreds of 16x16 sprites. I'm trying to get 1 sprite from that atlas to appear on all sides of the cube (any orientation is fine).
By tweaking the "s,t" parameters, I can get 4 of the 6 faces to work, but I can't seem to get the last 2 faces to show anything other than random garbage.
Do I need to modify my shader to perform some kind of custom UV wrapping on the atlas?
By tweaking the "s,t" parameters, I can get 4 of the 6 faces to work, but I can't seem to get the last 2 faces to show anything other than random garbage.
At least you have to use separate vertices and attributes, for 2 edges of the cube. This means you need at least 8+2*2=12 different sets of vertex attributes.
x y z u v
0 : -1 1 1 0 0
1 : 1 1 1 1 0
2 : -1 -1 1 0 1
3 : 1 -1 1 1 1
4 : -1 -1 -1 0 0
5 : 1 -1 -1 1 0
6 : -1 1 -1 0 1
7 : 1 1 -1 1 1
8 : -1 1 1 1 1
9 : -1 1 -1 1 0
10 : 1 1 1 0 1
11 : 1 1 -1 0 0
Note, the vertex attribute sets with the indices 0, 2, 4 and 6 have an identically u-coordinate of 0. And the vertex attribute sets with the indices 1, 3, 5 and 7 have an identically u-coordinate of 1.
If you want to wrap a texture to a quad you have to vary the u and the v coordinates. Because of that you have to add the separated vertex attribute sets 8, 9, 10 and 11.
Note, if you want to add normal vectors which are perpendicular to the side planes of the cube, the you need 24 vertex attributes sets. 4 for each side of the cube.

Remove Duplicates in Array and update refering indices. (Remove Duplicate Vertices and Faces)

i need to find a fast way to remove duplicate vertices and faces of a mesh.
The vertices and faces are stored in a Buffers, because I'm working with large data sets I use boost::shared_array<float> vertexBuffer and boost::shared_array<float> faceBuffer for the Buffers.
Next I create a KD-Search-Tree and check for each point if it has a neighbour with dist(point, neighbour)<tollerance. If a neighbour is found i map the neigbour index to the point index in a unordered_map:
oldToNewID[neighbour_id]=point_id
Now i would like to move all vertices that are stored as value in the map to the end of the vertexbuffer. The problem here is that I need to also change the ID of the Face - Points.
For example:
VertexBuffer:
1.3 4.5 3.7|1.8 4.5 3.7|1.3 4.5 3.7|4.3 8.5 2.7|4.9 4.5 2.7
-----------|-----------|-----------|-----------|-----------
Vertex 0 Vertex 1 Vertex 2 Vertex 3 Vertex 4
FaceBuffer:
0 1 3 | 1 2 3 | 4 0 1 | 2 3 4
------------------------------------------------
Face 0 | Face 1 | Face 2 | Face 3
Now Vertex 0 and Vertex 2 are duplicate. So now i somehow have to change the VertexBuffer and FaceBuffer efficiantly so that i will result in:
Updated VertexBuffer:
1.3 4.5 3.7|1.8 4.5 3.7|4.3 8.5 2.7|4.9 4.5 2.7|1.3 4.5 3.7
-----------|-----------|-----------|-----------|-----------
Vertex 0 Vertex 1 Vertex 3 Vertex 4 Vertex 2
NewID 0 NewID 1 NewID 2 NewID 3
Updated FaceBuffer:
0 1 2 | 1 0 2 | 3 0 1 | 0 2 3
------------------------------------------------
Face 0 | Face 1 | Face 2 | Face 3
The duplicates should be moved to the back of the buffer and the faces that where referencing to Vertex 2 needed to be changed. Also each face refering to Vertex 3 and Vertex 4 must be updated because they changed their position.
Any Ideas how to realise that?
Thanks alot!

Meshing options to generate number of the sides of and element (tetgen-triangle)

I wrote a finite element code in fortran 90.
This code is really fast, except the meshing process.
I used triangle and tetgen for meshing in 2D and 3D, respectively, so this process is fast, of course.
For example, for the unit square [0,1]x[0,1] in 2D I have a file with the coordinates of its nodes (for example, a mesh with 5 nodes):
1 0.0 0.0 # coordinates of node 1
2 1.0 0.0 # coordinates of node 2
3 1.0 1.0 # coordinates of node 3
4 0.0 1.0 # coordinates of node 4
5 0.5 0.5 # coordinates of node 5
called coordinate.dat, which have 4 elements (triangles) with nodes called element.dat
1 1 5 4 # vertices of triangle 1
2 1 2 5 # vertices of triangle 2
3 2 3 5 # vertices of triangle 3
4 5 2 4 # vertices of triangle 4
I also have a file where each row i is the number of its initial an final node, called edge.dat:
1 1 2 # initial and final node of edge 1
2 2 3 # initial and final node of edge 2
3 3 4 # initial and final node of edge 3
4 4 1 # initial and final node of edge 4
5 1 5 # initial and final node of edge 5
6 5 2 # initial and final node of edge 6
7 2 5 # initial and final node of edge 7
8 5 4 # initial and final node of edge 8
With this files, I need to generate the following information:
(1) Given an element (triangle or tetrahedron), I need to know the number of its sides (edges and faces, respectively). For example, I need to generate the following structure or file, called struct1.dat:
1 5 8 4 # triangle 1 has the edges number 5, 8 and 4
2 1 6 5 # triangle 2 has the edges number 1, 6 and 5
3 6 2 7 # triangle 2 has the edges number 6, 2 and 7
4 7 3 8 # triangle 4 has the edges number 7, 3 and 8
(2) Furthermore, given a side (edge or face) I need to know the element numbers of the 2 elements (or only one if the side is on the boundary) shared by that side. For example, I need to generate the following structure (or file) called struct2.dat:
1 2 0 # edge number 1 is only on element 2
2 3 0 # edge number 2 is only on element 3
3 4 0 # edge number 3 is only on element 4
4 1 0 # edge number 4 is only on element 1
5 1 2 # edge number 5 is sharing by elements 1 and 2
6 3 2 # edge number 6 is sharing by elements 3 and 2
7 4 3 # edge number 7 is sharing by elements 4 and 3
8 1 4 # edge number 8 is sharing by elements 1 and 4
For both of these structures, struct1.dat and struct2.dat, my code is very slow because I used a brute force approach with a lot of loops..
I am looking for an algorithm (a paper, or better: a subroutine in fortran available for download) optimized for this? I want to continue using triangle and tetgen, but I am willing to listen to other options.

how do i calculate the centroid of the brightest spot in a line of pixels?

i'd like to be able to calculate the 'mean brightest point' in a line of pixels. It's for a primitive 3D scanner.
for testing i simply stepped through the pixels and if the current pixel is brighter than the one before, the brightest point of that line will be set to the current pixel. This of course gives very jittery results throughout the image(s).
i'd like to get the 'average center of the brightness' instead, if that makes sense.
has to be a common thing, i'm simply lacking the right words for a google search.
Calculate the intensity-weighted average of the offset.
Given your example's intensities (guessed) and offsets:
0 0 0 0 1 3 2 3 1 0 0 0 0 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14
this would give you (5+3*6+2*7+3*8+9)/(1+3+2+3+1) = 7
You're looking for 1D Convolution which takes a filter with which you "convolve" the image. For example, you can use a Median filter (borrowing example from Wikipedia)
x = [2 80 6 3]
y[1] = Median[2 2 80] = 2
y[2] = Median[2 80 6] = Median[2 6 80] = 6
y[3] = Median[80 6 3] = Median[3 6 80] = 6
y[4] = Median[6 3 3] = Median[3 3 6] = 3
so
y = [2 6 6 3]
So here, the window size is 3 since you're looking at 3 pixels at a time and replacing the pixel around this window with the median. A window of 3 means, we look at the first pixel before and first pixel after the pixel we're currently evaluating, 5 would mean 2 pixels before and after, etc.
For a mean filter, you do the same thing except replace the pixel around the window with the average of all the values, i.e.
x = [2 80 6 3]
y[1] = Mean[2 2 80] = 28
y[2] = Mean[2 80 6] = 29.33
y[3] = Mean[80 6 3] = 29.667
y[4] = Mean[6 3 3] = 4
so
y = [28 29.33 29.667 4]
So for your problem, y[3] is the "mean brightest point".
Note how the borders are handled for y[1] (no pixels before it) and y[4] (no pixels after it)- this example "replicates" the pixel near the border. Therefore, we generally "pad" an image with replicated or constant borders, convolve the image and then remove those borders.
This is a standard operation which you'll find in many computational packages.
your problem is like finding the longest sequence problem. once you are able to determine a sequence( the starting point and the length), the all that remains is finding the median, which is the central element.
for finding the sequence, definition of bright and dark has to be present, either relative -> previous value or couple of previous values. absolute: a fixed threshold.