【cg】简易软光栅渲染

#include <iostream>
#include <string>
#include <raylib.h>
#include <raymath.h>
#include <vector>
#include "Vector2.hpp"
#include "Vector3.hpp"
#include "Matrix.hpp"
#include "Algorithm.hpp"

constexpr float fps = 120;	//fps
constexpr float dt = 1 / fps;	//每帧的时间
//float dz = 0.01 * dt;	//每帧z轴增加的距离
constexpr float dtheta = 90.0f * dt;	//每帧旋转的角度

Vector2 localVec2ToRayVec(const twoD::Vector2& vec)
{
	return Vector2{vec.x,vec.y};
}
Vector3 localVec3ToRayVec(const threeD::Vector3& vec)
{
	return Vector3{vec.x,vec.y,vec.z};
}

//透视投影
twoD::Vector2 Project(const threeD::Vector3& point)
{
	twoD::Vector2 pointPrj{ point.x / point.z, point.y / point.z };
	return pointPrj;
}


void renderPoints(std::vector<threeD::Vector3>& vertices, Color color)
{
	//point.z += dz;
	/*threeD::Vector3 centerPoint
	{
		(vertices[0].x + vertices[1].x) * 0.5f, 
		(vertices[0].y + vertices[3].y) * 0.5f,
		(vertices[0].z + vertices[4].z) * 0.5f
	};*/
	threeD::Vector3 centerPoint{0,0,0.125};
	//threeD::Vector3 centerPoint{ 0,0,0 };

	for (auto& vertex: vertices)
	{
		auto pointTmp = localVec2ToRayVec(
			twoD::Normalize2Screen(
				Project(threeD::Rotate(math::Axis::y, centerPoint, dtheta, vertex)),
			twoD::Vector2{ 800, 600 }));

		DrawCircleV(pointTmp, 5, color);
	}
}

void renderFaces(std::vector<threeD::Vector3>& vertices, 
	std::vector<twoD::Vector2>& faces, Color color)
{
	for (const auto& face : faces)
	{
		auto pointStart = vertices[face.first];
		auto pointTmpStart = localVec2ToRayVec(
			twoD::Normalize2Screen(Project(pointStart),
			twoD::Vector2{ 800, 600 }));


		auto pointEnd = vertices[face.second];
		auto pointTmpEnd = localVec2ToRayVec(
			twoD::Normalize2Screen(Project(pointEnd),
			twoD::Vector2{ 800, 600 }));

		DrawLineV(pointTmpStart, pointTmpEnd, color);
	}
}

std::vector<threeD::Vector3> getVertices() 
{
	std::vector<threeD::Vector3> vertices
	{
		{-0.05,-0.05,0.1},
		{0.05,-0.05,0.1},
		{0.05,0.05,0.1},
		{-0.05,0.05,0.1},

		{-0.05,-0.05,0.15},
		{0.05,-0.05,0.15},
		{0.05,0.05,0.15},
		{-0.05,0.05,0.15}
	};
	
    return vertices;
}

std::vector<std::pair<int,int>> getFaces()
{
	std::vector<std::pair<int,int>> facesTmp
	{
		std::make_pair<int,int>(0,1),
		std::make_pair<int,int>(1,2),
		std::make_pair<int,int>(2,3),
		std::make_pair<int,int>(3,0),

		std::make_pair<int,int>(4,5),
		std::make_pair<int,int>(5,6),
		std::make_pair<int,int>(6,7),
		std::make_pair<int,int>(7,4),

		std::make_pair<int,int>(0,4),
		std::make_pair<int,int>(1,5),
		std::make_pair<int,int>(2,6),
		std::make_pair<int,int>(3,7)
	};
	return facesTmp;
}

int main()
{
	SetTargetFPS(fps);
    InitWindow(800, 600, "3dmath");
    auto vertices = getVertices();
	auto faces = getFaces();
    while (!WindowShouldClose())
    {
		BeginDrawing();
		ClearBackground(BLACK);
		DrawFPS(700, 500);



		renderPoints(vertices, GREEN);
		renderFaces(vertices, faces, BLUE);
	
		/*auto pointTmpEnd = localVec2ToRayVec(
			twoD::Normalize2Screen(twoD::Vector2{-1,-1}, twoD::Vector2{ 800, 600 }));
		DrawCircleV(pointTmpEnd, 5, GREEN);*/

		EndDrawing();
    }

	/*threeD::Matrix3x3 mat;
	mat.GetProjectMat(threeD::Matrix3x3::CoordinatePlane::yz);
	mat.printMatrix();*/
	return 0;
	
}
posted @ 2026-02-21 17:22  仰望星河Leon  阅读(1)  评论(0)    收藏  举报