[DEV] tutorial 16 'fog' implementation

This commit is contained in:
Edouard DUPIN 2020-04-20 21:02:04 +02:00
parent 22c2ea296d
commit 2779326069
17 changed files with 2149124 additions and 97 deletions

1602527
res/Tree1.obj Normal file

File diff suppressed because it is too large Load Diff

544823
res/drone.obj Normal file

File diff suppressed because it is too large Load Diff

BIN
res/flower.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

346
res/grass.obj Normal file
View File

@ -0,0 +1,346 @@
# Blender v2.67 (sub 0) OBJ File: 'lowPolyPlants.blend'
# www.blender.org
mtllib grass.mtl
o Plane
v 0.063088 0.003610 -4.997082
v -1.136912 0.003610 -4.997082
v 0.063088 1.203610 -4.997082
v -1.136912 1.203610 -4.997082
v -0.536911 0.003610 -5.597082
v -0.536912 0.003610 -4.397082
v -0.536912 1.203610 -5.597083
v -0.536912 1.203610 -4.397082
v -5.427285 0.003610 -0.769423
v -6.627285 0.003610 -0.769423
v -5.427285 1.203610 -0.769424
v -6.627285 1.203610 -0.769424
v -6.027285 0.003610 -1.369424
v -6.027285 0.003610 -0.169423
v -6.027285 1.203610 -1.369424
v -6.027285 1.203610 -0.169424
v 0.627331 0.003610 1.154491
v 0.120805 0.003610 0.066634
v 0.627331 1.203610 1.154491
v 0.120805 1.203610 0.066634
v 0.917996 0.003610 0.357300
v -0.169860 0.003611 0.863825
v 0.917996 1.203610 0.357300
v -0.169860 1.203610 0.863825
v 2.736700 0.003610 -2.973755
v 1.939363 0.003610 -3.870557
v 2.736700 1.203610 -2.973755
v 1.939363 1.203610 -3.870557
v 2.786432 0.003610 -3.820825
v 1.889630 0.003610 -3.023487
v 2.786432 1.203610 -3.820825
v 1.889630 1.203610 -3.023487
v -2.374338 0.003610 -1.890785
v -3.247929 0.003610 -2.713487
v -2.374338 1.203610 -1.890785
v -3.247929 1.203610 -2.713487
v -2.399782 0.003610 -2.738931
v -3.222484 0.003611 -1.865341
v -2.399782 1.203610 -2.738931
v -3.222484 1.203610 -1.865341
v 6.729870 0.003610 2.758007
v 7.206303 0.003610 1.656639
v 6.729870 1.203610 2.758007
v 7.206303 1.203610 1.656640
v 7.518770 0.003610 2.445540
v 6.417403 0.003611 1.969108
v 7.518770 1.203610 2.445540
v 6.417403 1.203610 1.969108
v -2.949056 0.003611 6.270791
v -2.019891 0.003611 5.511415
v -2.949056 1.203610 6.270791
v -2.019891 1.203610 5.511416
v -2.104786 0.003610 6.355686
v -2.864161 0.003611 5.426521
v -2.104786 1.203610 6.355686
v -2.864161 1.203610 5.426521
v -11.257655 0.003611 -0.649311
v -12.123328 0.003610 0.181718
v -11.257655 1.203610 -0.649312
v -12.123328 1.203610 0.181718
v -12.106008 0.003611 -0.666633
v -11.274976 0.003610 0.199040
v -12.106008 1.203610 -0.666633
v -11.274976 1.203610 0.199040
v -5.115887 0.003611 6.717537
v -5.981559 0.003611 7.548567
v -5.115886 1.203610 6.717537
v -5.981559 1.203610 7.548567
v -5.964238 0.003611 6.700215
v -5.133207 0.003611 7.565888
v -5.964237 1.203610 6.700215
v -5.133207 1.203610 7.565887
v -4.044483 0.003611 4.351145
v -5.163255 0.003611 3.917153
v -4.044483 1.203610 4.351144
v -5.163255 1.203610 3.917153
v -4.386873 0.003611 3.574763
v -4.820865 0.003611 4.693535
v -4.386873 1.203610 3.574763
v -4.820865 1.203610 4.693534
v -5.924183 0.003610 -3.071747
v -6.925926 0.003611 -3.732436
v -5.924183 1.203610 -3.071747
v -6.925926 1.203610 -3.732436
v -6.094710 0.003611 -3.902963
v -6.755400 0.003611 -2.901220
v -6.094710 1.203610 -3.902963
v -6.755400 1.203610 -2.901221
v -7.753965 0.003611 1.248239
v -8.950217 0.003611 1.153468
v -7.753965 1.203610 1.248238
v -8.950217 1.203610 1.153467
v -8.304706 0.003611 0.602727
v -8.399476 0.003611 1.798979
v -8.304706 1.203610 0.602727
v -8.399476 1.203610 1.798979
v -1.912768 0.003610 0.464466
v -2.331797 0.003611 -0.659996
v -1.912768 1.203610 0.464466
v -2.331796 1.203610 -0.659996
v -1.560051 0.003611 -0.307279
v -2.684512 0.003611 0.111750
v -1.560051 1.203610 -0.307280
v -2.684513 1.203610 0.111750
v -4.742368 0.003611 1.656384
v -5.161397 0.003611 0.531922
v -4.742368 1.203610 1.656384
v -5.161397 1.203610 0.531922
v -4.389652 0.003611 0.884638
v -5.514113 0.003611 1.303667
v -4.389652 1.203610 0.884638
v -5.514114 1.203610 1.303667
v 4.391148 1.203610 0.926363
v 3.597642 1.203610 0.026169
v 4.391148 0.003611 0.926362
v 3.597642 0.003610 0.026169
v 3.544298 1.203610 0.873019
v 4.444492 1.203610 0.079513
v 3.544298 0.003610 0.873018
v 4.444492 0.003610 0.079513
v 4.806449 1.203610 3.812527
v 3.732761 1.203610 3.276623
v 4.806449 0.003611 3.812527
v 3.732761 0.003611 3.276624
v 4.001654 1.203610 4.081419
v 4.537557 1.203610 3.007731
v 4.001654 0.003611 4.081419
v 4.537557 0.003611 3.007731
v 6.609623 1.203610 -3.161592
v 7.186171 1.203610 -4.214014
v 6.609624 0.003611 -3.161592
v 7.186171 0.003610 -4.214014
v 6.371686 1.203610 -3.976077
v 7.424108 1.203610 -3.399530
v 6.371686 0.003611 -3.976076
v 7.424108 0.003610 -3.399530
v -0.487433 1.203610 1.980146
v -1.577091 1.203610 1.477509
v -0.487433 0.003611 1.980146
v -1.577091 0.003610 1.477509
v -1.283581 1.203610 2.273656
v -0.780943 1.203610 1.183998
v -1.283581 0.003610 2.273657
v -0.780943 0.003610 1.183998
v 10.179677 1.203610 0.517455
v 8.984852 1.203610 0.406136
v 10.179677 0.003611 0.517455
v 8.984852 0.003610 0.406136
v 9.526605 1.203610 1.059208
v 9.637924 1.203610 -0.135617
v 9.526605 0.003610 1.059208
v 9.637924 0.003610 -0.135617
v 5.869500 1.203610 5.422760
v 4.930470 1.203610 6.169901
v 5.869502 0.003611 5.422760
v 4.930470 0.003611 6.169903
v 5.773557 1.203610 6.265846
v 5.026414 1.203610 5.326817
v 5.773557 0.003611 6.265847
v 5.026415 0.003611 5.326817
v 0.637121 1.203610 -1.805795
v 1.213668 1.203610 -2.858217
v 0.637121 0.003611 -1.805795
v 1.213669 0.003611 -2.858217
v 0.399184 1.203610 -2.620279
v 1.451606 1.203610 -2.043732
v 0.399184 0.003611 -2.620279
v 1.451606 0.003611 -2.043732
v 1.740329 1.203610 -0.516481
v 1.336903 1.203610 -1.646635
v 1.740329 0.003611 -0.516482
v 1.336903 0.003610 -1.646635
v 0.973539 1.203610 -0.879845
v 2.103693 1.203610 -1.283271
v 0.973540 0.003611 -0.879845
v 2.103693 0.003611 -1.283271
v 3.117721 1.203610 2.883694
v 2.804810 1.203610 1.725209
v 3.117721 0.003611 2.883693
v 2.804810 0.003611 1.725209
v 2.382023 1.203610 2.460907
v 3.540508 1.203610 2.147995
v 2.382023 0.003611 2.460907
v 3.540508 0.003611 2.147995
v 1.243791 1.203610 8.964542
v 0.077800 1.203610 9.248203
v 1.243791 0.003611 8.964540
v 0.077800 0.003611 9.248203
v 0.802626 1.203610 9.689367
v 0.518964 1.203610 8.523376
v 0.802626 0.003611 9.689367
v 0.518964 0.003611 8.523376
v 4.819168 1.203610 -1.187724
v 4.506256 1.203610 -2.346208
v 4.819168 0.003611 -1.187724
v 4.506257 0.003611 -2.346208
v 4.083470 1.203610 -1.610511
v 5.241954 1.203610 -1.923421
v 4.083470 0.003611 -1.610510
v 5.241954 0.003611 -1.923421
v 0.499905 1.203610 -8.019167
v -0.210803 1.203610 -8.986067
v 0.499905 0.003610 -8.019167
v -0.210803 0.003610 -8.986066
v -0.338899 1.203610 -8.147264
v 0.628001 1.203610 -8.857970
v -0.338898 0.003610 -8.147264
v 0.628001 0.003610 -8.857970
v -2.017782 1.203610 3.719780
v -2.953916 1.203610 2.969012
v -2.017782 0.003611 3.719780
v -2.953915 0.003611 2.969012
v -2.861232 1.203610 3.812463
v -2.110465 1.203610 2.876329
v -2.861232 0.003611 3.812463
v -2.110465 0.003611 2.876329
v 1.450961 1.203610 5.045464
v 0.268659 1.203610 4.840132
v 1.450961 0.003611 5.045464
v 0.268660 0.003611 4.840132
v 0.757144 1.203610 5.533949
v 0.962477 1.203610 4.351647
v 0.757144 0.003611 5.533949
v 0.962477 0.003611 4.351646
vt 0.000100 0.000100
vt 0.999900 0.000100
vt 0.999900 0.999900
vt 0.000100 0.999900
usemtl Material.001
s off
f 2/1 1/2 3/3 4/4
f 6/1 5/2 7/3 8/4
f 10/1 9/2 11/3 12/4
f 14/1 13/2 15/3 16/4
f 18/1 17/2 19/3 20/4
f 22/1 21/2 23/3 24/4
f 26/1 25/2 27/3 28/4
f 30/1 29/2 31/3 32/4
f 34/1 33/2 35/3 36/4
f 38/1 37/2 39/3 40/4
f 42/1 41/2 43/3 44/4
f 46/1 45/2 47/3 48/4
f 50/1 49/2 51/3 52/4
f 54/1 53/2 55/3 56/4
f 58/1 57/2 59/3 60/4
f 62/1 61/2 63/3 64/4
f 66/1 65/2 67/3 68/4
f 70/1 69/2 71/3 72/4
f 74/1 73/2 75/3 76/4
f 78/1 77/2 79/3 80/4
f 82/1 81/2 83/3 84/4
f 86/1 85/2 87/3 88/4
f 90/1 89/2 91/3 92/4
f 94/1 93/2 95/3 96/4
f 98/1 97/2 99/3 100/4
f 102/1 101/2 103/3 104/4
f 106/1 105/2 107/3 108/4
f 110/1 109/2 111/3 112/4
f 115/1 116/2 114/3 113/4
f 119/1 120/2 118/3 117/4
f 123/1 124/2 122/3 121/4
f 127/1 128/2 126/3 125/4
f 131/1 132/2 130/3 129/4
f 135/1 136/2 134/3 133/4
f 139/1 140/2 138/3 137/4
f 143/1 144/2 142/3 141/4
f 147/1 148/2 146/3 145/4
f 151/1 152/2 150/3 149/4
f 155/1 156/2 154/3 153/4
f 159/1 160/2 158/3 157/4
f 163/1 164/2 162/3 161/4
f 167/1 168/2 166/3 165/4
f 171/1 172/2 170/3 169/4
f 175/1 176/2 174/3 173/4
f 179/1 180/2 178/3 177/4
f 183/1 184/2 182/3 181/4
f 187/1 188/2 186/3 185/4
f 191/1 192/2 190/3 189/4
f 195/1 196/2 194/3 193/4
f 199/1 200/2 198/3 197/4
f 203/1 204/2 202/3 201/4
f 207/1 208/2 206/3 205/4
f 211/1 212/2 210/3 209/4
f 215/1 216/2 214/3 213/4
f 219/1 220/2 218/3 217/4
f 223/1 224/2 222/3 221/4
f 1 4
f 5 8
f 9 12
f 13 16
f 180 177
f 184 181
f 17 20
f 21 24
f 25 28
f 29 32
f 33 36
f 37 40
f 41 44
f 45 48
f 49 52
f 53 56
f 57 60
f 61 64
f 65 68
f 69 72
f 196 193
f 200 197
f 73 76
f 77 80
f 81 84
f 85 88
f 89 92
f 93 96
f 97 100
f 101 104
f 105 108
f 109 112
f 204 201
f 208 205
f 188 185
f 192 189
f 120 117
f 144 141
f 152 149
f 140 137
f 164 161
f 168 165
f 136 133
f 116 113
f 148 145
f 212 209
f 216 213
f 132 129
f 124 121
f 172 169
f 176 173
f 220 217
f 224 221
f 160 157
f 128 125
f 156 153

