-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderContext.java
More file actions
95 lines (80 loc) · 2.68 KB
/
RenderContext.java
File metadata and controls
95 lines (80 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* RenderContext
*/
public class RenderContext extends Bitmap {
private final int m_scanBuffer[]; //filling with x min and x max with y axis
byte white = (byte) 0xFF;
// public final Vertex y_min = new Vertex(100.0f,200.0f);
// public final Vertex y_mid = new Vertex(50.0f,400.0f);
// public final Vertex y_max = new Vertex(200.0f,600.0f);
public RenderContext(int width, int height)
{
super(width, height);//goes to bitmap.
m_scanBuffer = new int[height*2];
}
public void DrawScanBuffer(int yCoord, int xMin, int XMax)
{
m_scanBuffer[yCoord*2] =xMin;
m_scanBuffer[yCoord*2 + 1] =XMax;
}
public void drawEdges(vertex y_min,vertex y_max,boolean whichside)
{
float xstep = (y_max.getY()-y_min.getY())/(y_max.getX() -y_min.getX());
float C = y_min.getY() -(y_min.getX()*xstep);
for(int x= (int)y_min.getY();x<=(int)y_max.getY();x++)
{
int xMin = (int)(((float)x -C)/xstep);
if (whichside) {//xmax
m_scanBuffer[x*2 + 1] =xMin;
// System.out.println("xstep is" +xstep + " xmax is"+xMin+" for y "+x);
}
else
{ //xmin
m_scanBuffer[x*2] =xMin;
// System.out.println("xstep is" +xstep+" xmin is"+xMin+" for y "+x);
}
}
}
public void drawTriangle(vertex y_min,vertex y_max,vertex y_mid)
{
vertex temp ;
if (y_max.getY()<y_min.getY()) {
temp = y_max;
y_max = y_min;
y_min = temp;
}
if (y_max.getY()<y_mid.getY()) {
temp = y_max;
y_max = y_mid;
y_mid = temp;
}
if (y_mid.getY()<y_min.getY()) {
temp = y_mid;
y_mid = y_min;
y_min = temp;
}
if (y_min.triangleArea(y_mid, y_max)> 0) {
drawEdges(y_min, y_max, false);
drawEdges(y_min, y_mid, true);
drawEdges(y_mid, y_max, true);
} else {
drawEdges(y_min, y_max, true);
drawEdges(y_min, y_mid, false);
drawEdges(y_mid, y_max, false);
}
fillShape((int)y_min.getY(), (int)y_max.getY());
}
public void fillShape(int yMin, int yMax)
{
for(int j =yMin;j<yMax;j++)
{
int xMin = m_scanBuffer[j*2];
int xMax = m_scanBuffer[j*2 + 1];
// System.out.println(xMin);
for(int i=xMin;i<xMax;i++)
{
drawPixel(i, j, white, white, white, white);
}
}
}
}