Split/tesselate rect into quads

Started by
-1 comments, last by belfegor 10 years, 11 months ago

I need to split window rectangle into multiple quads. I got this far but output is wrong and i can't see how to resolve it:


static const float quadDim = 200.0f;
 
        // dim is rect dimensions

        std::size_t horCnt  = (std::size_t)std::ceil(dim.x / quadDim);

        std::size_t vertCnt = (std::size_t)std::ceil(dim.y / quadDim);



        for(std::size_t i = 0; i < horCnt; ++i)

        {

            for(std::size_t j = 0; j < vertCnt; ++j)

            {

                float x = i * quadDim;

                float y = j * quadDim;



                float xx = std::fmod(x + quadDim, dim.x);

                float yy = std::fmod(y + quadDim, dim.y);



                D3DXVECTOR3 vertCoords[4];

                vertCoords[0] = D3DXVECTOR3(absPos.x + x,   absPos.y + y,  pos.z); // top left

                vertCoords[1] = D3DXVECTOR3(absPos.x + xx,  absPos.y + y,  pos.z); // top right

                vertCoords[2] = D3DXVECTOR3(absPos.x + xx,  absPos.y + yy, pos.z); // bottom left

                vertCoords[3] = D3DXVECTOR3(absPos.x + x,   absPos.y + yy, pos.z); // bottom right



                float r = float(i + 1) * (1.0f / float(horCnt));

                float g = float(j + 1) * (1.0f / float(vertCnt));

                D3DXVECTOR4 newColor( r, g, (r+g) * 0.5f, 1.0f); // debug color



                drawer->addVertex(Vertex(vertCoords[0], texCoords[0], newColor)); // top left

                drawer->addVertex(Vertex(vertCoords[1], texCoords[1], newColor)); // top right

                drawer->addVertex(Vertex(vertCoords[2], texCoords[2], newColor)); // bottom right

                drawer->addVertex(Vertex(vertCoords[3], texCoords[3], newColor)); // bottom left

            }

        }

quads.jpg

If you need more info please tell.

Thank you for your time.

EDIT:

Looks like i solved the vertex coords:

quads.jpg

float x = i * quadDim;
                float y = j * quadDim;

                float xx = std::fmod( x + quadDim, dim.x);
                float yy = std::fmod( y + quadDim, dim.y);

                xx = (xx < quadDim) ? (quadDim - xx) + x : xx;
                yy = (yy < quadDim) ? (quadDim - yy) + y : yy;

Now i have trouble with calculating texcoords:

quads2.jpg

float xint, xfrac, yint, yfrac;
                xfrac = std::modf((xx - x) / quadDim, &xint);
                yfrac = std::modf((yy - y) / quadDim, &yint);

                D3DXVECTOR2 texCoords[4];
                texCoords[0] = D3DXVECTOR2(0.0f,                  0.5f);
                texCoords[1] = D3DXVECTOR2(0.5f * (1.0f - xfrac), 0.5f);
                texCoords[2] = D3DXVECTOR2(0.5f * (1.0f - xfrac), 1.0f * (1.0f - yfrac));
                texCoords[3] = D3DXVECTOR2(0.0f,                  1.0f * (1.0f - yfrac));

I am using bottom left quarter of texture because it contains the component that i need to display on quads. Looks like only bottom (y) is wrong.

This topic is closed to new replies.

Advertisement