/* Native depth buffer. Projected vertices and smooth lighting are indexed. */ #include #include #include #ifdef _WIN32 #define API __declspec(dllexport) #else #define API #endif API void draw_faces(const float *points, const float *light, const int32_t *indices, const int32_t *materials, size_t count, uint8_t *rgba, float *depth, int width, int height, const uint8_t *colors) { for (size_t i = 0; i < (size_t)width * height; ++i) depth[i] = -INFINITY; for (size_t i = 0; i < count; ++i) { const int32_t *triangle = indices + 3*i; const float *a = points+3*triangle[0], *b = points+3*triangle[1], *c = points+3*triangle[2]; const uint8_t *color = colors + 6*materials[i]; float dx = b[0]-a[0], dy = b[1]-a[1], ex = c[0]-a[0], ey = c[1]-a[1]; float determinant = dx*ey-dy*ex; if (fabsf(determinant) < 1e-8f) continue; int xmin = (int)fmaxf(0, floorf(fminf(a[0], fminf(b[0], c[0])))); int xmax = (int)fminf(width-1, ceilf(fmaxf(a[0], fmaxf(b[0], c[0])))); int ymin = (int)fmaxf(0, floorf(fminf(a[1], fminf(b[1], c[1])))); int ymax = (int)fminf(height-1, ceilf(fmaxf(a[1], fmaxf(b[1], c[1])))); if (xmin > xmax || ymin > ymax) continue; float inv = 1.f/determinant, ux = ey*inv, vx = -dy*inv; float dz1 = b[2]-a[2], dz2 = c[2]-a[2]; float l0 = light[triangle[0]], dl1 = light[triangle[1]]-l0, dl2 = light[triangle[2]]-l0; for (int y = ymin; y <= ymax; ++y) { float row = y+.5f-a[1], col = xmin+.5f-a[0]; float u0 = (col*ey-row*ex)*inv, v0 = (row*dx-col*dy)*inv; for (int x = xmin; x <= xmax; ++x) { float u = u0+(x-xmin)*ux, v = v0+(x-xmin)*vx; if (u < -2e-5f || v < -2e-5f || u+v > 1.00002f) continue; float z = a[2] + dz1*u + dz2*v; size_t at = (size_t)y*width+x; if (z <= depth[at]) continue; depth[at] = z; float shade = l0 + dl1*u + dl2*v; for (int k = 0; k < 3; ++k) rgba[4*at+k] = (uint8_t)(color[k]+shade*(color[k+3]-color[k])); } } } } API void draw_edges(const float *segments, size_t count, uint8_t *rgba, const float *depth, int width, int height, const uint8_t *ink, float bias, int wire) { for (size_t i = 0; i < count; ++i) { const float *line = segments + 6*i; float x = line[0], y = line[1], dx = line[3]-x, dy = line[4]-y; int steep = fabsf(dy) > fabsf(dx); float major = steep ? y : x, minor = steep ? x : y; float dm = steep ? dy : dx, dn = steep ? dx : dy; if (fabsf(dm) < 1e-7f) continue; int extent = steep ? height : width; int lower = (int)fmaxf(0, floorf(fminf(major, major+dm))); int upper = (int)fminf(extent-1, floorf(fmaxf(major, major+dm))); for (int j = lower; j <= upper; ++j) { float t = fmaxf(0, fminf(1, (j+.5f-major)/dm)); float sample = minor + t*dn - .5f; int first = (int)floorf(sample); for (int k = 0; k < 2; ++k) { int px = steep ? first+k : j, py = steep ? j : first+k; if (px < 0 || px >= width || py < 0 || py >= height) continue; size_t at = (size_t)py*width+px; float z = line[2] + t*(line[5]-line[2]); if (!wire && z+bias < depth[at]) continue; float alpha = (1.f-fabsf(sample-(first+k))) * .83f; for (int c = 0; c < 3; ++c) rgba[4*at+c] = (uint8_t)(rgba[4*at+c]+alpha*(ink[c]-rgba[4*at+c])); } } } }