Texture lighting / color configuration.

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
origin
sigonasr2, Sig, Sigo 3 years ago
parent 482d512433
commit aade867128
  1. 13
      src/sig/Panel.java
  2. 9
      src/sig/SigRenderer.java
  3. 5
      src/sig/Texture.java
  4. 6
      src/sig/Triangle.java
  5. 4
      src/sig/Vector2.java
  6. 6
      src/sig/utils/DrawUtils.java
  7. 43
      src/sig/utils/OBJReader.java

@ -17,8 +17,6 @@ import java.awt.GraphicsEnvironment;
import java.awt.GraphicsConfiguration; import java.awt.GraphicsConfiguration;
import java.awt.Toolkit; import java.awt.Toolkit;
import sig.Texture;
public class Panel extends JPanel implements Runnable { public class Panel extends JPanel implements Runnable {
long startTime = System.nanoTime(); long startTime = System.nanoTime();
long endTime = System.nanoTime(); long endTime = System.nanoTime();
@ -77,6 +75,8 @@ public class Panel extends JPanel implements Runnable {
int[] p = pixel; // this avoid crash when resizing int[] p = pixel; // this avoid crash when resizing
//a=h/w //a=h/w
fTheta+=0.05f;
final int h=SigRenderer.SCREEN_HEIGHT; final int h=SigRenderer.SCREEN_HEIGHT;
if(p.length != width * height) return; if(p.length != width * height) return;
for (int x=0;x<width;x++) { for (int x=0;x<width;x++) {
@ -114,6 +114,7 @@ public class Panel extends JPanel implements Runnable {
triTransformed.T = t.T; triTransformed.T = t.T;
triTransformed.U = t.U; triTransformed.U = t.U;
triTransformed.V = t.V; triTransformed.V = t.V;
triTransformed.textured = t.textured;
Vector normal=new Vector(),line1=new Vector(),line2=new Vector(); Vector normal=new Vector(),line1=new Vector(),line2=new Vector();
line1 = Vector.subtract(triTransformed.B,triTransformed.A); line1 = Vector.subtract(triTransformed.B,triTransformed.A);
@ -138,6 +139,7 @@ public class Panel extends JPanel implements Runnable {
triViewed.T = triTransformed.T; triViewed.T = triTransformed.T;
triViewed.U = triTransformed.U; triViewed.U = triTransformed.U;
triViewed.V = triTransformed.V; triViewed.V = triTransformed.V;
triViewed.textured = triTransformed.textured;
int clippedTriangles = 0; int clippedTriangles = 0;
Triangle[] clipped = new Triangle[]{new Triangle(),new Triangle()}; Triangle[] clipped = new Triangle[]{new Triangle(),new Triangle()};
@ -151,6 +153,7 @@ public class Panel extends JPanel implements Runnable {
triProjected.B = Matrix.MultiplyVector(SigRenderer.matProj,clipped[i].B); triProjected.B = Matrix.MultiplyVector(SigRenderer.matProj,clipped[i].B);
triProjected.C = Matrix.MultiplyVector(SigRenderer.matProj,clipped[i].C); triProjected.C = Matrix.MultiplyVector(SigRenderer.matProj,clipped[i].C);
triProjected.col = clipped[i].col; triProjected.col = clipped[i].col;
triProjected.textured = clipped[i].textured;
triProjected.T = (Vector2)clipped[i].T.clone(); triProjected.T = (Vector2)clipped[i].T.clone();
triProjected.U = (Vector2)clipped[i].U.clone(); triProjected.U = (Vector2)clipped[i].U.clone();
triProjected.V = (Vector2)clipped[i].V.clone(); triProjected.V = (Vector2)clipped[i].V.clone();
@ -227,11 +230,15 @@ public class Panel extends JPanel implements Runnable {
} }
for (Triangle tt : triList) { for (Triangle tt : triList) {
if (tt.textured) {
DrawUtils.TexturedTriangle(p, DrawUtils.TexturedTriangle(p,
(int)tt.A.x,(int)tt.A.y,tt.T.u,tt.T.v,tt.T.w, (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.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, (int)tt.C.x,(int)tt.C.y,tt.V.u,tt.V.v,tt.V.w,
SigRenderer.dirtTex); SigRenderer.dirtTex,tt.col.getRed());
} 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) { 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);
} }

@ -2,11 +2,12 @@ package sig;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.swing.JFrame; import javax.swing.JFrame;
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.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.Arrays; import java.util.Arrays;
@ -80,7 +81,7 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
SigRenderer(JFrame f) { SigRenderer(JFrame f) {
dirtTex = new Texture(new File("dirt.png")); dirtTex = new Texture(new File("dirt.png"));
//cube = new Mesh(OBJReader.ReadOBJFile("teapot.obj")); //cube = new Mesh(OBJReader.ReadOBJFile("teapot.obj",false));
cube = new Mesh(Arrays.asList( cube = new Mesh(Arrays.asList(
new Triangle[]{ new Triangle[]{
new Triangle(new Vector(),new Vector(0,1,0),new Vector(1,1,0),new Vector2(0,1),new Vector2(0,0),new Vector2(1,0)), new Triangle(new Vector(),new Vector(0,1,0),new Vector(1,1,0),new Vector2(0,1),new Vector2(0,0),new Vector2(1,0)),
@ -97,6 +98,10 @@ public class SigRenderer implements KeyListener,MouseListener,MouseMotionListene
new Triangle(new Vector(1,0,1),new Vector(0,0,0),new Vector(1,0,0),new Vector2(0,1),new Vector2(1,0),new Vector2(1,1)), new Triangle(new Vector(1,0,1),new Vector(0,0,0),new Vector(1,0,0),new Vector2(0,1),new Vector2(1,0),new Vector2(1,1)),
})); }));
for (Triangle t : cube.triangles) {
t.textured=true;
}
Panel p = new Panel(); Panel p = new Panel();
f.getContentPane().addMouseListener(this); f.getContentPane().addMouseListener(this);

@ -17,13 +17,14 @@ public class Texture{
} }
} }
public Color getColor(float u,float v) { public Color getColor(float u,float v,float mult) {
int sx = (int)(u*tex.getWidth()-1f); int sx = (int)(u*tex.getWidth()-1f);
int sy = (int)(v*tex.getHeight()-1f); int sy = (int)(v*tex.getHeight()-1f);
if (sx<0||sx>=tex.getWidth()||sy<0||sy>=tex.getHeight()) { if (sx<0||sx>=tex.getWidth()||sy<0||sy>=tex.getHeight()) {
return new Color(0,0,0,0); return new Color(0,0,0,0);
} else { } else {
return new Color(tex.getRGB(sx,sy)); Color newCol = new Color(tex.getRGB(sx,sy));
return new Color((newCol.getRed()/255f)*mult,(newCol.getGreen()/255f)*mult,(newCol.getBlue()/255f)*mult);
} }
} }

@ -5,6 +5,7 @@ public class Triangle {
Vector A,B,C; Vector A,B,C;
Vector2 T,U,V; Vector2 T,U,V;
Color col = Color.WHITE; Color col = Color.WHITE;
public boolean textured=false;
public Triangle() { public Triangle() {
this(new Vector(),new Vector(),new Vector()); this(new Vector(),new Vector(),new Vector());
} }
@ -27,7 +28,8 @@ public class Triangle {
} }
@Override @Override
public String toString() { public String toString() {
return "Triangle [A=" + A + ", B=" + B + ", C=" + C + ", col=" + col + "]"; return "Triangle [A=" + A + ", B=" + B + ", C=" + C + ", T=" + T + ", U=" + U + ", V=" + V + ", col=" + col
+ "]";
} }
public Color getColor() { public Color getColor() {
return col; return col;
@ -85,6 +87,7 @@ public class Triangle {
if (insidePointCount==1&&outsidePointCount==2) { if (insidePointCount==1&&outsidePointCount==2) {
ExtraData t = new ExtraData(0); ExtraData t = new ExtraData(0);
out_tri[0].col = in.col; out_tri[0].col = in.col;
out_tri[0].textured = in.textured;
out_tri[0].A = inside_points[0]; out_tri[0].A = inside_points[0];
out_tri[0].T = inside_tex[0]; out_tri[0].T = inside_tex[0];
out_tri[0].B = Vector.IntersectPlane(plane_p, plane_n, inside_points[0], outside_points[0],t); out_tri[0].B = Vector.IntersectPlane(plane_p, plane_n, inside_points[0], outside_points[0],t);
@ -100,6 +103,7 @@ public class Triangle {
if (insidePointCount==2&&outsidePointCount==1) { if (insidePointCount==2&&outsidePointCount==1) {
ExtraData t = new ExtraData(0); ExtraData t = new ExtraData(0);
out_tri[0].col=out_tri[1].col=in.col; out_tri[0].col=out_tri[1].col=in.col;
out_tri[0].textured=out_tri[1].textured=in.textured;
out_tri[0].A = inside_points[0]; out_tri[0].A = inside_points[0];
out_tri[0].B = inside_points[1]; out_tri[0].B = inside_points[1];
out_tri[0].T = inside_tex[0]; out_tri[0].T = inside_tex[0];

@ -17,4 +17,8 @@ public class Vector2 {
protected Object clone(){ protected Object clone(){
return new Vector2(u,v,w); return new Vector2(u,v,w);
} }
@Override
public String toString() {
return "Vector2 [u=" + u + ", v=" + v + ", w=" + w + "]";
}
} }

@ -17,7 +17,7 @@ public class DrawUtils {
int x1, int y1, float u1,float v1,float w1, int x1, int y1, float u1,float v1,float w1,
int x2, int y2, float u2,float v2,float w2, int x2, int y2, float u2,float v2,float w2,
int x3, int y3, float u3,float v3,float w3, int x3, int y3, float u3,float v3,float w3,
Texture texture Texture texture, int colorMult
) { ) {
if (y2<y1) {int t=y1;y1=y2;y2=t;t=x1;x1=x2;x2=t;float u=u1;u1=u2;u2=u;float v=v1;v1=v2;v2=v;float w=w1;w1=w2;w2=w;} if (y2<y1) {int t=y1;y1=y2;y2=t;t=x1;x1=x2;x2=t;float u=u1;u1=u2;u2=u;float v=v1;v1=v2;v2=v;float w=w1;w1=w2;w2=w;}
if (y3<y1) {int t=y1;y1=y3;y3=t;t=x1;x1=x3;x3=t;float u=u1;u1=u3;u3=u;float v=v1;v1=v3;v3=v;float w=w1;w1=w3;w3=w;} if (y3<y1) {int t=y1;y1=y3;y3=t;t=x1;x1=x3;x3=t;float u=u1;u1=u3;u3=u;float v=v1;v1=v3;v3=v;float w=w1;w1=w3;w3=w;}
@ -79,7 +79,7 @@ public class DrawUtils {
tex_u=(1.0f-t)*tex_su+t*tex_eu; tex_u=(1.0f-t)*tex_su+t*tex_eu;
tex_v=(1.0f-t)*tex_sv+t*tex_ev; tex_v=(1.0f-t)*tex_sv+t*tex_ev;
tex_w=(1.0f-t)*tex_sw+t*tex_ew; tex_w=(1.0f-t)*tex_sw+t*tex_ew;
Draw(canvas,j,i,texture.getColor(tex_u/tex_w,tex_v/tex_w)); Draw(canvas,j,i,texture.getColor(tex_u/tex_w,tex_v/tex_w,colorMult/255f));
t+=tstep; t+=tstep;
} }
} }
@ -128,7 +128,7 @@ public class DrawUtils {
tex_u=(1.0f-t)*tex_su+t*tex_eu; tex_u=(1.0f-t)*tex_su+t*tex_eu;
tex_v=(1.0f-t)*tex_sv+t*tex_ev; tex_v=(1.0f-t)*tex_sv+t*tex_ev;
tex_w=(1.0f-t)*tex_sw+t*tex_ew; tex_w=(1.0f-t)*tex_sw+t*tex_ew;
Draw(canvas,j,i,texture.getColor(tex_u/tex_w,tex_v/tex_w)); Draw(canvas,j,i,texture.getColor(tex_u/tex_w,tex_v/tex_w,colorMult/255f));
t+=tstep; t+=tstep;
} }
} }

@ -7,19 +7,53 @@ import java.util.regex.Pattern;
import sig.Triangle; import sig.Triangle;
import sig.Vector; import sig.Vector;
import sig.Vector2;
public class OBJReader { public class OBJReader {
public static List<Triangle> ReadOBJFile(String f) { public static List<Triangle> ReadOBJFile(String f,boolean textured) {
String[] data = FileUtils.readFromFile(f); String[] data = FileUtils.readFromFile(f);
List<float[]> vertices = new ArrayList<>(); List<float[]> vertices = new ArrayList<>();
List<float[]> texs = new ArrayList<>();
List<Triangle> tris = new ArrayList<>(); List<Triangle> tris = new ArrayList<>();
for (String s : data) { for (String s : data) {
String[] split = s.split(Pattern.quote(" ")); String[] split = s.split(Pattern.quote(" "));
if (split[0].equalsIgnoreCase("v")) { if (split[0].equalsIgnoreCase("v")) {
vertices.add(new float[]{Float.parseFloat(split[1]),Float.parseFloat(split[2]),Float.parseFloat(split[3])}); vertices.add(new float[]{Float.parseFloat(split[1]),Float.parseFloat(split[2]),Float.parseFloat(split[3])});
} else } else
if (split[0].equalsIgnoreCase("vt")) {
texs.add(new float[]{Float.parseFloat(split[1]),Float.parseFloat(split[2])});
} else
if (split[0].equalsIgnoreCase("f")) { if (split[0].equalsIgnoreCase("f")) {
tris.add(new Triangle( if (textured) {
String[] spl1=split[1].split(Pattern.quote("/"));
String[] spl2=split[2].split(Pattern.quote("/"));
String[] spl3=split[3].split(Pattern.quote("/"));
Triangle tri = new Triangle(
new Vector(
vertices.get(Integer.parseInt(spl1[0])-1)[0],
vertices.get(Integer.parseInt(spl2[0])-1)[1],
vertices.get(Integer.parseInt(spl3[0])-1)[2]),
new Vector(
vertices.get(Integer.parseInt(spl1[0])-1)[0],
vertices.get(Integer.parseInt(spl2[0])-1)[1],
vertices.get(Integer.parseInt(spl3[0])-1)[2]),
new Vector(
vertices.get(Integer.parseInt(spl1[0])-1)[0],
vertices.get(Integer.parseInt(spl2[0])-1)[1],
vertices.get(Integer.parseInt(spl3[0])-1)[2]),
new Vector2(
texs.get(Integer.parseInt(spl1[1])-1)[0],
texs.get(Integer.parseInt(spl2[1])-1)[1]),
new Vector2(
texs.get(Integer.parseInt(spl1[1])-1)[0],
texs.get(Integer.parseInt(spl2[1])-1)[1]),
new Vector2(
texs.get(Integer.parseInt(spl1[1])-1)[0],
texs.get(Integer.parseInt(spl2[1])-1)[1]));
tri.textured=textured;
tris.add(tri);
} else {
Triangle tri = new Triangle(
new Vector( new Vector(
vertices.get(Integer.parseInt(split[1])-1)[0], vertices.get(Integer.parseInt(split[1])-1)[0],
vertices.get(Integer.parseInt(split[1])-1)[1], vertices.get(Integer.parseInt(split[1])-1)[1],
@ -31,7 +65,10 @@ public class OBJReader {
new Vector( new Vector(
vertices.get(Integer.parseInt(split[3])-1)[0], vertices.get(Integer.parseInt(split[3])-1)[0],
vertices.get(Integer.parseInt(split[3])-1)[1], vertices.get(Integer.parseInt(split[3])-1)[1],
vertices.get(Integer.parseInt(split[3])-1)[2]))); vertices.get(Integer.parseInt(split[3])-1)[2]));
tri.textured=textured;
tris.add(tri);
}
} }
} }
return tris; return tris;

Loading…
Cancel
Save