Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: RenderTexture question  (Read 8597 times)

0 Members and 2 Guests are viewing this topic.

Ockonal

  • Jr. Member
  • **
  • Posts: 58
    • ICQ Messenger - 449909010
    • View Profile
    • WinCode
RenderTexture question
« on: October 23, 2012, 12:23:40 am »
Hello, I have a render texture and now I found that it's turned with 2Pi radians. Now when I want to convert some coords with renderTarget()->convertCoords(x, y) I get them but refer to bottom&right, not left&top.

When I call renderTarget.display() the texture becomes "normal". That's a feature? :)
Developing 2d engine&game using: sfml, box2d, librocket, spark2, zoom&zoost, thor

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: RenderTexture question
« Reply #1 on: October 23, 2012, 12:29:35 am »
That is a Laurenture™.
From docs of RenderTexture::display():
Quote
This function updates the target texture with what has been drawn so far. Like for windows, calling this function is mandatory at the end of rendering. Not calling it may leave the texture in an undefined state.
Back to C++ gamedev with SFML in May 2023

Ockonal

  • Jr. Member
  • **
  • Posts: 58
    • ICQ Messenger - 449909010
    • View Profile
    • WinCode
Re: RenderTexture question
« Reply #2 on: October 23, 2012, 12:31:36 am »
I know about this but, probably, the main question is how to process convertCoords during drawing (before display call) if my texture has another coords and that function returns vector from another origin?
Developing 2d engine&game using: sfml, box2d, librocket, spark2, zoom&zoost, thor

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: RenderTexture question
« Reply #3 on: October 23, 2012, 01:02:48 am »
I don't think there is anything suggesting you can't call display of texture a few times.. not sure, might want Laurent to clarify that.
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: RenderTexture question
« Reply #4 on: October 23, 2012, 06:42:10 am »
Quote
Hello, I have a render texture and now I found that it's turned with 2Pi radians
You probably mean Pi, because with 2Pi you wouldn't see any difference ;D

Quote
the main question is how to process convertCoords during drawing (before display call)
I don't get it. The texture is flipped when you call display(), but it is just a visual issue, all the functions such as convertCoords work fine. So what exactly is your problem?
Laurent Gomila - SFML developer

Ockonal

  • Jr. Member
  • **
  • Posts: 58
    • ICQ Messenger - 449909010
    • View Profile
    • WinCode
Re: RenderTexture question
« Reply #5 on: October 23, 2012, 03:46:48 pm »
Okay, the video:

You can see that without camera movement texture taken for the water is fine. But it becomes invalid when camera moves.

A few code:

    sf::RectangleShape rectangle;
    rectangle.setOutlineColor(sf::Color::Blue);
    rectangle.setOutlineThickness(2);
    rectangle.setFillColor(sf::Color::Transparent);
    rectangle.setPosition(leftTop);
    rectangle.setSize(rightBottom);
    mTarget->draw(rectangle);

This is the code for blue rectangle you see in the video. It's calculated right (because it also moves with camera).

leftTop and rightBottom - magic coordinates I'm calculate using convertCoords from window.

        sf::IntRect rect = sf::IntRect(leftTop.x, leftTop.y, rightBottom.x, rightBottom.y);
        sf::Texture map(mTarget->getTexture());

        // Apply texture to polygon
        polygon.setTexture(&map);
        polygon.setTextureRect(rect);

mTarget - sf::RenderTexture, everything is drawn there. polygon - convex shape of the blue water you can see in video. I'm trying to set the texture coords I want to apply for the polygon. And they are wrong :( I can't understand why, because blue rectangle draws fine and texture coordinates which uses same leftTop/rightBottom.

Any ideas?
Developing 2d engine&game using: sfml, box2d, librocket, spark2, zoom&zoost, thor

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: RenderTexture question
« Reply #6 on: October 23, 2012, 04:36:21 pm »
Quote
leftTop and rightBottom - magic coordinates I'm calculate using convertCoords from window.
That seems to be where the problem is, so you should show the relevant code.
Laurent Gomila - SFML developer

Ockonal

  • Jr. Member
  • **
  • Posts: 58
    • ICQ Messenger - 449909010
    • View Profile
    • WinCode
Re: RenderTexture question
« Reply #7 on: October 23, 2012, 05:38:42 pm »
    b2Vec2 leftTopB     = prmLiquid->mFieldAABB.lowerBound;
    b2Vec2 rightBottomB = prmLiquid->mFieldAABB.upperBound;

    float yMax = some_float_value;

    // Key points for the subrect of the water (Full bounding box including surface deformation)
    sf::Vector2f leftTop     = prmDisplayManager->ConvertMetersCoords(leftTopB.x, yMax);
    sf::Vector2f rightBottom = prmDisplayManager->ConvertMetersCoords(rightBottomB.x, rightBottomB.y);
    rightBottom -= sf::Vector2f(leftTop.x, leftTop.y);

            inline sf::Vector2f ConvertMetersCoords(double x, double y)
            {
                sf::Vector2i pos(static_cast<int>(Meters2Pixels(x)), static_cast<int>(Meters2Pixels(y)));
                return renderTarget()->convertCoords(pos);
            }

leftTopB, rightBottomB are physical coordinates taken from box2d. They are in meters, not pixels.
yMax is the highest rectangle Y-points. There is some value in meters too.

leftTop, rightBottom: you saw them in my previous post, the blue rectangle is rendered using this variables.

