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; } 

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Later I want to rotate object and texture also. How can they be independent, if I calculate there in the one cycle?
Radius that is passed there is the same for object and view, and I changed it to make screenshots. Formula is: (center.x + radius * c + 1f) * 0.5f; So if radius is 0.25, it can't be 1, center is (0,0)
I dunno, even now I can't understand why it worked with cosine * 0.5 + 0.5f, but it worked. THANKS!
@AntonShkurenko texture coordinates are provided in texture space, vertex coordinates in world space, i.e. they are in different spaces. If you don't want to repeat a texture you normally provide texture coordinates in the range [0,1], i.e. "the position of the vertex on the texture".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.