Example #1
1
void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
  bool steep = std::abs(y1-y0) > std::abs(x1-x0) ;
    if(steep){
      std::swap(x0,y0);
      std::swap(x1,y1);
    }
  if(x1<x0){
    std::swap(x0,x1);
    std::swap(y0,y1);
  }
  int dx = x1-x0;
  int dy = y1-y0;
  int derror = std::abs(dy)*2;
  int error = 0;
  int y = y0;
  for (int x = x0; x<x1; x++){
  if(steep){
     image.set(y, x, color);
  }else{
     image.set(x, y, color);
  }
  error += 2*derror;
  if(error > dx){
    y += (y1>y0?1:-1);
    error -= 2*dx;
  }
  }
}
Example #2
0
void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
	bool steep = false;
	if (abs(x0 - x1)<abs(y0 - y1)) {
		swap(x0, y0);
		swap(x1, y1);
		steep = true;
	}
	if (x0>x1) {
		swap(x0, x1);
		swap(y0, y1);
	}
	int dx = x1 - x0;
	int dy = y1 - y0;
	int derror2 = abs(dy) * 2;
	int error2 = 0;
	int y = y0;
	for (int x = x0; x <= x1; x++) {
		if (steep) {
			image.set(y, x, color);
		}
		else {
			image.set(x, y, color);
		}
		error2 += derror2;
		if (error2 > dx) {
			y += (y1>y0 ? 1 : -1);
			error2 -= dx * 2;
		}
	}
}
Example #3
0
void line(int x0, int y0, int x1, int y1, const TGAColor & color, TGAImage & img) {
	bool steep = false;

	if (abs(x0 - x1) < abs(y0 - y1)){
		steep = true;
		swap(x0, y0);
		swap(x1, y1);
	}

	if (x0 > x1){
		swap(x0, x1);
		swap(y0, y1);
	}

	double ratio = (y1 - y0) / (float) (x1 - x0);

	for (int x = 0; x <= x1 - x0; ++x){
		int y = ratio * x;
		if (!steep){
			img.set(x + x0, y + y0, color);
		} else {
			img.set(y + y0, x + x0, color);
		}
	}
}
Example #4
0
void line(int x0, int y0, int x1, int y1, TGAImage& image, const TGAColor& color) {
    bool steep = false;
    if (std::abs(x0 - x1) < std::abs(y0 - y1)) { //transpose if steep line
        std::swap(x0, y0);
        std::swap(x1, y1);
        steep = true;
    }
    if (x0 > x1) { //make it left to right
        std::swap(x0, x1);
        std::swap(y0, y1);
    }
    int dx = x1 - x0;
    int dy = y1 - y0;
    float derror = std::abs(dy) * 2, error = 0;
    int y = y0;
    for (int x = x0; x <= x1; x++) {
        if (steep) {
            image.set(y, x, color);
        } else {
            image.set(x, y, color);
        }
        error += derror;
        if (error > dx * 2) {
            y += y1 > y0 ? 1 : -1;
            error -= dx * 2;
        }
    }
}
Example #5
0
 void triangle(Vector3D<int> A, Vector3D<int> B, Vector3D<int> C,
               TGAImage &image, TGAColor colour, int *buffer) {
     if (A[1] == B[1] && A[1] == C[1]) return;
     if (A[1]>B[1]) {
         std::swap(A, B);
     }
     if (A[1]>C[1]) {
         std::swap(A, C);
     }
     if (B[1]>C[1]) {
         std::swap(B, C);
     }
     int level = C[1] - A[1];
     for (int i = 0; i < level; ++i) {
         bool half = i > B[1] - A[1] || B[1] == A[1]; // part of triangle
         int segment = half ? C[1] - B[1] : B[1] - A[1];
         double alpha = (double)i/level;
         double beta  = (double)(i-(half ? B[1]-A[1] : 0))/segment;
         Vector3D<int> a = A + (Vector3D<double>(C - A) * alpha);
         Vector3D<int> b = half ? B  + Vector3D<double>(C - B) * beta : A  + Vector3D<double>(B - A)*beta;
         if (a[0] > b[0]) {
             std::swap(a, b);
         }
         for (int j=a[0]; j <= b[0]; j++) {
             double phi = (b[0] == a[0]) ? 1. : (double)(j - a[0])/(double)(b[0] - a[0]);
             Vector3D<int> p = Vector3D<double>(a) + Vector3D<double>(b - a) * phi;
             int id = p[0] + p[1] * width;
             if (buffer[id] < p[2]) {
                 buffer[id] = p[2];
                 image.set(p[0], p[1], colour);
             }
         }
     }
 }
	void clearBuffers(TGAColor& clearColor)
	{
		for (int y = 0; y < m_swHeight; ++y)
		{
			for (int x = 0; x < m_swWidth; ++x)
			{
				m_rgbColorBuffer.set(x, y, clearColor);
				m_depthBuffer[x + y * m_swWidth] = -1e30f;
			}
		}
	}
