I’m trying to map a rectangular texture on some segment of a circle. A segment of 70° in this context.
My problem is that the texture is just laid on to the circle and not squeezed to fit into it, so it’s probably a problem of texture coordinates.
The texture I’m using is on the top left and the desired result is the top right image.
I’m using the following code to draw the circle. The vertices are fed into a VAO that is drawn using GL_TRIANGLE_FAN. The first parameter of Vertex is the position and the second the texture coordinates.
std::vector<Vertex> drawCircle(GLfloat cx, GLfloat cy, GLfloat radius) {
std::vector<Vertex> vertices;
vertices.push_back(Vertex(glm::vec3(cx, cy, 0), glm::vec2(0.5, 0.0)));
for (float i = 55; i <= 125; i++)
{
float angle_radians = i * M_PI / 180.0f;
float x_val = cos(angle_radians);
float y_val = sin(angle_radians);
float x = radius * x_val;
float y = radius * y_val;
vertices.push_back(Vertex(glm::vec3(cx + x, cy + y, 0),
glm::vec2(x_val * 0.5 + 0.5, y_val * 0.5 + 0.5)));
}
return vertices;
}
Im using a standard vertex shader. Here is the main method my fragment shader:
void main()
{
float t_inv = 1.0 - texCoord0.t;
gl_FragColor = texture2D(texture, vec2(texCoord0.x, t_inv));
}
This results in the bottom left image.
I found some code online which changes the fragment shader to use a different uv mapping:
vec2 uv = vec2((texCoord0.s - t_inv / 2.0) / (1 - t_inv), t_inv);
gl_FragColor = texture2D(texture, uv);
which results in the bottom right image, which is better but not quite what I want to archieve.
Any help would be kindly appreciated.