TP3 barycentric rasterizer
This commit is contained in:
parent
d5616ba9d3
commit
f0576ed96b
@ -5,6 +5,7 @@ set (CMAKE_CXX_STANDARD 11)
|
|||||||
|
|
||||||
set(EXAMPLES
|
set(EXAMPLES
|
||||||
rasterizer
|
rasterizer
|
||||||
|
barycentricRasterizer
|
||||||
)
|
)
|
||||||
|
|
||||||
foreach(EXAMPLE ${EXAMPLES})
|
foreach(EXAMPLE ${EXAMPLES})
|
||||||
|
155
image-processing/3-Rasterization/c++/barycentricRasterizer.cpp
Normal file
155
image-processing/3-Rasterization/c++/barycentricRasterizer.cpp
Normal file
@ -0,0 +1,155 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <random>
|
||||||
|
#include <vector>
|
||||||
|
//Command-line parsing
|
||||||
|
#include "CLI11.hpp"
|
||||||
|
|
||||||
|
//Image filtering and I/O
|
||||||
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||||
|
#include <stb_image_write.h>
|
||||||
|
|
||||||
|
//Global flag to silent verbose messages
|
||||||
|
bool silent;
|
||||||
|
|
||||||
|
typedef struct point {
|
||||||
|
int x, y;
|
||||||
|
} point_t;
|
||||||
|
|
||||||
|
typedef struct bary_coord {
|
||||||
|
float lambda_1, lambda_2, lambda_3;
|
||||||
|
} bary_coord_t;
|
||||||
|
|
||||||
|
typedef struct triangle {
|
||||||
|
point_t p, q, r;
|
||||||
|
} triangle_t;
|
||||||
|
|
||||||
|
float maxf(float a, float b) {
|
||||||
|
return a > b ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_triangle(triangle_t t) {
|
||||||
|
printf("(%d %d) (%d %d) (%d %d)", t.p.x, t.p.y, t.q.x, t.q.y, t.r.x, t.r.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
int sig(int x) {
|
||||||
|
return (x != 0) * (x > 0 ? 1 : -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int dist(point_t p, point_t q) {
|
||||||
|
return sqrt((p.x-q.x)*(p.x-q.x) + (p.y-q.y)*(p.y-q.y));
|
||||||
|
}
|
||||||
|
|
||||||
|
int determinant(point_t p, point_t q, point_t r) {
|
||||||
|
int a, b, c, d;
|
||||||
|
a = q.x-p.x;
|
||||||
|
b = q.y-p.y;
|
||||||
|
c = r.x-p.x;
|
||||||
|
d = r.y-p.y;
|
||||||
|
return a*d - b*c;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char isInside(point_t a, triangle_t t) {
|
||||||
|
/* returns : 0 is not inside, 1 if inside, 2 if on edge */
|
||||||
|
int det_a1 = determinant(a, t.q, t.r);
|
||||||
|
int det_a2 = determinant(a, t.p, t.q);
|
||||||
|
int det_a3 = determinant(a, t.r, t.p);
|
||||||
|
|
||||||
|
if ((sig(det_a1)==sig(det_a2)) && (sig(det_a1)==sig(det_a3))) {
|
||||||
|
int dist_t1 = dist(t.q, t.r)*1.5;
|
||||||
|
int dist_t2 = dist(t.p, t.q)*1.5;
|
||||||
|
int dist_t3 = dist(t.r, t.p)*1.5;
|
||||||
|
if ((dist_t1 > abs(det_a1)) || (dist_t2 > abs(det_a2)) || (dist_t3 > abs(det_a3))) return 2;
|
||||||
|
return 1;
|
||||||
|
};
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bary_coord_t bary_coord_of(point_t a, triangle_t t) {
|
||||||
|
//TODO on peut juste faire des petites opérations sur les déterminant qu'on a déjà sinon
|
||||||
|
point_t p = t.p;
|
||||||
|
point_t q = t.q;
|
||||||
|
point_t r = t.r;
|
||||||
|
|
||||||
|
bary_coord_t bc;
|
||||||
|
|
||||||
|
float qpx = (q.x - p.x);
|
||||||
|
float rpy = (r.y - p.y);
|
||||||
|
|
||||||
|
bc.lambda_3 = ((a.x-p.x)*(p.y-q.y)/(qpx*rpy) + (a.y-p.y)/rpy)/(1 - (p.x-r.x)/qpx*(p.y-q.y)/rpy);
|
||||||
|
bc.lambda_2 = bc.lambda_3*(p.x - r.x)/qpx + (a.x - p.x)/qpx;
|
||||||
|
bc.lambda_1 = 1 - (bc.lambda_2+bc.lambda_3);
|
||||||
|
|
||||||
|
assert (bc.lambda_1 >= 0);
|
||||||
|
|
||||||
|
return bc;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char pxColor(float lambda, unsigned char color, bool colored) {
|
||||||
|
return colored ? (1 - lambda/4)*color : lambda*255;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
CLI::App app{"rasterizer"};
|
||||||
|
std::string outputImage= "output.png";
|
||||||
|
app.add_option("-o,--output", outputImage, "Output image")->required();
|
||||||
|
unsigned int nbTriangles = 5;
|
||||||
|
app.add_option("-n,--ntriangles", nbTriangles, "Number of triangles (5)");
|
||||||
|
bool colored = false;
|
||||||
|
app.add_flag("-c,--colored", colored, "Color triangles");
|
||||||
|
|
||||||
|
CLI11_PARSE(app, argc, argv);
|
||||||
|
srand(time(0));
|
||||||
|
|
||||||
|
//Image loading
|
||||||
|
int width=640,height=480, nbChannels=3;
|
||||||
|
std::vector<unsigned char> output(width*height*nbChannels);
|
||||||
|
|
||||||
|
int* colors = (int*)malloc(sizeof(int)*3*nbTriangles);
|
||||||
|
triangle_t* t = (triangle_t*)malloc(sizeof(triangle_t)*nbTriangles);
|
||||||
|
for (auto i=0; i < nbTriangles; i++) {
|
||||||
|
t[i] = {
|
||||||
|
rand()%width, rand()%height,
|
||||||
|
rand()%width, rand()%height,
|
||||||
|
rand()%width, rand()%height
|
||||||
|
};
|
||||||
|
|
||||||
|
colors[3*i] = rand()%255;
|
||||||
|
colors[3*i+1] = rand()%255;
|
||||||
|
colors[3*i+2] = rand()%255;
|
||||||
|
|
||||||
|
//print_triangle(t[i]);
|
||||||
|
//printf(" : (%d, %d, %d)\n", colors[3*i], colors[3*i+1], colors[3*i+2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(auto i = 0 ; i < width ; ++i) {
|
||||||
|
for(auto j = 0; j < height; ++j) {
|
||||||
|
auto indexPixel = nbChannels*(width*j+i);
|
||||||
|
output[indexPixel] = output[indexPixel+1] = output[indexPixel+2] = 0;
|
||||||
|
|
||||||
|
for (auto triangle_id=0; triangle_id < nbTriangles; triangle_id++) {
|
||||||
|
int valid = isInside({i, j}, t[triangle_id]);
|
||||||
|
if (valid == 1) {
|
||||||
|
bary_coord_t bc = bary_coord_of({i, j}, t[triangle_id]);
|
||||||
|
|
||||||
|
output[ indexPixel ] = pxColor(bc.lambda_1, colors[3*triangle_id], colored);
|
||||||
|
output[ indexPixel+1] = pxColor(bc.lambda_2, colors[3*triangle_id+1], colored);
|
||||||
|
output[ indexPixel+2] = pxColor(bc.lambda_3, colors[3*triangle_id+2], colored);
|
||||||
|
} else if (valid == 2) {
|
||||||
|
output[indexPixel] = output[indexPixel+1] = output[indexPixel+2] = 255;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Final export
|
||||||
|
std::cout<<"Exporting.."<<std::endl;
|
||||||
|
int errcode = stbi_write_png(outputImage.c_str(), width, height, nbChannels, output.data(), nbChannels*width);
|
||||||
|
if (!errcode) {
|
||||||
|
std::cout<<"Error while exporting the resulting image."<<std::endl;
|
||||||
|
exit(errcode);
|
||||||
|
}
|
||||||
|
|
||||||
|
exit(0);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user