Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
252 views
in Technique[技术] by (71.8m points)

c++ - How to make custome shape deformation?

I have a sf::RectangleShape 2D cube and want to create functionality to draw sides "like 3D" for that cube.
This is my solution:

                //X. Y
//gonyometric latters for clearity
sf::Vector2f A(300, 500);
sf::Vector2f B(500, 500);
sf::Vector2f E(300, 300);
sf::Vector2f F(500, 300);

sf::RectangleShape cube;
cube.setPosition(300, 300);
cube.setSize(sf::Vector2f(200,200));

cube.setTexture(&brick_texture);

// convex shape
sf::ConvexShape rightSide;

rightSide.setPointCount(4);
rightSide.setPosition(B);

rightSide.rotate(-45);


rightSide.setPoint(0 ,sf::Vector2f(0, 0));
sf::Vector2f C(100,0);
rightSide.setPoint(1, C);
sf::Vector2f D(100+(200 * getSin(45)), (0-200* getCos(45)));

rightSide.setPoint(2, D);

rightSide.setPoint(3, sf::Vector2f(0 + (200 * getSin(45)), (0 - 200 * getCos(45))));

rightSide.setTexture(&brick_texture);

This is how it looks: drawed cube

I can't figure out how to edit shape of texture to shape of rightSide to make it really like 3D.

Solution:

I used custome matrix of sf::Transform to manage shape deformation. And replaced convex by rectangleShape.

Fastest and easiest way to make it is create whole matrix with default parameters and change values you want. Other more comlex soution is to take matrix from your shape, convert it to float array inside 4x4 loop(here you can edit its elements with cute switch) and bind parts of that array properly to new 3x3 matrix.

This is result:

solved


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you could do this by passing a customized sf::Transform to your draw call as described here.

For an orthographic projection, I think a shear transformation is all you would need. However, sf::Transform does not support shearing out-of-the-box, so you would have to build the matrix yourself. (Note that you don't have to build the whole transformation matrix, just the shear part. Then you can use something like customTransform * sprite.getTransform().)

Alternatively, you could create just sprites that look 3D and then layer them in the proper order to build up your scene.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...