Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (804) Games in Android Showcase (239) games submitted by our members Games in WIP (868) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Counting Vertices to Triangles  (Read 4943 times) 0 Members and 1 Guest are viewing this topic.
Hydroque

JGO Coder

Medals: 25
Exp: 5 years

I'm always inspiring a good time.

 « Posted 2016-06-18 05:20:50 »

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

You think I haven't been monitoring the chat? http://pastebin.java-gaming.org/c47d35366491fHere is a compilation <3
Hydroque

JGO Coder

Medals: 25
Exp: 5 years

I'm always inspiring a good time.

 « Reply #1 - Posted 2016-06-18 05:53:35 »

 1  2  3 `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...

You think I haven't been monitoring the chat? http://pastebin.java-gaming.org/c47d35366491fHere is a compilation <3
 « Reply #2 - Posted 2016-06-18 07:48:52 »

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).
Hydroque

JGO Coder

Medals: 25
Exp: 5 years

I'm always inspiring a good time.

 « Reply #3 - Posted 2016-06-18 08:00:59 »

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.
There is where I got stuck.

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).
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.

You think I haven't been monitoring the chat? http://pastebin.java-gaming.org/c47d35366491fHere is a compilation <3
KaiHH

JGO Kernel

Medals: 796

 « Reply #4 - Posted 2016-06-18 09:53:36 »

Quote
yet it returns a number based on where im looking and how high the camera is.
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)
Does your intersect method even take the mouse coordinates into account (i.e. what is 'ray.getDirection()')?
theagentd
 « Reply #5 - Posted 2016-06-18 11:00:44 »

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 n*m vertices. A quad can be split up along the diagonal to get two triangles --> *2. Each triangle is 3 indices --> *3

Myomyomyo.
 « Reply #6 - Posted 2016-06-18 14:36:40 »

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

Just kidding don't hurt me
Hydroque

JGO Coder

Medals: 25
Exp: 5 years

I'm always inspiring a good time.

 « Reply #7 - Posted 2016-06-18 14:52:16 »

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.

You think I haven't been monitoring the chat? http://pastebin.java-gaming.org/c47d35366491fHere is a compilation <3
Pages: [1]
 ignore  |  Print

 Riven (579 views) 2019-09-04 15:33:17 hadezbladez (5497 views) 2018-11-16 13:46:03 hadezbladez (2398 views) 2018-11-16 13:41:33 hadezbladez (5757 views) 2018-11-16 13:35:35 hadezbladez (1220 views) 2018-11-16 13:32:03 EgonOlsen (4659 views) 2018-06-10 19:43:48 EgonOlsen (5680 views) 2018-06-10 19:43:44 EgonOlsen (3196 views) 2018-06-10 19:43:20 DesertCoockie (4094 views) 2018-05-13 18:23:11 nelsongames (5113 views) 2018-04-24 18:15:36
 princec 10x Ali-RS 6x philfrei 5x VaTTeRGeR 5x KaiHH 5x mudlee 5x elect 4x Rain8 3x SteveSmith 2x hansolo_ 2x Apo 1x JaredBGreat 1x ral0r2 1x SugarBlood 1x saocrinn 1x Abuse 1x
 A NON-ideal modular configuration for Eclipse with JavaFXby philfrei2019-12-19 19:35:12Java Gaming Resourcesby philfrei2019-05-14 16:15:13Deployment and Packagingby philfrei2019-05-08 15:15:36Deployment and Packagingby philfrei2019-05-08 15:13:34Deployment and Packagingby philfrei2019-02-17 20:25:53Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org