From fbeb3822fdeda07ba458e2173a7659f6c647c0f4 Mon Sep 17 00:00:00 2001 From: tterrag1098 Date: Mon, 16 Mar 2015 22:25:57 -0400 Subject: [PATCH] Add in base work for rendering techne models. Fix potential obelisk bug Thanks @RainWarrior :D --- .../machine/attractor/ObeliskRenderer.java | 2 +- .../java/crazypants/render/TechneUtil.java | 204 ++++++++++++++++++ 2 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 src/main/java/crazypants/render/TechneUtil.java diff --git a/src/main/java/crazypants/enderio/machine/attractor/ObeliskRenderer.java b/src/main/java/crazypants/enderio/machine/attractor/ObeliskRenderer.java index 0856056f..cd842b83 100644 --- a/src/main/java/crazypants/enderio/machine/attractor/ObeliskRenderer.java +++ b/src/main/java/crazypants/enderio/machine/attractor/ObeliskRenderer.java @@ -113,7 +113,7 @@ public class ObeliskRenderer extends TileEntitySpecialRend } ei.setEntityItemStack(getFloatingItem(te)); - ei.hoverStart = world.getTotalWorldTime() * 0.05f + (tick * 0.05f); + ei.hoverStart = (float) ((world.getTotalWorldTime() * 0.05f + (tick * 0.05f)) % (Math.PI * 2)); ei.age = 0; glPushMatrix(); diff --git a/src/main/java/crazypants/render/TechneUtil.java b/src/main/java/crazypants/render/TechneUtil.java new file mode 100644 index 00000000..513f52a2 --- /dev/null +++ b/src/main/java/crazypants/render/TechneUtil.java @@ -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 bakeModel(ModelRenderer model) { + return bakeModel(model, 1); + } + + public static List bakeModel(ModelRenderer model, float scale) { + return bakeModel(model, scale, new Matrix4f()); + } + + public static List 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 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 res = new ArrayList(); + for (ModelBox box : (List) 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 bakeModel(TechneModel model) { + return bakeModel(model, 1); + } + + public static List bakeModel(TechneModel model, float scale) { + return bakeModel(model, scale, new Matrix4f()); + } + + public static List 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 bakeModel(TechneModel model, float scale, Matrix4f m, boolean rotateYFirst) { + Map parts = (Map) ObfuscationReflectionHelper.getPrivateValue(TechneModel.class, model, "parts"); + List res = Lists.newArrayList(); + for (Map.Entry e : parts.entrySet()) { + GroupObject obj = bakeModel(e.getValue(), scale, m, rotateYFirst).get(0); + res.add(obj); + } + return res; + } + + public static void renderWithIcon(List 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 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 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; + } +} \ No newline at end of file