mirror of
https://github.com/GTNewHorizons/EnderIO.git
synced 2026-11-21 20:52:42 +00:00
Add in base work for rendering techne models. Fix potential obelisk bug
Thanks @RainWarrior :D
This commit is contained in:
1 parent
22e29a6d07
commit
fbeb3822fd
2 files changed
+205
-1
No files matched your search
@@ -0,0 +1,204 @@
|
||||
/*
|
||||
|
||||
Copyright © 2014 RainWarrior
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
3. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
|
||||
4. This notice may not be removed or altered from any source
|
||||
distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
*/
|
||||
|
||||
package crazypants.render;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.client.model.ModelBox;
|
||||
import net.minecraft.client.model.ModelRenderer;
|
||||
import net.minecraft.client.model.PositionTextureVertex;
|
||||
import net.minecraft.client.model.TexturedQuad;
|
||||
import net.minecraft.client.renderer.RenderHelper;
|
||||
import net.minecraft.client.renderer.Tessellator;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraftforge.client.model.obj.Face;
|
||||
import net.minecraftforge.client.model.obj.GroupObject;
|
||||
import net.minecraftforge.client.model.obj.TextureCoordinate;
|
||||
import net.minecraftforge.client.model.obj.Vertex;
|
||||
import net.minecraftforge.client.model.techne.TechneModel;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.util.vector.Matrix4f;
|
||||
import org.lwjgl.util.vector.Vector3f;
|
||||
import org.lwjgl.util.vector.Vector4f;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import cpw.mods.fml.common.ObfuscationReflectionHelper;
|
||||
|
||||
/**
|
||||
* Slightly modified to fit the EnderIO source.
|
||||
*
|
||||
* @author RainWarrior
|
||||
*/
|
||||
public class TechneUtil {
|
||||
|
||||
private static final Tessellator tes = Tessellator.instance;
|
||||
|
||||
public static List<GroupObject> bakeModel(ModelRenderer model) {
|
||||
return bakeModel(model, 1);
|
||||
}
|
||||
|
||||
public static List<GroupObject> bakeModel(ModelRenderer model, float scale) {
|
||||
return bakeModel(model, scale, new Matrix4f());
|
||||
}
|
||||
|
||||
public static List<GroupObject> bakeModel(ModelRenderer model, float scale, Matrix4f matrix) {
|
||||
return bakeModel(model, scale, matrix, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert ModelRenderer to a list of GroupObjects, for ease of use in ISBRH
|
||||
* and other static contexts.
|
||||
*
|
||||
* @param scale
|
||||
* the scale factor, usually last argument to rendering methods
|
||||
* @param matrix
|
||||
* initial transformation matrix (replaces calling
|
||||
* glTranslate/glRotate/e.t.c. before rendering)
|
||||
* @param rotateYFirst
|
||||
* true, of the order of rotations be like in
|
||||
* ModelRenderer.renderWithRotation, false if like in
|
||||
* ModelRenderer.render
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<GroupObject> bakeModel(ModelRenderer model, float scale, Matrix4f matrix, boolean rotateYFirst) {
|
||||
Matrix4f m = new Matrix4f(matrix);
|
||||
|
||||
m.translate(new Vector3f(model.offsetX + model.rotationPointX * scale, model.offsetY + model.rotationPointY * scale, model.offsetZ + model.rotationPointZ
|
||||
* scale));
|
||||
|
||||
if(!rotateYFirst) {
|
||||
m.rotate(model.rotateAngleZ, new Vector3f(0, 0, 1));
|
||||
}
|
||||
m.rotate(model.rotateAngleY, new Vector3f(0, 1, 0));
|
||||
m.rotate(model.rotateAngleX, new Vector3f(1, 0, 0));
|
||||
|
||||
if(rotateYFirst) {
|
||||
m.rotate(model.rotateAngleZ, new Vector3f(0, 0, 1));
|
||||
}
|
||||
|
||||
Vector4f vec = new Vector4f();
|
||||
List<GroupObject> res = new ArrayList<GroupObject>();
|
||||
for (ModelBox box : (List<ModelBox>) model.cubeList) {
|
||||
GroupObject obj = new GroupObject("", GL11.GL_QUADS);
|
||||
TexturedQuad[] quads = (TexturedQuad[]) ObfuscationReflectionHelper.getPrivateValue(ModelBox.class, box, "quadList", "field_78254_i");
|
||||
for (int i = 0; i < quads.length; i++) {
|
||||
Face face = new Face();
|
||||
face.vertices = new Vertex[4];
|
||||
face.textureCoordinates = new TextureCoordinate[4];
|
||||
for (int j = 0; j < 4; j++) {
|
||||
PositionTextureVertex pv = quads[i].vertexPositions[j];
|
||||
|
||||
vec.x = (float) pv.vector3D.xCoord * scale;
|
||||
vec.y = (float) pv.vector3D.yCoord * scale;
|
||||
vec.z = (float) pv.vector3D.zCoord * scale;
|
||||
vec.w = 1;
|
||||
|
||||
Matrix4f.transform(m, vec, vec);
|
||||
|
||||
face.vertices[j] = new Vertex(vec.x / vec.w, vec.y / vec.w, vec.z / vec.w);
|
||||
|
||||
face.textureCoordinates[j] = new TextureCoordinate(pv.texturePositionX, pv.texturePositionY);
|
||||
}
|
||||
obj.faces.add(face);
|
||||
}
|
||||
res.add(obj);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static List<GroupObject> bakeModel(TechneModel model) {
|
||||
return bakeModel(model, 1);
|
||||
}
|
||||
|
||||
public static List<GroupObject> bakeModel(TechneModel model, float scale) {
|
||||
return bakeModel(model, scale, new Matrix4f());
|
||||
}
|
||||
|
||||
public static List<GroupObject> bakeModel(TechneModel model, float scale, Matrix4f m) {
|
||||
return bakeModel(model, scale, m, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this to convert TechneModel to it's static representation
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<GroupObject> bakeModel(TechneModel model, float scale, Matrix4f m, boolean rotateYFirst) {
|
||||
Map<String, ModelRenderer> parts = (Map<String, ModelRenderer>) ObfuscationReflectionHelper.getPrivateValue(TechneModel.class, model, "parts");
|
||||
List<GroupObject> res = Lists.newArrayList();
|
||||
for (Map.Entry<String, ModelRenderer> e : parts.entrySet()) {
|
||||
GroupObject obj = bakeModel(e.getValue(), scale, m, rotateYFirst).get(0);
|
||||
res.add(obj);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void renderWithIcon(List<GroupObject> model, IIcon icon, Tessellator tes) {
|
||||
for (GroupObject go : model) {
|
||||
for (Face f : go.faces) {
|
||||
Vertex n = f.faceNormal;
|
||||
tes.setNormal(n.x, n.y, n.z);
|
||||
for (int i = 0; i < f.vertices.length; i++) {
|
||||
Vertex v = f.vertices[i];
|
||||
TextureCoordinate t = f.textureCoordinates[i];
|
||||
tes.addVertexWithUV(v.x, v.y, v.z, icon.getInterpolatedU(t.u * 16), icon.getInterpolatedV(t.v * 16));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void renderInventoryBlock(List<GroupObject> model, Block block, int metadata) {
|
||||
RenderHelper.disableStandardItemLighting();
|
||||
tes.startDrawingQuads();
|
||||
tes.setColorOpaque_F(1, 1, 1);
|
||||
renderWithIcon(model, block.getIcon(0, metadata), tes);
|
||||
tes.draw();
|
||||
RenderHelper.enableStandardItemLighting();
|
||||
}
|
||||
|
||||
public static boolean renderWorldBlock(List<GroupObject> model, IBlockAccess world, int x, int y, int z, Block block) {
|
||||
tes.setBrightness(block.getMixedBrightnessForBlock(world, x, y, z));
|
||||
tes.setColorOpaque_F(1, 1, 1);
|
||||
tes.addTranslation(x + .5F, y + .5F, z + .5F);
|
||||
renderWithIcon(model, block.getIcon(0, world.getBlockMetadata(x, y, z)), tes);
|
||||
tes.addTranslation(-x - .5F, -y - .5F, -z - .5F);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user