0

I'm working on a 3D rendering project following a YouTube tutorial series. I've gotten as far as rendering 3D textured models using OBJ file format.

3D render with model provided by the tutorial:

3D render with model provided by the tutorial

The issue is that when I make my own voxel-based model, the texture maps weirdly to the object.

Simple 2 colour voxel cube with warped texture:

Simple 2 colour voxel cube with warped texture

As far as I can tell it's some kind of interpolation issue, but I'm not familiar enough with the 3D OpenGL framework to know for certain.

Any help would be greatly appreciated.

OBJ File:

# www.blender.org
mtllib grass2.mtl
o ObjObject
v -0.400000 0.500000 0.300000
v -0.400000 0.000000 -0.400000
v -0.400000 -0.000000 0.400000
v -0.400000 0.500000 0.100000
v -0.400000 0.500000 0.000000
v -0.400000 0.500000 -0.300000
v -0.400000 0.600000 0.400000
v -0.400000 0.600000 0.300000
v -0.400000 0.600000 0.100000
v -0.400000 0.600000 0.000000
v -0.400000 0.600000 -0.300000
v -0.400000 0.600000 -0.400000
v -0.400000 0.800000 0.400000
v -0.400000 0.800000 -0.400000
v 0.400000 -0.000000 0.400000
v 0.400000 0.000000 -0.400000
v 0.400000 0.500000 0.300000
v 0.400000 0.500000 0.000000
v 0.400000 0.500000 -0.100000
v 0.400000 0.500000 -0.300000
v 0.400000 0.600000 0.400000
v 0.400000 0.600000 0.300000
v 0.400000 0.600000 0.000000
v 0.400000 0.600000 -0.100000
v 0.400000 0.600000 -0.300000
v 0.400000 0.600000 -0.400000
v 0.400000 0.800000 0.400000
v 0.400000 0.800000 -0.400000
v -0.300000 0.500000 0.400000
v -0.400000 0.600000 0.400000
v -0.400000 -0.000000 0.400000
v -0.300000 0.600000 0.400000
v -0.400000 0.800000 0.400000
v 0.000000 0.500000 0.400000
v 0.000000 0.600000 0.400000
v 0.100000 0.500000 0.400000
v 0.100000 0.600000 0.400000
v 0.300000 0.500000 0.400000
v 0.300000 0.600000 0.400000
v 0.400000 -0.000000 0.400000
v 0.400000 0.600000 0.400000
v 0.400000 0.800000 0.400000
v -0.400000 0.000000 -0.400000
v -0.400000 0.600000 -0.400000
v -0.300000 0.500000 -0.400000
v -0.400000 0.800000 -0.400000
v -0.300000 0.600000 -0.400000
v -0.100000 0.500000 -0.400000
v -0.100000 0.600000 -0.400000
v 0.000000 0.500000 -0.400000
v 0.000000 0.600000 -0.400000
v 0.300000 0.500000 -0.400000
v 0.300000 0.600000 -0.400000
v 0.400000 0.000000 -0.400000
v 0.400000 0.600000 -0.400000
v 0.400000 0.800000 -0.400000
v -0.400000 0.000000 -0.400000
v 0.400000 -0.000000 0.400000
v -0.400000 -0.000000 0.400000
v 0.400000 0.000000 -0.400000
v -0.400000 0.800000 0.400000
v 0.400000 0.800000 0.400000
v -0.400000 0.800000 -0.400000
v 0.400000 0.800000 -0.400000
v -0.400000 0.500000 0.300000
v -0.400000 0.000000 -0.400000
v -0.400000 -0.000000 0.400000
v -0.400000 0.500000 0.100000
v -0.400000 0.500000 0.000000
v -0.400000 0.500000 -0.300000
v -0.400000 0.600000 0.400000
v -0.400000 0.600000 0.300000
v -0.400000 0.600000 0.100000
v -0.400000 0.600000 0.000000
v -0.400000 0.600000 -0.300000
v -0.400000 0.600000 -0.400000
v -0.400000 0.800000 0.400000
v -0.400000 0.800000 -0.400000
v 0.400000 -0.000000 0.400000
v 0.400000 0.000000 -0.400000
v 0.400000 0.500000 0.300000
v 0.400000 0.500000 0.000000
v 0.400000 0.500000 -0.100000
v 0.400000 0.500000 -0.300000
v 0.400000 0.600000 0.400000
v 0.400000 0.600000 0.300000
v 0.400000 0.600000 0.000000
v 0.400000 0.600000 -0.100000
v 0.400000 0.600000 -0.300000
v 0.400000 0.600000 -0.400000
v 0.400000 0.800000 0.400000
v 0.400000 0.800000 -0.400000
v -0.300000 0.500000 0.400000
v -0.400000 0.600000 0.400000
v -0.400000 -0.000000 0.400000
v -0.300000 0.600000 0.400000
v -0.400000 0.800000 0.400000
v 0.000000 0.500000 0.400000
v 0.000000 0.600000 0.400000
v 0.100000 0.500000 0.400000
v 0.100000 0.600000 0.400000
v 0.300000 0.500000 0.400000
v 0.300000 0.600000 0.400000
v 0.400000 -0.000000 0.400000
v 0.400000 0.600000 0.400000
v 0.400000 0.800000 0.400000
v -0.400000 0.000000 -0.400000
v -0.400000 0.600000 -0.400000
v -0.300000 0.500000 -0.400000
v -0.400000 0.800000 -0.400000
v -0.300000 0.600000 -0.400000
v -0.100000 0.500000 -0.400000
v -0.100000 0.600000 -0.400000
v 0.000000 0.500000 -0.400000
v 0.000000 0.600000 -0.400000
v 0.300000 0.500000 -0.400000
v 0.300000 0.600000 -0.400000
v 0.400000 0.000000 -0.400000
v 0.400000 0.600000 -0.400000
v 0.400000 0.800000 -0.400000
v -0.400000 0.000000 -0.400000
v 0.400000 -0.000000 0.400000
v -0.400000 -0.000000 0.400000
v 0.400000 0.000000 -0.400000
v -0.400000 0.800000 0.400000
v 0.400000 0.800000 0.400000
v -0.400000 0.800000 -0.400000
v 0.400000 0.800000 -0.400000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.537109 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vt 0.904297 0.500000
vn -1.0000 0.0000 0.0000
vn 1.0000 0.0000 0.0000
vn -0.0000 -0.0000 1.0000
vn 0.0000 0.0000 -1.0000
vn 0.0000 -1.0000 -0.0000
vn 0.0000 1.0000 0.0000
usemtl palette
s 1
f 1/1/1 2/2/1 3/3/1
f 4/4/1 2/2/1 1/1/1
f 5/5/1 2/2/1 4/4/1
f 6/6/1 2/2/1 5/5/1
f 7/7/1 1/1/1 3/3/1
f 8/8/1 4/9/1 1/10/1
f 8/11/1 1/1/1 7/7/1
f 9/12/1 5/5/1 4/4/1
f 9/13/1 4/9/1 8/8/1
f 10/14/1 6/15/1 5/16/1
f 10/17/1 5/5/1 9/12/1
f 11/18/1 2/2/1 6/6/1
f 11/19/1 6/15/1 10/14/1
f 12/20/1 2/2/1 11/18/1
f 13/21/1 10/14/1 9/13/1
f 13/21/1 8/8/1 7/22/1
f 13/21/1 12/23/1 11/19/1
f 13/21/1 11/19/1 10/14/1
f 13/21/1 9/13/1 8/8/1
f 14/24/1 12/23/1 13/21/1
f 15/25/2 16/26/2 17/27/2
f 17/27/2 16/26/2 18/28/2
f 18/28/2 16/26/2 19/29/2
f 19/29/2 16/26/2 20/30/2
f 15/25/2 17/27/2 21/31/2
f 17/32/2 18/33/2 22/34/2
f 21/31/2 17/27/2 22/35/2
f 18/28/2 19/29/2 23/36/2
f 22/34/2 18/33/2 23/37/2
f 19/38/2 20/39/2 24/40/2
f 23/36/2 19/29/2 24/41/2
f 20/30/2 16/26/2 25/42/2
f 24/40/2 20/39/2 25/43/2
f 25/42/2 16/26/2 26/44/2
f 23/37/2 24/40/2 27/45/2
f 21/46/2 22/34/2 27/45/2
f 25/43/2 26/47/2 27/45/2
f 24/40/2 25/43/2 27/45/2
f 22/34/2 23/37/2 27/45/2
f 27/45/2 26/47/2 28/48/2
f 29/49/3 30/50/3 31/51/3
f 32/52/3 33/53/3 30/54/3
f 32/55/3 30/50/3 29/49/3
f 34/56/3 29/49/3 31/51/3
f 34/57/3 32/52/3 29/58/3
f 35/59/3 33/53/3 32/52/3
f 35/59/3 32/52/3 34/57/3
f 36/60/3 34/56/3 31/51/3
f 36/60/3 35/61/3 34/56/3
f 37/62/3 33/53/3 35/59/3
f 37/63/3 35/61/3 36/60/3
f 38/64/3 36/60/3 31/51/3
f 38/65/3 37/62/3 36/66/3
f 39/67/3 33/53/3 37/62/3
f 39/67/3 37/62/3 38/65/3
f 40/68/3 38/64/3 31/51/3
f 40/68/3 39/69/3 38/64/3
f 41/70/3 33/53/3 39/67/3
f 41/71/3 39/69/3 40/68/3
f 42/72/3 33/53/3 41/70/3
f 43/73/4 44/74/4 45/75/4
f 44/76/4 46/77/4 47/78/4
f 45/75/4 44/74/4 47/79/4
f 43/73/4 45/75/4 48/80/4
f 45/81/4 47/78/4 48/82/4
f 47/78/4 46/77/4 49/83/4
f 48/82/4 47/78/4 49/83/4
f 43/73/4 48/80/4 50/84/4
f 48/80/4 49/85/4 50/84/4
f 49/83/4 46/77/4 51/86/4
f 50/84/4 49/85/4 51/87/4
f 43/73/4 50/84/4 52/88/4
f 50/89/4 51/86/4 52/90/4
f 51/86/4 46/77/4 53/91/4
f 52/90/4 51/86/4 53/91/4
f 43/73/4 52/88/4 54/92/4
f 52/88/4 53/93/4 54/92/4
f 53/91/4 46/77/4 55/94/4
f 54/92/4 53/93/4 55/95/4
f 55/94/4 46/77/4 56/96/4
f 57/97/5 58/98/5 59/99/5
f 60/100/5 58/98/5 57/97/5
f 61/101/6 62/102/6 63/103/6
f 63/103/6 62/102/6 64/104/6
usemtl palette.001
f 65/105/1 66/106/1 67/107/1
f 68/108/1 66/106/1 65/105/1
f 69/109/1 66/106/1 68/108/1
f 70/110/1 66/106/1 69/109/1
f 71/111/1 65/105/1 67/107/1
f 72/112/1 68/113/1 65/114/1
f 72/115/1 65/105/1 71/111/1
f 73/116/1 69/109/1 68/108/1
f 73/117/1 68/113/1 72/112/1
f 74/118/1 70/119/1 69/120/1
f 74/121/1 69/109/1 73/116/1
f 75/122/1 66/106/1 70/110/1
f 75/123/1 70/119/1 74/118/1
f 76/124/1 66/106/1 75/122/1
f 77/125/1 74/118/1 73/117/1
f 77/125/1 72/112/1 71/126/1
f 77/125/1 76/127/1 75/123/1
f 77/125/1 75/123/1 74/118/1
f 77/125/1 73/117/1 72/112/1
f 78/128/1 76/127/1 77/125/1
f 79/129/2 80/130/2 81/131/2
f 81/131/2 80/130/2 82/132/2
f 82/132/2 80/130/2 83/133/2
f 83/133/2 80/130/2 84/134/2
f 79/129/2 81/131/2 85/135/2
f 81/136/2 82/137/2 86/138/2
f 85/135/2 81/131/2 86/139/2
f 82/132/2 83/133/2 87/140/2
f 86/138/2 82/137/2 87/141/2
f 83/142/2 84/143/2 88/144/2
f 87/140/2 83/133/2 88/145/2
f 84/134/2 80/130/2 89/146/2
f 88/144/2 84/143/2 89/147/2
f 89/146/2 80/130/2 90/148/2
f 87/141/2 88/144/2 91/149/2
f 85/150/2 86/138/2 91/149/2
f 89/147/2 90/151/2 91/149/2
f 88/144/2 89/147/2 91/149/2
f 86/138/2 87/141/2 91/149/2
f 91/149/2 90/151/2 92/152/2
f 93/153/3 94/154/3 95/155/3
f 96/156/3 97/157/3 94/158/3
f 96/159/3 94/154/3 93/153/3
f 98/160/3 93/153/3 95/155/3
f 98/161/3 96/156/3 93/162/3
f 99/163/3 97/157/3 96/156/3
f 99/163/3 96/156/3 98/161/3
f 100/164/3 98/160/3 95/155/3
f 100/164/3 99/165/3 98/160/3
f 101/166/3 97/157/3 99/163/3
f 101/167/3 99/165/3 100/164/3
f 102/168/3 100/164/3 95/155/3
f 102/169/3 101/166/3 100/170/3
f 103/171/3 97/157/3 101/166/3
f 103/171/3 101/166/3 102/169/3
f 104/172/3 102/168/3 95/155/3
f 104/172/3 103/173/3 102/168/3
f 105/174/3 97/157/3 103/171/3
f 105/175/3 103/173/3 104/172/3
f 106/176/3 97/157/3 105/174/3
f 107/177/4 108/178/4 109/179/4
f 108/180/4 110/181/4 111/182/4
f 109/179/4 108/178/4 111/183/4
f 107/177/4 109/179/4 112/184/4
f 109/185/4 111/182/4 112/186/4
f 111/182/4 110/181/4 113/187/4
f 112/186/4 111/182/4 113/187/4
f 107/177/4 112/184/4 114/188/4
f 112/184/4 113/189/4 114/188/4
f 113/187/4 110/181/4 115/190/4
f 114/188/4 113/189/4 115/191/4
f 107/177/4 114/188/4 116/192/4
f 114/193/4 115/190/4 116/194/4
f 115/190/4 110/181/4 117/195/4
f 116/194/4 115/190/4 117/195/4
f 107/177/4 116/192/4 118/196/4
f 116/192/4 117/197/4 118/196/4
f 117/195/4 110/181/4 119/198/4
f 118/196/4 117/197/4 119/199/4
f 119/198/4 110/181/4 120/200/4
f 121/201/5 122/202/5 123/203/5
f 124/204/5 122/202/5 121/201/5
f 125/205/6 126/206/6 127/207/6
f 127/207/6 126/206/6 128/208/6

