Search code examples
javaarraysnormalsvertices.obj

JAVA, make normals array have length of vertices array (from parsed OBJ file)


I am trying to reproduce OBJ file parsing of a CSharp application that takes obj file and make another file of it with its unique internal structure (based on that obj file) saving it to a file with unique extension - it is not 1:1 copy of that obj file: it just take vertices, normals, uvs (if any) and indices making arrays of them and making several different things with it (this is not important for my question at all but I am telling it anyway just to show a bit what it does).

I was able to do this by looking at the produced file (traversing it) byte by byte in binary mode in my JAVA app and by pure try/guess process trying to understand the logic behind it I succeeded emulating it - long process I can tell you.

Now, I have successfully "emulated" that application behavior in my JAVA app which now is able to produce that special file from the source obj file with the only exception that I somehow am not able to understand.

Inside the OBJ file there are different numbers of vertices and normals (for example it can have 266*3 vertices and yet only 30*3 normals) but that CSharp app which behavior I am trying to emulate is somehow able to make normals of lenght of those vertices, that is both arrays have the same length. And I am unable find any logic behind that calculation!

First with small OBJ files I thought it just take those normals values and simply copying them to the length of the vertices array which somehow miraculously worked when I had like 5*3 verties and 3 normals, but not if it was "unequal", that is when vertices.length/normals.length != int.

Is there some correlation between those two (vertices/normals) that enables that CSharp app "stretching/filling" the normals array length to the length of that vertices.length?

These are two OBJ files as examples (and yes: these are exactly the ones of those OBJs I used with that CSharp program, so it does not matter if their structure is somehow "not right" - it is not merit of my question and it has no effect on the question I am asking) + also I am not the one who came up with the idea of making normals to vertices length despite being shorter (talking about their respective arrays), and, BTW, it is simply requirement of the SW that uses that special file it produces so I understand WHY he does that - I am only trying to reproduce that process which I am unable to understand HOW the original author made it inside his CSharp app, that is all, so please if you can do not tell me it is nonsensical...yea, it might be but still IT NEEDS TO BE OF VERTICES ARRAY LENGTH:

This 1st one (the shorter one - vertices.length (v)/normals.length (vn) always equal some integer) work with the abovementioned pseudo-technique of mine (just copying normals over and over till it reaches the length of vertices array)

v 4.400000 -0.000000 0.400000
v 4.400000 0.960000 0.400000
v -0.400000 -0.000000 0.400000
v -0.400000 0.960000 0.400000
vt 0.998166 1.001956
vt 0.998167 0.002486
vt 0.003233 1.001956
vt 0.003233 0.002488
vn 0.0000 -0.0000 1.0000
s 1
f 1/1/1 2/2/1 3/3/1
f 2/2/1 4/4/1 3/3/1

This 2nd one (the longer one - vertices.length (v)/normals.length (vn) always equal some float) on the other hand never worked with that technique (as it needs exactly just int, not float with decimals) and that is the reason I am scratching my head to solve it

