Removing functionality from future labs
This commit is contained in:
parent
282ad80e46
commit
fb4cf5bd80
@ -16,5 +16,4 @@ add_executable(
|
||||
src/painter.hpp
|
||||
src/main.cpp
|
||||
|
||||
src/shaders.cpp
|
||||
)
|
127
src/shaders.cpp
127
src/shaders.cpp
@ -1,127 +0,0 @@
|
||||
#include <utility>
|
||||
#include "frame.hpp"
|
||||
#include "painter.hpp"
|
||||
|
||||
template<class ShaderClass>
|
||||
void Triangle(Frame *f, float x0, float y0, float x1, float y1, float x2, float y2, COLOR color, ShaderClass *shader) {
|
||||
// Отсортируем точки таким образом, чтобы выполнилось условие: y0 < y1 < y2
|
||||
if (y1 < y0) {
|
||||
std::swap(y1, y0);
|
||||
std::swap(x1, x0);
|
||||
}
|
||||
if (y2 < y1) {
|
||||
std::swap(y2, y1);
|
||||
std::swap(x2, x1);
|
||||
}
|
||||
if (y1 < y0) {
|
||||
std::swap(y1, y0);
|
||||
std::swap(x1, x0);
|
||||
}
|
||||
// Определяем номера строк пикселей, в которых располагаются точки треугольника
|
||||
int Y0 = (int) (y0 + 0.5f);
|
||||
int Y1 = (int) (y1 + 0.5f);
|
||||
int Y2 = (int) (y2 + 0.5f);
|
||||
// Отсечение невидимой части треугольника
|
||||
if (Y0 < 0) Y0 = 0;
|
||||
else if (Y0 >= f->height) Y0 = f->height;
|
||||
if (Y1 < 0) Y1 = 0;
|
||||
else if (Y1 >= f->height) Y1 = f->height;
|
||||
if (Y2 < 0) Y2 = 0;
|
||||
else if (Y2 >= f->height) Y2 = f->height;
|
||||
// Рисование верхней части треугольника
|
||||
for (float y = Y0 + 0.5f; y < Y1; y++) {
|
||||
int X0 = (int) ((y - y0) / (y1 - y0) * (x1 - x0) + x0 + 0.5f);
|
||||
int X1 = (int) ((y - y0) / (y2 - y0) * (x2 - x0) + x0 + 0.5f);
|
||||
if (X0 > X1) std::swap(X0, X1);
|
||||
if (X0 < 0) X0 = 0;
|
||||
if (X1 > f->width) X1 = f->width;
|
||||
for (int x = X0; x < X1; x++) {
|
||||
// f(x + 0.5, y)
|
||||
SetPixel(x, y, shader->getColor(x + 0.5f, y));
|
||||
}
|
||||
}
|
||||
// Рисование нижней части треугольника
|
||||
for (float y = Y1 + 0.5f; y < Y2; y++) {
|
||||
int X0 = (int) ((y - y1) / (y2 - y1) * (x2 - x1) + x1 + 0.5f);
|
||||
int X1 = (int) ((y - y0) / (y2 - y0) * (x2 - x0) + x0 + 0.5f);
|
||||
if (X0 > X1) std::swap(X0, X1);
|
||||
if (X0 < 0) X0 = 0;
|
||||
if (X1 > f->width) X1 = f->width;
|
||||
for (int x = X0; x < X1; x++) {
|
||||
// f(x + 0.5, y)
|
||||
SetPixel(x, y, shader->getColor(x + 0.5f, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class BarycentricInterpolator {
|
||||
float x0, y0, x1, y1, x2, y2, S;
|
||||
COLOR C0, C1, C2;
|
||||
public:
|
||||
BarycentricInterpolator(float _x0, float _y0, float _x1, float _y1, float
|
||||
_x2, float _y2, COLOR A0, COLOR A1, COLOR A2) :
|
||||
x0(_x0), y0(_y0), x1(_x1), y1(_y1), x2(_x2), y2(_y2),
|
||||
S((_y1 - _y2) * (_x0 - _x2) + (_x2 - _x1) * (_y0 - _y2)), C0(A0), C1(A1),
|
||||
C2(A2) {
|
||||
}
|
||||
|
||||
COLOR getColor(float x, float y) {
|
||||
// Барицентрическая интерполяция
|
||||
float h0 = ((y1 - y2) * (x - x2) + (x2 - x1) * (y - y2)) / S;
|
||||
float h1 = ((y2 - y0) * (x - x2) + (x0 - x2) * (y - y2)) / S;
|
||||
float h2 = 1 - h0 - h1;
|
||||
float r = h0 * C0.red + h1 * C1.red + h2 * C2.red;
|
||||
float g = h0 * C0.green + h1 * C1.green + h2 * C2.green;
|
||||
float b = h0 * C0.blue + h1 * C1.blue + h2 * C2.blue;
|
||||
float a = h0 * C0.alpha + h1 * C1.alpha + h2 * C2.alpha;
|
||||
return COLOR(r, g, b, a);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class RadialBrush {
|
||||
float cx, cy; // Центр прямоугольника
|
||||
COLOR C0, C1; // Цвета радиальной заливки
|
||||
float angle; // Начальный угол заливки
|
||||
public:
|
||||
RadialBrush(float _x0, float _y0, float _x1, float _y1, COLOR A0, COLOR A1, float
|
||||
_angle) :
|
||||
cx((_x0 + _x1) / 2.0f), cy((_y0 + _y1) / 2.0f),
|
||||
C0(A0), C1(A1), angle(_angle) {
|
||||
}
|
||||
|
||||
COLOR getColor(float x, float y) {
|
||||
double dx = (double) x - cx, dy = (double) y - cy;
|
||||
double radius = sqrt(dx * dx + dy * dy);
|
||||
float h0 = (sin(radius / 10 + angle) + 1.0f) / 2;
|
||||
float h1 = 1 - h0;
|
||||
float r = h0 * C0.red + h1 * C1.red;
|
||||
float g = h0 * C0.green + h1 * C1.green;
|
||||
float b = h0 * C0.blue + h1 * C1.blue;
|
||||
return COLOR(r, g, b);
|
||||
}
|
||||
};
|
||||
|
||||
COLOR ColorFromHSV(double hue, double saturation, double value) {
|
||||
int hi = int(floor(hue / 60)) % 6;
|
||||
double f = hue / 60 - floor(hue / 60);
|
||||
value = value * 255;
|
||||
const auto v = (unsigned char) (value);
|
||||
const auto p = (unsigned char) (value * (1 - saturation));
|
||||
const auto q = (unsigned char) (value * (1 - f * saturation));
|
||||
const auto t = (unsigned char) (value * (1 - (1 - f) * saturation));
|
||||
switch (hi) {
|
||||
case 0:
|
||||
return COLOR{v, t, p};
|
||||
case 1:
|
||||
return COLOR{q, v, p};
|
||||
case 2:
|
||||
return COLOR{p, v, t};
|
||||
case 3:
|
||||
return COLOR{p, q, v};
|
||||
case 4:
|
||||
return COLOR{t, p, v};
|
||||
default:
|
||||
return COLOR{v, p, q};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user