Render simple shading via normals and provide a wireframe mode if necessary.
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
a4e7731412
commit
cd6dd3217a
@ -77,7 +77,7 @@ public class Panel extends JPanel implements Runnable {
|
|||||||
for (int y=0;y<height;y++) {
|
for (int y=0;y<height;y++) {
|
||||||
boolean found=false;
|
boolean found=false;
|
||||||
if (!found) {
|
if (!found) {
|
||||||
p[ (int)(x*SigRenderer.RESOLUTION) + (int)(y*SigRenderer.RESOLUTION) * width] = Color.WHITE.getRGB();
|
p[ (int)(x*SigRenderer.RESOLUTION) + (int)(y*SigRenderer.RESOLUTION) * width] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -130,9 +130,17 @@ public class Panel extends JPanel implements Runnable {
|
|||||||
if (normal.x*(triTranslated.A.x-SigRenderer.vCamera.x)+
|
if (normal.x*(triTranslated.A.x-SigRenderer.vCamera.x)+
|
||||||
normal.y*(triTranslated.A.y-SigRenderer.vCamera.y)+
|
normal.y*(triTranslated.A.y-SigRenderer.vCamera.y)+
|
||||||
normal.z*(triTranslated.A.z-SigRenderer.vCamera.z)<0) {
|
normal.z*(triTranslated.A.z-SigRenderer.vCamera.z)<0) {
|
||||||
|
|
||||||
|
Vector3f lightDir = new Vector3f(0,0,-1);
|
||||||
|
l = (float)Math.sqrt(lightDir.x*lightDir.x+lightDir.y*lightDir.y+lightDir.z*lightDir.z);
|
||||||
|
lightDir.x/=l; lightDir.y/=l; lightDir.z/=l;
|
||||||
|
|
||||||
|
float dp = normal.x*lightDir.x+normal.y*lightDir.y+normal.z*lightDir.z;
|
||||||
|
|
||||||
Matrix.MultiplyMatrixVector(triTranslated.A, triProjected.A, SigRenderer.matProj);
|
Matrix.MultiplyMatrixVector(triTranslated.A, triProjected.A, SigRenderer.matProj);
|
||||||
Matrix.MultiplyMatrixVector(triTranslated.B, triProjected.B, SigRenderer.matProj);
|
Matrix.MultiplyMatrixVector(triTranslated.B, triProjected.B, SigRenderer.matProj);
|
||||||
Matrix.MultiplyMatrixVector(triTranslated.C, triProjected.C, SigRenderer.matProj);
|
Matrix.MultiplyMatrixVector(triTranslated.C, triProjected.C, SigRenderer.matProj);
|
||||||
|
triProjected.setColor(new Color(dp,dp,dp));
|
||||||
|
|
||||||
triProjected.A.x+=1f;
|
triProjected.A.x+=1f;
|
||||||
triProjected.A.y+=1f;
|
triProjected.A.y+=1f;
|
||||||
@ -147,7 +155,10 @@ public class Panel extends JPanel implements Runnable {
|
|||||||
triProjected.C.x*=0.5f*SigRenderer.SCREEN_WIDTH;
|
triProjected.C.x*=0.5f*SigRenderer.SCREEN_WIDTH;
|
||||||
triProjected.C.y*=0.5f*SigRenderer.SCREEN_HEIGHT;
|
triProjected.C.y*=0.5f*SigRenderer.SCREEN_HEIGHT;
|
||||||
|
|
||||||
DrawUtils.DrawTriangle(p,(int)triProjected.A.x,(int)triProjected.A.y,(int)triProjected.B.x,(int)triProjected.B.y,(int)triProjected.C.x,(int)triProjected.C.y,Color.BLACK);
|
DrawUtils.FillTriangle(p,(int)triProjected.A.x,(int)triProjected.A.y,(int)triProjected.B.x,(int)triProjected.B.y,(int)triProjected.C.x,(int)triProjected.C.y,triProjected.getColor());
|
||||||
|
if (SigRenderer.WIREFRAME) {
|
||||||
|
DrawUtils.DrawTriangle(p,(int)triProjected.A.x,(int)triProjected.A.y,(int)triProjected.B.x,(int)triProjected.B.y,(int)triProjected.C.x,(int)triProjected.C.y,Color.BLACK);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
i += 1;
|
i += 1;
|
||||||
|
@ -4,6 +4,9 @@ import javax.vecmath.Matrix4f;
|
|||||||
import javax.vecmath.Point2d;
|
import javax.vecmath.Point2d;
|
||||||
import javax.vecmath.Tuple3d;
|
import javax.vecmath.Tuple3d;
|
||||||
import javax.vecmath.Vector3f;
|
import javax.vecmath.Vector3f;
|
||||||
|
|
||||||
|
import sig.utils.DrawUtils;
|
||||||
|
|
||||||
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;
|
||||||
@ -18,6 +21,9 @@ import java.awt.Dimension;
|
|||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
|
|
||||||
public class SigRenderer implements KeyListener,MouseListener,MouseMotionListener{
|
public class SigRenderer implements KeyListener,MouseListener,MouseMotionListener{
|
||||||
|
|
||||||
|
public static boolean WIREFRAME = false;
|
||||||
|
|
||||||
public static Mesh cube;
|
public static Mesh cube;
|
||||||
public static int SCREEN_WIDTH=1280;
|
public static int SCREEN_WIDTH=1280;
|
||||||
public static int SCREEN_HEIGHT=720;
|
public static int SCREEN_HEIGHT=720;
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package sig;
|
package sig;
|
||||||
import javax.vecmath.Vector3f;
|
import javax.vecmath.Vector3f;
|
||||||
|
import java.awt.Color;
|
||||||
|
|
||||||
public class Triangle {
|
public class Triangle {
|
||||||
Vector3f A,B,C;
|
Vector3f A,B,C;
|
||||||
|
Color col = Color.WHITE;
|
||||||
Triangle(Vector3f A,Vector3f B,Vector3f C) {
|
Triangle(Vector3f A,Vector3f B,Vector3f C) {
|
||||||
this.A=A;
|
this.A=A;
|
||||||
this.B=B;
|
this.B=B;
|
||||||
@ -16,4 +18,10 @@ public class Triangle {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return "Triangle [A=" + A + ", B=" + B + ", C=" + C + "]";
|
return "Triangle [A=" + A + ", B=" + B + ", C=" + C + "]";
|
||||||
}
|
}
|
||||||
|
public Color getColor() {
|
||||||
|
return col;
|
||||||
|
}
|
||||||
|
public void setColor(Color col) {
|
||||||
|
this.col=col;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,150 @@
|
|||||||
package sig.utils;
|
package sig.utils;
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import sig.SigRenderer;
|
import sig.SigRenderer;
|
||||||
|
|
||||||
public class DrawUtils {
|
public class DrawUtils {
|
||||||
|
static void drawLine(int[] canvas,int sx,int ex,int ny,Color col) {
|
||||||
|
for (int i=sx;i<=ex;i++) {
|
||||||
|
Draw(canvas,i,ny,col);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static void FillTriangle(int[] canvas,int x1, int y1, int x2, int y2, int x3, int y3, Color col)
|
||||||
|
{
|
||||||
|
int t1x=0, t2x=0, y=0, minx=0, maxx=0, t1xp=0, t2xp=0;
|
||||||
|
boolean changed1 = false;
|
||||||
|
boolean changed2 = false;
|
||||||
|
int signx1=0, signx2=0, dx1=0, dy1=0, dx2=0, dy2=0;
|
||||||
|
int e1=0, e2=0;
|
||||||
|
// Sort vertices
|
||||||
|
if (y1>y2) {int t=y1;y1=y2;y2=t;t=x1;x1=x2;x2=t;}
|
||||||
|
if (y1>y3) {int t=y1;y1=y3;y3=t;t=x1;x1=x3;x3=t;}
|
||||||
|
if (y2>y3) {int t=y2;y2=y3;y3=t;t=x2;x2=x3;x3=t;}
|
||||||
|
|
||||||
|
t1x = t2x = x1; y = y1; // Starting points
|
||||||
|
dx1 = (int)(x2 - x1); if (dx1<0) { dx1 = -dx1; signx1 = -1; }
|
||||||
|
else signx1 = 1;
|
||||||
|
dy1 = (int)(y2 - y1);
|
||||||
|
|
||||||
|
dx2 = (int)(x3 - x1); if (dx2<0) { dx2 = -dx2; signx2 = -1; }
|
||||||
|
else signx2 = 1;
|
||||||
|
dy2 = (int)(y3 - y1);
|
||||||
|
|
||||||
|
if (dy1 > dx1) { // swap values
|
||||||
|
int t=dx1;dx1=dy1;dy1=t;
|
||||||
|
changed1 = true;
|
||||||
|
}
|
||||||
|
if (dy2 > dx2) { // swap values
|
||||||
|
int t=dy2;dy2=dx2;dx2=t;
|
||||||
|
changed2 = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
e2 = (int)(dx2 >> 1);
|
||||||
|
// Flat top, just process the second half
|
||||||
|
boolean goNext=false;
|
||||||
|
if (y1 == y2) goNext=true;
|
||||||
|
if (!goNext) {
|
||||||
|
e1 = (int)(dx1 >> 1);
|
||||||
|
|
||||||
|
for (int i = 0; i < dx1;) {
|
||||||
|
t1xp = 0; t2xp = 0;
|
||||||
|
if (t1x<t2x) { minx = t1x; maxx = t2x; }
|
||||||
|
else { minx = t2x; maxx = t1x; }
|
||||||
|
// process first line until y value is about to change
|
||||||
|
loop3:
|
||||||
|
while (i<dx1) {
|
||||||
|
i++;
|
||||||
|
e1 += dy1;
|
||||||
|
while (e1 >= dx1) {
|
||||||
|
e1 -= dx1;
|
||||||
|
if (changed1) t1xp = signx1;//t1x += signx1;
|
||||||
|
else break loop3;
|
||||||
|
}
|
||||||
|
if (changed1) break;
|
||||||
|
else t1x += signx1;
|
||||||
|
}
|
||||||
|
// Move line
|
||||||
|
// process second line until y value is about to change
|
||||||
|
loop2:
|
||||||
|
while (true) {
|
||||||
|
e2 += dy2;
|
||||||
|
while (e2 >= dx2) {
|
||||||
|
e2 -= dx2;
|
||||||
|
if (changed2) t2xp = signx2;//t2x += signx2;
|
||||||
|
else break loop2;
|
||||||
|
}
|
||||||
|
if (changed2) break;
|
||||||
|
else t2x += signx2;
|
||||||
|
}
|
||||||
|
if (minx>t1x) minx = t1x; if (minx>t2x) minx = t2x;
|
||||||
|
if (maxx<t1x) maxx = t1x; if (maxx<t2x) maxx = t2x;
|
||||||
|
drawLine(canvas,minx, maxx, y,col); // Draw line from min to max points found on the y
|
||||||
|
// Now increase y
|
||||||
|
if (!changed1) t1x += signx1;
|
||||||
|
t1x += t1xp;
|
||||||
|
if (!changed2) t2x += signx2;
|
||||||
|
t2x += t2xp;
|
||||||
|
y += 1;
|
||||||
|
if (y == y2) break;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Second half
|
||||||
|
dx1 = (int)(x3 - x2); if (dx1<0) { dx1 = -dx1; signx1 = -1; }
|
||||||
|
else signx1 = 1;
|
||||||
|
dy1 = (int)(y3 - y2);
|
||||||
|
t1x = x2;
|
||||||
|
|
||||||
|
if (dy1 > dx1) { // swap values
|
||||||
|
int t=dy1;dy1=dx1;dx1=t;
|
||||||
|
changed1 = true;
|
||||||
|
}
|
||||||
|
else changed1 = false;
|
||||||
|
|
||||||
|
e1 = (int)(dx1 >> 1);
|
||||||
|
|
||||||
|
for (int i = 0; i <= dx1; i++) {
|
||||||
|
t1xp = 0; t2xp = 0;
|
||||||
|
if (t1x<t2x) { minx = t1x; maxx = t2x; }
|
||||||
|
else { minx = t2x; maxx = t1x; }
|
||||||
|
// process first line until y value is about to change
|
||||||
|
loop3:
|
||||||
|
while (i<dx1) {
|
||||||
|
e1 += dy1;
|
||||||
|
while (e1 >= dx1) {
|
||||||
|
e1 -= dx1;
|
||||||
|
if (changed1) { t1xp = signx1; break; }//t1x += signx1;
|
||||||
|
else break loop3;
|
||||||
|
}
|
||||||
|
if (changed1) break;
|
||||||
|
else t1x += signx1;
|
||||||
|
if (i<dx1) i++;
|
||||||
|
}
|
||||||
|
// process second line until y value is about to change
|
||||||
|
loop2:
|
||||||
|
while (t2x != x3) {
|
||||||
|
e2 += dy2;
|
||||||
|
while (e2 >= dx2) {
|
||||||
|
e2 -= dx2;
|
||||||
|
if (changed2) t2xp = signx2;
|
||||||
|
else break loop2;
|
||||||
|
}
|
||||||
|
if (changed2) break;
|
||||||
|
else t2x += signx2;
|
||||||
|
}
|
||||||
|
if (minx>t1x) minx = t1x; if (minx>t2x) minx = t2x;
|
||||||
|
if (maxx<t1x) maxx = t1x; if (maxx<t2x) maxx = t2x;
|
||||||
|
drawLine(canvas,minx, maxx, y,col);
|
||||||
|
if (!changed1) t1x += signx1;
|
||||||
|
t1x += t1xp;
|
||||||
|
if (!changed2) t2x += signx2;
|
||||||
|
t2x += t2xp;
|
||||||
|
y += 1;
|
||||||
|
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,Color col) {
|
||||||
DrawLine(canvas,x1,y1,x2,y2,col);
|
DrawLine(canvas,x1,y1,x2,y2,col);
|
||||||
DrawLine(canvas,x2,y2,x3,y3,col);
|
DrawLine(canvas,x2,y2,x3,y3,col);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user