Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

Planet-LOD

> 编程语言
Open source

Planet Rendering: Adaptive Spherical Level of Detail based on Triangle Subdivision

340 stars0 likes2 views
WebsiteGitHub

About

Planet Rendering: Adaptive Spherical Level of Detail based on Triangle Subdivision

Planet-LOD - Spherical Level-of-Detail

License : MIT

http://opensource.org/licenses/MIT

Summary

This is a simple tutorial of how to render planet using a triangle subdivision approach. The source is optimized for compactness and to make the algorithm easy to understand. The algorithm renders an icosahedron with 12 triangles, each of which is being sub-divided using a recursive function.

If you would render the planet in a game engine, you would have to render a triangle patch for NxN triangles with a VBO inside the draw_triangle function, rather than a single triangle with gl immediate mode. The center of detail is where the camera would be in the game. The camera in the demo is above for demonstration purpose.

What the code is :

  • A simple demo to show how a planet rendering with LOD works
  • As short as possible so you can experiment with the algorithm
  • Easy to understand

What the code is not:

  • A ready to use planet rendering library with shaders frustum culling etc
  • A way to render a planet with best performance
  • A demonstration of modern high performance gl rendering

Discussion Forum

http://www.gamedev.net/topic/677700-planet-rendering-spherical-level-of-detail-in-less-than-100-lines-of-c/ https://www.reddit.com/r/gamedev/comments/4d2oez/planet_rendering_spherical_levelofdetail_in_less/

Screenshot

struct World
{
	static void draw_triangle(vec3f p1, vec3f p2, vec3f p3)
	{
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
		glBegin(GL_TRIANGLES);
		glVertex3f(p1.x, p1.y, p1.z);
		glVertex3f(p2.x, p2.y, p2.z);
		glVertex3f(p3.x, p3.y, p3.z);
		glEnd();
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	}
	static void draw_recursive(vec3f p1,vec3f p2,vec3f p3, vec3f center , float size=1)
	{
		float ratio = gui.screen[0].slider["lod.ratio"].val; // default : 1
		float minsize = gui.screen[0].slider["detail"].val;  // default : 0.01

		double dot = double(((p1+p2+p3)/3).dot(center));
		double dist = acos(clamp(dot, -1, 1)) / M_PI;

		if (dist > 0.5) return;//culling

		if (dist > double(ratio)*double(size) || size  p({ 
			{ -1, t, 0 }, { 1, t, 0 }, { -1, -t, 0 }, { 1, -t, 0 },
			{ 0, -1, t }, { 0, 1, t }, { 0, -1, -t }, { 0, 1, -t },
			{ t, 0, -1 }, { t, 0, 1 }, { -t, 0, -1 }, { -t, 0, 1 },
		});
		std::vector idx({ 
			0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 0, 10, 11,
			1, 5, 9, 5, 11, 4, 11, 10, 2, 10, 7, 6, 10, 7, 6, 7, 1, 8,
			3, 9, 4, 3, 4, 2, 3, 2, 6, 3, 6, 8, 3, 8, 9,
			4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7, 9, 8, 1
		});

		loopi(0, idx.size() / 3)
		{
			draw_recursive(
				p[idx[i * 3 + 0]].norm(), // triangle point 1
				p[idx[i * 3 + 1]].norm(), // triangle point 2
				p[idx[i * 3 + 2]].norm(), // triangle point 3
				center);
		}
	}
};

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言