FillTexturePolygon bugs fixed

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
gpu
sigonasr2 2 years ago
parent f307b36e76
commit 519ce6d10a
  1. 26
      src/sig/JavaProjectTemplate.java
  2. 61
      src/sig/engine/Panel.java
  3. 2
      src/sig/engine/Point.java
  4. 7
      src/sig/engine/PolygonStructure.java

@ -9,6 +9,7 @@ import sig.engine.Panel;
import sig.engine.Point;
import sig.engine.Sprite;
import sig.engine.Transform;
import sig.engine.PolygonStructure;
import java.awt.event.KeyEvent;
import java.util.List;
@ -69,25 +70,28 @@ public class JavaProjectTemplate {
game.Draw_Text(10,40,"Mouse X: "+Mouse.x+" Mouse Y:"+Mouse.y,Font.PROFONT_12); //Draw Mouse coordinates in tiny font
game.Draw_Text_Ext(10,52,"Hello World 2!",Font.PROFONT_36,Color.MAGENTA); //Draw in larger font
game.Fill_Triangle(160,160,190,190,50,250,new Color(255,0,0,150)); //Draw a colored triangle
//game.Fill_Triangle(160,160,190,190,50,250,new Color(255,0,0,150)); //Draw a colored triangle
game.FillTexturedTriangle( //Define the uv tex coords of a triangle and a sprite and draw a texture onto it
game.FillTexturedPolygon( //Define the uv tex coords of a triangle and a sprite and draw a texture onto it
List.of(
new Point<Double>(400d,400d),
new Point<Double>(400d,550d),
new Point<Double>(550d,550d)
new Point<Double>(600d,400d),
new Point<Double>(600d,550d),
new Point<Double>(750d,550d),
new Point<Double>(750d,0d)
),
List.of(
new Point<Double>(0d,0d),
new Point<Double>(0d,1d),
new Point<Double>(1d,1d)
)
,
new Point<Double>(1d,1d),
new Point<Double>(1d,0d)
),
List.of(
new Color(0,0,0,0),
new Color(0,0,0,255),
Color.WHITE,
Color.RED
), bookSpr);
Color.RED,
Color.WHITE
), bookSpr, PolygonStructure.FAN);
}
public static void main(String[] args) {

@ -771,17 +771,16 @@ public class Panel extends JPanel implements Runnable,KeyListener {
public void FillTexturedTriangle(List<Point<Double>>vPoints,List<Point<Double>>vTex,List<Color>vColour,Sprite sprTex){
try {
Point<Double> p1 = vPoints.get(0);
Point<Double> p2 = vPoints.get(1);
Point<Double> p3 = vPoints.get(2);
vColour=new ArrayList<Color>(vColour);
Point<Double> p1 = vPoints.get(0).clone();
Point<Double> p2 = vPoints.get(1).clone();
Point<Double> p3 = vPoints.get(2).clone();
Double tempP;
Color tempC;
if (p2.y < p1.y){tempP=p1.y;p1.y=p2.y;p2.y=tempP;tempP=p1.x;p1.x=p2.x;p2.x=tempP;tempP=vTex.get(0).x;vTex.get(0).x=vTex.get(1).x;vTex.get(1).x=tempP;vTex.get(0).y=vTex.get(1).y;vTex.get(1).y=tempP;tempC=vColour.get(0).clone();vColour.set(0,vColour.get(1));vColour.set(1,tempC);}
if (p3.y < p1.y){tempP=p1.y;p1.y=p3.y;p3.y=tempP;tempP=p1.x;p1.x=p3.x;p3.x=tempP;tempP=vTex.get(0).x;vTex.get(0).x=vTex.get(2).x;vTex.get(2).x=tempP;vTex.get(0).y=vTex.get(2).y;vTex.get(2).y=tempP;tempC=vColour.get(0).clone();vColour.set(0,vColour.get(2));vColour.set(2,tempC);}
if (p3.y < p2.y){tempP=p2.y;p2.y=p3.y;p2.y=tempP;tempP=p2.x;p2.x=p3.x;p3.x=tempP;tempP=vTex.get(1).x;vTex.get(1).x=vTex.get(2).x;vTex.get(2).x=tempP;vTex.get(1).y=vTex.get(2).y;vTex.get(2).y=tempP;tempC=vColour.get(1).clone();vColour.set(1,vColour.get(2));vColour.set(2,tempC);}
if (p2.y < p1.y){tempP=(double)p1.y;p1.y=p2.y;p2.y=tempP;tempP=(double)p1.x;p1.x=p2.x;p2.x=tempP;tempP=vTex.get(0).x;vTex.get(0).x=vTex.get(1).x;vTex.get(1).x=tempP;vTex.get(0).y=vTex.get(1).y;vTex.get(1).y=tempP;tempC=vColour.get(0);vColour.set(0,vColour.get(1));vColour.set(1,tempC);}
if (p3.y < p1.y){tempP=(double)p1.y;p1.y=p3.y;p3.y=tempP;tempP=(double)p1.x;p1.x=p3.x;p3.x=tempP;tempP=vTex.get(0).x;vTex.get(0).x=vTex.get(2).x;vTex.get(2).x=tempP;vTex.get(0).y=vTex.get(2).y;vTex.get(2).y=tempP;tempC=vColour.get(0);vColour.set(0,vColour.get(2));vColour.set(2,tempC);}
if (p3.y < p2.y){tempP=(double)p2.y;p2.y=p3.y;p3.y=tempP;tempP=(double)p2.x;p2.x=p3.x;p3.x=tempP;tempP=vTex.get(1).x;vTex.get(1).x=vTex.get(2).x;vTex.get(2).x=tempP;vTex.get(1).y=vTex.get(2).y;vTex.get(2).y=tempP;tempC=vColour.get(1);vColour.set(1,vColour.get(2));vColour.set(2,tempC);}
Point<Integer> dPos1 = new Point<Integer>((int)(p2.x-p1.x),(int)(p2.y-p1.y));
Point<Integer> dTex1 = new Point<Integer>((int)(vTex.get(1).x-vTex.get(0).x),(int)(vTex.get(1).y-vTex.get(0).y));
int dcr1 = vColour.get(1).r - vColour.get(0).r;
@ -869,7 +868,7 @@ public class Panel extends JPanel implements Runnable,KeyListener {
vColour.get(0).b + (int)((float)(i - p1.y) * dcb2_step), vColour.get(0).a + (int)((float)(i - p1.y) * dca2_step));
int temp;
Point<Float> tempF;
if (ax > bx) {temp=ax;ax=bx;bx=temp;tempF=tex_s.clone();tex_s=tex_e.clone();tex_e=tempF;tempC=col_e;tempC=col_s.clone();col_s=col_e;col_e=tempC;}
if (ax > bx) {temp=ax;ax=bx;bx=temp;tempF=tex_s.clone();tex_s=tex_e.clone();tex_e=tempF;tempC=col_s.clone();col_s=col_e.clone();col_e=tempC;}
float tstep = 1.0f / ((float)(bx - ax));
float t = 0.0f;
@ -968,6 +967,50 @@ public class Panel extends JPanel implements Runnable,KeyListener {
Draw(x, y, col);
}
public void FillTexturedPolygon(List<Point<Double>>vPoints,List<Point<Double>>vTex,List<Color>vColour,Sprite sprTex,PolygonStructure structure) {
try {
if (vPoints.size() < 3 || vTex.size() < 3 || vColour.size() < 3)
return;
if (structure == PolygonStructure.LIST)
{
for (int tri = 0; tri < vPoints.size() / 3; tri++)
{
List<Point<Double>> vP = new ArrayList<Point<Double>>(List.of(vPoints.get(tri * 3 + 0), vPoints.get(tri * 3 + 1), vPoints.get(tri * 3 + 2)));
List<Point<Double>> vT = new ArrayList<Point<Double>>(List.of(vTex.get(tri * 3 + 0), vTex.get(tri * 3 + 1), vTex.get(tri * 3 + 2)));
List<Color> vC = new ArrayList<Color>(List.of(vColour.get(tri * 3 + 0), vColour.get(tri * 3 + 1), vColour.get(tri * 3 + 2)));
FillTexturedTriangle(vP, vT, vC, sprTex);
}
return;
}
if (structure == PolygonStructure.STRIP)
{
for (int tri = 2; tri < vPoints.size(); tri++)
{
List<Point<Double>> vP = new ArrayList<Point<Double>>(List.of(vPoints.get(tri - 2), vPoints.get(tri - 1), vPoints.get(tri)));
List<Point<Double>> vT = new ArrayList<Point<Double>>(List.of(vTex.get(tri - 2), vTex.get(tri - 1), vTex.get(tri)));
List<Color> vC = new ArrayList<Color>(List.of(vColour.get(tri - 2), vColour.get(tri - 1), vColour.get(tri)));
FillTexturedTriangle(vP, vT, vC, sprTex);
}
return;
}
if (structure == PolygonStructure.FAN)
{
for (int tri = 2; tri < vPoints.size(); tri++)
{
List<Point<Double>> vP;
vP = new ArrayList<Point<Double>>(List.of(vPoints.get(0).clone(), vPoints.get(tri - 1).clone(), vPoints.get(tri).clone()));
List<Point<Double>> vT = new ArrayList<Point<Double>>(List.of(vTex.get(0).clone(), vTex.get(tri - 1).clone(), vTex.get(tri).clone()));
List<Color> vC = new ArrayList<Color>(List.of(vColour.get(0).clone(), vColour.get(tri - 1).clone(), vColour.get(tri).clone()));
FillTexturedTriangle(vP, vT, vC, sprTex);
}
return;
}
} catch (CloneNotSupportedException e) {}
}
Color ColorLerp(Color c1, Color c2, float t)
{ return new Color((int)((c2.r * t) + c1.r * (1.0f - t)),(int)((c2.g * t) + c1.g * (1.0f - t)),(int)((c2.b * t) + c1.b * (1.0f - t)),(int)((c2.a * t) + c1.a * (1.0f - t))); }

@ -39,7 +39,7 @@ public class Point<T>{
}
@Override
protected Point<T> clone() throws CloneNotSupportedException {
public Point<T> clone() throws CloneNotSupportedException {
return new Point<T>(x,y);
}
}

@ -0,0 +1,7 @@
package sig.engine;
public enum PolygonStructure{
LIST,
STRIP,
FAN,
}
Loading…
Cancel
Save