Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (806)
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
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Counting Vertices to Triangles  (Read 4953 times)
0 Members and 1 Guest are viewing this topic.
Offline 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 Smiley

You think I haven't been monitoring the chat? http://pastebin.java-gaming.org/c47d35366491fHere is a compilation <3
Offline 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
Offline ShadedVertex
« 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).
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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
Offline KaiHH

JGO Kernel


Medals: 798



« 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()')?
Offline 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.
Offline ShadedVertex
« 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 Tongue

Just kidding don't hurt me persecutioncomplex
Offline 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 (587 views)
2019-09-04 15:33:17

hadezbladez (5528 views)
2018-11-16 13:46:03

hadezbladez (2410 views)
2018-11-16 13:41:33

hadezbladez (5790 views)
2018-11-16 13:35:35

hadezbladez (1233 views)
2018-11-16 13:32:03

EgonOlsen (4669 views)
2018-06-10 19:43:48

EgonOlsen (5688 views)
2018-06-10 19:43:44

EgonOlsen (3205 views)
2018-06-10 19:43:20

DesertCoockie (4104 views)
2018-05-13 18:23:11

nelsongames (5125 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!