[DEV] tutorial 10 'load .obj file' implementation
This commit is contained in:
parent
201eacbd69
commit
ff26151b39
BIN
res/stall.blend
Normal file
BIN
res/stall.blend
Normal file
Binary file not shown.
1696
res/stall.obj
Normal file
1696
res/stall.obj
Normal file
File diff suppressed because it is too large
Load Diff
BIN
res/stallTexture.png
Normal file
BIN
res/stallTexture.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 50 KiB |
@ -10,6 +10,7 @@ import models.TexturedModel;
|
||||
import renderEngine.DisplayManager;
|
||||
import renderEngine.DisplayManagerDraw;
|
||||
import renderEngine.Loader;
|
||||
import renderEngine.OBJLoader;
|
||||
import renderEngine.Renderer;
|
||||
import shaders.StaticShader;
|
||||
import textures.ModelTexture;
|
||||
@ -37,109 +38,15 @@ public class MainGameLoop {
|
||||
StaticShader shader = new StaticShader();
|
||||
Renderer renderer = new Renderer(shader);
|
||||
|
||||
// float[] vertices = {
|
||||
// -0.5f, 0.5f, 0f,//v0
|
||||
// -0.5f, -0.5f, 0f,//v1
|
||||
// 0.5f, -0.5f, 0f,//v2
|
||||
// 0.5f, 0.5f, 0f,//v3
|
||||
// };
|
||||
// float[] textureCoords = {
|
||||
// 0.0f, 0.0f,//v0
|
||||
// 0.0f, 1.0f,//v1
|
||||
// 1.0f, 1.0f,//v2
|
||||
// 1.0f, 0.0f,//v3
|
||||
// };
|
||||
//
|
||||
// int[] indices = {
|
||||
// 0,1,3,//top left triangle (v0, v1, v3)
|
||||
// 3,1,2//bottom right triangle (v3, v1, v2)
|
||||
// };
|
||||
//
|
||||
float[] vertices = {
|
||||
-0.5f,0.5f,-0.5f,
|
||||
-0.5f,-0.5f,-0.5f,
|
||||
0.5f,-0.5f,-0.5f,
|
||||
0.5f,0.5f,-0.5f,
|
||||
|
||||
-0.5f,0.5f,0.5f,
|
||||
-0.5f,-0.5f,0.5f,
|
||||
0.5f,-0.5f,0.5f,
|
||||
0.5f,0.5f,0.5f,
|
||||
|
||||
0.5f,0.5f,-0.5f,
|
||||
0.5f,-0.5f,-0.5f,
|
||||
0.5f,-0.5f,0.5f,
|
||||
0.5f,0.5f,0.5f,
|
||||
|
||||
-0.5f,0.5f,-0.5f,
|
||||
-0.5f,-0.5f,-0.5f,
|
||||
-0.5f,-0.5f,0.5f,
|
||||
-0.5f,0.5f,0.5f,
|
||||
|
||||
-0.5f,0.5f,0.5f,
|
||||
-0.5f,0.5f,-0.5f,
|
||||
0.5f,0.5f,-0.5f,
|
||||
0.5f,0.5f,0.5f,
|
||||
|
||||
-0.5f,-0.5f,0.5f,
|
||||
-0.5f,-0.5f,-0.5f,
|
||||
0.5f,-0.5f,-0.5f,
|
||||
0.5f,-0.5f,0.5f
|
||||
|
||||
};
|
||||
|
||||
float[] textureCoords = {
|
||||
|
||||
0,0,
|
||||
0,1,
|
||||
1,1,
|
||||
1,0,
|
||||
0,0,
|
||||
0,1,
|
||||
1,1,
|
||||
1,0,
|
||||
0,0,
|
||||
0,1,
|
||||
1,1,
|
||||
1,0,
|
||||
0,0,
|
||||
0,1,
|
||||
1,1,
|
||||
1,0,
|
||||
0,0,
|
||||
0,1,
|
||||
1,1,
|
||||
1,0,
|
||||
0,0,
|
||||
0,1,
|
||||
1,1,
|
||||
1,0
|
||||
|
||||
|
||||
};
|
||||
|
||||
int[] indices = {
|
||||
0,1,3,
|
||||
3,1,2,
|
||||
4,5,7,
|
||||
7,5,6,
|
||||
8,9,11,
|
||||
11,9,10,
|
||||
12,13,15,
|
||||
15,13,14,
|
||||
16,17,19,
|
||||
19,17,18,
|
||||
20,21,23,
|
||||
23,21,22
|
||||
|
||||
};
|
||||
|
||||
|
||||
RawModel model = loader.loadToVAO(vertices, textureCoords, indices);
|
||||
ModelTexture texture = new ModelTexture(loader.loadTexture("tree_sample"));
|
||||
RawModel model = OBJLoader.loadObjModel("stall", loader);
|
||||
|
||||
ModelTexture texture = new ModelTexture(loader.loadTexture("stallTexture"));
|
||||
|
||||
TexturedModel staticModel = new TexturedModel(model, texture);
|
||||
|
||||
Entity entity = new Entity(staticModel, new Vector3f(0,0,-2), new Vector3f(0,0,0), 1);
|
||||
Entity entity = new Entity(staticModel, new Vector3f(0,0,-20), new Vector3f(0,0,0), 1);
|
||||
|
||||
Camera camera = new Camera();
|
||||
|
||||
@ -148,7 +55,7 @@ public class MainGameLoop {
|
||||
public void draw() {
|
||||
//entity.increasePosition(0.0f, 0, -0.01f);
|
||||
//entity.increaseRotation(0, 0, 0.01f);
|
||||
entity.increaseRotation(0.01f, 0.01f, 0.0f);
|
||||
entity.increaseRotation(0.01f, 0.02f, 0.0f);
|
||||
camera.move();
|
||||
renderer.prepare();
|
||||
shader.start();
|
||||
|
101
src/renderEngine/OBJLoader.java
Normal file
101
src/renderEngine/OBJLoader.java
Normal file
@ -0,0 +1,101 @@
|
||||
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;
|
||||
|
||||
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, 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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user