Giter VIP home page Giter VIP logo

tinysoftrenderer's Introduction

Logo

TinySoftRenderer

A tiny soft-renderer built from scratch using C++ 11
View Demo Report Bug

About The Project

The original intention of building such a 3D rendering system from scratch without any help of graphics library is to get a thorough understanding of the three-dimensional rendering process.

Logo

Built With

This project was totally refactored based on previous naive version I built 2 years ago. Now, I utilize the following third-party libraries to build this renderer. Please note that SDL2 is just for displaying the rendered results as well as handling mouse and key events.

Getting Started

Prerequisites

I build this project on Windows platform. Please make sure your system is equipped with the following softwares.

  • cmake:at least version 3.5
  • Microsoft visual studio 2017 or 2019

Installation

Please compile the project for x64 platform.

  1. Clone the repo

    git clone https://github.com/ZeusYang/TinySoftRenderer.git
  2. Use cmake to build the project:

    cd build
    cmake ..
    make
    

    or using cmake-gui is ok.

Please note that copy external/dlls/*.dll (for example: SDL2.dll) to the corresponding example binary directory for execution (like build/Release). Release mode is much more efficient than debug mode.

Usage

Please check out examples/ for more details.

Logo

int main(int argc, char* args[])
{
	constexpr int width =  666;
	constexpr int height = 500;

	TRWindowsApp::ptr winApp = TRWindowsApp::getInstance(width, height, "TinySoftRenderer-By yangwc");

	if (winApp == nullptr)
	{
		return -1;
	}

	bool generatedMipmap = true;
	TRRenderer::ptr renderer = std::make_shared<TRRenderer>(width, height);

	//Load scene
	TRSceneParser parser;
	parser.parse("../../scenes/complicatedscene.scene", renderer, generatedMipmap);

	renderer->setViewMatrix(TRMathUtils::calcViewMatrix(parser.m_scene.cameraPos,
		parser.m_scene.cameraFocus, parser.m_scene.cameraUp));
	renderer->setProjectMatrix(TRMathUtils::calcPerspProjectMatrix(parser.m_scene.frustumFovy,
		static_cast<float>(width) / height, parser.m_scene.frustumNear, parser.m_scene.frustumFar),
		parser.m_scene.frustumNear, parser.m_scene.frustumFar);

	winApp->readyToStart();

	//Blinn-Phong lighting
	renderer->setShaderPipeline(std::make_shared<TRBlinnPhongShadingPipeline>());

	glm::vec3 cameraPos = parser.m_scene.cameraPos;
	glm::vec3 lookAtTarget = parser.m_scene.cameraFocus;

	//Rendering loop
	while (!winApp->shouldWindowClose())
	{
		//Process event
		winApp->processEvent();

		//Clear frame buffer (both color buffer and depth buffer)
		renderer->clearColorAndDepth(glm::vec4(0.0f, 0.0f, 0.0f, 1.0f), 0.0f);

		//Draw call
		renderer->setViewerPos(cameraPos);
		auto numTriangles = renderer->renderAllDrawableMeshes();

		//Display to screen
		double deltaTime = winApp->updateScreenSurface(renderer->commitRenderedColorBuffer(),
                                                       width,  height, 3, numTriangles);

	}

	renderer->unloadDrawableMesh();

	return 0;
}

Features

  • Affine and perspective correct per vertex parameter interpolation.

  • Screen space back face culling (more robust compared to implementation in ndc space).

  • Z-buffering (reversed z) and depth testing for 3D rendering.

  • Sutherland Hodgeman homogeneous cliping. Refs: link1, link2

  • Accelerated edge function-based triangle rasterization (Implement top left fill rule). Refs: link

  • Texture mapping, nearest texture sampling, and bilinear texture sampling.

  • Tiling and morton curve memory layout for accessing to texture. (But it turns out that high-frequency address mapping is also time-consuming...) Refs: link1, link2

  • Implement Phong/Blinn-Phong illumination algorithm.

  • Regular light source for lighting: point light source, spot light source, and direcitonal light source.

Logo

  • Mipmap texture mapping, and trilinear sampling. Refs: link1, link2

Logo

  • Tangent space normal mapping.

Logo

  • Reinhard tone mapping (from HDR -> LDR).

Logo

  • Multi sampling anti-aliasing (MSAA 4X, and MSAA 8X)

Logo

  • Alpha blending, and alpha to coverage algorithm based on MSAA (order independent transparency).

Logo

  • Multi-thread parallelization using tbb as backend. The cpu usage could reach to 100%.

License

Distributed under the MIT License. See LICENSE for more information.

Contact

[email protected]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.