Per-side texturing and textures loaded in.

origin
Joshua Sigona 3 years ago
parent 6c0b5ccd32
commit 87c2c555b8
  1. 12
      cube.obj
  2. 4
      src/sig/BlockType.java
  3. 24
      src/sig/Cube.java
  4. 2
      src/sig/Mesh.java
  5. 49
      src/sig/SigRenderer.java
  6. 6
      src/sig/Texture.java
  7. 9
      src/sig/TextureType.java

@ -12,15 +12,15 @@ vt 0 1 #2
vt 1 0 #3 vt 1 0 #3
vt 1 1 #4 vt 1 1 #4
f 1/2 3/1 7/3 f 1/2 3/1 7/3 #South
f 1/2 7/3 5/4 f 1/2 7/3 5/4
f 5/2 7/1 8/3 f 5/2 7/1 8/3 #East
f 5/2 8/3 6/4 f 5/2 8/3 6/4
f 6/2 8/1 4/3 f 6/2 8/1 4/3 #North
f 6/2 4/3 2/4 f 6/2 4/3 2/4
f 2/2 4/1 3/3 f 2/2 4/1 3/3 #West
f 2/2 3/3 1/4 f 2/2 3/3 1/4
f 3/2 4/1 8/3 f 3/2 4/1 8/3 #Top
f 3/2 8/3 7/4 f 3/2 8/3 7/4
f 6/2 2/1 1/3 f 6/2 2/1 1/3 #Bottom
f 6/2 1/3 5/4 f 6/2 1/3 5/4

@ -222,4 +222,8 @@ public enum BlockType {
sides[RIGHT]=sideRight; sides[RIGHT]=sideRight;
sides[LEFT]=sideLeft; sides[LEFT]=sideLeft;
} }
public Texture getTexture(int side) {
return SigRenderer.blockTextures.get(sides[side]);
}
} }

@ -0,0 +1,24 @@
package sig;
import sig.utils.OBJReader;
public class Cube extends Mesh{
Cube(BlockType type) {
super(type);
this.triangles=OBJReader.ReadOBJFile("cube.obj",true);
triangles.get(0).tex=type.getTexture(BlockType.FRONT);
triangles.get(1).tex=type.getTexture(BlockType.FRONT);
triangles.get(2).tex=type.getTexture(BlockType.RIGHT);
triangles.get(3).tex=type.getTexture(BlockType.RIGHT);
triangles.get(4).tex=type.getTexture(BlockType.BACK);
triangles.get(5).tex=type.getTexture(BlockType.BACK);
triangles.get(6).tex=type.getTexture(BlockType.LEFT);
triangles.get(7).tex=type.getTexture(BlockType.LEFT);
triangles.get(8).tex=type.getTexture(BlockType.TOP);
triangles.get(9).tex=type.getTexture(BlockType.TOP);
triangles.get(10).tex=type.getTexture(BlockType.BOTTOM);
triangles.get(11).tex=type.getTexture(BlockType.BOTTOM);
}
}

@ -21,4 +21,6 @@ public class Mesh {
t.tex=te; t.tex=te;
} }
} }
Mesh(BlockType type) {;
}
} }

