merge remote and local changes

This commit is contained in:
HP
2026-06-07 10:39:29 +08:00
119 changed files with 6484 additions and 105 deletions
-14
View File
@@ -1,14 +0,0 @@
#pragma once
#include "Vector2.h"
namespace RenderData
{
struct BoundingBox2D
{
Math::Vector2Int min;
Math::Vector2Int max;
BoundingBox2D() : min(), max() {}
BoundingBox2D(const Math::Vector2Int min, const Math::Vector2Int max) : min(min), max(max) {}
};
}
-40
View File
@@ -1,40 +0,0 @@
#pragma once
#include <cstdint>
namespace RenderData
{
struct Color
{
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
Color() : r(0), g(0), b(0), a(0) {}
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
this->r = ClampToByte(r);
this->g = ClampToByte(g);
this->b = ClampToByte(b);
this->a = ClampToByte(a);
}
uint32_t to_rgba() const
{
uint32_t value = (static_cast<uint32_t>(r) << 24) | (static_cast<uint32_t>(g) << 16) | (static_cast<uint32_t>(b) << 8) | (static_cast<uint32_t>(a));
return value;
}
static uint8_t ClampToByte(const int32_t value)
{
if (value < 0) return 0;
if (value > 255) return 255;
return static_cast<uint8_t>(value);
}
static Color Red() { return Color(255, 0, 0, 255); }
static Color Green() { return Color(0, 255, 0, 255); }
static Color Blue() { return Color(0, 0, 255, 255); }
static Color White() { return Color(255, 255, 255, 255); }
static Color Black() { return Color(0, 0, 0, 255); }
};
}
+105
View File
@@ -0,0 +1,105 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <limits>
#include <vector>
namespace RenderData
{
class Image
{
private:
int32_t width;
int32_t height;
std::vector<uint32_t> pixels;
static bool calculate_pixel_count(int32_t width, int32_t height, size_t& count)
{
if (width <= 0 || height <= 0)
{
count = 0;
return false;
}
const size_t safeWidth = static_cast<size_t>(width);
const size_t safeHeight = static_cast<size_t>(height);
if (safeWidth > std::numeric_limits<size_t>::max() / safeHeight)
{
count = 0;
return false;
}
count = safeWidth * safeHeight;
return true;
}
public:
Image() : width(0), height(0) {}
Image(int32_t width, int32_t height) : width(0), height(0)
{
size_t pixelCount = 0;
if (calculate_pixel_count(width, height, pixelCount))
{
this->width = width;
this->height = height;
pixels.assign(pixelCount, 0);
}
}
Image(int32_t width, int32_t height, const std::vector<uint32_t>& pixels) : width(0), height(0)
{
size_t pixelCount = 0;
if (calculate_pixel_count(width, height, pixelCount) && pixels.size() == pixelCount)
{
this->width = width;
this->height = height;
this->pixels = pixels;
}
}
int32_t get_width() const { return width; }
int32_t get_height() const { return height; }
size_t total_pixels() const { return pixels.size(); }
const uint32_t* data() const { return pixels.data(); }
uint32_t* data() { return pixels.data(); }
bool is_valid() const
{
size_t pixelCount = 0;
return calculate_pixel_count(width, height, pixelCount) && pixels.size() == pixelCount;
}
uint32_t get_pixel(int32_t x, int32_t y) const
{
if (x < 0 || x >= width || y < 0 || y >= height)
{
return 0;
}
size_t index = static_cast<size_t>(y) * width + x;
return pixels[index];
}
uint32_t get_pixel_fast(int32_t x, int32_t y) const
{
size_t index = static_cast<size_t>(y) * width + x;
return pixels[index];
}
void set_pixel(int32_t x, int32_t y, uint32_t color)
{
if (x < 0 || x >= width || y < 0 || y >= height)
{
return;
}
size_t index = static_cast<size_t>(y) * width + x;
pixels.at(index) = color;
}
};
}
+63
View File
@@ -0,0 +1,63 @@
#pragma once
#include <cstdint>
#include "Image.h"
namespace RenderData
{
struct Sprite
{
const Image* image;
int32_t x;
int32_t y;
int32_t width;
int32_t height;
Sprite() : image(nullptr), x(0), y(0), width(0), height(0) {}
Sprite(const Image* image) :
image(image),
x(0),
y(0),
width(image ? image->get_width() : 0),
height(image ? image->get_height() : 0)
{}
Sprite(const Image* image, int32_t x, int32_t y, int32_t width, int32_t height) :
image(image),
x(x),
y(y),
width(width),
height(height)
{}
bool is_valid() const
{
if (image == nullptr || !image->is_valid())
{
return false;
}
if (x < 0 || y < 0 || width <= 0 || height <= 0)
{
return false;
}
return x + width <= image->get_width() && y + height <= image->get_height();
}
uint32_t get_pixel(int32_t localX, int32_t localY) const
{
if (!is_valid() || localX < 0 || localX >= width || localY < 0 || localY >= height)
{
return 0;
}
return image->get_pixel(x + localX, y + localY);
}
uint32_t get_pixel_fast(int32_t localX, int32_t localY) const
{
return image->get_pixel_fast(x + localX, y + localY);
}
};
}
-67
View File
@@ -1,67 +0,0 @@
#pragma once
#include "Vector2.h"
#include "BoundingBox.h"
#include "Vertex.h"
#include <cstdint>
#include <algorithm>
#include <cmath>
#include <Vector3.h>
#include <cstdlib>
namespace RenderData
{
struct Triangle
{
Scene::Vertex v0;
Scene::Vertex v1;
Scene::Vertex v2;
Triangle() : v0(), v1(), v2() {}
Triangle(const Scene::Vertex a, const Scene::Vertex b, const Scene::Vertex c) : v0(a), v1(b), v2(c) {}
BoundingBox2D get_boundingBox() const
{
using namespace Math;
int32_t minX = static_cast<int32_t>(std::floor(std::min({ v0.position.x, v1.position.x, v2.position.x })));
int32_t maxX = static_cast<int32_t>(std::ceil(std::max({ v0.position.x, v1.position.x, v2.position.x })));
int32_t minY = static_cast<int32_t>(std::floor(std::min({ v0.position.y, v1.position.y, v2.position.y })));
int32_t maxY = static_cast<int32_t>(std::ceil(std::max({ v0.position.y, v1.position.y, v2.position.y })));
Vector2Int min(minX, minY);
Vector2Int max(maxX, maxY);
return BoundingBox2D(min, max);
}
/// <summary>
/// 给定屏幕像素坐标,输出该点的面积坐标
/// </summary>
/// <param name="pos">要计算的屏幕像素点</param>
/// <param name="w0">面积坐标的 x 分量(引用)</param>
/// <param name="w1">面积坐标的 y 分量(引用)</param>
/// <param name="w2">面积坐标的 z 分量(引用)</param>
/// <returns>是否计算成功</returns>
bool get_barycentric(const Math::Vector2& p, float& w0, float& w1, float& w2) const
{
using namespace Math;
const float x0 = v0.position.x;
const float y0 = v0.position.y;
const float x1 = v1.position.x;
const float y1 = v1.position.y;
const float x2 = v2.position.x;
const float y2 = v2.position.y;
const float square2D = (y1 - y2) * (x0 - x2) + (x2 - x1) * (y0 - y2);
if (std::abs(square2D) < 1e-6f)
{
return false;
}
w0 = ((y1 - y2) * (p.x - x2) + (x2 - x1) * (p.y - y2)) / square2D;
w1 = ((y2 - y0) * (p.x - x2) + (x0 - x2) * (p.y - y2)) / square2D;
w2 = 1.0f - w0 - w1;
return true;
}
};
}