Electroblob's Wizardry

Electroblob's Wizardry

18M Downloads

Support for textured spheres! They'd be awesome :)

FavouriteDragon opened this issue ยท 4 comments

commented

As an addon developer, something that I think would be really cool is the ability to give spheres a texture. Currently, you can only colour them, and I'm not good enough to make a method to give them a texture without breaking everything :P
Thanks!

commented

The reason I never textured them was it's difficult to map a texture made of square pixels onto a sphere without it looking pretty rubbish, especially given that I use a UV sphere. You'd find all the pixels get squashed at the top and bottom. If you don't mind that, the code would probably be something like this:

buffer.begin(GL11.GL_TRIANGLE_STRIP, DefaultVertexFormats.POSITION_TEX_COLOR);

boolean goingUp = inside;

buffer.pos(0, goingUp ? -radius : radius, 0).color(r, g, b, a).endVertex(); // Start at the north pole

for(float longitude = -(float)Math.PI; longitude <= (float)Math.PI; longitude += longStep){

	// Leave the poles out since they only have a single point per stack instead of two
	for(float theta = (float)Math.PI/2 - latStep; theta >= -(float)Math.PI/2 + latStep; theta -= latStep){

		float latitude = goingUp ? -theta : theta;

		float hRadius = radius * MathHelper.cos(latitude);
		float vy = radius * MathHelper.sin(latitude);
		float vx = hRadius * MathHelper.sin(longitude);
		float vz = hRadius * MathHelper.cos(longitude);

                float u = longitude / (2*(float)Math.PI) + 0.5f;
                float v = 0.5f - latitude / (float)Math.PI; // Alternative mapping: v = 0.5f - vy / (2*radius);

		buffer.pos(vx, vy, vz).tex(u, v).color(r, g, b, a).endVertex();

		vx = hRadius * MathHelper.sin(longitude + longStep);
		vz = hRadius * MathHelper.cos(longitude + longStep);

                float u = (longitude + longStep) / (2*(float)Math.PI) + 0.5f;

		buffer.pos(vx, vy, vz).tex(u, v).color(r, g, b, a).endVertex();
	}

	// The next pole
        float u = longitude / (2*(float)Math.PI) + 0.5f;
	buffer.pos(0, goingUp ? radius : -radius, 0),tex(u, goingUp ? 0 : 1).color(r, g, b, a).endVertex();

	goingUp = !goingUp;
}

All I've done is add some extra calculations to change the latitude/longitude angles to UV texture coordinates, and then added a tex(u, v) call to each vertex.

commented

Ok cool- thank you!

commented

I should also bind the texture through mc's render engine, right?

commented

After testing it, it gives me a flat horizontal image instead of a sphere