@ -7,7 +7,10 @@ import sig.utils.OBJReader;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener; import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import java.util.ArrayList; import java.util.ArrayList;
@ -48,11 +51,11 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
public static float roll = 0; public static float roll = 0;
final float MOVESPEED = 0.2f; final float MOVESPEED = 0.2f;
final float TURNSPEED = 0.2f; final float TURNSPEED = 0.05f;
public static float[] depthBuffer; public static float[] depthBuffer;
public static Mesh DIRT_CUBE=new Mesh("cube.obj","dirt.png"); public static HashMap<TextureType,Texture> blockTextures = new HashMap<TextureType,Texture>();
boolean upHeld=false,downHeld=false,leftHeld=false,rightHeld=false, boolean upHeld=false,downHeld=false,leftHeld=false,rightHeld=false,
aHeld=false,sHeld=false,dHeld=false,wHeld=false; aHeld=false,sHeld=false,dHeld=false,wHeld=false;
@ -63,10 +66,10 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
public void runGameLoop() { public void runGameLoop() {
if (upHeld) { if (upHeld) {
pitch+=MOVESPEED; pitch+=TURNSPEED;
} }
if (downHeld) { if (downHeld) {
pitch-=MOVESPEED; pitch-=TURNSPEED;
} }
if (rightHeld) { if (rightHeld) {
roll-=MOVESPEED; roll-=MOVESPEED;
@ -95,8 +98,8 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
} }
} }
public static void addBlock(Vector pos,Mesh type) { public static void addBlock(Vector pos,BlockType type) {
Block b = new Block(pos,type); Block b = new Block(pos,new Cube(type));
blockGrid.put(pos.x+"_"+pos.y+"_"+pos.z,b); blockGrid.put(pos.x+"_"+pos.y+"_"+pos.z,b);
b.updateFaces(); b.updateFaces();
updateRenderGrid(); updateRenderGrid();
@ -120,8 +123,10 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
Random r = new Random(438107); Random r = new Random(438107);
for (int x=0;x<32;x++) { for (int x=0;x<32;x++) {
for (int z=0;z<32;z++) { for (int z=0;z<32;z++) {
for (int y=0;y<r.nextInt(64);y++) { if (Math.random()<=0.5) {
addBlock(new Vector(x,y,z),DIRT_CUBE); addBlock(new Vector(x,0,z),BlockType.DIRT);
} else {
addBlock(new Vector(x,0,z),BlockType.SNOW_DIRT);
} }
} }
} }
@ -165,8 +170,32 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
}.start(); }.start();
} }
public static void main(String[] args) { public static void main(String[] args) {
JFrame f = new JFrame("SigRenderer");
new SigRenderer(f); try {
final int BLOCK_WIDTH = 128;
final int BLOCK_HEIGHT = 128;
BufferedImage img = ImageIO.read(new File("textures.png"));
WritableRaster r = img.getRaster();
for (TextureType tt : TextureType.values()) {
int[] pixelData = new int[tt.texWidth*BLOCK_WIDTH*tt.texHeight*BLOCK_HEIGHT];
int startX=tt.texX*BLOCK_WIDTH;
int startY=tt.texY*BLOCK_HEIGHT;
for (int x=0;x<tt.texWidth*BLOCK_WIDTH;x++) {
for (int y=0;y<tt.texHeight*BLOCK_HEIGHT;y++) {
int[] pixel = r.getPixel(x+startX,y+startY,new int[4]);
pixelData[x+y*tt.texWidth*BLOCK_WIDTH]=pixel[2]+(pixel[1]<<8)+(pixel[0]<<16)+(pixel[3]<<24);
}
}
blockTextures.put(tt,new Texture(pixelData,tt.texWidth*BLOCK_WIDTH,tt.texHeight*BLOCK_HEIGHT));
}
JFrame f = new JFrame("SigRenderer");
new SigRenderer(f);
} catch (IOException e) {
System.err.println("Cannot find game textures! (textures.png required)");
}
} }
@Override @Override

@ -33,6 +33,12 @@ public class Texture{
} }
} }
public Texture(int[] tex,int width,int height) {
this.tex=tex;
this.width=width;
this.height=height;
}
public int getColor(float u,float v,float mult) { public int getColor(float u,float v,float mult) {
int sx = (int)(u*width-1f); int sx = (int)(u*width-1f);
int sy = (int)(v*height-1f); int sy = (int)(v*height-1f);

@ -1,5 +1,12 @@
package sig; package sig;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import javax.imageio.ImageIO;
public enum TextureType { public enum TextureType {
GRASS_TOP(0,0), GRASS_TOP(0,0),
STONE(1,0), STONE(1,0),
@ -255,8 +262,8 @@ public enum TextureType {
(14,15), (14,15),
(15,15),*/ (15,15),*/
; ;
int texX,texY,texWidth,texHeight; int texX,texY,texWidth,texHeight;
Texture tex;
TextureType(int texX,int texY) { TextureType(int texX,int texY) {
this(texX,texY,1,1); this(texX,texY,1,1);

Loading…
Cancel
Save