ConvertMetersCoords takes coordinates in meters, converts them into pixels (using some float-coefficient).
renderTarget() is an object of sf::RenderTexture.
Developing 2d engine&game using: sfml, box2d, librocket, spark2, zoom&zoost, thor

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: RenderTexture question
« Reply #8 on: October 23, 2012, 06:40:49 pm »
Do you use one or more views when you draw to renderTarget()? Or only the default one?

You should also print all the intermediate values, you'll probably quickly find where coordinates start to be wrong.
Laurent Gomila - SFML developer

Ockonal

  • Jr. Member
  • **
  • Posts: 58
    • ICQ Messenger - 449909010
    • View Profile
    • WinCode
Re: RenderTexture question
« Reply #9 on: October 23, 2012, 07:07:32 pm »
I have a view inside my display manager and all changes are applied to it.

The problem is that blues rectangle coordinates doesn't change. They are always the same even when camera is moving.
Developing 2d engine&game using: sfml, box2d, librocket, spark2, zoom&zoost, thor

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: RenderTexture question
« Reply #10 on: October 23, 2012, 08:49:57 pm »
So maybe you must pass the view to convertCoords? This is necessary if the view is not active when you call it.
Laurent Gomila - SFML developer

Ockonal

  • Jr. Member
  • **
  • Posts: 58
    • ICQ Messenger - 449909010
    • View Profile
    • WinCode
Re: RenderTexture question
« Reply #11 on: October 24, 2012, 02:56:20 pm »
Now I don't know what to do :(

Everything is rendered into sf::RenderTexture. Before water rendering I call:

mRenderTarget->setView(myCamera);
// Calculate polygon

sf::IntRect rect = sf::IntRect(leftTop.x, leftTop.y, rightBottom.x, rightBottom.y);
polygon.setTexture(&map);
polygon.setTextureRect(rect);

When I show up leftTop and rightBottom they are always the same (of course). Before camera moves everything is fine and texture coordinates are okay. Now I try to move camera somewhere else:

myCamera->move(-4, 0);
mRenderTarget->setView(myCamera);

Sure, now leftTop and rightBottom becomes invalid (I don't convert their coordinates refer to view perspective).

So, the first try is:
leftTop = mRenderTarget->convertCoords(sf::Vector2i(leftTop.x, leftTop.y), *myCamera);
rightBottom = mRenderTarget->convertCoords(sf::Vector2i(rightBottom.x, rightBottom.y), *myCamera);

Now they are changing when camera moves but the coordinates are incorrect and I see same troubles as in video I've uploaded before.

At the end of render cycle I do:

// mApp is sf::RenderWindow
mRenderTarget->display();
mApp->draw(sf::Sprite(mRenderTarget->getTexture()));
mApp->display();
Developing 2d engine&game using: sfml, box2d, librocket, spark2, zoom&zoost, thor

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: RenderTexture question
« Reply #12 on: October 24, 2012, 03:12:37 pm »
I'm lost in your rendering process. It's a little complicated to follow. Could you describe all the rendering steps of a complete cycle, i.e. what do you draw at each step, with which texture and to which target?

Of course if would be even nicer if you could write a complete and minimal program that just reproduces this problem, but it requires a little more work from you (but... that's the only way to track more complex bugs like this one, staring at the original code is sometimes not enough) :)
Laurent Gomila - SFML developer

Ockonal

  • Jr. Member
  • **
  • Posts: 58
    • ICQ Messenger - 449909010
    • View Profile
    • WinCode
Re: RenderTexture question
« Reply #13 on: October 24, 2012, 03:29:48 pm »
Okay, I need a few time to reproduce small example. A few tests:

Here is a few attachments. Blue circle is rendered in (0, 0). Magenta circle is rendered in leftTop

leftTop = mRenderTarget->convertCoords(sf::Vector2i(leftTop.x, leftTop.y), *mCamera);

The value of leftTop is in attachment names (x;y) (forum has renamed them and now they are: x?y

[attachment deleted by admin]
« Last Edit: October 24, 2012, 03:32:22 pm by Ockonal »
Developing 2d engine&game using: sfml, box2d, librocket, spark2, zoom&zoost, thor

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: RenderTexture question
« Reply #14 on: October 24, 2012, 03:44:04 pm »
Ok, I start to understand what's going on. In this context you mustn't use convertCoords, it doesn't do what you need: it converts a point in target coordinates to a point in world coordinates. Typically, it is used to know the world coordinates of a mouse click (which is initially given in pixels of the window, not in units of your 2D world).

One clue that should make you notice that there's something wrong: you had to convert leftTop, a Vector2f, to a Vector2i. When you need to perform this kind of conversion, it usually means that you badly mix coordinate systems; integer and float coordinates should never interfere.

What you want to do is the inverse: given a point in world coordinates (the topLeft corner of your rectangle entity), where will this point be drawn to the render-texture, with the given view?

So instead of calling convertCoords, you can try calling mCamera->getTransform().transformPoint(leftTop). It's a kind of hack though, this inverse conversion is not "officially" supported in the API (look at View::getTransform(), it is marked as "internal use only") :D

It may not be easy to understand with words, so if you don't get it take a paper and draw the world (which contains all your entities), the render-texture (which shows a small part of your world according to the current view), and ask yourself "where does my starting point belong? (world or target)", and "where does the point I want to calculate belong? (world or target)". This gives you the conversion you need to apply (world to target, or target to world), and the function you need to call.
« Last Edit: October 24, 2012, 03:48:39 pm by Laurent »
Laurent Gomila - SFML developer

 

anything