Reduce number of startDrawing/draw calls to the tessellator (thanks for the tip), add some render methods to WavefrontObject, removed unnecessary commenting

This commit is contained in:
pahimar 2013-04-02 14:38:24 -04:00
parent 64f0663e88
commit bf09bdb8a6
8 changed files with 175 additions and 238 deletions

View file

@ -1,15 +1,12 @@
package com.pahimar.ee3.client.model;
import net.minecraft.client.model.ModelBase;
import net.minecraft.client.renderer.Tessellator;
import net.minecraftforge.client.model.obj.GroupObject;
import net.minecraftforge.client.model.obj.WavefrontObject;
import net.minecraftforge.common.ForgeDirection;
import org.lwjgl.opengl.GL11;
import com.pahimar.ee3.lib.Models;
import com.pahimar.ee3.lib.Reference;
import com.pahimar.ee3.lib.Textures;
import com.pahimar.ee3.tileentity.TileAludel;
@ -29,43 +26,37 @@ import cpw.mods.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class ModelAludel extends ModelBase {
private float scale;
private WavefrontObject modelAludelOBJ;
public ModelAludel() {
scale = 1F;
modelAludelOBJ = new WavefrontObject(Models.ALUDEL);
}
public ModelAludel(float scale) {
public void render() {
this.scale = scale;
modelAludelOBJ = new WavefrontObject(Models.ALUDEL);
}
public void render(Tessellator tessellator, float scale) {
if (modelAludelOBJ.groupObjects.size() != 0) {
for (GroupObject groupObject : modelAludelOBJ.groupObjects) {
groupObject.render(tessellator, Reference.MODEL_TEXTURE_OFFSET, scale);
}
}
modelAludelOBJ.renderAll();
}
public void render(TileAludel aludel, double x, double y, double z) {
GL11.glPushMatrix();
GL11.glDisable(GL11.GL_LIGHTING);
correctRotation(x, y, z, aludel.getOrientation());
// Scale, Translate, Rotate
scaleTranslateRotate(x, y, z, aludel.getOrientation());
// Bind texture
FMLClientHandler.instance().getClient().renderEngine.bindTexture(Textures.MODEL_ALUDEL);
this.render(Tessellator.instance, scale);
// Render
this.render();
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glPopMatrix();
}
private void correctRotation(double x, double y, double z, ForgeDirection orientation) {
private void scaleTranslateRotate(double x, double y, double z, ForgeDirection orientation) {
if (orientation == ForgeDirection.NORTH) {
GL11.glTranslated(x + 1, y, z);

View file

@ -1,14 +1,11 @@
package com.pahimar.ee3.client.model;
import net.minecraft.client.model.ModelBase;
import net.minecraft.client.renderer.Tessellator;
import net.minecraftforge.client.model.obj.GroupObject;
import net.minecraftforge.client.model.obj.WavefrontObject;
import org.lwjgl.opengl.GL11;
import com.pahimar.ee3.lib.Models;
import com.pahimar.ee3.lib.Reference;
import com.pahimar.ee3.lib.Textures;
import com.pahimar.ee3.tileentity.TileCalcinator;
@ -28,42 +25,35 @@ import cpw.mods.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class ModelCalcinator extends ModelBase {
private float scale;
private WavefrontObject modelCalcinatorOBJ;
public ModelCalcinator() {
scale = 1F;
modelCalcinatorOBJ = new WavefrontObject(Models.CALCINATOR);
}
public ModelCalcinator(float scale) {
public void render() {
this.scale = scale;
modelCalcinatorOBJ = new WavefrontObject(Models.CALCINATOR);
}
public void render(Tessellator tessellator, float scale) {
if (modelCalcinatorOBJ.groupObjects.size() != 0) {
for (GroupObject groupObject : modelCalcinatorOBJ.groupObjects) {
if (groupObject.name.equalsIgnoreCase("calcinator")) {
groupObject.render(tessellator, Reference.MODEL_TEXTURE_OFFSET, scale);
}
}
}
modelCalcinatorOBJ.renderAll();
}
public void render(TileCalcinator calcinator, double x, double y, double z) {
GL11.glPushMatrix();
GL11.glDisable(GL11.GL_LIGHTING);
// Scale, Translate, Rotate
GL11.glScalef(1.0F, 1.0F, 1.0F);
GL11.glTranslatef((float) x + 0.5F, (float) y + 0.0F, (float) z + 1.2F);
GL11.glRotatef(45F, 0F, 1F, 0F);
GL11.glRotatef(-90F, 1F, 0F, 0F);
// Bind texture
FMLClientHandler.instance().getClient().renderEngine.bindTexture(Textures.MODEL_CALCINATOR);
this.render(Tessellator.instance, scale);
// Render
this.render();
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glPopMatrix();
}

View file

@ -1,6 +1,5 @@
package com.pahimar.ee3.client.renderer.item;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.item.ItemStack;
import net.minecraftforge.client.IItemRenderer;
@ -47,37 +46,41 @@ public class ItemAludelRenderer implements IItemRenderer {
@Override
public void renderItem(ItemRenderType type, ItemStack item, Object... data) {
float scale;
switch (type) {
case ENTITY: {
scale = 0.66F;
renderAludel(-0.5F * scale, 0.0F * scale, 0.5F * scale, scale);
break;
renderAludel(-0.5F, 0.0F, 0.5F, 0.66F);
return;
}
case EQUIPPED: {
scale = 0.66F;
renderAludel(0.5F * scale, 0.0F * scale, 1.25F * scale, scale);
break;
renderAludel(0.5F, 0.0F, 1.25F, 0.66F);
return;
}
case INVENTORY: {
scale = 0.85F;
renderAludel(-1.0F * scale, -1.2F * scale, 0.0F * scale, scale);
break;
renderAludel(-1.0F, -1.2F, 0.0F, 0.85F);
return;
}
default:
break;
return;
}
}
private void renderAludel(float x, float y, float z, float scale) {
FMLClientHandler.instance().getClient().renderEngine.bindTexture(Textures.MODEL_ALUDEL);
GL11.glPushMatrix(); //start
GL11.glPushMatrix();
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glTranslatef(x, y, z); //size
// Scale, Translate, Rotate
GL11.glScalef(scale, scale, scale);
GL11.glTranslatef(x, y, z);
GL11.glRotatef(-90F, 1F, 0, 0);
aludelModel.render(Tessellator.instance, scale);
// Bind texture
FMLClientHandler.instance().getClient().renderEngine.bindTexture(Textures.MODEL_ALUDEL);
// Render
aludelModel.render();
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glPopMatrix(); //end
GL11.glPopMatrix();
}
}

View file

@ -1,6 +1,5 @@
package com.pahimar.ee3.client.renderer.item;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.item.ItemStack;
import net.minecraftforge.client.IItemRenderer;
@ -47,38 +46,41 @@ public class ItemCalcinatorRenderer implements IItemRenderer {
@Override
public void renderItem(ItemRenderType type, ItemStack item, Object... data) {
float scale;
switch (type) {
case ENTITY: {
scale = 1.0F;
renderCalcinator(-0.5F * scale, 0.0F * scale, 0.5F * scale, scale);
break;
renderCalcinator(-0.5F, 0.0F, 0.5F, 1.0F);
return;
}
case EQUIPPED: {
scale = 1.0F;
renderCalcinator(0.0F * scale, 0.0F * scale, 1.0F * scale, scale);
break;
renderCalcinator(0.0F, 0.0F, 1.0F, 1.0F);
return;
}
case INVENTORY: {
scale = 1.0F;
renderCalcinator(0.0F * scale, -0.1F * scale, 1.0F * scale, scale);
break;
renderCalcinator(0.0F, -0.1F, 1.0F, 1.0F);
return;
}
default:
break;
return;
}
}
private void renderCalcinator(float x, float y, float z, float scale) {
FMLClientHandler.instance().getClient().renderEngine.bindTexture(Textures.MODEL_CALCINATOR);
GL11.glPushMatrix(); //start
GL11.glPushMatrix();
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glTranslatef(x, y, z); //size
// Scale, Translate, Rotate
GL11.glScalef(scale, scale, scale);
GL11.glTranslatef(x, y, z);
GL11.glRotatef(-90F, 1F, 0, 0);
calcinatorModel.render(Tessellator.instance, scale);
// Bind texture
FMLClientHandler.instance().getClient().renderEngine.bindTexture(Textures.MODEL_CALCINATOR);
// Render
calcinatorModel.render();
GL11.glEnable(GL11.GL_LIGHTING);
GL11.glPopMatrix(); //end
GL11.glPopMatrix();
}
}

View file

@ -12,7 +12,55 @@ public class Face {
public Vertex[] vertexNormals;
public Vertex faceNormal;
public TextureCoordinate[] textureCoordinates;
public int glDrawingMode;
public void addFaceForRender(Tessellator tessellator) {
addFaceForRender(tessellator, 0.0002F);
}
public void addFaceForRender(Tessellator tessellator, float textureOffset) {
if (faceNormal == null) {
faceNormal = this.calculateFaceNormal();
}
tessellator.setNormal(faceNormal.x, faceNormal.y, faceNormal.z);
float averageU = 0F;
float averageV = 0F;
if (textureCoordinates.length != 0) {
for (int i = 0; i < textureCoordinates.length; ++i) {
averageU += textureCoordinates[i].u;
averageV += textureCoordinates[i].v;
}
averageU = averageU / textureCoordinates.length;
averageV = averageV / textureCoordinates.length;
}
float offsetU, offsetV;
for (int i = 0; i < vertices.length; ++i) {
if (textureCoordinates.length != 0) {
offsetU = textureOffset;
offsetV = textureOffset;
if (textureCoordinates[i].u > averageU) {
offsetU = -offsetU;
}
if (textureCoordinates[i].v > averageV) {
offsetV = -offsetV;
}
tessellator.addVertexWithUV(vertices[i].x, vertices[i].y, vertices[i].z, textureCoordinates[i].u + offsetU, textureCoordinates[i].v + offsetV);
}
else {
tessellator.addVertex(vertices[i].x, vertices[i].y, vertices[i].z);
}
}
}
public Vertex calculateFaceNormal() {
@ -24,110 +72,4 @@ public class Face {
return new Vertex((float)normalVector.xCoord, (float)normalVector.yCoord, (float)normalVector.zCoord);
}
public void render(Tessellator tessellator, float scale) {
this.render(tessellator, 0F, scale);
}
public void render(Tessellator tessellator, float textureOffset, float scale) {
tessellator.startDrawing(glDrawingMode);
if (faceNormal == null) {
faceNormal = this.calculateFaceNormal();
}
tessellator.setNormal(faceNormal.x, faceNormal.y, faceNormal.z);
float averageU = 0F;
float averageV = 0F;
if (textureCoordinates.length != 0) {
for (int i = 0; i < textureCoordinates.length; ++i) {
averageU += textureCoordinates[i].u;
averageV += textureCoordinates[i].v;
}
averageU = averageU / textureCoordinates.length;
averageV = averageV / textureCoordinates.length;
}
float offsetU, offsetV;
for (int i = 0; i < vertices.length; ++i) {
if (textureCoordinates.length != 0) {
offsetU = textureOffset;
offsetV = textureOffset;
if (textureCoordinates[i].u > averageU) {
offsetU = -offsetU;
}
if (textureCoordinates[i].v > averageV) {
offsetV = -offsetV;
}
tessellator.addVertexWithUV(vertices[i].x * scale, vertices[i].y * scale, vertices[i].z * scale, textureCoordinates[i].u + offsetU, textureCoordinates[i].v + offsetV);
}
else {
tessellator.addVertex(vertices[i].x * scale, vertices[i].y * scale, vertices[i].z * scale);
}
}
tessellator.draw();
}
public void render(Tessellator tessellator, int glDrawingMode, float scale) {
this.render(tessellator, glDrawingMode, 0F, scale);
}
public void render(Tessellator tessellator, int glDrawingMode, float textureOffset, float scale) {
tessellator.startDrawing(glDrawingMode);
if (faceNormal == null) {
faceNormal = this.calculateFaceNormal();
}
tessellator.setNormal(faceNormal.x, faceNormal.y, faceNormal.z);
float averageU = 0F;
float averageV = 0F;
if (textureCoordinates.length != 0) {
for (int i = 0; i < textureCoordinates.length; ++i) {
averageU += textureCoordinates[i].u;
averageV += textureCoordinates[i].v;
}
averageU = averageU / textureCoordinates.length;
averageV = averageV / textureCoordinates.length;
}
float offsetU, offsetV;
for (int i = 0; i < vertices.length; ++i) {
if (textureCoordinates.length != 0) {
offsetU = textureOffset;
offsetV = textureOffset;
if (textureCoordinates[i].u > averageU) {
offsetU = -offsetU;
}
if (textureCoordinates[i].v > averageV) {
offsetV = -offsetV;
}
tessellator.addVertexWithUV(vertices[i].x * scale, vertices[i].y * scale, vertices[i].z * scale, textureCoordinates[i].u + offsetU, textureCoordinates[i].v + offsetV);
}
else {
tessellator.addVertex(vertices[i].x * scale, vertices[i].y * scale, vertices[i].z * scale);
}
}
tessellator.draw();
}
}

View file

@ -3,6 +3,9 @@ package net.minecraftforge.client.model.obj;
import java.util.ArrayList;
import net.minecraft.client.renderer.Tessellator;
import org.lwjgl.opengl.GL11;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@ -11,42 +14,37 @@ public class GroupObject {
public String name;
public ArrayList<Face> faces = new ArrayList<Face>();
public int glDrawingMode;
public GroupObject() {
name = "";
this("");
}
public GroupObject(String name) {
this(name, GL11.GL_TRIANGLES);
}
public GroupObject(String name, int glDrawingMode) {
this.name = name;
this.glDrawingMode = glDrawingMode;
}
public void render(Tessellator tessellator, float scale) {
public void render() {
if (faces.size() > 0) {
Tessellator tessellator = Tessellator.instance;
tessellator.startDrawing(glDrawingMode);
for (Face face : faces) {
face.render(tessellator, 0F, scale);
}
face.addFaceForRender(tessellator);
}
public void render(Tessellator tessellator, float textureOffset, float scale) {
for (Face face : faces) {
face.render(tessellator, textureOffset, scale);
}
}
public void render(Tessellator tessellator, int glDrawingMode, float scale) {
for (Face face : faces) {
face.render(tessellator, glDrawingMode, 0F, scale);
}
}
public void render(Tessellator tessellator, int glDrawingMode, float textureOffset, float scale) {
for (Face face : faces) {
face.render(tessellator, glDrawingMode, textureOffset, scale);
tessellator.draw();
}
}
}

View file

@ -6,32 +6,17 @@ import cpw.mods.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class Vertex {
/**
*
*/
public float x, y, z;
/**
*
*/
public Vertex() {
}
/**
* @param x
* @param y
*/
public Vertex(float x, float y) {
this(x, y, 0F);
}
/**
* @param x
* @param y
* @param z
*/
public Vertex(float x, float y, float z) {
this.x = x;

View file

@ -6,12 +6,9 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.logging.Level;
import org.lwjgl.opengl.GL11;
import com.pahimar.ee3.core.helper.LogHelper;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@ -81,12 +78,14 @@ public class WavefrontObject {
}
}
else if (currentLine.startsWith("f ")) {
Face face = parseFace(currentLine);
if (face != null) {
if (currentGroupObject == null) {
currentGroupObject = new GroupObject("Default");
}
Face face = parseFace(currentLine);
if (face != null) {
currentGroupObject.faces.add(face);
}
}
@ -96,6 +95,7 @@ public class WavefrontObject {
if (currentGroupObject != null) {
groupObjects.add(currentGroupObject);
}
currentGroupObject = group;
}
}
@ -116,6 +116,35 @@ public class WavefrontObject {
}
}
public void renderAll() {
for (GroupObject groupObject : groupObjects) {
groupObject.render();
}
}
public void renderOnly(String ... groupNames) {
for (GroupObject groupObject : groupObjects) {
for (String groupName : groupNames) {
if (groupName.equalsIgnoreCase(groupObject.name)) {
groupObject.render();
}
}
}
}
public void renderAllExcept(String ... excludedGroupNames) {
for (GroupObject groupObject : groupObjects) {
for (String excludedGroupName : excludedGroupNames) {
if (!excludedGroupName.equalsIgnoreCase(groupObject.name)) {
groupObject.render();
}
}
}
}
private Vertex parseVertex(String line) {
Vertex vertex = null;
@ -170,13 +199,10 @@ public class WavefrontObject {
String[] subTokens = null;
if (tokens.length == 3) {
face.glDrawingMode = GL11.GL_TRIANGLES;
currentGroupObject.glDrawingMode = GL11.GL_TRIANGLES;
}
else if (tokens.length == 4) {
face.glDrawingMode = GL11.GL_QUADS;
}
else {
currentGroupObject.glDrawingMode = GL11.GL_QUADS;
}
face.vertices = new Vertex[tokens.length];