Optimized texture pixel getting.

origin
Joshua Sigona 3 years ago
parent 16e2b942bb
commit a99f5a81e3
  1. 2
      cube.obj
  2. 6
      pom.xml
  3. 16
      src/sig/Panel.java
  4. 21
      src/sig/SigRenderer.java
  5. 37
      src/sig/Texture.java
  6. 6
      src/sig/Triangle.java
  7. 12
      src/sig/utils/DrawUtils.java

@ -13,7 +13,7 @@ vt 1 0 #3
vt 1 1 #4
f 1/2 3/1 7/3
f 1/2 7/3 8/4
f 1/2 7/3 5/4
f 5/2 7/1 8/3
f 5/2 8/3 6/4
f 6/2 8/1 4/3

@ -17,6 +17,12 @@
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.jogamp.jogl/jogl-all-main -->
<dependency>
<groupId>org.jogamp.jogl</groupId>
<artifactId>jogl-all-main</artifactId>
<version>2.3.2</version>
</dependency>
</dependencies>
<build>

@ -9,8 +9,6 @@ import java.awt.Color;
import java.awt.Image;
import java.awt.image.MemoryImageSource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.awt.image.ColorModel;
import java.awt.GraphicsEnvironment;
@ -137,7 +135,7 @@ public class Panel extends JPanel implements Runnable {
if (t.b!=null) {
dp = Math.max(0.1f,Math.min(1,(1f/((triTransformed.b.pos.x-SigRenderer.vCamera.x)*(triTransformed.b.pos.x-SigRenderer.vCamera.x)+
(triTransformed.b.pos.y-SigRenderer.vCamera.y)*(triTransformed.b.pos.y-SigRenderer.vCamera.y)+
(triTransformed.b.pos.z-SigRenderer.vCamera.z)*(triTransformed.b.pos.z-SigRenderer.vCamera.z))*4)));
(triTransformed.b.pos.z-SigRenderer.vCamera.z)*(triTransformed.b.pos.z-SigRenderer.vCamera.z))*64)))*0.5f+Math.max(0.1f,Math.min(1,1-Vector.dotProduct(normal,SigRenderer.vLookDir)))*0.5f;
} else {
dp = Math.max(0.1f,Vector.dotProduct(lightDir,normal));
}
@ -154,7 +152,7 @@ public class Panel extends JPanel implements Runnable {
triViewed.B = Matrix.MultiplyVector(matView,triTransformed.B);
triViewed.C = Matrix.MultiplyVector(matView,triTransformed.C);
triTransformed.copyExtraDataTo(triViewed);
triViewed.setColor(new Color(dp,0,0));
triViewed.setColor((0)+(0<<8)+((int)(dp*255)<<16));
int clippedTriangles = 0;
Triangle[] clipped = new Triangle[]{new Triangle(),new Triangle()};
@ -212,14 +210,14 @@ public class Panel extends JPanel implements Runnable {
}
}
Collections.sort(accumulatedTris, new Comparator<Triangle>() {
/*Collections.sort(accumulatedTris, new Comparator<Triangle>() {
@Override
public int compare(Triangle t1, Triangle t2) {
float z1=(t1.A.z+t1.B.z+t1.C.z)/3f;
float z2=(t2.A.z+t2.B.z+t2.C.z)/3f;
return (z1<z2)?1:(z1==z2)?0:-1;
}
});
});*/
for (Triangle t : accumulatedTris) {
Triangle[] clipped = new Triangle[]{new Triangle(),new Triangle()};
@ -251,12 +249,12 @@ public class Panel extends JPanel implements Runnable {
(int)tt.A.x,(int)tt.A.y,tt.T.u,tt.T.v,tt.T.w,
(int)tt.B.x,(int)tt.B.y,tt.U.u,tt.U.v,tt.U.w,
(int)tt.C.x,(int)tt.C.y,tt.V.u,tt.V.v,tt.V.w,
tt.tex,tt.col.getRed());
tt.tex,(tt.col&0xFF0000)>>16);
} else {
DrawUtils.FillTriangle(p,(int)tt.A.x,(int)tt.A.y,(int)tt.B.x,(int)tt.B.y,(int)tt.C.x,(int)tt.C.y,tt.getColor());
}
if (SigRenderer.WIREFRAME) {
DrawUtils.DrawTriangle(p,(int)tt.A.x,(int)tt.A.y,(int)tt.B.x,(int)tt.B.y,(int)tt.C.x,(int)tt.C.y,Color.WHITE);
DrawUtils.DrawTriangle(p,(int)tt.A.x,(int)tt.A.y,(int)tt.B.x,(int)tt.B.y,(int)tt.C.x,(int)tt.C.y,Color.WHITE.getRGB());
}
}
}
@ -296,4 +294,4 @@ public class Panel extends JPanel implements Runnable {
try {Thread.sleep(5);} catch (InterruptedException e) {}
}
}
}
}

