//receipt of b/w color peaks in accordance with its height TGAColor getColorVertex(int x){ if(x<0){ x = 0; } if(x>255){ x = 255; } return TGAColor(x, x, x, 255); }
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; }
#include <vector> #include <cmath> #include "tgaimage.h" #include "model.h" #include "geometry.h" const TGAColor white = TGAColor(255, 255, 255, 255); const TGAColor red = TGAColor(255, 0, 0, 255); Model *model = NULL; const int width = 800; const int height = 800; 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);
TGAColor TGAImage::get(int x, int y) { if (!data || x<0 || y<0 || x>=width || y>=height) { return TGAColor(); } return TGAColor(data+(x+y*width)*bytespp, bytespp); }
TGAColor TGAImage::get(int x, int y) { if (!m_data || x<0 || y<0 || x>=m_width || y>=m_height) { return TGAColor(); } return TGAColor(m_data + index(x, y), m_bytespp); }
virtual bool fragment(Vec3f bar, TGAColor &color) { Vec4f p = varying_tri*bar; color = TGAColor(255, 255, 255)*(p[2]/m_lightDistance); return false; }
#include <vector> #include <cmath> #include "tgaimage.h" #include "model.h" #include "geometry.h" const TGAColor WHITE = TGAColor(255, 255, 255, 255); const TGAColor DGREY = TGAColor( 29, 29, 29, 255); const TGAColor RED = TGAColor(255, 0, 0, 255); Model *model = NULL; const int WIDTH = 1600; const int HEIGHT = 1600; /* void wrong_line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) { for (float i = 0.; i < 1.; i += .01) { int x = x0 * (1. - i) + x1 * i; int y = y0 * (1. - i) + y1 * i; image.set(x, y, color); } std::clog << "Printed" << std::endl; } */ 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)) {
void rendering() { TGAImage image(width, height, TGAImage::RGB); for (int i = 0; i < faces[0].size(); ++i) { Vector3D <int> gridCoordinates[NUMBER]; for (int j = 0; j < 3; ++j) { Vector3D<double> point = vertices[faces[j][i][0]]; gridCoordinates[j] = Vector3D<int> (int((point[0] + 1.) * width * 0.5 + 0.5),int((point[1] + 1.) * height * 0.5 + 0.5), int((point[2] + 1.) * depth * 0.5) + 0.5); } Vector3D<double> normal = (vertices[faces[2][i][0]] - vertices[faces[0][i][0]]) ^ (vertices[faces[1][i][0]] - vertices[faces[0][i][0]]); normal.normalize(); double intensity = normal * light; if (intensity > 0) { /* we can see it */ /*Vector2D<int> uv[NUMBER]; for (int j = 0; j < NUMBER; ++j) { int id = faces[j][i][1]; uv[j] = Vector2D<int> (textureVertices[id][0] * diffusemap.get_width(), textureVertices[id][1] * diffusemap.get_height()); }*/ triangle(gridCoordinates[0], gridCoordinates[1], gridCoordinates[2], image, TGAColor(intensity*255, intensity*255, intensity*255, 255), buffer); } } image.flip_vertically(); image.write_tga_file("picture.tga"); }
#include <algorithm> #include "tgaimage.h" #include "model.h" #include "texture.h" const int width = 800; const int height = 800; const int depth = 255; const Vector3f kLightDirection{ 1.f / 3, 1.f / 3, 1.f / 3 }; //const Vector3f kLightDirection{ 0, 0, 1 }; const Vector3f kEye{ 1, 1, 3 }; const Vector3f kCenter{ 0, 0, 0 }; const Vector3f kUp{ 0, 1, 0 }; const TGAColor red = TGAColor(depth, 0, 0, depth); const TGAColor white = TGAColor(depth, depth, depth, depth); class IShader { public: virtual Matrix43f Vertex(int face_idx) = 0; virtual bool Fragment(Vector3f barycentric_coords, TGAColor& color) = 0; }; class Shader : public IShader { public: Shader(Model& model, Texture& texture, Texture& normal_map, Matrix44f uniform_world, Matrix44f uniform_M) : model_(model), uniform_world_(uniform_world), texture_(texture), normal_map_(normal_map), uniform_M_(uniform_M), uniform_MIT_(uniform_M_.InvertTranspose()) {}