v 4.400000 0.000000 -0.400000
v 4.400000 0.960000 -0.400000
v 4.400000 -0.000000 0.400000
v 4.400000 0.960000 0.400000
v -0.400000 -0.000000 0.400000
v -0.400000 0.960000 0.400000
v -0.400000 0.000000 -0.400000
v -0.400000 0.960000 -0.400000
v -0.400000 0.000000 -0.400000
v -0.400000 0.960000 -0.400000
v 4.400000 0.000000 -0.400000
v 4.400000 0.960000 -0.400000
v 4.400000 0.960000 0.400000
v 4.400000 0.960000 -0.400000
v -0.400000 0.960000 0.400000
v -0.400000 0.960000 -0.400000
v 4.400000 0.000000 -0.400000
v 4.250000 0.000000 -0.250000
v -0.400000 0.000000 -0.400000
v -0.250000 0.000000 -0.250000
v -0.400000 -0.000000 0.400000
v -0.250000 -0.000000 0.250000
v 4.400000 -0.000000 0.400000
v 4.250000 -0.000000 0.250000
v 4.250000 0.850000 -0.250000
v 4.250000 0.000000 -0.250000
v 4.250000 0.850000 0.250000
v 4.250000 -0.000000 0.250000
v 4.250000 0.850000 0.250000
v 4.250000 -0.000000 0.250000
v -0.250000 0.850000 0.250000
v -0.250000 -0.000000 0.250000
v -0.250000 0.850000 0.250000
v -0.250000 -0.000000 0.250000
v -0.250000 0.850000 -0.250000
v -0.250000 0.000000 -0.250000
v -0.250000 0.850000 -0.250000
v -0.250000 0.000000 -0.250000
v 4.250000 0.850000 -0.250000
v 4.250000 0.000000 -0.250000
v 4.250000 0.850000 -0.250000
v 4.250000 0.850000 0.250000
v -0.250000 0.850000 -0.250000
v -0.250000 0.850000 0.250000
v 4.212176 1.138000 -0.122500
v 4.212176 0.960000 -0.122500
v 4.122500 1.138000 -0.212176
v 4.122500 0.960000 -0.212176
v 4.000000 1.138000 -0.245000
v 4.000000 0.960000 -0.245000
v 3.877500 1.138000 -0.212176
v 3.877500 0.960000 -0.212176
v 3.787824 1.138000 -0.122500
v 3.787824 0.960000 -0.122500
v 3.755000 1.138000 -0.000000
v 3.755000 0.960000 -0.000000
v 3.787824 1.138000 0.122500
v 3.787824 0.960000 0.122500
v 3.877500 1.138000 0.212176
v 3.877500 0.960000 0.212176
v 4.000000 1.138000 0.245000
v 4.000000 0.960000 0.245000
v 4.122500 1.138000 0.212176
v 4.122500 0.960000 0.212176
v 4.212176 1.138000 0.122500
v 4.212176 0.960000 0.122500
v 4.245000 1.138000 0.000000
v 4.245000 0.960000 -0.000000
v 4.212176 1.138000 -0.122500
v 4.122500 1.138000 -0.212176
v 4.000000 1.138000 0.000000
v 4.000000 1.138000 -0.245000
v 3.877500 1.138000 -0.212176
v 3.787824 1.138000 -0.122500
v 3.755000 1.138000 -0.000000
v 3.787824 1.138000 0.122500
v 3.877500 1.138000 0.212176
v 4.000000 1.138000 0.245000
v 4.122500 1.138000 0.212176
v 4.212176 1.138000 0.122500
v 4.245000 1.138000 0.000000
v 3.412176 1.138000 -0.122500
v 3.412176 0.960000 -0.122500
v 3.322500 1.138000 -0.212176
v 3.322500 0.960000 -0.212176
v 3.200000 1.138000 -0.245000
v 3.200000 0.960000 -0.245000
v 3.077500 1.138000 -0.212176
v 3.077500 0.960000 -0.212176
v 2.987824 1.138000 -0.122500
v 2.987824 0.960000 -0.122500
v 2.955000 1.138000 -0.000000
v 2.955000 0.960000 -0.000000
v 2.987823 1.138000 0.122500
v 2.987823 0.960000 0.122500
v 3.077500 1.138000 0.212176
v 3.077500 0.960000 0.212176
v 3.200000 1.138000 0.245000
v 3.200000 0.960000 0.245000
v 3.322500 1.138000 0.212176
v 3.322500 0.960000 0.212176
v 3.412176 1.138000 0.122500
v 3.412176 0.960000 0.122500
v 3.445000 1.138000 0.000000
v 3.445000 0.960000 -0.000000
v 3.412176 1.138000 -0.122500
v 3.322500 1.138000 -0.212176
v 3.200000 1.138000 0.000000
v 3.200000 1.138000 -0.245000
v 3.077500 1.138000 -0.212176
v 2.987824 1.138000 -0.122500
v 2.955000 1.138000 -0.000000
v 2.987823 1.138000 0.122500
v 3.077500 1.138000 0.212176
v 3.200000 1.138000 0.245000
v 3.322500 1.138000 0.212176
v 3.412176 1.138000 0.122500
v 3.445000 1.138000 0.000000
v 2.612176 1.138000 -0.122500
v 2.612176 0.960000 -0.122500
v 2.522500 1.138000 -0.212176
v 2.522500 0.960000 -0.212176
v 2.400000 1.138000 -0.245000
v 2.400000 0.960000 -0.245000
v 2.277500 1.138000 -0.212176
v 2.277500 0.960000 -0.212176
v 2.187824 1.138000 -0.122500
v 2.187824 0.960000 -0.122500
v 2.155000 1.138000 -0.000000
v 2.155000 0.960000 -0.000000
v 2.187823 1.138000 0.122500
v 2.187823 0.960000 0.122500
v 2.277500 1.138000 0.212176
v 2.277500 0.960000 0.212176
v 2.400000 1.138000 0.245000
v 2.400000 0.960000 0.245000
v 2.522500 1.138000 0.212176
v 2.522500 0.960000 0.212176
v 2.612176 1.138000 0.122500
v 2.612176 0.960000 0.122500
v 2.645000 1.138000 0.000000
v 2.645000 0.960000 -0.000000
v 2.612176 1.138000 -0.122500
v 2.522500 1.138000 -0.212176
v 2.400000 1.138000 0.000000
v 2.400000 1.138000 -0.245000
v 2.277500 1.138000 -0.212176
v 2.187824 1.138000 -0.122500
v 2.155000 1.138000 -0.000000
v 2.187823 1.138000 0.122500
v 2.277500 1.138000 0.212176
v 2.400000 1.138000 0.245000
v 2.522500 1.138000 0.212176
v 2.612176 1.138000 0.122500
v 2.645000 1.138000 0.000000
v 1.812176 1.138000 -0.122500
v 1.812176 0.960000 -0.122500
v 1.722499 1.138000 -0.212176
v 1.722499 0.960000 -0.212176
v 1.599999 1.138000 -0.245000
v 1.599999 0.960000 -0.245000
v 1.477499 1.138000 -0.212176
v 1.477499 0.960000 -0.212176
v 1.387823 1.138000 -0.122500
v 1.387823 0.960000 -0.122500
v 1.354999 1.138000 -0.000000
v 1.354999 0.960000 -0.000000
v 1.387823 1.138000 0.122500
v 1.387823 0.960000 0.122500
v 1.477499 1.138000 0.212176
v 1.477499 0.960000 0.212176
v 1.599999 1.138000 0.245000
v 1.599999 0.960000 0.245000
v 1.722499 1.138000 0.212176
v 1.722499 0.960000 0.212176
v 1.812176 1.138000 0.122500
v 1.812176 0.960000 0.122500
v 1.844999 1.138000 0.000000
v 1.844999 0.960000 -0.000000
v 1.812176 1.138000 -0.122500
v 1.722499 1.138000 -0.212176
v 1.599999 1.138000 0.000000
v 1.599999 1.138000 -0.245000
v 1.477499 1.138000 -0.212176
v 1.387823 1.138000 -0.122500
v 1.354999 1.138000 -0.000000
v 1.387823 1.138000 0.122500
v 1.477499 1.138000 0.212176
v 1.599999 1.138000 0.245000
v 1.722499 1.138000 0.212176
v 1.812176 1.138000 0.122500
v 1.844999 1.138000 0.000000
v 1.012175 1.138000 -0.122500
v 1.012175 0.960000 -0.122500
v 0.922499 1.138000 -0.212176
v 0.922499 0.960000 -0.212176
v 0.799999 1.138000 -0.245000
v 0.799999 0.960000 -0.245000
v 0.677499 1.138000 -0.212176
v 0.677499 0.960000 -0.212176
v 0.587823 1.138000 -0.122500
v 0.587823 0.960000 -0.122500
v 0.554999 1.138000 -0.000000
v 0.554999 0.960000 -0.000000
v 0.587823 1.138000 0.122500
v 0.587823 0.960000 0.122500
v 0.677499 1.138000 0.212176
v 0.677499 0.960000 0.212176
v 0.799999 1.138000 0.245000
v 0.799999 0.960000 0.245000
v 0.922499 1.138000 0.212176
v 0.922499 0.960000 0.212176
v 1.012175 1.138000 0.122500
v 1.012175 0.960000 0.122500
v 1.044999 1.138000 0.000000
v 1.044999 0.960000 -0.000000
v 1.012175 1.138000 -0.122500
v 0.922499 1.138000 -0.212176
v 0.799999 1.138000 0.000000
v 0.799999 1.138000 -0.245000
v 0.677499 1.138000 -0.212176
v 0.587823 1.138000 -0.122500
v 0.554999 1.138000 -0.000000
v 0.587823 1.138000 0.122500
v 0.677499 1.138000 0.212176
v 0.799999 1.138000 0.245000
v 0.922499 1.138000 0.212176
v 1.012175 1.138000 0.122500
v 1.044999 1.138000 0.000000
v 0.212175 1.138000 -0.122500
v 0.212175 0.960000 -0.122500
v 0.122499 1.138000 -0.212176
v 0.122499 0.960000 -0.212176
v -0.000001 1.138000 -0.245000
v -0.000001 0.960000 -0.245000
v -0.122501 1.138000 -0.212176
v -0.122501 0.960000 -0.212176
v -0.212177 1.138000 -0.122500
v -0.212177 0.960000 -0.122500
v -0.245001 1.138000 -0.000000
v -0.245001 0.960000 -0.000000
v -0.212177 1.138000 0.122500
v -0.212177 0.960000 0.122500
v -0.122501 1.138000 0.212176
v -0.122501 0.960000 0.212176
v -0.000001 1.138000 0.245000
v -0.000001 0.960000 0.245000
v 0.122499 1.138000 0.212176
v 0.122499 0.960000 0.212176
v 0.212175 1.138000 0.122500
v 0.212175 0.960000 0.122500
v 0.244999 1.138000 0.000000
v 0.244999 0.960000 -0.000000
v 0.212175 1.138000 -0.122500
v 0.122499 1.138000 -0.212176
v -0.000001 1.138000 0.000000
v -0.000001 1.138000 -0.245000
v -0.122501 1.138000 -0.212176
v -0.212177 1.138000 -0.122500
v -0.245001 1.138000 -0.000000
v -0.212177 1.138000 0.122500
v -0.122501 1.138000 0.212176
v -0.000001 1.138000 0.245000
v 0.122499 1.138000 0.212176
v 0.212175 1.138000 0.122500
v 0.244999 1.138000 0.000000
vn 1.0000 0.0000 0.0000
vn -1.0000 0.0000 0.0000
vn -0.0000 0.0000 -1.0000
vn 0.0000 1.0000 0.0000
vn -0.0000 -1.0000 -0.0000
vn 0.0000 -0.0000 1.0000
vn 0.9071 -0.0000 -0.4210
vn 0.8181 -0.0000 -0.5751
vn 0.5751 -0.0000 -0.8181
vn 0.4210 -0.0000 -0.9071
vn 0.0890 0.0000 -0.9960
vn -0.0890 0.0000 -0.9960
vn -0.4210 0.0000 -0.9071
vn -0.5751 0.0000 -0.8181
vn -0.8181 0.0000 -0.5751
vn -0.9071 -0.0000 -0.4210
vn -0.9960 0.0000 -0.0890
vn -0.9960 -0.0000 0.0890
vn -0.9071 0.0000 0.4210
vn -0.8181 0.0000 0.5751
vn -0.5751 0.0000 0.8181
vn -0.4210 0.0000 0.9071
vn -0.0890 0.0000 0.9960
vn 0.0890 -0.0000 0.9960
vn 0.4210 -0.0000 0.9071
vn 0.5751 -0.0000 0.8181
vn 0.8181 -0.0000 0.5751
vn 0.9071 -0.0000 0.4210
vn 0.9960 -0.0000 0.0890
vn 0.9960 -0.0000 -0.0890
s 1
f 1//1 2//1 3//1
f 2//1 4//1 3//1
f 5//2 6//2 7//2
f 6//2 8//2 7//2
f 9//3 10//3 11//3
f 10//3 12//3 11//3
f 13//4 14//4 15//4
f 14//4 16//4 15//4
f 17//5 18//5 19//5
f 20//5 19//5 18//5
f 19//5 20//5 21//5
f 22//5 21//5 20//5
f 21//5 22//5 23//5
f 24//5 23//5 22//5
f 17//5 23//5 24//5
f 24//5 18//5 17//5
f 25//2 26//2 27//2
f 26//2 28//2 27//2
f 29//3 30//3 31//3
f 30//3 32//3 31//3
f 33//1 34//1 35//1
f 34//1 36//1 35//1
f 37//6 38//6 39//6
f 38//6 40//6 39//6
f 41//5 42//5 43//5
f 42//5 44//5 43//5
f 45//7 46//8 47//9
f 46//8 48//10 47//9
f 47//9 48//10 49//11
f 48//10 50//12 49//11
f 49//11 50//12 51//13
f 50//12 52//14 51//13
f 51//13 52//14 53//15
f 52//14 54//16 53//15
f 53//15 54//16 55//17
f 54//16 56//18 55//17
f 55//17 56//18 57//19
f 56//18 58//20 57//19
f 57//19 58//20 59//21
f 58//20 60//22 59//21
f 59//21 60//22 61//23
f 60//22 62//24 61//23
f 61//23 62//24 63//25
f 62//24 64//26 63//25
f 63//25 64//26 65//27
f 64//26 66//28 65//27
f 65//27 66//28 67//29
f 66//28 68//30 67//29
f 67//29 68//30 45//7
f 68//30 46//8 45//7
f 69//4 70//4 71//4
f 70//4 72//4 71//4
f 72//4 73//4 71//4
f 73//4 74//4 71//4
f 74//4 75//4 71//4
f 75//4 76//4 71//4
f 76//4 77//4 71//4
f 77//4 78//4 71//4
f 78//4 79//4 71//4
f 79//4 80//4 71//4
f 80//4 81//4 71//4
f 81//4 69//4 71//4
f 82//7 83//8 84//9
f 83//8 85//10 84//9
f 84//9 85//10 86//11
f 85//10 87//12 86//11
f 86//11 87//12 88//13
f 87//12 89//14 88//13
f 88//13 89//14 90//15
f 89//14 91//16 90//15
f 90//15 91//16 92//17
f 91//16 93//18 92//17
f 92//17 93//18 94//19
f 93//18 95//20 94//19
f 94//19 95//20 96//21
f 95//20 97//22 96//21
f 96//21 97//22 98//23
f 97//22 99//24 98//23
f 98//23 99//24 100//25
f 99//24 101//26 100//25
f 100//25 101//26 102//27
f 101//26 103//28 102//27
f 102//27 103//28 104//29
f 103//28 105//30 104//29
f 104//29 105//30 82//7
f 105//30 83//8 82//7
f 106//4 107//4 108//4
f 107//4 109//4 108//4
f 109//4 110//4 108//4
f 110//4 111//4 108//4
f 111//4 112//4 108//4
f 112//4 113//4 108//4
f 113//4 114//4 108//4
f 114//4 115//4 108//4
f 115//4 116//4 108//4
f 116//4 117//4 108//4
f 117//4 118//4 108//4
f 118//4 106//4 108//4
f 119//7 120//8 121//9
f 120//8 122//10 121//9
f 121//9 122//10 123//11
f 122//10 124//12 123//11
f 123//11 124//12 125//13
f 124//12 126//14 125//13
f 125//13 126//14 127//15
f 126//14 128//16 127//15
f 127//15 128//16 129//17
f 128//16 130//18 129//17
f 129//17 130//18 131//19
f 130//18 132//20 131//19
f 131//19 132//20 133//21
f 132//20 134//22 133//21
f 133//21 134//22 135//23
f 134//22 136//24 135//23
f 135//23 136//24 137//25
f 136//24 138//26 137//25
f 137//25 138//26 139//27
f 138//26 140//28 139//27
f 139//27 140//28 141//29
f 140//28 142//30 141//29
f 141//29 142//30 119//7
f 142//30 120//8 119//7
f 143//4 144//4 145//4
f 144//4 146//4 145//4
f 146//4 147//4 145//4
f 147//4 148//4 145//4
f 148//4 149//4 145//4
f 149//4 150//4 145//4
f 150//4 151//4 145//4
f 151//4 152//4 145//4
f 152//4 153//4 145//4
f 153//4 154//4 145//4
f 154//4 155//4 145//4
f 155//4 143//4 145//4
f 156//7 157//8 158//9
f 157//8 159//10 158//9
f 158//9 159//10 160//11
f 159//10 161//12 160//11
f 160//11 161//12 162//13
f 161//12 163//14 162//13
f 162//13 163//14 164//15
f 163//14 165//16 164//15
f 164//15 165//16 166//17
f 165//16 167//18 166//17
f 166//17 167//18 168//19
f 167//18 169//20 168//19
f 168//19 169//20 170//21
f 169//20 171//22 170//21
f 170//21 171//22 172//23
f 171//22 173//24 172//23
f 172//23 173//24 174//25
f 173//24 175//26 174//25
f 174//25 175//26 176//27
f 175//26 177//28 176//27
f 176//27 177//28 178//29
f 177//28 179//30 178//29
f 178//29 179//30 156//7
f 179//30 157//8 156//7
f 180//4 181//4 182//4
f 181//4 183//4 182//4
f 183//4 184//4 182//4
f 184//4 185//4 182//4
f 185//4 186//4 182//4
f 186//4 187//4 182//4
f 187//4 188//4 182//4
f 188//4 189//4 182//4
f 189//4 190//4 182//4
f 190//4 191//4 182//4
f 191//4 192//4 182//4
f 192//4 180//4 182//4
f 193//7 194//8 195//9
f 194//8 196//10 195//9
f 195//9 196//10 197//11
f 196//10 198//12 197//11
f 197//11 198//12 199//13
f 198//12 200//14 199//13
f 199//13 200//14 201//15
f 200//14 202//16 201//15
f 201//15 202//16 203//17
f 202//16 204//18 203//17
f 203//17 204//18 205//19
f 204//18 206//20 205//19
f 205//19 206//20 207//21
f 206//20 208//22 207//21
f 207//21 208//22 209//23
f 208//22 210//24 209//23
f 209//23 210//24 211//25
f 210//24 212//26 211//25
f 211//25 212//26 213//27
f 212//26 214//28 213//27
f 213//27 214//28 215//29
f 214//28 216//30 215//29
f 215//29 216//30 193//7
f 216//30 194//8 193//7
f 217//4 218//4 219//4
f 218//4 220//4 219//4
f 220//4 221//4 219//4
f 221//4 222//4 219//4
f 222//4 223//4 219//4
f 223//4 224//4 219//4
f 224//4 225//4 219//4
f 225//4 226//4 219//4
f 226//4 227//4 219//4
f 227//4 228//4 219//4
f 228//4 229//4 219//4
f 229//4 217//4 219//4
f 230//7 231//8 232//9
f 231//8 233//10 232//9
f 232//9 233//10 234//11
f 233//10 235//12 234//11
f 234//11 235//12 236//13
f 235//12 237//14 236//13
f 236//13 237//14 238//15
f 237//14 239//16 238//15
f 238//15 239//16 240//17
f 239//16 241//18 240//17
f 240//17 241//18 242//19
f 241//18 243//20 242//19
f 242//19 243//20 244//21
f 243//20 245//22 244//21
f 244//21 245//22 246//23
f 245//22 247//24 246//23
f 246//23 247//24 248//25
f 247//24 249//26 248//25
f 248//25 249//26 250//27
f 249//26 251//28 250//27
f 250//27 251//28 252//29
f 251//28 253//30 252//29
f 252//29 253//30 230//7
f 253//30 231//8 230//7
f 254//4 255//4 256//4
f 255//4 257//4 256//4
f 257//4 258//4 256//4
f 258//4 259//4 256//4
f 259//4 260//4 256//4
f 260//4 261//4 256//4
f 261//4 262//4 256//4
f 262//4 263//4 256//4
f 263//4 264//4 256//4
f 264//4 265//4 256//4
f 265//4 266//4 256//4
f 266//4 254//4 256//4