Example #7
0
void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
    bool rot = false;
    if (std::abs(y1 - y0) < std::abs(x1 - x0)) {
        std::swap(y0, x0);
        std::swap(y1, x1);
        rot = true;
    }
    if (y0 > y1) {
        std::swap(y0, y1);
        std::swap(x0, x1);
    }
    for (int y = y0; y <= y1; y++) {
        float t = (y - y0)*1./(y1 - y0);
        int x = x0*(1.-t) + x1*t;
        if (rot) {
            image.set(y, x, color);
        } else {
            image.set(x, y, color);
        } 
    }
}
Example #8
0
void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
    bool steep = false;
    if (std::abs(x0-x1)<std::abs(y0-y1)) {
        std::swap(x0, y0);
        std::swap(x1, y1);
        steep = true;
    }
    if (x0>x1) {
        std::swap(x0, x1);
        std::swap(y0, y1);
    }

    for (int x=x0; x<=x1; x++) {
        float t = (x-x0)/(float)(x1-x0);
        int y = y0*(1.-t) + y1*t;
        if (steep) {
            image.set(y, x, color);
        } else {
            image.set(x, y, color);
        }
    }
}
Example #9
0
void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
    bool steep = false;

    /* If the line is steep, we transpose the image. */
    if (std::abs(x0 - x1) < std::abs(y0 - y1)) {
        std::swap(x0, y0);
        std::swap(x1, y1);
        steep = true;
    }

    /* Make it left-to-right. */
    if (x0 > x1) {
        std::swap(x0, x1);
        std::swap(y0, y1);
    }

    int dx = x1 - x0;
    int dy = y1 - y0;
    int derror2 = std::abs(dy) * 2;
    int  error2 = 0;
    int y = y0;

    for (int x = x0; x <= x1; x++) {
        if (steep) {
            image.set(y, x, color); // if transposed, de-transpose
        } else {
            image.set(x, y, color);
        }
        error2 += derror2;

        if (error2 > dx) {
            y += (y1 > y0 ? 1 : -1);
            error2 -= dx * 2;
        }
    }

}
Example #10
0
void line(int x0, int y0, int x1, int y1, TGAImage &img, TGAColor color) {

	int deltax = abs(x1 - x0);
	int deltay = abs(y1 - y0);
	bool flag = deltay > deltax;
	int a, b, border, error = 0, delta, deltaerr, sa, sb;

	if (flag) {
		a = y0;
		b = x0;
		border = y1;
		delta = deltay;
		deltaerr = deltax;
		sa = y1 > y0 ? 1 : -1;
		sb = x1 > x0 ? 1 : -1;
	}
	else {
		a = x0;
		b = y0;
		border = x1;
		delta = deltax;
		deltaerr = deltay;
		sa = x1 > x0 ? 1 : -1;
		sb = y1 > y0 ? 1 : -1;
	}

	while (a != border + sa) {
		flag ? img.set(b, a, color) : img.set(a, b, color);
		error = error + deltaerr;
		if (2 * error >= delta) {
			b = b += sb;
			error = error - delta;
		}
		a += sa;
	}
}
Example #11
0
void triangle(Vec3i t0, Vec3i t1, Vec3i t2, Vec2i uv0, Vec2i uv1, Vec2i uv2,
              float intensity, int* zbuf, TGAImage& image) {
    if (isDegenerated(t0,t1,t2)) return;
    if (t0.y > t1.y) {
        std::swap(t0, t1);
        std::swap(uv0, uv1);
    }
    if (t0.y > t2.y) {
        std::swap(t0, t2);
        std::swap(uv0, uv2);
    }
    if (t1.y > t2.y) {
        std::swap(t1, t2);
        std::swap(uv1, uv2);
    }
    int* boundbox = getBoundBox(t0, t1, t2);
    Vec2i vertex(t1.x - t0.x, t1.y - t0.y);
    Vec2i tmpUv(uv1.x - uv0.x, uv1.y - uv0.y);
    for (int x = boundbox[0]; x <= boundbox[2]; x++) {
        for (int y = boundbox[1]; y <= boundbox[3]; y++) {
            if (insideTriangle(x, y, t0, t1, t2)) {
                int idx = x + y * width;
                int z = find_z(x, y, t0, t1, t2);
                Vec2i P(x, y), v(t0.x, t0.y);
                Vec2i s01(t1.x - t0.x, t1.y - t0.y), s02(t2.x - t0.x, t2.y - t0.y), sp0(t0.x - P.x, t0.y - P.y);
                Vec3i tmp1(s01.x, s02.x, sp0.x), tmp2(s01.y, s02.y, sp0.y);
                Vec3i tmp3 = tmp1 ^ tmp2;
                Vec3f res(tmp3.x, tmp3.y, tmp3.z);
                if (res.z != 0) {
                    res = res * (1 / res.z);
                } else {
                    continue;
                }
                Vec2f uvP =  uv0 * (1 - res.x - res.y) + uv1 * res.x + uv2 * res.y;
                if (zbuf[idx] < z) {
                    zbuf[idx] =  z;
                    TGAColor color = model->getDiffusive(uvP);
                    image.set(x, y, TGAColor(color.r * intensity , color.g * intensity , color.b * intensity , 255));
                }
            }
        }
    }
    delete[] boundbox;
}