Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void drawTriangles(
- float* vertices, float* colors_, float* uv_, uint32_t* indices, int triangle_count,
- const gfxm::mat4& proj, const gfxm::mat4& view, const gfxm::mat4& model
- ) {
- int dither_table[64] = {
- 0, 1, 0, 1, 0, 1, 0, 1,
- 1, 0, 1, 0, 1, 0, 1, 0,
- 0, 1, 0, 1, 0, 1, 0, 1,
- 1, 0, 1, 0, 1, 0, 1, 0,
- 0, 1, 0, 1, 0, 1, 0, 1,
- 1, 0, 1, 0, 1, 0, 1, 0,
- 0, 1, 0, 1, 0, 1, 0, 1,
- 1, 0, 1, 0, 1, 0, 1, 0,
- };
- uint32_t dither_table2[64] = {
- 0x00000000, 0x00101010, 0x00000000, 0x00101010, 0x00000000, 0x00101010, 0x00000000, 0x00101010,
- 0x00101010, 0x00000000, 0x00101010, 0x00000000, 0x00101010, 0x00000000, 0x00101010, 0x00000000,
- 0x00000000, 0x00101010, 0x00000000, 0x00101010, 0x00000000, 0x00101010, 0x00000000, 0x00101010,
- 0x00101010, 0x00000000, 0x00101010, 0x00000000, 0x00101010, 0x00000000, 0x00101010, 0x00000000,
- 0x00000000, 0x00101010, 0x00000000, 0x00101010, 0x00000000, 0x00101010, 0x00000000, 0x00101010,
- 0x00101010, 0x00000000, 0x00101010, 0x00000000, 0x00101010, 0x00000000, 0x00101010, 0x00000000,
- 0x00000000, 0x00101010, 0x00000000, 0x00101010, 0x00000000, 0x00101010, 0x00000000, 0x00101010,
- 0x00101010, 0x00000000, 0x00101010, 0x00000000, 0x00101010, 0x00000000, 0x00101010, 0x00000000
- };
- const gfxm::mat4 transform = proj * view * model;
- for (int ti = 0; ti < triangle_count; ++ti) {
- //float* triangle = vertices + ti * 3 * 3;
- //float* colors = colors_ + ti * 3 * 3;
- float triangle[12] = {
- vertices[indices[ti * 3] * 3], vertices[indices[ti * 3] * 3 + 1], vertices[indices[ti * 3] * 3 + 2], 1.f,
- vertices[indices[ti * 3 + 1] * 3], vertices[indices[ti * 3 + 1] * 3 + 1], vertices[indices[ti * 3 + 1] * 3 + 2], 1.f,
- vertices[indices[ti * 3 + 2] * 3], vertices[indices[ti * 3 + 2] * 3 + 1], vertices[indices[ti * 3 + 2] * 3 + 2], 1.f
- };
- float colors[9] = {
- colors_[indices[ti * 3] * 3], colors_[indices[ti * 3] * 3 + 1], colors_[indices[ti * 3] * 3 + 2],
- colors_[indices[ti * 3 + 1] * 3], colors_[indices[ti * 3 + 1] * 3 + 1], colors_[indices[ti * 3 + 1] * 3 + 2],
- colors_[indices[ti * 3 + 2] * 3], colors_[indices[ti * 3 + 2] * 3 + 1], colors_[indices[ti * 3 + 2] * 3 + 2]
- };
- float uv[6] = {
- uv_[indices[ti * 3] * 2], uv_[indices[ti * 3] * 2 + 1],
- uv_[indices[ti * 3 + 1] * 2], uv_[indices[ti * 3 + 1] * 2 + 1],
- uv_[indices[ti * 3 + 2] * 2], uv_[indices[ti * 3 + 2] * 2 + 1]
- };
- for (int i = 0; i < 3; ++i) {
- gfxm::vec4 p = transform * gfxm::vec4(triangle[i * 4], triangle[i * 4 + 1], triangle[i * 4 + 2], 1.f);
- triangle[i * 4] = p.x;
- triangle[i * 4 + 1] = p.y;
- triangle[i * 4 + 2] = p.z;
- triangle[i * 4 + 3] = 1.f / p.w;
- }
- float tri[9] = {
- .0f, .0f, .0f,
- .0f, .0f, .0f,
- .0f, .0f, .0f
- };
- for (int i = 0; i < 3; ++i) {
- int bi = i * 3;
- int sbi = i * 4;
- tri[bi] = (triangle[sbi] + 1) * (sdGetCanvasWidth() * .5f) + 0;
- tri[bi + 1] = (triangle[sbi + 1] + 1) * (sdGetCanvasHeight() * .5f) + 0;
- tri[bi + 2] = (triangle[sbi + 2] + 1) * triangle[sbi + 3] - 1.f;
- //tri[bi + 2] = (triangle[bi + 2] + 1) * (zfar - znear) * .5f + znear;
- }
- // TODO: Check if triangle is facing the camera
- // discard early if not
- // use cross product for this
- int minx = int(tri[0]);
- int miny = int(tri[1]);
- int maxx = minx;
- int maxy = miny;
- for (int i = 1; i < 3; ++i) {
- minx = (int)std::fmin(minx, tri[i * 3]);
- maxx = (int)std::fmax(maxx, tri[i * 3]);
- miny = (int)std::fmin(miny, tri[i * 3 + 1]);
- maxy = (int)std::fmax(maxy, tri[i * 3 + 1]);
- }
- minx = std::max(0, minx);
- maxx = std::min(sdGetCanvasWidth() - 1, maxx);
- miny = std::max(0, miny);
- maxy = std::min(sdGetCanvasHeight() - 1, maxy);
- const float ABx = tri[3] - tri[0];
- const float ABy = tri[4] - tri[1];
- const float BCx = tri[6] - tri[3];
- const float BCy = tri[7] - tri[4];
- const float CAx = tri[0] - tri[6];
- const float CAy = tri[1] - tri[7];
- const float tri_area = 2.f * triangle_area(
- tri[0], tri[1],
- tri[3], tri[4],
- tri[6], tri[7]
- );
- float sign = 1.f;
- float tcross = (ABx * BCy) - (ABy * BCx);
- if (tcross <= .0f) {
- continue;
- }
- if (tcross < .0f) {
- sign = -sign;
- }
- for (int y = miny; y <= maxy; ++y) {
- for (int x = minx; x <= maxx; ++x) {
- const float APx = x - tri[0];
- const float APy = y - tri[1];
- const float BPx = x - tri[3];
- const float BPy = y - tri[4];
- const float CPx = x - tri[6];
- const float CPy = y - tri[7];
- const float crossA = ((ABx * APy) - (ABy * APx)) * sign;
- const float crossB = ((BCx * BPy) - (BCy * BPx)) * sign;
- const float crossC = ((CAx * CPy) - (CAy * CPx)) * sign;
- if (crossA >= 0 && crossB >= 0 && crossC >= 0) {
- uint32_t dith = dither_table[((x & 0b111) + (y & 0b111) * 8)];
- uint32_t dith2 = dither_table2[((x & 0b111) + (y & 0b111) * 8)];
- const float wA = (crossB) / tri_area;
- const float wB = (crossC) / tri_area;
- const float wC = (crossA) / tri_area;
- float src_depth = (
- tri[2] * wA + tri[5] * wB + tri[8] * wC
- );
- uint32_t isrc_depth = (uint32_t)((src_depth + 1.f) * .5f * 255.f);
- {
- // Check depth
- uint32_t bb = sdGetBackBuffer()[x + y * sdGetCanvasWidth()];
- uint32_t idst_depth = (bb & 0xFF000000) >> 24;
- if (isrc_depth > idst_depth) {
- continue;
- }
- }
- float tuv[2] = {
- uv[0] * wA + uv[2] * wB + uv[4] * wC,
- uv[1] * wA + uv[3] * wB + uv[5] * wC
- };
- uint32_t texel = sampleTextureNearest(current_texture, tuv[0], tuv[1]);
- // Flip RGB to BGR and replace alpha with depth
- texel = (isrc_depth << 24)
- | ((texel & 0x00FF0000) >> 16)
- | (texel & 0x0000FF00)
- | ((texel & 0x000000FF) << 16);
- /*
- texel =
- (isrc_depth << 24)
- | (((((texel & 0x00FF0000) >> 16) / 32 + dith) * 32))
- | (((((texel & 0x0000FF00) >> 8) / 32 + dith) * 32) << 8)
- | ((((texel & 0x000000FF) / 32 + dith) * 32) << 16);*/
- float fcolor[3] = {
- colors[0] * wA + colors[3] * wB + colors[6] * wC,
- colors[1] * wA + colors[4] * wB + colors[7] * wC,
- colors[2] * wA + colors[5] * wB + colors[8] * wC
- };
- uint32_t c =
- (isrc_depth << 24)
- | (int((fcolor[0] + dith * .07f) * 7.f) * 32 << 16)
- | (int((fcolor[1] + dith * .07f) * 7.f) * 32 << 8)
- | int((fcolor[2] + dith * .07f) * 7.f) * 32;
- /*
- uint32_t c =
- 0xFF000000
- | (int(fcolor[0] * 255.f) << 16)
- | (int(fcolor[1] * 255.f) << 8)
- | int(fcolor[2] * 255.f);*/
- //texel = (texel & 0xFF808080);
- //texel = texel & 0xFFC0C0C0;
- //texel = texel & 0xFFE0E0E0;
- //texel = texel & 0xFFF0F0F0;
- //texel += dith2;
- // Outline BEGIN
- const uint32_t outline_color_a = 0xFFAA3366;
- const uint32_t outline_color_b = 0xFF000000;
- const uint32_t outline_color_c = 0xFF3366AA;
- uint32_t& l = sdGetBackBuffer()[x - 1 + y * sdGetCanvasWidth()];
- uint32_t& r = sdGetBackBuffer()[x + 1 + y * sdGetCanvasWidth()];
- uint32_t& u = sdGetBackBuffer()[x + (y + 1) * sdGetCanvasWidth()];
- uint32_t& d = sdGetBackBuffer()[x + (y - 1) * sdGetCanvasWidth()];
- if ((x - 1) >= 0 && (l & 0xFF000000) == 0xFF000000) {
- l = outline_color_b;
- }
- if ((x + 1) < sdGetCanvasWidth() && (r & 0xFF000000) == 0xFF000000) {
- r = outline_color_b;
- }
- if ((y + 1) < sdGetCanvasHeight() && (u & 0xFF000000) == 0xFF000000) {
- u = outline_color_b;
- }
- if ((y - 1) >= 0 && (d & 0xFF000000) == 0xFF000000) {
- d = outline_color_b;
- }
- // Outline END
- sdGetBackBuffer()[x + y * sdGetCanvasWidth()] = texel;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement