Texturing triangle fans

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Texturing triangle fans

Post by MarauderIIC »

It's late, and I got (the texture coordinate part of) this code from wherever, and it works just fine, but I'm not sure why the equation for the texture coordinates is what it is. Why "+1 * 0.5"? (Java)

Code: Select all

		
		float texcoord[] = {0.5f, 0.5f};

		texture.enable();
		texture.bind();
		gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);
		// Clock face
		gl.glBegin(GL.GL_TRIANGLE_FAN);
		{
			//gl.glColor3f(1, 1, 1);

			gl.glTexCoord2fv(texcoord, 0);
			gl.glVertex2f(0, 0);

			final float max = NUM_PARTS * ANGLE_CHANGE;
			for (float rad = 0; rad < max; rad += ANGLE_CHANGE) {
				final double cosr = cos(rad);
				final double sinr = sin(rad);
				texcoord[0] = (float)((cosr + 1.0) * 0.5); //<--this part
				texcoord[1] = (float)((sinr + 1.0) * 0.5); //<-- & this part
				gl.glTexCoord2fv(texcoord, 0);
				gl.glVertex2d(cosr * radius, sinr * radius);
			}
		}
		gl.glEnd();
		texture.disable();
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: Texturing triangle fans

Post by qpHalcy0n »

They're just mapping sin and cos, which have a [-1, 1] range to something more usable for a texture lookup [0, 1].

More conventionally you'd see it as " * 0.5 + 0.5" (eg: cosr * 0.5 + 0.5) so that -1 maps to 0 and 1 maps to 1. The way they've presented it there works just the same.
Post Reply