Page 1 of 1

Texturing triangle fans

Posted: Sat Oct 17, 2009 1:28 am
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();

Re: Texturing triangle fans

Posted: Sat Oct 17, 2009 2:29 am
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.