Co-authored-by: sigonasr2 <sigonasr2@gmail.com>

gpu
sigonasr2 2 years ago
parent a0c0adfc6e
commit ef4a529157
  1. 2
      src/sig/JavaProjectTemplate.java
  2. 33
      src/sig/engine/Edge.java
  3. 133
      src/sig/engine/Panel.java

@ -86,6 +86,8 @@ public class JavaProjectTemplate {
game.Draw(100,20,Color.BLACK); //That's not a dead pixel, it's a black one!
game.Draw_Line(10,10,35,35,Color.BLACK); //Line Drawing
game.FillCircle(WINDOW_WIDTH/2, WINDOW_HEIGHT/2, 100, Color.BLACK);
game.Draw_Sprite(450,75,bookSpr,Color.GREEN); //Sprite drawing (with green tint)
game.Draw_Sprite(450,75+bookSpr.getHeight(),bookSpr,Transform.VERTICAL); //Sprite drawing with vertical flip

@ -1,33 +0,0 @@
package sig.engine;
public class Edge {
Point a,b;
int min_y;
int max_y;
int min_x;
int max_x;
double x_of_min_y;
double inverse_slope;
public Edge(Point<Integer> a, Point<Integer> b) {
super();
this.a = a;
this.b = b;
min_y=Math.min(a.y, b.y);
max_y=Math.max(a.y, b.y);
min_x=Math.min(a.x, b.x);
max_x=Math.max(a.x, b.x);
if (a.y==min_y) {
x_of_min_y=a.x;
} else {
x_of_min_y=b.x;
}
inverse_slope=(double)(a.x-b.x)/(a.y-b.y);
}
@Override
public java.lang.String toString() {
return "Edge [a=" + a + ", b=" + b + ", min_y=" + min_y + ", max_y=" + max_y + ", min_x=" + min_x + ", max_x="
+ max_x + ", x_of_min_y=" + x_of_min_y + ", inverse_slope=" + inverse_slope + "]";
}
}

@ -5,7 +5,6 @@ import java.awt.Toolkit;
import java.awt.image.ColorModel;
import java.awt.image.MemoryImageSource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
@ -268,138 +267,46 @@ public class Panel extends JPanel implements Runnable,KeyListener {
}
public void FillCircle(double center_x,double center_y,double r,Color col) {
int counter=0;
List<Point<Integer>> points = new ArrayList<Point<Integer>>();
List<Point<Double>> points = new ArrayList<Point<Double>>();
points.add(new Point<Double>(center_x,center_y));
for (double theta=0;theta<Math.PI*2;theta+=((Math.PI*2)/CIRCLE_PRECISION)) {
//System.out.println("Loop "+counter+++". Theta:"+theta);
//System.out.println("X:"+(Math.sin(theta)*r+center_x)+" Y:"+(Math.cos(theta)*r+center_y));
points.add(new Point<Integer>((int)(Math.round(Math.sin(theta)*r+center_x)),(int)(Math.round(Math.cos(theta)*r+center_y))));
points.add(new Point<Double>((double)(Math.round(Math.sin(theta)*r+center_x)),(double)(Math.round(Math.cos(theta)*r+center_y))));
}
FillPolygon(0,0,col,points);
FillPolygon(0d,0d,col,points,PolygonStructure.FAN);
}
public void FillOval(double center_x,double center_y,double w,double h,Color col) {
int counter=0;
List<Point<Integer>> points = new ArrayList<Point<Integer>>();
List<Point<Double>> points = new ArrayList<Point<Double>>();
double r = Math.max(w,h);
double ratio = Math.min(w,h)/r;
points.add(new Point<Double>(center_x,center_y));
for (double theta=0;theta<Math.PI*2;theta+=((Math.PI*2)/CIRCLE_PRECISION)) {
//System.out.println("Loop "+counter+++". Theta:"+theta);
//System.out.println("X:"+(Math.sin(theta)*r+center_x)+" Y:"+(Math.cos(theta)*r+center_y));
Point<Integer> newP = new Point<Integer>((int)(Math.round(Math.sin(theta)*r)),(int)(Math.round(Math.cos(theta)*r)));
Point<Double> newP = new Point<Double>((double)(Math.round(Math.sin(theta)*r)),(double)(Math.round(Math.cos(theta)*r)));
if (w<h) {
newP.x=(int)Math.round(newP.x*ratio);
newP.x=(double)Math.round(newP.x*ratio);
} else {
newP.y=(int)Math.round(newP.y*ratio);
newP.y=(double)Math.round(newP.y*ratio);
}
newP.x+=(int)center_x;
newP.y+=(int)center_y;
newP.x+=center_x;
newP.y+=center_y;
points.add(newP);
}
FillPolygon(0,0,col,points);
FillPolygon(0d,0d,col,points,PolygonStructure.FAN);
}
public void FillPolygon(double x_offset,double y_offset,Color col,List<Point<Integer>>points) {
Edge[] edges = new Edge[points.size()];
List<Edge> edges_sorted = new ArrayList<Edge>();
for (int i=0;i<points.size();i++) {
edges[i] = new Edge(points.get(i),points.get((i+1)%points.size()));
if (!Double.isInfinite(edges[i].inverse_slope)) {
if (edges_sorted.size()==0) {
edges_sorted.add(edges[i]);
} else {
boolean inserted=false;
for (int j=0;j<edges_sorted.size();j++) {
Edge e2 = edges_sorted.get(j);
if (e2.min_y>=edges[i].min_y) {
edges_sorted.add(j,edges[i]);
inserted=true;
break;
}
}
if (!inserted) {
edges_sorted.add(edges[i]);
}
}
}
}
//System.out.println(edges_sorted);
List<Edge> active_edges = new ArrayList<Edge>();
scanLine = edges_sorted.get(0).min_y-1;
nextScanLine = scanLine+1;
do {
for (int i=0;i<active_edges.size();i+=2) {
if (i>=active_edges.size()-1) break;
Edge e1 = active_edges.get(i);
Edge e2 = active_edges.get(i+1);
//System.out.println("Drawing from "+((int)Math.round(e1.x_of_min_y))+" to "+e2.x_of_min_y+" on line "+scanLine);
for (int x=(int)Math.round(e1.x_of_min_y);x<=e2.x_of_min_y;x++) {
if (x<0||x>JavaProjectTemplate.WINDOW_WIDTH) continue;
int index = (scanLine+(int)y_offset)*getWidth()+x+(int)x_offset;
if (index<pixel.length&&index>=0) {
Draw(index,col.getColor());
}
}
}
List<Edge> new_active_edges = new ArrayList<Edge>();
for (int i=0;i<active_edges.size();i++) {
Edge e = active_edges.get(i);
if (e.max_y==scanLine+1) {
active_edges.remove(i--);
} else {
e.x_of_min_y+=e.inverse_slope;
}
}
scanLine++;
for (int i=0;i<active_edges.size();i++) {
Edge e = active_edges.get(i);
boolean inserted=false;
for (int j=0;j<new_active_edges.size();j++) {
Edge e2 = new_active_edges.get(j);
if (e2.x_of_min_y>e.x_of_min_y) {
new_active_edges.add(j,e);
inserted=true;
break;
}
}
if (!inserted) {
new_active_edges.add(e);
}
}
active_edges=new_active_edges;
GetNextScanLineEdges(edges_sorted, active_edges);
}
while (active_edges.size()>0);
public void FillPolygon(double x_offset,double y_offset,Color col,List<Point<Double>>points,PolygonStructure structure) {
List<Point<Double>> tex = new ArrayList<>();
List<Color> cols = new ArrayList<>();
for (int i=0;i<points.size();i++) {
tex.add(new Point<Double>((double)0,(double)0));
cols.add(col);
}
FillTexturedPolygon(points,tex,cols,null,structure);
}
private void GetNextScanLineEdges(List<Edge> edges_sorted, List<Edge> active_edges) {
if (scanLine==nextScanLine) {
for (int i=0;i<edges_sorted.size();i++) {
Edge e = edges_sorted.get(i);
if (e.min_y==scanLine) {
e = edges_sorted.remove(i--);
boolean inserted=false;
for (int j=0;j<active_edges.size();j++) {
if (e.x_of_min_y<active_edges.get(j).x_of_min_y) {
active_edges.add(j,e);
inserted=true;
break;
}
}
if (!inserted) {
active_edges.add(e);
}
} else
if (e.min_y>scanLine) {
nextScanLine=e.min_y;
break;
}
}
}
}
public void Draw(int x, int y, Color col) {
if (x<0||y<0||x>=JavaProjectTemplate.WINDOW_WIDTH||y>=JavaProjectTemplate.WINDOW_HEIGHT) return;
Draw(y*JavaProjectTemplate.WINDOW_WIDTH+x,col.getColor());

Loading…
Cancel
Save