generated from sigonasr2/JavaProjectTemplate
FillTexturedTriangle implemented
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
59eb833694
commit
61814de77e
@ -5,10 +5,12 @@ import sig.engine.Color;
|
|||||||
import sig.engine.Font;
|
import sig.engine.Font;
|
||||||
import sig.engine.Key;
|
import sig.engine.Key;
|
||||||
import sig.engine.Panel;
|
import sig.engine.Panel;
|
||||||
|
import sig.engine.Point;
|
||||||
import sig.engine.Sprite;
|
import sig.engine.Sprite;
|
||||||
import sig.engine.Transform;
|
import sig.engine.Transform;
|
||||||
|
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
class Player{
|
class Player{
|
||||||
AnimatedSprite spr=new AnimatedSprite("player.png",32,32);
|
AnimatedSprite spr=new AnimatedSprite("player.png",32,32);
|
||||||
@ -63,10 +65,28 @@ public class JavaProjectTemplate {
|
|||||||
|
|
||||||
game.Draw_Animated_Sprite(pl.x,pl.y,pl.spr,pl.animationFrame);
|
game.Draw_Animated_Sprite(pl.x,pl.y,pl.spr,pl.animationFrame);
|
||||||
|
|
||||||
game.Draw_Text(10,40,new sig.engine.String("Hello World!"),Font.PROFONT_12);
|
game.Draw_Text(10,40,"Hello World!",Font.PROFONT_12);
|
||||||
game.Draw_Text_Ext(10,52,new sig.engine.String("Hello World 2!"),Font.PROFONT_36,Color.MAGENTA);
|
game.Draw_Text_Ext(10,52,"Hello World 2!",Font.PROFONT_36,Color.MAGENTA);
|
||||||
|
|
||||||
game.Fill_Triangle(160,160,190,190,50,250,Color.RED);
|
game.Fill_Triangle(160,160,190,190,50,250,Color.RED);
|
||||||
|
|
||||||
|
game.FillTexturedTriangle(
|
||||||
|
List.of(
|
||||||
|
new Point<Double>(400d,400d),
|
||||||
|
new Point<Double>(400d,550d),
|
||||||
|
new Point<Double>(550d,550d)
|
||||||
|
),
|
||||||
|
List.of(
|
||||||
|
new Point<Double>(0d,0d),
|
||||||
|
new Point<Double>(0d,1d),
|
||||||
|
new Point<Double>(1d,1d)
|
||||||
|
)
|
||||||
|
,
|
||||||
|
List.of(
|
||||||
|
Color.BLUE,
|
||||||
|
Color.BLUE,
|
||||||
|
Color.BLUE
|
||||||
|
), bookSpr);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -23,6 +23,12 @@ public class Color {
|
|||||||
public Color(int r, int g, int b) {
|
public Color(int r, int g, int b) {
|
||||||
this(r,g,b,255);
|
this(r,g,b,255);
|
||||||
}
|
}
|
||||||
|
public Color(float r, float g, float b) {
|
||||||
|
this((int)(r*255),(int)(g*255),(int)(b*255),255);
|
||||||
|
}
|
||||||
|
public Color(float r, float g, float b,float a) {
|
||||||
|
this((int)(r*255),(int)(g*255),(int)(b*255),(int)(a*255));
|
||||||
|
}
|
||||||
|
|
||||||
public Color(int r, int g, int b,int a) {
|
public Color(int r, int g, int b,int a) {
|
||||||
super();
|
super();
|
||||||
@ -32,6 +38,10 @@ public class Color {
|
|||||||
this.a = a;
|
this.a = a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Color(int rgb) {
|
||||||
|
this((rgb>>>16)&0xff,(rgb>>>8)&0xff,rgb&0xff,(rgb>>>24)&0xff);
|
||||||
|
}
|
||||||
|
|
||||||
public int getColor() {
|
public int getColor() {
|
||||||
return (a<<24)+(r<<16)+(g<<8)+b;
|
return (a<<24)+(r<<16)+(g<<8)+b;
|
||||||
}
|
}
|
||||||
@ -41,5 +51,10 @@ public class Color {
|
|||||||
return "Color [r=" + r + ", g=" + g + ", b=" + b + ", a=" + a + "]";
|
return "Color [r=" + r + ", g=" + g + ", b=" + b + ", a=" + a + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Color clone() throws CloneNotSupportedException {
|
||||||
|
return new Color(r,g,b,a);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -445,11 +445,19 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
|||||||
//System.out.println("Key List: "+KEYS);
|
//System.out.println("Key List: "+KEYS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Draw_Text(double x, double y, String s, Font f) {
|
public void Draw_Text(double x, double y, java.lang.String s, Font f){
|
||||||
|
Draw_Text(x,y,new String(s),f);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Draw_Text_Ext(double x, double y, java.lang.String s, Font f, Color col){
|
||||||
|
Draw_Text_Ext(x,y,new String(s),f,col);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Draw_Text(double x, double y, String s, Font f) {
|
||||||
Draw_Text_Ext(x,y,s,f,Color.BLACK);
|
Draw_Text_Ext(x,y,s,f,Color.BLACK);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Draw_Text_Ext(double x, double y, String s, Font f, Color col) {
|
private void Draw_Text_Ext(double x, double y, String s, Font f, Color col) {
|
||||||
java.lang.String finalS = s.toString();
|
java.lang.String finalS = s.toString();
|
||||||
int charCount=0;
|
int charCount=0;
|
||||||
int yOffset=0;
|
int yOffset=0;
|
||||||
@ -855,6 +863,136 @@ 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);
|
||||||
|
|
||||||
|
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);}
|
||||||
|
|
||||||
|
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;
|
||||||
|
int dcg1 = vColour.get(1).g - vColour.get(0).g;
|
||||||
|
int dcb1 = vColour.get(1).b - vColour.get(0).b;
|
||||||
|
int dca1 = vColour.get(1).a - vColour.get(0).a;
|
||||||
|
|
||||||
|
Point<Integer> dPos2 = new Point<Integer>((int)(p3.x-p1.x),(int)(p3.y-p1.y));
|
||||||
|
Point<Integer> dTex2 = new Point<Integer>((int)(vTex.get(2).x-vTex.get(0).x),(int)(vTex.get(2).y-vTex.get(0).y));
|
||||||
|
int dcr2 = vColour.get(2).r - vColour.get(0).r;
|
||||||
|
int dcg2 = vColour.get(2).g - vColour.get(0).g;
|
||||||
|
int dcb2 = vColour.get(2).b - vColour.get(0).b;
|
||||||
|
int dca2 = vColour.get(2).a - vColour.get(0).a;
|
||||||
|
|
||||||
|
float dax_step = 0, dbx_step = 0, dcr1_step = 0, dcr2_step = 0, dcg1_step = 0, dcg2_step = 0, dcb1_step = 0, dcb2_step = 0, dca1_step = 0, dca2_step = 0;
|
||||||
|
Point<Float> vTex1Step=null, vTex2Step=null;
|
||||||
|
|
||||||
|
if (dPos1.y!=0)
|
||||||
|
{
|
||||||
|
dax_step = dPos1.x / (float)Math.abs(dPos1.y);
|
||||||
|
vTex1Step = new Point<Float>(dTex1.x / (float)Math.abs(dPos1.y),dTex1.y / (float)Math.abs(dPos1.y));
|
||||||
|
dcr1_step = dcr1 / (float)Math.abs(dPos1.y);
|
||||||
|
dcg1_step = dcg1 / (float)Math.abs(dPos1.y);
|
||||||
|
dcb1_step = dcb1 / (float)Math.abs(dPos1.y);
|
||||||
|
dca1_step = dca1 / (float)Math.abs(dPos1.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dPos2.y!=0)
|
||||||
|
{
|
||||||
|
dbx_step = dPos2.x / (float)Math.abs(dPos2.y);
|
||||||
|
vTex2Step = new Point<Float>(dTex2.x / (float)Math.abs(dPos2.y),dTex2.y / (float)Math.abs(dPos2.y));
|
||||||
|
dcr2_step = dcr2 / (float)Math.abs(dPos2.y);
|
||||||
|
dcg2_step = dcg2 / (float)Math.abs(dPos2.y);
|
||||||
|
dcb2_step = dcb2 / (float)Math.abs(dPos2.y);
|
||||||
|
dca2_step = dca2 / (float)Math.abs(dPos2.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
Point<Double> vStart;
|
||||||
|
Point<Double> vEnd;
|
||||||
|
int vStartIdx;
|
||||||
|
|
||||||
|
for (int pass = 0; pass < 2; pass++)
|
||||||
|
{
|
||||||
|
if (pass == 0)
|
||||||
|
{
|
||||||
|
vStart = p1; vEnd = p2; vStartIdx = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dPos1 = new Point<Integer>((int)(p3.x-p2.x),(int)(p3.y-p2.y));
|
||||||
|
dTex1 = new Point<Integer>((int)(vTex.get(2).x-vTex.get(1).x),(int)(vTex.get(2).y-vTex.get(1).y));
|
||||||
|
dcr1 = vColour.get(2).r - vColour.get(1).r;
|
||||||
|
dcg1 = vColour.get(2).g - vColour.get(1).g;
|
||||||
|
dcb1 = vColour.get(2).b - vColour.get(1).b;
|
||||||
|
dca1 = vColour.get(2).a - vColour.get(1).a;
|
||||||
|
dcr1_step = 0; dcg1_step = 0; dcb1_step = 0; dca1_step = 0;
|
||||||
|
|
||||||
|
if (dPos2.y!=0) dbx_step = dPos2.x / (float)Math.abs(dPos2.y);
|
||||||
|
if (dPos1.y!=0)
|
||||||
|
{
|
||||||
|
dax_step = dPos1.x / (float)Math.abs(dPos1.y);
|
||||||
|
vTex1Step = new Point<Float>(dTex1.x/(float)Math.abs(dPos1.y),dTex1.y/(float)Math.abs(dPos1.y));
|
||||||
|
dcr1_step = dcr1 / (float)Math.abs(dPos1.y);
|
||||||
|
dcg1_step = dcg1 / (float)Math.abs(dPos1.y);
|
||||||
|
dcb1_step = dcb1 / (float)Math.abs(dPos1.y);
|
||||||
|
dca1_step = dca1 / (float)Math.abs(dPos1.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
vStart = p2; vEnd = p3; vStartIdx = 1;
|
||||||
|
}
|
||||||
|
if (dPos1.y!=0)
|
||||||
|
{
|
||||||
|
for (int i = (int)Math.floor(vStart.y); i <= vEnd.y; i++)
|
||||||
|
{
|
||||||
|
int ax = (int)(vStart.x + (float)(i - vStart.y) * dax_step);
|
||||||
|
int bx = (int)(p1.x + (float)(i - p1.y) * dbx_step);
|
||||||
|
|
||||||
|
Point<Float> tex_s = new Point<Float>((float)(vTex.get(vStartIdx).x + (float)(i - vStart.y) * vTex1Step.x),(float)(vTex.get(vStartIdx).y + (float)(i - vStart.y) * vTex1Step.y));
|
||||||
|
Point<Float> tex_e = new Point<Float>((float)(vTex.get(0).x + (float)(i - p1.y) * vTex2Step.x),(float)(vTex.get(0).y + (float)(i - p1.y) * vTex2Step.y));
|
||||||
|
|
||||||
|
Color col_s= new Color(vColour.get(vStartIdx).r + (int)((float)(i - vStart.y) * dcr1_step), vColour.get(vStartIdx).g + (int)((float)(i - vStart.y) * dcg1_step),
|
||||||
|
vColour.get(vStartIdx).b + (int)((float)(i - vStart.y) * dcb1_step), vColour.get(vStartIdx).a + (int)((float)(i - vStart.y) * dca1_step));
|
||||||
|
|
||||||
|
Color col_e= new Color(vColour.get(0).r + (int)((float)(i - p1.y) * dcr2_step), vColour.get(0).g + (int)((float)(i - p1.y) * dcg2_step),
|
||||||
|
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;}
|
||||||
|
|
||||||
|
float tstep = 1.0f / ((float)(bx - ax));
|
||||||
|
float t = 0.0f;
|
||||||
|
for (int j = ax; j < bx; j++)
|
||||||
|
{
|
||||||
|
Color pixel = ColorLerp(col_s, col_e, t);
|
||||||
|
if (sprTex != null) {
|
||||||
|
Point<Float> lerpComponents = new Point<Float>(
|
||||||
|
tex_s.x*(1.0f-t)+tex_e.x*t,
|
||||||
|
tex_s.y*(1.0f-t)+tex_e.y*t
|
||||||
|
);
|
||||||
|
Color c=new Color(sprTex.getImg().getRGB((int)Math.min(lerpComponents.x*sprTex.width,sprTex.width-1),(int)Math.min(lerpComponents.y*sprTex.height,sprTex.height-1)));
|
||||||
|
pixel.r = (int)Math.min(255.0f,Math.max(0,(float)pixel.r * c.r / 255.0f));
|
||||||
|
pixel.g = (int)Math.min(255.0f,Math.max(0,(float)pixel.g * c.g / 255.0f));
|
||||||
|
pixel.b = (int)Math.min(255.0f,Math.max(0,(float)pixel.b * c.b / 255.0f));
|
||||||
|
pixel.a = (int)Math.min(255.0f,Math.max(0,(float)pixel.a * c.a / 255.0f));
|
||||||
|
}
|
||||||
|
Draw(j, i, pixel);
|
||||||
|
t += tstep;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} 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))); }
|
||||||
|
|
||||||
|
|
||||||
public void Clear(Color col){
|
public void Clear(Color col){
|
||||||
for (int y=0;y<JavaProjectTemplate.WINDOW_HEIGHT;y++) {
|
for (int y=0;y<JavaProjectTemplate.WINDOW_HEIGHT;y++) {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package sig.engine;
|
package sig.engine;
|
||||||
|
|
||||||
public class Point<T>{
|
public class Point<T>{
|
||||||
T x,y;
|
public T x,y;
|
||||||
|
|
||||||
public Point(T x, T y) {
|
public Point(T x, T y) {
|
||||||
super();
|
super();
|
||||||
@ -37,4 +37,9 @@ public class Point<T>{
|
|||||||
public java.lang.String toString() {
|
public java.lang.String toString() {
|
||||||
return "Point(" + x + "," + y + ")";
|
return "Point(" + x + "," + y + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Point<T> clone() throws CloneNotSupportedException {
|
||||||
|
return new Point<T>(x,y);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user