@ -15,6 +15,7 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.awt.Toolkit;
import java.awt.BorderLayout;
@ -40,14 +41,14 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
public static float fAspectRatio = (float)SCREEN_HEIGHT/SCREEN_WIDTH;
public static Matrix matProj = Matrix.MakeProjection(fFov,fAspectRatio,fNear,fFar);
public static Vector vCamera = new Vector(0.5f,2f,0.5f);
public static Vector vCamera = new Vector(0.5f,2f,-15f);
public static Vector vLookDir = new Vector(0,0,1);
public static float yaw = 0;
public static float pitch = 0;
public static float yaw = (float)(-Math.PI/8);
public static float pitch = (float)(Math.PI/8);
public static float roll = 0;
final float MOVESPEED = 0.03f;
final float TURNSPEED = 0.03f;
final float MOVESPEED = 0.2f;
final float TURNSPEED = 0.2f;
public static Texture dirtTex;
@ -58,6 +59,8 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
boolean upHeld=false,downHeld=false,leftHeld=false,rightHeld=false,
aHeld=false,sHeld=false,dHeld=false,wHeld=false;
public static MouseEvent storedMouseEvent;
public void runGameLoop() {
if (upHeld) {
pitch+=MOVESPEED;
@ -110,9 +113,10 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
SigRenderer(JFrame f) {
//cube = new Mesh(OBJReader.ReadOBJFile("teapot.obj",false));
for (int x=0;x<16;x++) {
for (int z=0;z<16;z++) {
for (int y=0;y<16;y++) {
Random r = new Random(438107);
for (int x=0;x<32;x++) {
for (int z=0;z<32;z++) {
for (int y=0;y<r.nextInt(64);y++) {
addBlock(new Vector(x,y,z),DIRT_CUBE);
}
}
@ -163,6 +167,7 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
@Override
public void mouseClicked(MouseEvent e) {
storedMouseEvent=e;
}
@Override

@ -1,30 +1,49 @@
package sig;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
public class Texture{
BufferedImage tex;
int[] tex;
int width;
int height;
public Texture(File f) {
try {
this.tex = ImageIO.read(f);
BufferedImage i = ImageIO.read(f);
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
width=i.getWidth();
height=i.getHeight();
tex = new int[width*height];
WritableRaster r = i.getRaster();
for (int x=0;x<width;x++) {
for (int y=0;y<height;y++) {
int[] pixel = r.getPixel(x,y,new int[3]);
tex[x+y*width]=pixel[2]+(pixel[1]<<8)+(pixel[0]<<16);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public Color getColor(float u,float v,float mult) {
int sx = (int)(u*tex.getWidth()-1f);
int sy = (int)(v*tex.getHeight()-1f);
if (sx<0||sx>=tex.getWidth()||sy<0||sy>=tex.getHeight()) {
return new Color(0,0,0,0);
public int getColor(float u,float v,float mult) {
int sx = (int)(u*width-1f);
int sy = (int)(v*height-1f);
if (sx<0||sx>=width||sy<0||sy>=height) {
return 0;
} else {
Color newCol = new Color(tex.getRGB(sx,sy));
return new Color((newCol.getRed()/255f)*mult,(newCol.getGreen()/255f)*mult,(newCol.getBlue()/255f)*mult);
int indice = (int)(u*width-1)+(int)(v*height-1)*width;
//return tex[indice];
return (int)((tex[indice]&0xFF)*mult) + ((int)(((tex[indice]&0xFF00)>>8)*mult)<<8) + ((int)(((tex[indice]&0xFF0000)>>16)*mult)<<16);
/*Color newCol = new Color(tex.getRGB(sx,sy));
return new Color((newCol.getRed()/255f)*mult,(newCol.getGreen()/255f)*mult,(newCol.getBlue()/255f)*mult);*/
}
}

@ -4,7 +4,7 @@ import java.awt.Color;
public class Triangle {
Vector A,B,C;
Vector2 T,U,V;
Color col = Color.WHITE;
int col = Color.WHITE.getRGB();
Block b = null;
public Texture tex = null;
public Triangle() {
@ -42,10 +42,10 @@ public class Triangle {
return "Triangle [A=" + A + ", B=" + B + ", C=" + C + ", T=" + T + ", U=" + U + ", V=" + V + ", col=" + col
+ "]";
}
public Color getColor() {
public int getColor() {
return col;
}
public void setColor(Color col) {
public void setColor(int col) {
this.col=col;
}

@ -8,7 +8,7 @@ import sig.SigRenderer;
import sig.Texture;
public class DrawUtils {
static void drawLine(int[] canvas,int sx,int ex,int ny,Color col) {
static void drawLine(int[] canvas,int sx,int ex,int ny,int col) {
for (int i=sx;i<=ex;i++) {
Draw(canvas,i,ny,col);
}
@ -140,7 +140,7 @@ public class DrawUtils {
}
}
}
public static void FillTriangle(int[] canvas,int x1, int y1, int x2, int y2, int x3, int y3, Color col)
public static void FillTriangle(int[] canvas,int x1, int y1, int x2, int y2, int x3, int y3, int col)
{
int t1x=0, t2x=0, y=0, minx=0, maxx=0, t1xp=0, t2xp=0;
boolean changed1 = false;
@ -274,12 +274,12 @@ public class DrawUtils {
if (y>y3) return;
}
}
public static void DrawTriangle(int[]canvas,int x1,int y1,int x2,int y2,int x3,int y3,Color col) {
public static void DrawTriangle(int[]canvas,int x1,int y1,int x2,int y2,int x3,int y3,int col) {
DrawLine(canvas,x1,y1,x2,y2,col);
DrawLine(canvas,x2,y2,x3,y3,col);
DrawLine(canvas,x3,y3,x1,y1,col);
}
public static void DrawLine(int[] canvas,int x1,int y1,int x2,int y2,Color col) {
public static void DrawLine(int[] canvas,int x1,int y1,int x2,int y2,int col) {
int x,y,dx,dy,dx1,dy1,px,py,xe,ye,i;
dx=x2-x1;dy=y2-y1;
dx1=Math.abs(dx);dy1=Math.abs(dy);
@ -328,10 +328,10 @@ public class DrawUtils {
}
}
}
public static void Draw(int[] canvas,int x,int y,Color col) {
public static void Draw(int[] canvas,int x,int y,int col) {
if (x>=0&&y>=0&&x<SigRenderer.SCREEN_WIDTH&&y<SigRenderer.SCREEN_HEIGHT) {
//System.out.println(x+","+y);
canvas[x+y*SigRenderer.SCREEN_WIDTH]=col.getRGB();
canvas[x+y*SigRenderer.SCREEN_WIDTH]=col;
}
}
}

Loading…
Cancel
Save