Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / How would you calculate smooth normals ?

Author
Message
janbo
15
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 20th Nov 2017 22:06 Edited at: 20th Nov 2017 22:28
Hi guys,
As stated above I need to find a decent algorithm to find "smooth" normals.

Just for clarification:


Normally you share vertices between faces to get smooth normals and this can easily be done in a modeling program.
Anyhow I need to calculate them for every vertex in an object, also objects generated by AGK
Also I can't use a smooth normal texture, it has to be a vertex attribute.
So here you come in and write some thoughts about how you would solve this problem
I though about iterating through the objects vertices and make groups of those vertices, so I could calculate one smooth normal per group, but how I did it was way to slow.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 20th Nov 2017 23:16 Edited at: 20th Nov 2017 23:19
if you know how to calculate the normal from a face.
the vertex which hang on different faces you can add this normals and then divide by count faces, you get a average value vector.
something like this
x = (n1.x + n2.x + n3.x) / 3.0
y ...
z ...
AGK (Steam) V2017.09.25 : Windows 10 Pro 64 Bit : AMD (17.11.2) Radeon R7 265 : Mac mini OS High Sierra (10.13)
Jack
19
Years of Service
User Offline
Joined: 4th Oct 2004
Location: [Germany]
Posted: 21st Nov 2017 01:38 Edited at: 21st Nov 2017 01:42
Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 21st Nov 2017 02:03

for each normal, count up all the points that share this point, add all the normals together , then divide by the numbers of shared points..

PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code
janbo
15
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 21st Nov 2017 12:00 Edited at: 21st Nov 2017 12:36
Oh sorry guys I should have made it clear that I don't need the calculation itself but the algorithm to find the neighbor vertices.
0x5f3759df is very interesting through.

Quote: "count up all the points that share this point"

Exactly, but how would you do this.

I already have an array populated with every data from the object.
I want to create a new vertex attribute for it "avgnormals".
The code to insert new vertex attributes into the mesh data is also done.


I started with the source array and a target array.
The target array would have 2 dimensions one for the groups and one for the vertices itself.
Then I would iterate through all arrays/dimensions and compare the positions, if there is another vertex with the same position then add it to the group, if not then create a new group.
It ended up being too slow... but maybe I made it to complicated or something, but I can't think of another solution right now.
This bothers my for some days already
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 21st Nov 2017 19:58
Are you building it in memory or is this from a model?
janbo
15
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 22nd Nov 2017 14:11 Edited at: 22nd Nov 2017 14:41
I use CreateMemblockFromObjectMesh() and fill the data into my Array.
Not sure what to answer...memblocks are the memory but I read the data from a model
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 22nd Nov 2017 14:53 Edited at: 22nd Nov 2017 14:55
i would do this task more functional with smaller tasks that return a list/data.
AGK (Steam) V2017.09.25 : Windows 10 Pro 64 Bit : AMD (17.11.2) Radeon R7 265 : Mac mini OS High Sierra (10.13)
Stab in the Dark software
Valued Member
21
Years of Service
User Offline
Joined: 12th Dec 2002
Playing: Badges, I don't need no stinkin badges
Posted: 22nd Nov 2017 16:24 Edited at: 22nd Nov 2017 16:26
Quote: "Oh sorry guys I should have made it clear that I don't need the calculation itself but the algorithm to find the neighbor vertices.
0x5f3759df is very interesting through."


I use this algorithm to find the vertices that are within a specified distance of each other and weld them.
It is very fast and robust, except it is in C++ and I am not willing to convert it to tier1.
If you look it over you may glean something from it. If you have any questions on how to convert I will do my best to answer them.

P.S. to use this in C++ just paste into a file called weld.h and include in your code.

The coffee is lovely dark and deep,and I have code to write before I sleep.
janbo
15
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 22nd Nov 2017 16:59 Edited at: 22nd Nov 2017 20:54
Thanks, I had a quick glance over it...looks like I should be able to convert it to T1.
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 22nd Nov 2017 19:30
I was going to say if it was a .obj file the verticies are grouped by face so the work is done for you. I wonder if you could save your memblock to a .obj file and the read it as a text file?
janbo
15
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 22nd Nov 2017 20:56 Edited at: 22nd Nov 2017 20:59
After starting to convert Stab's code...I realized how easy it actually should be and just wrote it from scratch
So it works now...still a bit slow but I can optimize it like you would do it with a sort algorithm...you know

I think I don't get Face normals if I read the mesh data from in AppGameKit with CreateMemblockFromObjectMesh().
Not sure if you can change that in a model program... anyway I get a normals for every vertex...i.e there is no data structure for faces only indices and vertices
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 22nd Nov 2017 21:16
A .obj file is just a text file
janbo
15
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 22nd Nov 2017 22:48 Edited at: 22nd Nov 2017 22:58
Yeah I know
I would need to parse the file and I would still need to find the adjacent faces and calculate the average normal vector for each vertex ?

The result are outlines without hard edge artifacts comming for the next ShaderPack update:

Attachments

Login to view attachments
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 23rd Nov 2017 02:15
ok. my apologies
Jack
19
Years of Service
User Offline
Joined: 4th Oct 2004
Location: [Germany]
Posted: 23rd Nov 2017 18:19 Edited at: 23rd Nov 2017 18:24
Hehe, I knew 0x5f3759df was the key
thx stab for the c++ code.

Outlines are nice. Can't wait for the update.

[/url]
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 26th Nov 2017 19:54
Those new outlines look great! I'm still buying on pay day!
janbo
15
Years of Service
User Offline
Joined: 10th Nov 2008
Location: Germany
Posted: 26th Nov 2017 22:30 Edited at: 26th Nov 2017 22:31
Wow cjb, your posts take a lot of space ...all those Badges
Also leave a review on steam once you are there

I made it so you can choose between regular normals and soft normals via a parameter in the outline command if the model has soft normals already and don't need to iterate through all the vertices to generate the new normals.
I might now have all parts to generate face normals so I could achieve flat shading
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 27th Nov 2017 12:42
Quote: "
I think I don't get Face normals if I read the mesh data from in AppGameKit with CreateMemblockFromObjectMesh()."


If you have the vertices, calculating the face normal should be simple. As long as it's not a dynamic model, you can pre-calculate these.

Nice shader.

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Login to post a reply

Server time is: 2024-04-19 00:48:35
Your offset time is: 2024-04-19 00:48:35