Anton Shkurenko
Anton Shkurenko

Reputation: 4327

Texture not scaled OpenGL ES 2.0

I am trying to attach textures to my game objects, I found, that texture is always cropped to my object's size, so if I set radius to the 1f, I can see full texture, else I can see just center cropped. Here you can see, how I create circle. I'm pretty new to the OpenGL, so I really don't know what else I can show you. Thanks for help.

Here is medium: Medium radius Here is full screen radius: Full radius

private ViewObjectBuilder appendCircle(Geometry.Circle circle, int numPoints, float aspectRatio) {

    final int startVertex = mOffset / FLOATS_PER_VERTEX;
    final int numVertices = sizeOfCircleInVertices(numPoints);

    mVertexData[mOffset] = circle.center.x / aspectRatio;
    mTextureData[mOffset++] = (circle.center.x + 1f) * 0.5f;
    mVertexData[mOffset] = circle.center.y;
    mTextureData[mOffset++] = (circle.center.y + 1f) * 0.5f;

    for (int i = 0; i <= numPoints; i++) {
      float angleInRadians = ((float) i / (float) numPoints) * ((float) Math.PI * 2f);

      final float c = (float) Math.cos(angleInRadians);
      final float s = (float) Math.sin(angleInRadians);

      mVertexData[mOffset] =
          circle.center.x + circle.radius * c / aspectRatio;

      mTextureData[mOffset++] =
          (circle.center.x + circle.radius * c + 1f) * 0.5f;

      mVertexData[mOffset] = circle.center.y + circle.radius * s;

      mTextureData[mOffset++] =
          (circle.center.y + circle.radius * s + 1f) * 0.5f;
    }

    mDrawList.add(() -> glDrawArrays(GL_TRIANGLE_FAN, startVertex, numVertices));
    return this;
  }

Upvotes: 0

Views: 261

Answers (1)

Thomas
Thomas

Reputation: 88707

It's probably due to recalculating the texture coordinates based on the circle radius. If you want to resize the triangle fan and let the texture scale just reposition the vertices (or better transform the vertex positions with a matrix).

Assuming the squad you are forming with your triangle fan should display the entire texture the coordinates would be 0.0/0.0, 0.0/1.0, 1.0/0.0 and 1.0/1.0. As you can see they are independent of vertex coordinates and in fact don't change at all (unless you want to animate the texture somehow).

Edit:

Rereading your code it seems your object contains more triangles to form an actual circular shape. In that case you'd just calculate the texture coordinates as you do (didn't fully check for correctness) but use a radius of 1 and thus you can use sine and cosine directly with an offset:

mTextureData[mOffset++] = c * 0.5f + 0.5f;
//assume c = 1.0 then your coordinate becomes 1.0, 
//if c = -1.0 then your coordinate becomes 0.0 etc.

Btw, incrementing the offset like above might lead to hard to track bugs if you add more code or move lines around. Better increment separately at the end of the loop.

As for rotating the circle: normally you'd just rotate the vertex positions, i.e. recalculate those. The texture coordinates are not changes. Alternatively you keep the vertex positions and rotate the texture coordinates but that would make it more complicated if scaling, repositioning etc. are employed as well.

Upvotes: 1

Related Questions