Texture File:

Texture File

OBJ Loader:

package Voxel2.renderEngine;

import Voxel2.models.RawModel;
import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.util.vector.Vector3f;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class OBJLoader {

    public static RawModel loadObjModel(String fileName, Loader loader) {
        FileReader fr = null;
        try {
            fr = new FileReader(new File("res/models/" + fileName + ".obj"));
        } catch (FileNotFoundException e) {
            System.err.println("Couldn't load model!");
            e.printStackTrace();
        }
        BufferedReader reader = new BufferedReader(fr);
        String line;
        List<Vector3f> vertices = new ArrayList<>();
        List<Vector2f> textures = new ArrayList<>();
        List<Vector3f> normals = new ArrayList<>();
        List<Integer> indices = new ArrayList<>();
        float[] verticesArray = null;
        float[] normalsArray = null;
        float[] textureArray = null;
        int[] indicesArray = null;

        try {
            while(true) {
                line = reader.readLine();
                String[] currentLine = line.split(" ");
                if (line.startsWith("v ")) {
                    Vector3f vertex = new Vector3f(Float.parseFloat(currentLine[1]), Float.parseFloat(currentLine[2]), Float.parseFloat(currentLine[3]));
                    vertices.add(vertex);
                } else if (line.startsWith("vt ")) {
                    Vector2f texture = new Vector2f(Float.parseFloat(currentLine[1]), Float.parseFloat(currentLine[2]));
                    textures.add(texture);
                } else if (line.startsWith("vn ")) {
                    Vector3f normal = new Vector3f(Float.parseFloat(currentLine[1]), Float.parseFloat(currentLine[2]), Float.parseFloat(currentLine[3]));
                    normals.add(normal);
                } else if (line.startsWith("f ")) {
                    textureArray = new float[vertices.size() * 2];
                    normalsArray = new float[vertices.size() * 3];
                    break;
                }
            }

            while (line != null) {
                if (!line.startsWith("f ")) {
                    line = reader.readLine();
                    continue;
                }
                String[] currentLine = line.split(" ");
                String[] vertex1 = currentLine[1].split("/");
                String[] vertex2 = currentLine[2].split("/");
                String[] vertex3 = currentLine[3].split("/");

                processVertex(vertex1, indices, textures, normals, textureArray, normalsArray);
                processVertex(vertex2, indices, textures, normals, textureArray, normalsArray);
                processVertex(vertex3, indices, textures, normals, textureArray, normalsArray);
                line = reader.readLine();
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        verticesArray = new float[vertices.size() * 3];
        indicesArray = new int[indices.size()];

        int vertexPointer = 0;
        for (Vector3f vertex : vertices) {
            verticesArray[vertexPointer++] = vertex.x;
            verticesArray[vertexPointer++] = vertex.y;
            verticesArray[vertexPointer++] = vertex.z;
        }

        for (int i = 0; i < indices.size(); i ++) {
            indicesArray[i] = indices.get(i);
        }

        return loader.loadToVAO(verticesArray, textureArray, indicesArray);
    }

    private static void processVertex(String[] vertexData, List<Integer> indices, List<Vector2f> textures,
                                      List<Vector3f> normals, float[] textureArray, float[] normalsArray) {
        int currentVertexPointer = Integer.parseInt(vertexData[0]) - 1;
        indices.add(currentVertexPointer);
        Vector2f currentTex = textures.get(Integer.parseInt(vertexData[1]) -1);
        textureArray[currentVertexPointer * 2] = currentTex.x;
        textureArray[currentVertexPointer * 2 + 1] = 1- currentTex.y;
        Vector3f currentNorm = normals.get(Integer.parseInt(vertexData[2]) - 1);
        normalsArray[currentVertexPointer * 3] = currentNorm.x;
        normalsArray[currentVertexPointer * 3 + 1] = currentNorm.y;
        normalsArray[currentVertexPointer * 3 + 2] = currentNorm.z;
    }
}

Renderer:

package Voxel2.renderEngine;

import Voxel2.models.RawModel;
import Voxel2.models.TexturedModel;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.util.vector.Matrix4f;

import Voxel2.shaders.StaticShader;
import Voxel2.toolbox.Maths;

import Voxel2.entities.Entity;

public class Renderer {
    
    private static final float FOV = 70;
    private static final float NEAR_PLANE = 0.1f;
    private static final float FAR_PLANE = 1000;
    
    private Matrix4f projectionMatrix;
    
    public Renderer(StaticShader shader){
        createProjectionMatrix();
        shader.start();
        shader.loadProjectionMatrix(projectionMatrix);
        shader.stop();
    }

    public void prepare() {
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glClearColor(0.3f, 0.3f, 0.3f, 1);
    }

    public void render(Entity entity, StaticShader shader) {
        TexturedModel model = entity.getModel();
        RawModel rawModel = model.getRawModel();
        GL30.glBindVertexArray(rawModel.getVaoID());
        GL20.glEnableVertexAttribArray(0);
        GL20.glEnableVertexAttribArray(1);
        Matrix4f transformationMatrix = Maths.createTransformationMatrix(entity.getPosition(),
                entity.getRotX(), entity.getRotY(), entity.getRotZ(), entity.getScale());
        shader.loadTransformationMatrix(transformationMatrix);
        GL13.glActiveTexture(GL13.GL_TEXTURE0);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getTexture().getID());
        GL11.glDrawElements(GL11.GL_TRIANGLES, rawModel.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
        GL20.glDisableVertexAttribArray(0);
        GL20.glDisableVertexAttribArray(1);
        GL30.glBindVertexArray(0);
    }
    
    private void createProjectionMatrix(){
        float aspectRatio = (float) Display.getWidth() / (float) Display.getHeight();
        float y_scale = (float) ((1f / Math.tan(Math.toRadians(FOV / 2f))) * aspectRatio);
        float x_scale = y_scale / aspectRatio;
        float frustum_length = FAR_PLANE - NEAR_PLANE;

        projectionMatrix = new Matrix4f();
        projectionMatrix.m00 = x_scale;
        projectionMatrix.m11 = y_scale;
        projectionMatrix.m22 = -((FAR_PLANE + NEAR_PLANE) / frustum_length);
        projectionMatrix.m23 = -1;
        projectionMatrix.m32 = -((2 * NEAR_PLANE * FAR_PLANE) / frustum_length);
        projectionMatrix.m33 = 0;
    }

}

Fragment Shader:

#version 150

in vec2 pass_textureCoordinates;

out vec4 out_Color;

uniform sampler2D modelTexture;

void main(void){

    out_Color = texture(modelTexture,pass_textureCoordinates);

}

Vertex Shader:

#version 150

in vec3 position;
in vec2 textureCoordinates;

out vec2 pass_textureCoordinates;

uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;

void main(void){

    gl_Position = projectionMatrix * viewMatrix * transformationMatrix * vec4(position,1.0);
    pass_textureCoordinates = textureCoordinates;
}
TontonVelu
  • 471
  • 2
  • 8
  • 21

0 Answers0