Skip to content

Commit

Permalink
Add K4 to stock and fix lightness/darkness
Browse files Browse the repository at this point in the history
  • Loading branch information
cam72cam committed Sep 15, 2017
1 parent da588e7 commit 44f178a
Show file tree
Hide file tree
Showing 20 changed files with 130,173 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ public EntityRollingStockDefinition(String defID, JsonObject data) throws Except
name = data.get("name").getAsString();
// model = (OBJModel) OBJLoader.INSTANCE.loadModel(new
// ResourceLocation(data.get("model").getAsString()));
model = new OBJModel(new ResourceLocation(data.get("model").getAsString()));
float darken = 0;
if (data.has("darken_model")) {
darken = data.get("darken_model").getAsFloat();
System.out.println("DARKEN!!!!!" + darken);
}
model = new OBJModel(new ResourceLocation(data.get("model").getAsString()), darken);
JsonObject passenger = data.get("passenger").getAsJsonObject();
passengerCenter = new Vec3d(passenger.get("center_x").getAsDouble(), passenger.get("center_y").getAsDouble(), 0);
passengerCompartmentLength = passenger.get("length").getAsDouble();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

public class Face {
public int[][] points;
public String mtl;

public Face(String[] args) {
public Face(String[] args, String mtl) {
this.mtl = mtl;
points = new int[args.length][];
for(int i = 0; i < args.length; i++) {
points[i] = parsePoint(args[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ public class OBJModel {
List<Vec3d> vertexNormals = new ArrayList<Vec3d>();
List<Vec2f> vertexTextures = new ArrayList<Vec2f>();

Map<String, String> groupMtlMap = new HashMap<String, String>();
Map<String, Material> materials = new HashMap<String, Material>();
public float darken;

public OBJModel(ResourceLocation modelLoc) throws Exception {
public OBJModel(ResourceLocation modelLoc, float darken) throws Exception {
InputStream input = ImmersiveRailroading.proxy.getResourceStream(modelLoc);
Scanner reader = new Scanner(input);
this.darken = darken;

String currentGroupName = "defaultName";
List<Face> currentGroup = new ArrayList<Face>();
Expand All @@ -54,14 +55,12 @@ public OBJModel(ResourceLocation modelLoc) throws Exception {
break;
case "usemtl":
currentMaterial = args[0];
groupMtlMap.put(currentGroupName, currentMaterial);
break;
case "o":
case "g":
currentGroupName = args[0];
currentGroup = new ArrayList<Face>();
groups.put(currentGroupName, currentGroup);
groupMtlMap.put(currentGroupName, currentMaterial);
break;
case "v":
vertices.add(new Vec3d(Float.parseFloat(args[0]), Float.parseFloat(args[1]), Float.parseFloat(args[2])));
Expand All @@ -73,7 +72,7 @@ public OBJModel(ResourceLocation modelLoc) throws Exception {
vertexTextures.add(new Vec2f(Float.parseFloat(args[0]), Float.parseFloat(args[1])));
break;
case "f":
currentGroup.add(new Face(args));
currentGroup.add(new Face(args, currentMaterial));
break;
case "s":
//Ignore
Expand Down
130 changes: 61 additions & 69 deletions src/main/java/cam72cam/immersiverailroading/render/obj/OBJRender.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,12 @@ public void drawDirectGroups(Iterable<String> groupNames) {
}

public void drawDirectGroups(Iterable<String> groupNames, double scale) {
Map<Integer, Map<Material, List<Face>>> stuff = new HashMap<Integer, Map<Material, List<Face>>>();
Map<Integer, List<Face>> stuff = new HashMap<Integer, List<Face>>();

int triFaceCount = 0;
int quadFaceCount = 0;

for (String group : groupNames) {
Material currentMTL = model.materials.get(model.groupMtlMap.get(group));
List<Face> faces = model.groups.get(group);

for (Face face : faces) {
Expand All @@ -85,14 +84,10 @@ public void drawDirectGroups(Iterable<String> groupNames, double scale) {
}

if (!stuff.containsKey(type)) {
stuff.put(type, new HashMap<Material, List<Face>>());
stuff.put(type, new ArrayList<Face>());
}

if (!stuff.get(type).containsKey(currentMTL)) {
stuff.get(type).put(currentMTL, new ArrayList<Face>());
}

stuff.get(type).get(currentMTL).add(face);
stuff.get(type).add(face);
}
}

Expand All @@ -104,9 +99,8 @@ public void drawDirectGroups(Iterable<String> groupNames, double scale) {
FloatBuffer quadColorBuffer = BufferUtils.createFloatBuffer(quadFaceCount * 4 * 4);

for (Integer type : stuff.keySet()) {
Map<Material, List<Face>> stuffType = stuff.get(type);
for (Material currentMTL : stuffType.keySet()) {
List<Face> faces = stuffType.get(currentMTL);
for (Face face : stuff.get(type)) {
Material currentMTL = model.materials.get(face.mtl);
if (currentMTL.Ka != null) {
GL11.glMaterial(GL11.GL_FRONT_AND_BACK, GL11.GL_AMBIENT, currentMTL.Ka);
}
Expand All @@ -116,72 +110,70 @@ public void drawDirectGroups(Iterable<String> groupNames, double scale) {
float a = 0;
if (currentMTL.Kd != null) {
GL11.glMaterial(GL11.GL_FRONT_AND_BACK, GL11.GL_DIFFUSE, currentMTL.Kd);
float darken = 0.06f;
r = Math.max(0, currentMTL.Kd.get(0) - darken);
g = Math.max(0, currentMTL.Kd.get(1) - darken);
b = Math.max(0, currentMTL.Kd.get(2) - darken);
r = Math.max(0, currentMTL.Kd.get(0) - model.darken);
g = Math.max(0, currentMTL.Kd.get(1) - model.darken);
b = Math.max(0, currentMTL.Kd.get(2) - model.darken);
a = currentMTL.Kd.get(3);
GL11.glColor4f(r, g, b, currentMTL.Kd.get(3));
}
if (currentMTL.Ks != null) {
GL11.glMaterial(GL11.GL_FRONT_AND_BACK, GL11.GL_SPECULAR, currentMTL.Ks);
}
for (Face face : faces) {
if (type == GL11.GL_POLYGON) {
GL11.glBegin(type);
GL11.glColor4f(r, g, b, currentMTL.Kd.get(3));
for (int[] point : face.points) {
Vec3d v;
Vec2f vt;
Vec3d vn;

switch (point.length) {
case 3:
vn = model.vertexNormals.get(point[2]);
GL11.glNormal3d(vn.x, vn.y, vn.z);
case 2:
if (point[1] != -1) {
vt = model.vertexTextures.get(point[1]);
GL11.glTexCoord2f(vt.x, 1 - vt.y);
}
case 1:
v = model.vertices.get(point[0]);
GL11.glVertex3d(v.x * scale, v.y * scale, v.z * scale);
break;
default:
break;
if (type == GL11.GL_POLYGON) {
GL11.glBegin(type);
//GL11.glColor4f(r, g, b, currentMTL.Kd.get(3));
for (int[] point : face.points) {
Vec3d v;
//Vec2f vt;
//Vec3d vn;

switch (point.length) {
case 3:
//vn = model.vertexNormals.get(point[2]);
//GL11.glNormal3d(vn.x, vn.y, vn.z);
//System.out.println(String.format("%s %s %s", vn.x, vn.y, vn.z));
case 2:
if (point[1] != -1) {
//vt = model.vertexTextures.get(point[1]);
//GL11.glTexCoord2f(vt.x, 1 - vt.y);
}
case 1:
v = model.vertices.get(point[0]);
GL11.glVertex3d(v.x * scale, v.y * scale, v.z * scale);
break;
default:
break;
}
GL11.glEnd();
} else {
for (int[] point : face.points) {
if (type == GL11.GL_QUADS) {
Vec3d v = model.vertices.get(point[0]);
Vec3d vn = model.vertexNormals.get(point[2]);
quadBuffer.put((float) (v.x * scale));
quadBuffer.put((float) (v.y * scale));
quadBuffer.put((float) (v.z * scale));
quadNormalBuffer.put((float) (vn.x));
quadNormalBuffer.put((float) (vn.y));
quadNormalBuffer.put((float) (vn.z));
quadColorBuffer.put(r);
quadColorBuffer.put(g);
quadColorBuffer.put(b);
quadColorBuffer.put(a);
} else {
Vec3d v = model.vertices.get(point[0]);
Vec3d vn = model.vertexNormals.get(point[2]);
triBuffer.put((float) (v.x * scale));
triBuffer.put((float) (v.y * scale));
triBuffer.put((float) (v.z * scale));
triNormalBuffer.put((float) (vn.x));
triNormalBuffer.put((float) (vn.y));
triNormalBuffer.put((float) (vn.z));
triColorBuffer.put(r);
triColorBuffer.put(g);
triColorBuffer.put(b);
triColorBuffer.put(a);
}
}
GL11.glEnd();
} else {
for (int[] point : face.points) {
if (type == GL11.GL_QUADS) {
Vec3d v = model.vertices.get(point[0]);
Vec3d vn = model.vertexNormals.get(point[2]);
quadBuffer.put((float) (v.x * scale));
quadBuffer.put((float) (v.y * scale));
quadBuffer.put((float) (v.z * scale));
quadNormalBuffer.put((float) (vn.x));
quadNormalBuffer.put((float) (vn.y));
quadNormalBuffer.put((float) (vn.z));
quadColorBuffer.put(r);
quadColorBuffer.put(g);
quadColorBuffer.put(b);
quadColorBuffer.put(a);
} else {
Vec3d v = model.vertices.get(point[0]);
Vec3d vn = model.vertexNormals.get(point[2]);
triBuffer.put((float) (v.x * scale));
triBuffer.put((float) (v.y * scale));
triBuffer.put((float) (v.z * scale));
triNormalBuffer.put((float) (vn.x));
triNormalBuffer.put((float) (vn.y));
triNormalBuffer.put((float) (vn.z));
triColorBuffer.put(r);
triColorBuffer.put(g);
triColorBuffer.put(b);
triColorBuffer.put(a);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class RailBuilderRender {

static {
try {
baseRailModel = new OBJRender(new OBJModel(new ResourceLocation(ImmersiveRailroading.MODID, "models/block/track_1m.obj")));
baseRailModel = new OBJRender(new OBJModel(new ResourceLocation(ImmersiveRailroading.MODID, "models/block/track_1m.obj"), 0.05f));
} catch (Exception e) {
ImmersiveRailroading.logger.catching(e);
}
Expand Down
Loading

0 comments on commit 44f178a

Please sign in to comment.