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

Author Topic: Render to texture implementation question  (Read 24009 times)

0 Members and 3 Guests are viewing this topic.

wizardofoz

  • Newbie
  • *
  • Posts: 18
    • View Profile
Render to texture implementation question
« on: November 15, 2008, 02:23:48 pm »
Hi all, I have used SFML library for a couple of days and I found it very easy and clean.

"Render to texture" (with a seamless integration of PostFX) is really a necessary feature that would make SFML even more powerful and suitable for bigger projects.

While I'm waiting for the "official" version of this feature I tried to implement it by myself.

I'm writing a RenderImage class (derived from Image and RenderTarget classes) using WGL_ARB_pbuffer extension as well explained in both NVIDIA and ATI articles.

My RenderImage class seems to work. At the moment I can render Drawables into that class and then render the result to the window  by creating a Sprite from RenderImage (I haven't tested it with PostFX yet).

But I've a problem with the render origin. OpenGL axis origin (X=0; Y=0) of the rendering context of the texture is in the bottom left corner instead of the upper left corner.

I would like to know how can I change the axis origin of my texture with SFML. Have I to modify something in the View member of the RenderTarget class?

Thanks to all

coral

  • Newbie
  • *
  • Posts: 37
    • View Profile
Render to texture implementation question
« Reply #1 on: November 15, 2008, 04:28:17 pm »
I am not really sure if this fixes it, but:
Quote

void sf::RenderWindow::PreserveOpenGLStates     ( bool    Preserve  )      

Tell SFML to preserve external OpenGL states, at the expense of more CPU charge.

Use this function if you don't want SFML to mess up your own OpenGL states (if any). Don't enable state preservation if not needed, as it will allow SFML to do internal optimizations and improve performances. This parameter is false by default

wizardofoz

  • Newbie
  • *
  • Posts: 18
    • View Profile
Render to texture implementation question
« Reply #2 on: November 15, 2008, 05:37:23 pm »
This could solve the problem but at performance cost.

Normally I would use glOrtho() to invert the axes but SMFL uses its own matrices system...

I hate matrices ( :wink: )...

wizardofoz

  • Newbie
  • *
  • Posts: 18
    • View Profile
Render to texture implementation question
« Reply #3 on: November 15, 2008, 05:59:29 pm »
PreserveOpenGLStates() only preserves custom states but doesn't change the SFML states used in the rendering of the Drawable.

wizardofoz

  • Newbie
  • *
  • Posts: 18
    • View Profile
Render to texture implementation question
« Reply #4 on: November 16, 2008, 10:37:24 am »
I "potentially" solved the problem:

SetView(View(FloatRect(0,myBufferWidth,myBufferHeight,0)));

This simple line inverts y-axis and all works but...

But in the original code this line breaks the program because myCurrentView is a constant:
const View* myCurrentView;


So if I use SetView(), this function generates weirds values and corrupts the program.

I had to modify:
const View * myCurrentView;
to
View myCurrentView;

and so SetView() works without any problem.

I was happy because I succeded to implement RenderImage without changing any single line of the original SFML code (except for CreateTexture() that I had to make it virtual).

Now I had to change a core part of SFML that is myCurrentView member of RenderTarget class so I don't think my RenderImage class could be useful for you..

But if you find a solution I will post my RenderImage class if it can be of any help..

Best regards.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Render to texture implementation question
« Reply #5 on: November 17, 2008, 07:52:39 am »
I'd be glad to see your code. I'm especially interested because you're using PBuffers and not FBOs, so you need a high amount of non-portable code.

Regarding your view problem, I guess you're passing a local variable that's why the pointer kept by the RenderWindow gets corrupted after your function ends.
Laurent Gomila - SFML developer

wizardofoz

  • Newbie
  • *
  • Posts: 18
    • View Profile
Render to texture implementation question
« Reply #6 on: November 17, 2008, 02:59:26 pm »
Quote
I guess you're passing a local variable

Sure! Thanks, yesterday I was tired.. I solved with this two lines:

Code: [Select]

// Inverts Y-Axis
GetDefaultView().SetFromRect(FloatRect(0,static_cast<float>(myBufferWidth),static_cast<float>(myBufferHeight),0));
SetView(GetDefaultView());


Quote
I'd be glad to see your code. I'm especially interested because you're using PBuffers and not FBOs, so you need a high amount of non-portable code.


We focus only on Win32 platforms and I'm not a professional programmer so my RenderImage class has been tested only on Windows XP and Windows Vista.

I made some changes to my RenderImage class today so it should work without any SFML code modification, simply by adding RenderImage.hpp and RenderImage.cpp to the Graphics Project of SFML Solution.

-----------------------------

Here is my code, please feel free to throw it away, modify or use it. I hope it could be of little help.

Quote
I'm especially interested because you're using PBuffers and not FBOs

Have PBuffers a better compatibility than FBO?
 ( http://lwjgl.org/wiki/doku.php/lwjgl/tutorials/opengl/pbuffervsfbo )

My RenderImage references: http://ati.amd.com/developer/ATIpbuffer.pdf

-----------------------------

I also made an example to show RenderImage use, it's included in the rar file.



RenderImage and Example sources here:
http://www.megaupload.com/?d=9C19OO5R

-----------------------------

TODO:

Add checking to control that we haven't lost our pbuffer due to a display mode change with:
Code: [Select]
wglQueryPbufferARB(hPBuffer, WGL_PBUFFER_LOST_ARB, &flag);