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

Author Topic: [FIXED] sf::RenderImage::SetActive()  (Read 2556 times)

0 Members and 2 Guests are viewing this topic.

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
[FIXED] sf::RenderImage::SetActive()
« on: March 11, 2010, 10:43:51 pm »
Looks like when I am using OpenGL and try to draw to a render image I must draw something (using sf::Sprite) to it, like this:
Code: [Select]
// Activate the window
surface.SetActive(true);
// Explicitely draw a new empty sprite
sf::Sprite sprite;
surface.Draw(sprite);
// OpenGL drawing ...

If I don't, it never activates for rendering. Looks like the problem of explicit binding in images that someone told you a week ago.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[FIXED] sf::RenderImage::SetActive()
« Reply #1 on: March 12, 2010, 07:50:43 am »
Which revision are you using?

Can you provide a complete and minimal example that reproduces this problem?
Laurent Gomila - SFML developer

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
[FIXED] sf::RenderImage::SetActive()
« Reply #2 on: March 14, 2010, 09:34:39 pm »
Sure, sorry, it took so long. I am using dynamic SFML2, haven't tried static.

I use last revision.

The last two functions are used to provide a 2D projection (taking OpenGL units as pixels).

Code: [Select]
//////////////////////////////////////////////////////////////////////////
// Headers
//////////////////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>

// Forward declarations
void glEnable2D();
void glDisable2D();

//////////////////////////////////////////////////////////////////////////
// Main
//////////////////////////////////////////////////////////////////////////
int main()
{
// Data
sf::RenderImage surface;
sf::Image image;

// Load an image
image.LoadFromFile("floor.png");

// Create and activate surface
surface.Create(800, 600);
surface.SetActive(true);

// - ATTENTION, PROBLEM HERE -
// Draw an empty sprite (uncomment this to make this program work)
{
//sf::Sprite sprite;
//surface.Draw(sprite);
}
// - ATTENTION, PROBLEM HERE

// Initialize OpenGL for rendering to surface
{
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_ONE, GL_ONE);
glDepthMask(GL_TRUE);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.f, 1.f, 1.f, 3000.f);
glMatrixMode(GL_MODELVIEW);
}

// Bind loaded image
image.Bind();
// Draw image to surface
glEnable2D();
glBegin(GL_QUADS);
{
glTexCoord2f(0.f, 1.f); glVertex2f(0, 0);
glTexCoord2f(0.f, 0.f); glVertex2f(0, 200);
glTexCoord2f(1.f, 0.f); glVertex2f(200, 200);
glTexCoord2f(1.f, 1.f); glVertex2f(200, 0);
}
glEnd();
glDisable2D();

// Update surface pixels
surface.Display();
// Save surface to file
surface.GetImage().SaveToFile("surface.png");

/*
Now it's supposed to see the image floor.png drawn to surface.png,
but it isn't as long as you uncomment the code above
*/
}

//////////////////////////////////////////////////////////////////////////
// Enable 2D projection
//////////////////////////////////////////////////////////////////////////
void glEnable2D()
{
static int vPort[4];

glGetIntegerv(GL_VIEWPORT, vPort);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

glOrtho(vPort[0], vPort[0]+vPort[2], vPort[1], vPort[1]+vPort[3], -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(0.375f, 0.375f, 0.f);

glPushAttrib(GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
}

//////////////////////////////////////////////////////////////////////////
// Disable 2D projection
//////////////////////////////////////////////////////////////////////////
void glDisable2D()
{
glPopAttrib();
glMatrixMode(GL_PROJECTION);
glPopMatrix();  
glMatrixMode(GL_MODELVIEW);
glPopMatrix();      
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[FIXED] sf::RenderImage::SetActive()
« Reply #3 on: March 14, 2010, 11:40:22 pm »
Hi

Thanks for your code :)

You just forgot to setup the viewport. A call to glViewport makes the code work without drawing the sprite.
Laurent Gomila - SFML developer

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
[FIXED] sf::RenderImage::SetActive()
« Reply #4 on: March 15, 2010, 12:22:57 pm »
Oh my bad. I'm sorry to wonder you with this  :oops:
Thanks for your help!

 

anything