BTW the arrays are of float and int, specifically:

int verticesLength = some-value-here;
int indicesLength = some-value-here;
float[] vertices = new float[verticesLength * 3];
float[] normals = new float[verticesLength * 3];
float[] uvs = new float[verticesLength * 2];
int[] indices = new int[indicesLength * 3];

I am posting here also the output made from the 2nd obj file (the longer one) for that normals array produced by the CSharp app I am trying to emulate - it is quite long but if you can notice how it changed the length of the normals array (2nd line) to be the length of the vertices array (this is requirement which needs to be met by the app the file is used in, unfortunately):

1.0 0.0 0.0 -0.0 -1.0 -0.0 1.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0 -0.0 0.0 -1.0 -0.0 -1.0 -0.0 1.0 0.0 0.0 -0.0 0.0 -1.0 0.0 1.0 0.0 -1.0 0.0 0.0 -0.0 -1.0 -0.0 -0.0 -1.0 -0.0 -0.0 0.0 -1.0 1.0 0.0 0.0 0.0 -0.0 1.0 -1.0 0.0 0.0 0.0 1.0 0.0 -1.0 0.0 0.0 -0.0 0.0 -1.0 -0.0 -1.0 -0.0 -0.0 -1.0 -0.0 1.0 0.0 0.0 -1.0 0.0 0.0 -0.0 0.0 -1.0 0.0 1.0 0.0 -0.0 -1.0 -0.0 -1.0 0.0 0.0 -0.0 -1.0 -0.0 -1.0 0.0 0.0 -0.0 0.0 -1.0 0.0 -0.0 1.0 -1.0 0.0 0.0 -0.0 -1.0 -0.0 -1.0 0.0 0.0 -0.0 0.0 -1.0 0.0 -0.0 1.0 -0.0 -1.0 -0.0 -0.0 0.0 -1.0 1.0 0.0 0.0 0.0 -0.0 1.0 -0.0 -1.0 -0.0 1.0 0.0 0.0 -0.0 -1.0 -0.0 0.9071 -0.0 -0.421 0.8181 -0.0 -0.5751 0.5751 -0.0 -0.8181 0.421 -0.0 -0.9071 0.089 0.0 -0.996 -0.089 0.0 -0.996 -0.421 0.0 -0.9071 -0.5751 0.0 -0.8181 -0.8181 0.0 -0.5751 -0.9071 -0.0 -0.421 -0.996 0.0 -0.089 -0.996 -0.0 0.089 -0.9071 0.0 0.421 -0.8181 0.0 0.5751 -0.5751 0.0 0.8181 -0.421 0.0 0.9071 -0.089 0.0 0.996 0.089 -0.0 0.996 0.421 -0.0 0.9071 0.5751 -0.0 0.8181 0.8181 -0.0 0.5751 0.9071 -0.0 0.421 0.996 -0.0 0.089 0.996 -0.0 -0.089 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.9071 -0.0 -0.421 0.8181 -0.0 -0.5751 0.5751 -0.0 -0.8181 0.421 -0.0 -0.9071 0.089 0.0 -0.996 -0.089 0.0 -0.996 -0.421 0.0 -0.9071 -0.5751 0.0 -0.8181 -0.8181 0.0 -0.5751 -0.9071 -0.0 -0.421 -0.996 0.0 -0.089 -0.996 -0.0 0.089 -0.9071 0.0 0.421 -0.8181 0.0 0.5751 -0.5751 0.0 0.8181 -0.421 0.0 0.9071 -0.089 0.0 0.996 0.089 -0.0 0.996 0.421 -0.0 0.9071 0.5751 -0.0 0.8181 0.8181 -0.0 0.5751 0.9071 -0.0 0.421 0.996 -0.0 0.089 0.996 -0.0 -0.089 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.9071 -0.0 -0.421 0.8181 -0.0 -0.5751 0.5751 -0.0 -0.8181 0.421 -0.0 -0.9071 0.089 0.0 -0.996 -0.089 0.0 -0.996 -0.421 0.0 -0.9071 -0.5751 0.0 -0.8181 -0.8181 0.0 -0.5751 -0.9071 -0.0 -0.421 -0.996 0.0 -0.089 -0.996 -0.0 0.089 -0.9071 0.0 0.421 -0.8181 0.0 0.5751 -0.5751 0.0 0.8181 -0.421 0.0 0.9071 -0.089 0.0 0.996 0.089 -0.0 0.996 0.421 -0.0 0.9071 0.5751 -0.0 0.8181 0.8181 -0.0 0.5751 0.9071 -0.0 0.421 0.996 -0.0 0.089 0.996 -0.0 -0.089 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.9071 -0.0 -0.421 0.8181 -0.0 -0.5751 0.5751 -0.0 -0.8181 0.421 -0.0 -0.9071 0.089 0.0 -0.996 -0.089 0.0 -0.996 -0.421 0.0 -0.9071 -0.5751 0.0 -0.8181 -0.8181 0.0 -0.5751 -0.9071 -0.0 -0.421 -0.996 0.0 -0.089 -0.996 -0.0 0.089 -0.9071 0.0 0.421 -0.8181 0.0 0.5751 -0.5751 0.0 0.8181 -0.421 0.0 0.9071 -0.089 0.0 0.996 0.089 -0.0 0.996 0.421 -0.0 0.9071 0.5751 -0.0 0.8181 0.8181 -0.0 0.5751 0.9071 -0.0 0.421 0.996 -0.0 0.089 0.996 -0.0 -0.089 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.9071 -0.0 -0.421 0.8181 -0.0 -0.5751 0.5751 -0.0 -0.8181 0.421 -0.0 -0.9071 0.089 0.0 -0.996 -0.089 0.0 -0.996 -0.421 0.0 -0.9071 -0.5751 0.0 -0.8181 -0.8181 0.0 -0.5751 -0.9071 -0.0 -0.421 -0.996 0.0 -0.089 -0.996 -0.0 0.089 -0.9071 0.0 0.421 -0.8181 0.0 0.5751 -0.5751 0.0 0.8181 -0.421 0.0 0.9071 -0.089 0.0 0.996 0.089 -0.0 0.996 0.421 -0.0 0.9071 0.5751 -0.0 0.8181 0.8181 -0.0 0.5751 0.9071 -0.0 0.421 0.996 -0.0 0.089 0.996 -0.0 -0.089 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.9071 -0.0 -0.421 0.8181 -0.0 -0.5751 0.5751 -0.0 -0.8181 0.421 -0.0 -0.9071 0.089 0.0 -0.996 -0.089 0.0 -0.996 -0.421 0.0 -0.9071 -0.5751 0.0 -0.8181 -0.8181 0.0 -0.5751 -0.9071 -0.0 -0.421 -0.996 0.0 -0.089 -0.996 -0.0 0.089 -0.9071 0.0 0.421 -0.8181 0.0 0.5751 -0.5751 0.0 0.8181 -0.421 0.0 0.9071 -0.089 0.0 0.996 0.089 -0.0 0.996 0.421 -0.0 0.9071 0.5751 -0.0 0.8181 0.8181 -0.0 0.5751 0.9071 -0.0 0.421 0.996 -0.0 0.089 0.996 -0.0 -0.089 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0

