Browse Source

三角形绘制优化

master
blobt 5 years ago
parent
commit
9880a0f297
  1. 26
      src/Raster.cc
  2. 22
      src/draw_triangle.cc

26
src/Raster.cc

@ -163,8 +163,16 @@ void Raster::drawSpan(const Span &span) {
Rgba color;
float length = span.xEnd - span.xStart;
for (int i = span.xStart; i <= span.xEnd; i++) {
color = colorLerp(span.color1, span.color2, (float) (i - span.xStart) / length);
int startX = tmax<int>(span.xStart, 0);
int endX = tmin<int>(span.xEnd, _width);
float scale = (startX - span.xStart) / length;
//优化3 x轴越界处理
for (int i = startX; i <= endX; i++) {
color = colorLerp(span.color1, span.color2, (float) (i - startX) / length);
setPixel(i, span.y, color);
}
}
@ -182,16 +190,20 @@ void Raster::drawEge(const Ege& e1, const Ege& e2) {
float scale2 = 0;
//优化2 不画三角形坐标y以为的部分
// int startY = tmin<int>(e2.y2, _height);
// int endY = tmax<int>(e2.y1, 0);
// scale2 = (startY - _height) / yOffset2;
//优化2 y轴越界处理
int startY2 = tmin<int>(e2.y2, _height);
int endY2 = tmax<int>(e2.y1, 0);
scale2 = (e2.y2 - startY2) / yOffset2;
int startY1 = tmin<int>(e1.y2, _height);
float s = (e1.y2 - startY1) / yOffset1;
scale1 = tmax<float>(scale1, s);
//printf("")
Rgba colorE1;
Rgba colorE2;
for (int y = e2.y2; y > e2.y1; y--) {
for (int y = startY2; y > endY2; y--) {
int x2;
if (e2.x2 < e2.x1) {

22
src/draw_triangle.cc

@ -72,17 +72,17 @@ void example3() {
void example4() {
int2 p[3] = {
int2(50, 50),
int2(250, 250),
int2(300, 150)
};
// int2 p[3] = {
// int2(100, 150),
// int2(250, 250),
// int2(350, 50)
// };
// int2 p[3] = {
// int2(-50, 50),
// int2(250, 800),
// int2(300, 150)
// };
int2 p[3] = {
int2(100, 150),
int2(250, 250),
int2(350, 50)
};
Ege e0(p[0].x, p[0].y, p[1].x, p[1].y, Rgba(255, 0, 0, 0), Rgba(0, 255, 0, 0));
Ege e1(p[1].x, p[1].y, p[2].x, p[2].y, Rgba(0, 255, 0, 0), Rgba(0, 0, 255, 0));

Loading…
Cancel
Save