Jump to content
  • Advertisement
Sign in to follow this  

Split/tesselate rect into quads

This topic is 1951 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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






If you need more info please tell.


Thank you for your time.



Looks like i solved the vertex coords:




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:



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.

Edited by belfegor

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!