Skip to content
This repository has been archived by the owner on Jan 1, 2022. It is now read-only.

Commit

Permalink
Output 32x32 and 128x128 images at the same time for wiki usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kobata committed May 11, 2013
1 parent 23e0165 commit b6b5c44
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
8 changes: 7 additions & 1 deletion src/itemrender/ItemRenderMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class ItemRenderMod {

public static boolean gl32_enabled = false;

public static final int DEFAULT_TEXTURE_SIZE = 128;
public static final int GRID_TEXTURE_SIZE = 32;

@Mod.PreInit
public void preInit(FMLPreInitializationEvent e) {
gl32_enabled = GLContext.getCapabilities().OpenGL32;
Expand All @@ -28,7 +31,10 @@ public void preInit(FMLPreInitializationEvent e) {
public void init(FMLInitializationEvent e) {
if(gl32_enabled) {
TickRegistry.registerTickHandler(new RenderTickHandler(), Side.CLIENT);
KeyBindingRegistry.registerKeyBinding(new KeybindRenderInventoryBlock());
KeybindRenderInventoryBlock defaultRender = new KeybindRenderInventoryBlock(DEFAULT_TEXTURE_SIZE, "");
RenderTickHandler.keybindToRender = defaultRender;
KeyBindingRegistry.registerKeyBinding(defaultRender);
KeyBindingRegistry.registerKeyBinding(new KeybindRenderInventoryBlock(GRID_TEXTURE_SIZE, "_grid"));
KeyBindingRegistry.registerKeyBinding(new KeybindToggleRender());
}
}
Expand Down
45 changes: 33 additions & 12 deletions src/itemrender/client/KeybindRenderInventoryBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,26 @@ public class KeybindRenderInventoryBlock extends KeyBindingRegistry.KeyHandler {
new KeyBinding[]{new KeyBinding("Render Inventory Block", Keyboard.KEY_P)};
private static boolean[] repeatings = new boolean[]{false};

private static final int RENDER_TEXTURE_SIZE = 128;
private int renderTextureSize = 128;

public static int framebufferID = -1;
public static int depthbufferID = -1;
public static int textureID = -1;
public int framebufferID = -1;
public int depthbufferID = -1;
public int textureID = -1;

private String filenameSuffix = "";

private RenderItem itemRenderer = new RenderItem();

public KeybindRenderInventoryBlock() {
public KeybindRenderInventoryBlock(int textureSize, String filename_suffix) {
super(keyBindings, repeatings);

renderTextureSize = textureSize;
filenameSuffix = filename_suffix;

createFramebuffer();
}

private void createFramebuffer() {
framebufferID = GL30.glGenFramebuffers();
textureID = GL11.glGenTextures();
int currentTexture = GL11.glGetInteger(GL11.GL_TEXTURE_BINDING_2D);
Expand All @@ -57,7 +66,7 @@ public KeybindRenderInventoryBlock() {
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, RENDER_TEXTURE_SIZE, RENDER_TEXTURE_SIZE, 0, GL12.GL_BGRA,
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, renderTextureSize, renderTextureSize, 0, GL12.GL_BGRA,
GL11.GL_UNSIGNED_BYTE, (java.nio.ByteBuffer) null);

// Restore old texture
Expand All @@ -66,7 +75,7 @@ public KeybindRenderInventoryBlock() {
// Create depth buffer
depthbufferID = GL30.glGenRenderbuffers();
GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthbufferID);
GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL11.GL_DEPTH_COMPONENT, RENDER_TEXTURE_SIZE, RENDER_TEXTURE_SIZE);
GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL11.GL_DEPTH_COMPONENT, renderTextureSize, renderTextureSize);

// Bind depth buffer to the framebuffer
GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL30.GL_RENDERBUFFER, depthbufferID);
Expand All @@ -78,6 +87,18 @@ public KeybindRenderInventoryBlock() {
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
}

private void deleteFramebuffer() {
GL30.glDeleteFramebuffers(framebufferID);
GL11.glDeleteTextures(textureID);
GL30.glDeleteRenderbuffers(depthbufferID);
}

public void resizeFramebuffer(int newSize) {
deleteFramebuffer();
renderTextureSize = newSize;
createFramebuffer();
}

@Override
public void keyDown(EnumSet<TickType> types, KeyBinding kb, boolean tickEnd, boolean isRepeat) {
if(!tickEnd) return;
Expand All @@ -91,7 +112,7 @@ public void keyDown(EnumSet<TickType> types, KeyBinding kb, boolean tickEnd, boo
// Remember viewport info.
IntBuffer viewportInfo = GLAllocation.createDirectIntBuffer(16);
GL11.glGetInteger(GL11.GL_VIEWPORT, viewportInfo);
GL11.glViewport(0, 0, RENDER_TEXTURE_SIZE, RENDER_TEXTURE_SIZE);
GL11.glViewport(0, 0, renderTextureSize, renderTextureSize);

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPushMatrix();
Expand Down Expand Up @@ -149,16 +170,16 @@ public void keyDown(EnumSet<TickType> types, KeyBinding kb, boolean tickEnd, boo

IntBuffer texture = BufferUtils.createIntBuffer(width * height);
GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, texture);
//GL11.glReadPixels(0, 0, RENDER_TEXTURE_SIZE, RENDER_TEXTURE_SIZE, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, texture);
//GL11.glReadPixels(0, 0, renderTextureSize, renderTextureSize, GL12.GL_BGRA, GL12.GL_UNSIGNED_INT_8_8_8_8_REV, texture);
int[] texture_array = new int[width * height];
texture.get(texture_array);

BufferedImage image =
new BufferedImage(RENDER_TEXTURE_SIZE, RENDER_TEXTURE_SIZE, BufferedImage.TYPE_INT_ARGB);
image.setRGB(0, 0, RENDER_TEXTURE_SIZE, RENDER_TEXTURE_SIZE, texture_array, 0, width);
new BufferedImage(renderTextureSize, renderTextureSize, BufferedImage.TYPE_INT_ARGB);
image.setRGB(0, 0, renderTextureSize, renderTextureSize, texture_array, 0, width);

File file = new File(minecraft.mcDataDir,
String.format("rendered/item_%d_%d.png", current.getItem().itemID, current.getItemDamage()));
String.format("rendered/item_%d_%d%s.png", current.getItem().itemID, current.getItemDamage(), filenameSuffix));
file.mkdirs();
try {
ImageIO.write(image, "png", file);
Expand Down
5 changes: 3 additions & 2 deletions src/itemrender/client/RenderTickHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@SideOnly(Side.CLIENT)
public class RenderTickHandler implements ITickHandler {
public static boolean renderPreview = false;
public static KeybindRenderInventoryBlock keybindToRender;

public RenderTickHandler() {
}
Expand All @@ -21,11 +22,11 @@ public void tickStart(EnumSet<TickType> type, Object... tickData) {

@Override
public void tickEnd(EnumSet<TickType> type, Object... tickData) {
if(renderPreview) {
if(keybindToRender != null && renderPreview) {
int originalTexture = GL11.glGetInteger(GL11.GL_TEXTURE_BINDING_2D);

// Bind framebuffer texture
GL11.glBindTexture(GL11.GL_TEXTURE_2D, KeybindRenderInventoryBlock.textureID);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, keybindToRender.textureID);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2i(0, 0);
Expand Down

0 comments on commit b6b5c44

Please sign in to comment.