1099
res/lowPolyTree.obj Normal file

File diff suppressed because it is too large Load Diff

BIN
res/lowPolyTree.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 296 B

View File

@ -0,0 +1,40 @@
package objConverter;
public class ModelData {
private float[] vertices;
private float[] textureCoords;
private float[] normals;
private int[] indices;
private float furthestPoint;
public ModelData(float[] vertices, float[] textureCoords, float[] normals, int[] indices,
float furthestPoint) {
this.vertices = vertices;
this.textureCoords = textureCoords;
this.normals = normals;
this.indices = indices;
this.furthestPoint = furthestPoint;
}
public float[] getVertices() {
return vertices;
}
public float[] getTextureCoords() {
return textureCoords;
}
public float[] getNormals() {
return normals;
}
public int[] getIndices() {
return indices;
}
public float getFurthestPoint() {
return furthestPoint;
}
}

View File

@ -0,0 +1,161 @@
package objConverter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.vecmath.Vector2f;
import javax.vecmath.Vector3f;
public class OBJFileLoader {
private static final String RES_LOC = "res/";
public static ModelData loadOBJ(String objFileName) {
FileReader isr = null;
File objFile = new File(RES_LOC + objFileName + ".obj");
try {
isr = new FileReader(objFile);
} catch (FileNotFoundException e) {
System.err.println("File not found in res; don't use any extention");
}
BufferedReader reader = new BufferedReader(isr);
String line;
List<Vertex> vertices = new ArrayList<Vertex>();
List<Vector2f> textures = new ArrayList<Vector2f>();
List<Vector3f> normals = new ArrayList<Vector3f>();
List<Integer> indices = new ArrayList<Integer>();
try {
while (true) {
line = reader.readLine();
if (line.startsWith("v ")) {
String[] currentLine = line.split(" ");
Vector3f vertex = new Vector3f((float) Float.valueOf(currentLine[1]),
(float) Float.valueOf(currentLine[2]),
(float) Float.valueOf(currentLine[3]));
Vertex newVertex = new Vertex(vertices.size(), vertex);
vertices.add(newVertex);
} else if (line.startsWith("vt ")) {
String[] currentLine = line.split(" ");
Vector2f texture = new Vector2f((float) Float.valueOf(currentLine[1]),
(float) Float.valueOf(currentLine[2]));
textures.add(texture);
} else if (line.startsWith("vn ")) {
String[] currentLine = line.split(" ");
Vector3f normal = new Vector3f((float) Float.valueOf(currentLine[1]),
(float) Float.valueOf(currentLine[2]),
(float) Float.valueOf(currentLine[3]));
normals.add(normal);
} else if (line.startsWith("f ")) {
break;
}
}
while (line != null && line.startsWith("f ")) {
String[] currentLine = line.split(" ");
String[] vertex1 = currentLine[1].split("/");
String[] vertex2 = currentLine[2].split("/");
String[] vertex3 = currentLine[3].split("/");
processVertex(vertex1, vertices, indices);
processVertex(vertex2, vertices, indices);
processVertex(vertex3, vertices, indices);
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
System.err.println("Error reading the file");
}
removeUnusedVertices(vertices);
float[] verticesArray = new float[vertices.size() * 3];
float[] texturesArray = new float[vertices.size() * 2];
float[] normalsArray = new float[vertices.size() * 3];
float furthest = convertDataToArrays(vertices, textures, normals, verticesArray,
texturesArray, normalsArray);
int[] indicesArray = convertIndicesListToArray(indices);
ModelData data = new ModelData(verticesArray, texturesArray, normalsArray, indicesArray,
furthest);
return data;
}
private static void processVertex(String[] vertex, List<Vertex> vertices, List<Integer> indices) {
int index = Integer.parseInt(vertex[0]) - 1;
Vertex currentVertex = vertices.get(index);
int textureIndex = Integer.parseInt(vertex[1]) - 1;
int normalIndex = Integer.parseInt(vertex[2]) - 1;
if (!currentVertex.isSet()) {
currentVertex.setTextureIndex(textureIndex);
currentVertex.setNormalIndex(normalIndex);
indices.add(index);
} else {
dealWithAlreadyProcessedVertex(currentVertex, textureIndex, normalIndex, indices,
vertices);
}
}
private static int[] convertIndicesListToArray(List<Integer> indices) {
int[] indicesArray = new int[indices.size()];
for (int i = 0; i < indicesArray.length; i++) {
indicesArray[i] = indices.get(i);
}
return indicesArray;
}
private static float convertDataToArrays(List<Vertex> vertices, List<Vector2f> textures,
List<Vector3f> normals, float[] verticesArray, float[] texturesArray,
float[] normalsArray) {
float furthestPoint = 0;
for (int i = 0; i < vertices.size(); i++) {
Vertex currentVertex = vertices.get(i);
if (currentVertex.getLength() > furthestPoint) {
furthestPoint = currentVertex.getLength();
}
Vector3f position = currentVertex.getPosition();
Vector2f textureCoord = textures.get(currentVertex.getTextureIndex());
Vector3f normalVector = normals.get(currentVertex.getNormalIndex());
verticesArray[i * 3] = position.x;
verticesArray[i * 3 + 1] = position.y;
verticesArray[i * 3 + 2] = position.z;
texturesArray[i * 2] = textureCoord.x;
texturesArray[i * 2 + 1] = 1 - textureCoord.y;
normalsArray[i * 3] = normalVector.x;
normalsArray[i * 3 + 1] = normalVector.y;
normalsArray[i * 3 + 2] = normalVector.z;
}
return furthestPoint;
}
private static void dealWithAlreadyProcessedVertex(Vertex previousVertex, int newTextureIndex,
int newNormalIndex, List<Integer> indices, List<Vertex> vertices) {
if (previousVertex.hasSameTextureAndNormal(newTextureIndex, newNormalIndex)) {
indices.add(previousVertex.getIndex());
} else {
Vertex anotherVertex = previousVertex.getDuplicateVertex();
if (anotherVertex != null) {
dealWithAlreadyProcessedVertex(anotherVertex, newTextureIndex, newNormalIndex,
indices, vertices);
} else {
Vertex duplicateVertex = new Vertex(vertices.size(), previousVertex.getPosition());
duplicateVertex.setTextureIndex(newTextureIndex);
duplicateVertex.setNormalIndex(newNormalIndex);
previousVertex.setDuplicateVertex(duplicateVertex);
vertices.add(duplicateVertex);
indices.add(duplicateVertex.getIndex());
}
}
}
private static void removeUnusedVertices(List<Vertex> vertices){
for(Vertex vertex:vertices){
if(!vertex.isSet()){
vertex.setTextureIndex(0);
vertex.setNormalIndex(0);
}
}
}
}

View File

@ -0,0 +1,66 @@
package objConverter;
import javax.vecmath.Vector3f;
public class Vertex {
private static final int NO_INDEX = -1;
private Vector3f position;
private int textureIndex = NO_INDEX;
private int normalIndex = NO_INDEX;
private Vertex duplicateVertex = null;
private int index;
private float length;
public Vertex(int index,Vector3f position){
this.index = index;
this.position = position;
this.length = position.length();
}
public int getIndex(){
return index;
}
public float getLength(){
return length;
}
public boolean isSet(){
return textureIndex!=NO_INDEX && normalIndex!=NO_INDEX;
}
public boolean hasSameTextureAndNormal(int textureIndexOther,int normalIndexOther){
return textureIndexOther==textureIndex && normalIndexOther==normalIndex;
}
public void setTextureIndex(int textureIndex){
this.textureIndex = textureIndex;
}
public void setNormalIndex(int normalIndex){
this.normalIndex = normalIndex;
}
public Vector3f getPosition() {
return position;
}
public int getTextureIndex() {
return textureIndex;
}
public int getNormalIndex() {
return normalIndex;
}
public Vertex getDuplicateVertex() {
return duplicateVertex;
}
public void setDuplicateVertex(Vertex duplicateVertex) {
this.duplicateVertex = duplicateVertex;
}
}

View File

@ -7,6 +7,7 @@ import java.util.Map;
import javax.vecmath.Matrix4f;
import javax.vecmath.Vector2f;
import javax.vecmath.Vector3f;
import org.lwjgl.opengl.GL11;
@ -22,6 +23,7 @@ public class MasterRenderer {
private static final float FOV = 70;
private static final float NEAR_PLANE = 0.1f;
private static final float FAR_PLANE = 10000;
private static final Vector3f SKY_COLOUR = new Vector3f(0.5f, 0.5f, 0.5f);
private Matrix4f projectionMatrix;
@ -55,15 +57,17 @@ public class MasterRenderer {
public void render(Light sun, Camera camera) {
prepare();
shader.start();
shader.loadSkyColour(SKY_COLOUR);
shader.loadLight(sun);
shader.loadViewMatrix(camera);
renderer.render(entities);
shader.stop();
entities.clear();
terrainShader.start();
terrainShader.loadSkyColour(SKY_COLOUR);
terrainShader.loadLight(sun);
terrainShader.loadViewMatrix(camera);
terrainRenderer.render(terrains);
terrainRenderer.render(terrains);
terrainShader.stop();
terrains.clear();
}
@ -86,14 +90,16 @@ public class MasterRenderer {
public void prepare() {
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glClearColor(1, 0, 0, 1);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
GL11.glClearColor(SKY_COLOUR.x, SKY_COLOUR.y, SKY_COLOUR.z, 1.0f);
}
public void cleanUp () {
shader.cleanUp();
terrainShader.cleanUp();
}
private void createProjectionMatrix() {
//float aspectRatio = (float) Display.getWidth() / (float) Display.getHeight();
Vector2f windowsSize = DisplayManager.getSize();

View File

@ -1,101 +1,12 @@
package renderEngine;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import javax.vecmath.Vector2f;
import javax.vecmath.Vector3f;
import models.RawModel;
import objConverter.ModelData;
import objConverter.OBJFileLoader;
public class OBJLoader {
public static RawModel loadObjModel(String fileName, Loader loader) {
FileReader fr = null;
try {
fr = new FileReader(new File("res/" + fileName + ".obj"));
} catch (FileNotFoundException e) {
System.out.println("Couldn't load file! " + fileName);
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(fr);
String line;
List<Vector3f> vertices = new ArrayList<Vector3f>();
List<Vector2f> textures = new ArrayList<Vector2f>();
List<Vector3f> normals = new ArrayList<Vector3f>();
List<Integer> indices = new ArrayList<Integer>();
float[] verticesArray = null;
float[] normalsArray = null;
float[] texturesArray = 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 ")) {
texturesArray = 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, texturesArray, normalsArray);
processVertex(vertex2, indices, textures, normals, texturesArray, normalsArray);
processVertex(vertex3, indices, textures, normals, texturesArray, normalsArray);
line = reader.readLine();
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
verticesArray = new float[vertices.size()*3];
indicesArray = new int[indices.size()*3];
int vertexPointer = 0;
for (Vector3f vertex: vertices) {
verticesArray[vertexPointer++] = vertex.x;
verticesArray[vertexPointer++] = vertex.y;
verticesArray[vertexPointer++] = vertex.z;
}
for (int iii=0; iii<indices.size(); iii++) {
indicesArray[iii] = indices.get(iii);
}
return loader.loadToVAO(verticesArray, texturesArray, normalsArray, 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] = 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;
ModelData data = OBJFileLoader.loadOBJ("tree");
return loader.loadToVAO(data.getVertices(), data.getTextureCoords(), data.getNormals(), data.getIndices());
}
}

View File

@ -1,6 +1,7 @@
package shaders;
import javax.vecmath.Matrix4f;
import javax.vecmath.Vector3f;
import entities.Camera;
import entities.Light;
@ -18,6 +19,7 @@ public class StaticShader extends ShaderProgram {
private int location_reflectivity;
private int location_shineDamper;
private int location_useFakeLighting;
private int location_skyColor;
public StaticShader() {
super(VERTEX_FILE, FRAGMENT_FILE);
@ -40,6 +42,11 @@ public class StaticShader extends ShaderProgram {
location_reflectivity = super.getUniformLocation("reflectivity");
location_shineDamper = super.getUniformLocation("shineDamper");
location_useFakeLighting = super.getUniformLocation("useFakeLighting");
location_skyColor = super.getUniformLocation("skyColor");
}
public void loadSkyColour(Vector3f color) {
super.loadVector(location_skyColor, color);
}
public void loadFakeLightingVariable(boolean useFake) {

View File

@ -1,6 +1,7 @@
package shaders;
import javax.vecmath.Matrix4f;
import javax.vecmath.Vector3f;
import entities.Camera;
import entities.Light;
@ -17,6 +18,7 @@ public class TerrainShader extends ShaderProgram {
private int location_lightColour;
private int location_reflectivity;
private int location_shineDamper;
private int location_skyColor;
public TerrainShader() {
super(VERTEX_FILE, FRAGMENT_FILE);
@ -38,7 +40,13 @@ public class TerrainShader extends ShaderProgram {
location_lightColour = super.getUniformLocation("lightColour");
location_reflectivity = super.getUniformLocation("reflectivity");
location_shineDamper = super.getUniformLocation("shineDamper");
location_skyColor = super.getUniformLocation("skyColor");
}
public void loadSkyColour(Vector3f color) {
super.loadVector(location_skyColor, color);
}
public void loadShineVariable(float shineDamper, float reflectivity) {
super.loadFloat(location_reflectivity, reflectivity);
super.loadFloat(location_shineDamper, shineDamper);
@ -47,13 +55,16 @@ public class TerrainShader extends ShaderProgram {
public void loadTransformationMatrix(Matrix4f matrix) {
super.loadMatrix(location_transformationMatrix, matrix);
}
public void loadLight(Light light) {
super.loadVector(location_lightPosition, light.getPosition());
super.loadVector(location_lightColour, light.getColour());
}
public void loadProjectionMatrix(Matrix4f projection) {
super.loadMatrix(location_projectionMatrix, projection);
}
public void loadViewMatrix(Camera camera) {
super.loadMatrix(location_viewMatrix, Maths.createViewMatrix(camera));
}

View File

@ -4,6 +4,8 @@ in vec2 pass_textureCoordinates;
in vec3 surfaceNormal;
in vec3 toLightVector;
in vec3 toCameraVector;
// FOW: Fog Of War result calculation
in float visibility;
out vec4 out_Color;
@ -11,6 +13,7 @@ uniform sampler2D textureSampler;
uniform vec3 lightColour;
uniform float reflectivity;
uniform float shineDamper;
uniform vec3 skyColor;
void main(void) {
@ -40,5 +43,6 @@ void main(void) {
}
out_Color = vec4(diffuse,1.0) * textureColour + vec4(finalSpecular, 1.0);
out_Color = mix(vec4(skyColor,1.0), out_Color, visibility);
}

View File

@ -4,6 +4,8 @@ in vec2 pass_textureCoordinates;
in vec3 surfaceNormal;
in vec3 toLightVector;
in vec3 toCameraVector;
// FOW: Fog Of War result calculation
in float visibility;
out vec4 out_Color;
@ -11,6 +13,7 @@ uniform sampler2D textureSampler;
uniform vec3 lightColour;
uniform float reflectivity;
uniform float shineDamper;
uniform vec3 skyColor;
void main(void) {
@ -33,5 +36,6 @@ void main(void) {
vec3 finalSpecular = damperFactor * reflectivity * lightColour;
out_Color = vec4(diffuse,1.0) * texture(textureSampler,pass_textureCoordinates) + vec4(finalSpecular, 1.0);
out_Color = mix(vec4(skyColor,1.0), out_Color, visibility);
}

View File

@ -8,19 +8,30 @@ out vec2 pass_textureCoordinates;
out vec3 surfaceNormal;
out vec3 toLightVector;
out vec3 toCameraVector;
// FOW: Fog Of War result calculation
out float visibility;
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform vec3 lightPosition;
const float density = 0.007;
const float gradient = 1.5;
void main(void) {
vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
gl_Position = projectionMatrix * viewMatrix * worldPosition;
vec4 positionRelativeToCam = viewMatrix * worldPosition;
gl_Position = projectionMatrix * positionRelativeToCam;
pass_textureCoordinates = textureCoords;
surfaceNormal = (transformationMatrix * vec4(normal, 0.0)).xyz;
toLightVector = lightPosition - worldPosition.xyz;
toCameraVector = (inverse(viewMatrix) * vec4(0.0,0.0,0.0,1.0)).xyz - worldPosition.xyz;
float distance = length(positionRelativeToCam.xyz);
visibility = exp(-pow((distance*density),gradient));
visibility = clamp(visibility, 0.0, 1.0);
}

View File

@ -8,6 +8,8 @@ out vec2 pass_textureCoordinates;
out vec3 surfaceNormal;
out vec3 toLightVector;
out vec3 toCameraVector;
// FOW: Fog Of War result calculation
out float visibility;
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
@ -16,9 +18,14 @@ uniform vec3 lightPosition;
uniform float useFakeLighting;
const float density = 0.007;
const float gradient = 1.5;
void main(void) {
vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
gl_Position = projectionMatrix * viewMatrix * worldPosition;
vec4 positionRelativeToCam = viewMatrix * worldPosition;
gl_Position = projectionMatrix * positionRelativeToCam;
pass_textureCoordinates = textureCoords;
vec3 actualNormal = normal;
@ -28,5 +35,9 @@ void main(void) {
surfaceNormal = (transformationMatrix * vec4(actualNormal, 0.0)).xyz;
toLightVector = lightPosition - worldPosition.xyz;
toCameraVector = (inverse(viewMatrix) * vec4(0.0,0.0,0.0,1.0)).xyz - worldPosition.xyz;
float distance = length(positionRelativeToCam.xyz);
visibility = exp(-pow((distance*density),gradient));
visibility = clamp(visibility, 0.0, 1.0);
}