GithubHelp home page GithubHelp logo

templeblock / trianglepacker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ands/trianglepacker

0.0 1.0 0.0 48 KB

A C/C++ single-file library that packs triangles of a 3D mesh into a rectangle/texture.

C 100.00%

trianglepacker's Introduction

Triangle Packer

trianglepacker.h is a C/C++ single-file library that packs triangles from a 3D mesh into a 2D map with some specified map size, border and spacing between triangles. It uses a fast greedy algorithm. The output is not optimal!

I implemented this to see how it performs in a light mapping context. Unfortunately, there were often visible seams between adjacent triangles. Neighbours in the mesh are located in completely different places in the map and thus are not interpolated correctly. Even slight differences of their edge intensities were very noticeable. Another disadvantage is, that all mesh vertices will be unique and can not be reused. I can imagine other use cases for this code, but please tell me yours too ;).

To paste the implementation into your project, insert the following lines:

#define TRIANGLEPACKER_IMPLEMENTATION
#include "trianglepacker.h"

Triangle Packer Example Output

Example usage

The following example packs all triangles into a specified map resolution and returns the determined 3D->2D scaling factor.

float scale3Dto2D;
if (!tpPackIntoRect(
	// mesh (consecutive triangle positions):
	(const float*)mesh->positions, mesh->vertexCount,
	// map (pixel-space parameters):
	map->width, map->height, map->border, map->triangleSpacing,
	// output (a [0..1]x[0..1] uv coordinate for each input vertex):
	mesh->uvs,
	// output (3D units to 2D pixel space scaling factor):
	&scale3Dto2D))
{
	fprintf(stderr, "Could not find a scaling factor to fit all triangles into the map!\n");
	return;
}

trianglepacker's People

Contributors

ands avatar

Watchers

simongao avatar

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.