Hello,
I am currently trying to render some polygons with a texture. I’ve already succesfully rendered a circle with a football texture but struggle to render a triangle with a texture.
It seems that I am not calculating (or setting?) the parameter values texture_dx and texture_dy properly.
What I get is this (I am using Box2D for collision/physics):
As you can see, the texture is not exactly on the rendered object, it sometimes “overlaps” and takes the other parts to. The Image is a simple yellow triangle with a white background:
What i want is that it only draws the yellow stuff, and not the white (I know I could just make the whole image yellow but I want to use this method to render triangle-sprites later e.g. spikes).
code:
/*1. get vertices and transfer them to pixels*/
b2Vec2 vertices_triangle[3];
for(int i = 0; i < 3; i++)
{
vertices_triangle[i] = triangle_body->GetWorldPoint(triangleShape.GetVertex(i));
}
// Calculate "render"vertices of the triangle
b2Vec2 A;
b2Vec2 B;
b2Vec2 C;
A.x = ((SCALED_WIDTH / 2.0f) + vertices_triangle[0].x) * PMFACTOR - (a / 2.0f);
A.y = ((SCALED_HEIGHT / 2.0f) + vertices_triangle[0].y) * PMFACTOR - (a / 2.0f);
B.x = ((SCALED_WIDTH / 2.0f) + vertices_triangle[1].x) * PMFACTOR - (a / 2.0f);
B.y = ((SCALED_HEIGHT / 2.0f) + vertices_triangle[1].y) * PMFACTOR - (a / 2.0f);
C.x = ((SCALED_WIDTH / 2.0f) + vertices_triangle[2].x) * PMFACTOR - (a / 2.0f);
C.y = ((SCALED_HEIGHT / 2.0f) + vertices_triangle[2].y) * PMFACTOR - (a / 2.0f);
/*2. draw*/
Sint16 vx[3] = {static_cast<Sint16>(B.x), static_cast<Sint16>(C.x), static_cast<Sint16>(A.x)};
Sint16 vy[3] = {static_cast<Sint16>(B.y), static_cast<Sint16>(C.y), static_cast<Sint16>(A.y)};
// dx? dy?
texturedPolygon(renderer, vx, vy, 3, surface, 10, 20);
I don’t think that anything is wrong with the calculations of the vertices - it just seems that I am not using the texture_dx and texture_y parametera properly.
I appreciate any help! Thanks