UPDATE: So I kind-of achieved partial success by creating List of pairs of v/vn from the indices section (f) where I think it basically say vertices line v is associated to normals line vn, then sorting the list, removing duplicates and finally recreating new normals array in length of the vertices array all of that like this but it is still somewhat "off", anyway:

//indicesPairs is CopyOnWriteArrayList<int[]> filled during obj file parsing
Collections.sort(indicesPairs, (o1, o2) -> {
    for (int i = 0; i < o1.length; i++) {
        if (o1[i] < o2[i]) {
            return -1;
        }
        if (o1[i] > o2[i]) {
            return 1;
        }
    }
    return 0;
});
CopyOnWriteArrayList<Float> normalsNew = new CopyOnWriteArrayList<>();
for (int i = 0; i < indicesPairs.size(); i++) {
    int normalsBlock = indicesPairs.get(i)[1] - 1;
    normalsNew.add(normals.get(normalsBlock * 3));
    normalsNew.add(normals.get(normalsBlock * 3) + 1);
    normalsNew.add(normals.get(normalsBlock * 3) + 2);
}

So I guess what I need now is some kind of a correction of those normals which now produce some kind of light where shadows should be and such...but how to do that and what kind of correction?


Solution

  • Finally I solved it myself: after lots of tests I found out my partial solution in Update section of my question (at the end) was actually in principle very right, the only problem was mistake I made when selecting the right index of array(s).

    So the final solution to make normals to the length of vertices is this (it's understood there needs to be some normals defined in OBJ file and indices/faces "f" needs to have both vertice "v" and normal "vn" value in form of f v//vn, or if object has uvs then f v/vt/vn ...in case there is just one value for f like f some-integer-here then the OBJ file should have defined normals in exact length of vertices thus no need to do this process as it already has expected length (expected meaning for this case of my question) - if not then it is most probably something wrong with the OBJ file itself):

    // "normals" is CopyOnWriteArrayList<Float> filled with actual normals defined in OBJ file
    // "indicesPairs" is CopyOnWriteArrayList<int[]> filled during OBJ file parsing
    // it takes "f v/vt/vn" or "f v//vn" and makes array of 1st & 3rd value "new int[]{v, vn}"
    // both values are lowered by 1 as index starts at 0, not 1
    // also any duplicate is removed so it has the right expected length: "vertices.length/3"
    Collections.sort(indicesPairs, (o1, o2) -> {
        for (int i = 0; i < o1.length; i++) {
            if (o1[i] < o2[i]) {
                return -1;
            }
            if (o1[i] > o2[i]) {
                return 1;
            }
        }
        return 0;
    });
    
    for (int i = 0; i < indicesPairs.size(); i++) {
    
        int normalsBlock = indicesPairs.get(i)[1];
    
        float n1 = normals.get(normalsBlock * 3);
        float n2 = normals.get((normalsBlock * 3) + 1);
        float n3 = normals.get((normalsBlock * 3) + 2);
    
        normalsNew.add(n1);
        normalsNew.add(n2);
        normalsNew.add(n3);
    }