Created
April 19, 2020 18:21
-
-
Save kdrnic/7fceb9d888514a52272924921e961be2 to your computer and use it in GitHub Desktop.
Laziest gouraud shader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Random internet code to solve system of 3 lin eqs: | |
float a,b,c,d,l,m,n,k,p,D,q,r,s,x,y,z; | |
printf("PROGRAM TO SOLVE THREE VARIABLE LINEAR SIMULTANEOUS EQUATIONS"); | |
printf("The equations are of the form:" | |
"ax+by+cz+d=0" | |
"lx+my+nz+k=0" | |
"px+qy+rz+s=0"); | |
printf("Enter the coefficients in the order a,b,c,d,l,m,n,k,p,q,r,s"); | |
scanf("%f%f%f%f%f%f%f%f%f%f%f%f",&a,&b,&c,&d,&l,&m,&n,&k,&p,&q,&r,&s); | |
printf("The equations you have input are:"); | |
printf(" %.2f*x + %.2f*y + %.2f*z + %.2f = 0",a,b,c,d); | |
printf(" %.2f*x + %.2f*y + %.2f*z + %.2f = 0",l,m,n,k); | |
printf(" %.2f*x + %.2f*y + %.2f*z + %.2f = 0",p,q,r,s); | |
D = (a*m*r+b*p*n+c*l*q)-(a*n*q+b*l*r+c*m*p); | |
x = ((b*r*k+c*m*s+d*n*q)-(b*n*s+c*q*k+d*m*r))/D; | |
y = ((a*n*s+c*p*k+d*l*r)-(a*r*k+c*l*s+d*n*p))/D; | |
z = ((a*q*k+b*l*s+d*m*p)-(a*m*s+b*p*k+d*l*q))/D; | |
printf("The solutions to the above three equations are :"); | |
printf(" x = %5.2f y = %5.2f z = %5.2f",x,y,z); | |
getch(); | |
return 0; | |
*/ | |
void kdr_affinelit(BITMAP *bmp, V3D_f *v1, V3D_f *v2, V3D_f *v3) | |
{ | |
const int v1x = v1->x; | |
const int v2x = v2->x; | |
const int v3x = v3->x; | |
const int v1y = v1->y; | |
const int v2y = v2->y; | |
const int v3y = v3->y; | |
const int v1c = v1->c; | |
const int v2c = v2->c; | |
const int v3c = v3->c; | |
#if 1 | |
#define KDR_AFFINELIT_LINTERP | |
//Linear interpolation: | |
// | |
//v1x * a + v1y * b + c = v1c | |
//v2x * a + v2y * b + c = v2c | |
//v3x * a + v3y * b + c = v3c | |
// | |
//The above transformed: | |
// | |
//v1x * a + v1y * b + c - v1c = 0 | |
//v2x * a + v2y * b + c - v2c = 0 | |
//v3x * a + v3y * b + c - v3c = 0 | |
int D, a = 0, b = 0, c = 0; | |
//Thus plugging the linear interpolation eqs into the solver code above: | |
#define Ex a | |
#define Ey b | |
#define Ez c | |
#define ED D | |
#define Ea ((__typeof__(D))(v1x )) | |
#define Eb ((__typeof__(D))(v1y )) | |
#define Ec ((__typeof__(D))(1 )) | |
#define Ed ((__typeof__(D))((-v1c))) | |
#define El ((__typeof__(D))(v2x )) | |
#define Em ((__typeof__(D))(v2y )) | |
#define En ((__typeof__(D))(1 )) | |
#define Ek ((__typeof__(D))((-v2c))) | |
#define Ep ((__typeof__(D))(v3x )) | |
#define Eq ((__typeof__(D))(v3y )) | |
#define Er ((__typeof__(D))(1 )) | |
/* | |
Random internet code to solve system of 3 lin eqs: | |
float a,b,c,d,l,m,n,k,p,D,q,r,s,x,y,z; | |
printf("PROGRAM TO SOLVE THREE VARIABLE LINEAR SIMULTANEOUS EQUATIONS"); | |
printf("The equations are of the form:" | |
"ax+by+cz+d=0" | |
"lx+my+nz+k=0" | |
"px+qy+rz+s=0"); | |
printf("Enter the coefficients in the order a,b,c,d,l,m,n,k,p,q,r,s"); | |
scanf("%f%f%f%f%f%f%f%f%f%f%f%f",&a,&b,&c,&d,&l,&m,&n,&k,&p,&q,&r,&s); | |
printf("The equations you have input are:"); | |
printf(" %.2f*x + %.2f*y + %.2f*z + %.2f = 0",a,b,c,d); | |
printf(" %.2f*x + %.2f*y + %.2f*z + %.2f = 0",l,m,n,k); | |
printf(" %.2f*x + %.2f*y + %.2f*z + %.2f = 0",p,q,r,s); | |
D = (a*m*r+b*p*n+c*l*q)-(a*n*q+b*l*r+c*m*p); | |
x = ((b*r*k+c*m*s+d*n*q)-(b*n*s+c*q*k+d*m*r))/D; | |
y = ((a*n*s+c*p*k+d*l*r)-(a*r*k+c*l*s+d*n*p))/D; | |
z = ((a*q*k+b*l*s+d*m*p)-(a*m*s+b*p*k+d*l*q))/D; | |
printf("The solutions to the above three equations are :"); | |
printf(" x = %5.2f y = %5.2f z = %5.2f",x,y,z); | |
getch(); | |
return 0; | |
*/ | |
void kdr_affinelit(BITMAP *bmp, V3D_f *v1, V3D_f *v2, V3D_f *v3) | |
{ | |
const int v1x = v1->x; | |
const int v2x = v2->x; | |
const int v3x = v3->x; | |
const int v1y = v1->y; | |
const int v2y = v2->y; | |
const int v3y = v3->y; | |
const int v1c = v1->c; | |
const int v2c = v2->c; | |
const int v3c = v3->c; | |
#if 1 | |
#define KDR_AFFINELIT_LINTERP | |
//Linear interpolation: | |
// | |
//v1x * a + v1y * b + c = v1c | |
//v2x * a + v2y * b + c = v2c | |
//v3x * a + v3y * b + c = v3c | |
// | |
//The above transformed: | |
// | |
//v1x * a + v1y * b + c - v1c = 0 | |
//v2x * a + v2y * b + c - v2c = 0 | |
//v3x * a + v3y * b + c - v3c = 0 | |
int D, a = 0, b = 0, c = 0; | |
//Thus plugging the linear interpolation eqs into the solver code above: | |
#define Ex a | |
#define Ey b | |
#define Ez c | |
#define ED D | |
#define Ea ((__typeof__(D))(v1x )) | |
#define Eb ((__typeof__(D))(v1y )) | |
#define Ec ((__typeof__(D))(1 )) | |
#define Ed ((__typeof__(D))((-v1c))) | |
#define El ((__typeof__(D))(v2x )) | |
#define Em ((__typeof__(D))(v2y )) | |
#define En ((__typeof__(D))(1 )) | |
#define Ek ((__typeof__(D))((-v2c))) | |
#define Ep ((__typeof__(D))(v3x )) | |
#define Eq ((__typeof__(D))(v3y )) | |
#define Er ((__typeof__(D))(1 )) | |
#define Es ((__typeof__(D))((-v3c))) | |
#define ERes 256 | |
ED = (Ea * Em * Er + Eb * Ep * En + Ec * El * Eq) - (Ea * En * Eq + Eb * El * Er + Ec * Em * Ep); | |
if(ED){ | |
Ex = (((Eb * Er * Ek + Ec * Em * Es + Ed * En * Eq) - (Eb * En * Es + Ec * Eq * Ek + Ed * Em * Er)) * ERes) / ED; | |
Ey = (((Ea * En * Es + Ec * Ep * Ek + Ed * El * Er) - (Ea * Er * Ek + Ec * El * Es + Ed * En * Ep)) * ERes) / ED; | |
//Constant term needs no fixed point factor | |
Ez = (((Ea * Eq * Ek + Eb * El * Es + Ed * Em * Ep) - (Ea * Em * Es + Eb * Ep * Ek + Ed * El * Eq)) ) / ED; | |
} | |
#undef Ex | |
#undef Ey | |
#undef Ez | |
#undef Ea | |
#undef Eb | |
#undef Ec | |
#undef Ed | |
#undef El | |
#undef Em | |
#undef En | |
#undef Ek | |
#undef Ep | |
#undef Eq | |
#undef Er | |
#undef Es | |
#undef ED | |
#endif | |
const uint16_t *max = ktaz_ptr.y; | |
for(ktaz_ptr = ktaz_p0; ktaz_ptr.y < max; ktaz_ptr.y++, ktaz_ptr.n++){ | |
for(; *ktaz_ptr.n; *ktaz_ptr.n -= 1, ktaz_ptr.l++, ktaz_ptr.x++){ | |
for(; *ktaz_ptr.l; *ktaz_ptr.l -= 1){ | |
const int x = *ktaz_ptr.x + *ktaz_ptr.l - 1; | |
const int y = *ktaz_ptr.y; | |
_putpixel(bmp, x, y, | |
#ifdef KDR_AFFINELIT_LINTERP | |
MIN(MAX(0, (x * a + y * b) / ERes + c), 255) | |
#undef ERes | |
#undef KDR_AFFINELIT_LINTERP | |
#else | |
(v3c + v2c + v1c) / 3 | |
#endif | |
); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment