Files
cs-render/Program.cs
T
2026-10-09 17:46:45 +02:00

287 lines
8.3 KiB
C#

using System.Numerics;
using Raylib_cs;
namespace cs_render;
class Program
{
public static readonly int Width = 1024;
public static readonly int Height = 1024;
public static readonly int FPS = 240;
public static readonly float Timescale = 1f;
static void Main(/* string[] args */)
{
Raylib.InitWindow(Width, Height, "Hello, Raylib World!");
Raylib.SetTargetFPS(FPS);
//Raylib.HideCursor();
Color[] PixelBuffer = new Color[Width * Height];
Image image = Raylib.GenImageColor(Width, Height, Color.Black);
Texture2D FrameBuffer = Raylib.LoadTextureFromImage(image);
Vector2 a = new(0,Height-1);
Vector2 b = new(Width-1,Height-1);
Vector2 c = new(Width/2,0);
while(!Raylib.WindowShouldClose())
{
float delta = 1f / FPS;
delta += Timescale * delta;
Array.Fill(PixelBuffer, Color.Black);
//Console.Clear();
Raylib.BeginDrawing();
RasterizeTri(a,b,c,Color.Red,Color.Green,Color.Blue,PixelBuffer);
Raylib.UpdateTexture(FrameBuffer, PixelBuffer);
Raylib.DrawTexture(FrameBuffer, 0, 0, Color.White);
//Console.WriteLine("Frametime: " + Raylib.GetFrameTime());
Raylib.DrawText(Raylib.GetFrameTime().ToString(), 5, 5, 24, Color.Lime);
Raylib.EndDrawing();
}
Raylib.UnloadImage(image);
Raylib.UnloadTexture(FrameBuffer);
Raylib.CloseWindow();
}
static void Plot(Vector2 pos, Color color, Color[] buffer)
{
int x = (int)MathF.Round(pos.X);
int y = (int)MathF.Round(pos.Y);
if(x < 0 || x >= Width || y < 0 || y >= Height) return;
int index = y * Width + x;
buffer[index] = color;
}
static void Plot(int x, int y, Color color, Color[] buffer)
{
if(x < 0 || x >= Width || y < 0 || y >= Height) return;
int index = y * Width + x;
buffer[index] = color;
}
static void PlotLine(Vector2 pos0, Vector2 pos1, Color color, Color[] buffer)
{
int x0 = (int)MathF.Round(pos0.X);
int y0 = (int)MathF.Round(pos0.Y);
int x1 = (int)MathF.Round(pos1.X);
int y1 = (int)MathF.Round(pos1.Y);
if(MathF.Abs(x1-x0) > MathF.Abs(y1-y0)) PlotLineH(x0,y0, x1, y1, color, buffer);
else PlotLineV(x0,y0, x1, y1, color, buffer);
}
static void PlotLine(int x0, int y0, int x1, int y1, Color color, Color[] buffer)
{
if(MathF.Abs(x1-x0) > MathF.Abs(y1-y0)) PlotLineH(x0,y0, x1, y1, color, buffer);
else PlotLineV(x0,y0, x1, y1, color, buffer);
}
private static void PlotLineH(int x0, int y0, int x1, int y1, Color color, Color[] buffer)
{
if (x0 > x1)
{
(x0,x1) = (x1,x0);
(y0,y1) = (y1,y0);
}
int dx = x1 - x0;
int dy = y1 - y0;
int dir = dy < 0 ? -1 : 1;
dy *= dir;
if(dx != 0)
{
int y = y0;
int p = 2*dy - dx;
for(int i = 0; i < dx + 1; i++)
{
Plot(x0 + i, y, color, buffer);
if(p >= 0) {
y += dir;
p -= 2*dx;
}
p += 2*dy;
}
}
}
private static void PlotLineV(int x0, int y0, int x1, int y1, Color color, Color[] buffer)
{
if (y0 > y1)
{
(x0,x1) = (x1,x0);
(y0,y1) = (y1,y0);
}
int dx = x1 - x0;
int dy = y1 - y0;
int dir = dx < 0 ? -1 : 1;
dx *= dir;
if(dy != 0)
{
int x = x0;
int p = 2*dx - dy;
for(int i = 0; i < dy + 1; i++)
{
Plot(x, y0 + i, color, buffer);
if(p >= 0) {
x += dir;
p -= 2*dy;
}
p += 2*dx;
}
}
}
private static void RasterizeTri(Vector2 a, Vector2 b, Vector2 c, Color colorA, Color colorB, Color colorC, Color[] buffer)
{
int minX = Math.Clamp((int)MathF.Floor(MathF.Min(a.X, MathF.Min(b.X, c.X))), 0, Width);
int maxX = Math.Clamp((int)MathF.Ceiling(MathF.Max(a.X, MathF.Max(b.X, c.X))), 0, Width);
int minY = Math.Clamp((int)MathF.Floor(MathF.Min(a.Y, MathF.Min(b.Y, c.Y))), 0, Height);
int maxY = Math.Clamp((int)MathF.Ceiling(MathF.Max(a.Y, MathF.Max(b.Y, c.Y))), 0, Height);
float stepX1 = -(b.Y - a.Y);
float stepX2 = -(c.Y - b.Y);
float stepX3 = -(a.Y - c.Y);
float stepY1 = b.X - a.X;
float stepY2 = c.X - b.X;
float stepY3 = a.X - c.X;
float c1 = -stepY1 * a.Y - stepX1 * a.X;
float c2 = -stepY2 * b.Y - stepX2 * b.X;
float c3 = -stepY3 * c.Y - stepX3 * c.X;
float re1 = stepX1 * minX + stepY1 * minY + c1;
float re2 = stepX2 * minX + stepY2 * minY + c2;
float re3 = stepX3 * minX + stepY3 * minY + c3;
float invsum = 1f / (re1 + re2 + re3);
float RA = colorA.R * invsum;
float GA = colorA.G * invsum;
float BA = colorA.B * invsum;
float AA = colorA.A * invsum;
float RB = colorB.R * invsum;
float GB = colorB.G * invsum;
float BB = colorB.B * invsum;
float AB = colorB.A * invsum;
float RC = colorC.R * invsum;
float GC = colorC.G * invsum;
float BC = colorC.B * invsum;
float AC = colorC.A * invsum;
float stepXnumR = stepX2 * RA + stepX3 * RB + stepX1 * RC;
float stepXnumG = stepX2 * GA + stepX3 * GB + stepX1 * GC;
float stepXnumB = stepX2 * BA + stepX3 * BB + stepX1 * BC;
float stepXnumA = stepX2 * AA + stepX3 * AB + stepX1 * AC;
float stepYnumR = stepY2 * RA + stepY3 * RB + stepY1 * RC;
float stepYnumG = stepY2 * GA + stepY3 * GB + stepY1 * GC;
float stepYnumB = stepY2 * BA + stepY3 * BB + stepY1 * BC;
float stepYnumA = stepY2 * AA + stepY3 * AB + stepY1 * AC;
float Rr = re2 * RA + re3 * RB + re1 * RC;
float Gr = re2 * GA + re3 * GB + re1 * GC;
float Br = re2 * BA + re3 * BB + re1 * BC;
float Ar = re2 * AA + re3 * AB + re1 * AC;
for(int y = minY; y < maxY; y++)
{
int index = y * Width + minX;
float e1 = re1;
float e2 = re2;
float e3 = re3;
float Rp = Rr;
float Gp = Gr;
float Bp = Br;
float Ap = Ar;
for(int x = minX; x < maxX; x++)
{
if(e1 <= 0 && e2 <= 0 && e3 <= 0) {
buffer[index] = new Color(
(byte)Math.Clamp(Rp, 0, 255),
(byte)Math.Clamp(Gp, 0, 255),
(byte)Math.Clamp(Bp, 0, 255),
(byte)Math.Clamp(Ap, 0, 255));
}
index++;
e1 += stepX1;
e2 += stepX2;
e3 += stepX3;
Rp += stepXnumR;
Gp += stepXnumG;
Bp += stepXnumB;
Ap += stepXnumA;
}
re1 += stepY1;
re2 += stepY2;
re3 += stepY3;
Rr += stepYnumR;
Gr += stepYnumG;
Br += stepYnumB;
Ar += stepYnumA;
}
}
private static bool PointOnRight(Vector2 a, Vector2 b, Vector2 p)
{
Vector2 eV = b - a;
Vector2 pV = p - a;
float e = eV.X * pV.Y - eV.Y * pV.X;
if(e <= 0) return true;
return false;
}
static bool PointInTri(Vector2 v0, Vector2 v1, Vector2 v2, Vector2 p)
{
bool lineA = PointOnRight(v0,v1,p);
bool lineB = PointOnRight(v1,v2,p);
bool lineC = PointOnRight(v2,v0,p);
if(lineA && lineB && lineC) return true;
return false;
}
static bool PointInTri(int x0, int y0, int x1, int y1, int x2, int y2, int px, int py)
{
Vector2 v0 = new(x0, y0);
Vector2 v1 = new(x1, y1);
Vector2 v2 = new(x2, y2);
Vector2 p = new(px, py);
bool lineA = PointOnRight(v0,v1,p);
bool lineB = PointOnRight(v1,v2,p);
bool lineC = PointOnRight(v2,v0,p);
if(lineA && lineB && lineC) return true;
return false;
}
}