Counting Vertices to Triangles

Hi JGO,

So I have an array of vertices that I generate. I then generate indices that connect them together.

I store each Triangle as an object for later use. This is done using indices. I need to create a table of Triangles beforehand.

I don’t know any method of pre-calculating how many vertices will generate the amount of triangles.

I have 20*20=400 vertices and I end up with 722 triangles.

Google likes to tell me how to find the perimeter :slight_smile:

final int vertices = 20;
final int count = vertices * vertices;
final int triangles = (vertices-1) * (vertices-1) * 2;

I ended up getting the same amount of vertices by backtracking the number 722.
I did sqrt(722) which gave me a decimal. I didn’t want that.
I noticed 722 was a multiple of two so I did 722/2. I got 361.
I noticed that 361 LCM was 19 by sqrt(361).
I found that 19 is vertices - 1.
Using this logic I determined (vertices-1) * (verticles-1) * 2 = triangle count.

I am not sure why though…

Alright, this isn’t really confusing if you think about it. I assure you, there’s no LCM calculation involved.

Following common sense, there should be fewer triangles than vertices, because for every three vertices, there’s at least one triangle. That means the number of vertices has to be three times the number of triangles, and (no. of vertices)/3 = (no. of triangles).

Right?

Well…yeah, kinda, but like I said, for every three vertices, there’s AT LEAST one triangle. But that’s not the case for most models, because there are hundreds of triangles in the models we use, and one vertex can be a part of multiple triangles (which is why we use the index buffer).

That means we can’t calculate the number of triangles using the number of vertices, but we CAN calculate the number of triangles using the length of the indices array.

Let’s say we’ve got a 2D quad:

vertices = {-1, -1, 0, -1, 1, 0, 1, 1, 0, 1, 1, 0, -1, -1, 0, 1, -1, 0};

As you can see, there’s a repetition, because we’re using the same vertex for two triangles (since a quadrilateral consists of two triangles). Look at the third vertex (1, 1, 0). It’s used twice. The first vertex is also used twice.

So we use the index buffer and it looks a little something like this:

vertices = {-1, -1, 0, -1, 1, 0, 1, 1, 0, 1, -1, 0};
indices = {0, 1, 2, 2, 0, 3};

Now, the vertex array doesn’t have any repetition, but the indices array does.

That’s why we can’t take the vertex array’s length and divide it by three to get the number of triangles. We have to use the indices array’s length and divided it by three to get the number of triangles (three vertices for every triangle, as mentioned above).

There is where I got stuck.

Its not a freak accident. It is a variation of the way I generate indices. I didn’t realize I already calculated the indices. Which turned out to be 6 * (vertices-1) * (vertices-1). I am not sure how (vertices-1) * (vertices-1) * 3 managed to get the correct number. I don’t think that is how it works. Who knows I never tested. So idk. Todo.

I am having another problem with ray intersection on a triangle. Not sure what I am doing wrong. I can verify that I have correct coordinates. Its working in the negative direction. My vertices are offset by 5.24, yet it returns a number based on where im looking and how high the camera is.

MouseManager.intersectRayTriangle(camera.getPosition(), ray.getDirection(), pack.triangles[0].a, pack.triangles[0].b, pack.triangles[0].c)

I cut out the JOML intersection method.

[quote]yet it returns a number based on where im looking and how high the camera is.
[/quote]
Did you look at the answer on the lwjgl forum mentioned in your other JGO thread dedicated to mouse picking?

[quote]MouseManager.intersectRayTriangle(camera.getPosition(), ray.getDirection(), pack.triangles[0].a, pack.triangles[0].b, pack.triangles[0].c)
[/quote]
Does your intersect method even take the mouse coordinates into account (i.e. what is ‘ray.getDirection()’)?

You’re drawing a 2D grid using triangles.

Let’s do this in 1D. You want to form a line from vertices. You draw a line from vertex i to i+1, but the last vertex has no other vertex to draw a line to. You end up with n-1 lines for n vertices.

Now, expand to 2D. You want to draw quads from a grid of vertices. You form a quad between (x, y, x+1, y+1), but again, the last vertices along x and y have no pairs. You end up with (n-1)(m-1) quads from nm vertices. A quad can be split up along the diagonal to get two triangles --> *2. Each triangle is 3 indices --> *3

Well…if you don’t manage to figure out how to do ray-? intersection, I guess there’s always the selection buffer and GL_SELECTION :stuck_out_tongue:

Just kidding don’t hurt me :persecutioncomplex:

Well the system works correctly detecting a triangle. But I’m not sure it’s in the correct coordinates. I’m not sure about the first parameter. I did take it from context and variable name so I’ll check up on that. I’m just confused on why it’s negative and checking negative direction. I draw my terrain positive x positive z. I’m completely aware of my terrain being able to check with generic boolean logic, but it gets hard when there is dynamic y.