SFML community forums

Help => Graphics => Topic started by: panithadrum on November 09, 2010, 10:52:02 am

Title: Can't draw with OpenGL in sf::RenderImage
Post by: panithadrum on November 09, 2010, 10:52:02 am
I am doing the same code on a RenderWindow and a RenderImage, but looks that the RenderImage is not working for me.
That's the code using a RenderWindow
Code: [Select]
int main()
{
// Window
sf::RenderWindow window;
window.Create(sf::VideoMode(800, 600), "What?");

// Setup OpenGL
glClearColor(1.f, 0.f, 0.f, 1.f);
glClearDepth(1.f);
glEnable(GL_DEPTH_TEST);
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);
// Clear
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Setup camera
glLoadIdentity();
glTranslatef(0.f, 0.f, -100.f);
// Draw something
glColor4f(0.f, 0.f, 1.f, 1.f);
glBegin(GL_POLYGON);
{
glVertex2f(-10.f, -10.f);
glVertex2f(-10.f, 10.f);

glVertex2f(10.f, 10.f);
glVertex2f(10.f, -10.f);
}
glEnd();

// Display and wait
window.Display();
while(window.IsOpened())
{
sf::Event event;
while(window.GetEvent(event))
{
if(event.Type == sf::Event::Closed) window.Close();
}
}
}

and the output
(http://img268.imageshack.us/img268/9400/windowv.th.jpg) (http://img268.imageshack.us/i/windowv.jpg/)
Uploaded with ImageShack.us (http://imageshack.us)

This is the code for the RenderImage
Code: [Select]
int main()
{
// Render image
sf::RenderImage image;
image.Create(800, 600);

// Setup OpenGL
glClearColor(1.f, 0.f, 0.f, 1.f);
glClearDepth(1.f);
glEnable(GL_DEPTH_TEST);
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);
// Clear
glClearColor(1.f, 0.f, 0.f, 1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Setup camera
glLoadIdentity();
glTranslatef(0.f, 0.f, -100.f);
// Draw something
glColor4f(0.f, 0.f, 1.f, 1.f);
glBegin(GL_POLYGON);
{
glVertex2f(-10.f, -10.f);
glVertex2f(-10.f, 10.f);

glVertex2f(10.f, 10.f);
glVertex2f(10.f, -10.f);
}
glEnd();

// Image
image.Display();
image.GetImage().SaveToFile("image.png");
}

and the output image.png
(http://img9.imageshack.us/img9/7079/imagedt.th.png) (http://img9.imageshack.us/i/imagedt.png/)
Uploaded with ImageShack.us (http://imageshack.us)

I am using W7x64 with VS2010. Am I forgetting something? glClear works, but not the polygon itself.
Title: Can't draw with OpenGL in sf::RenderImage
Post by: Laurent on November 09, 2010, 12:23:22 pm
The code looks good, I'll test that as soon as possible.
Title: Can't draw with OpenGL in sf::RenderImage
Post by: Laurent on November 09, 2010, 09:49:24 pm
You forgot to call glViewport.
Title: Can't draw with OpenGL in sf::RenderImage
Post by: panithadrum on November 09, 2010, 10:00:50 pm
Quote from: "Laurent"
You forgot to call glViewport.

Oh, thanks! Why should I write more code in the RenderImage section? I suppose it's because the internal configuration.
EDIT: Oh I guessed, it's because RenderWindow sets the viewport with its width and height.
Title: Can't draw with OpenGL in sf::RenderImage
Post by: Laurent on November 09, 2010, 10:11:05 pm
Quote
Oh, thanks! Why should I write more code in the RenderImage section? I suppose it's because the internal configuration.
EDIT: Oh I guessed, it's because RenderWindow sets the viewport with its width and height.

I think it's OpenGL. In SFML, both RenderWindow and RenderImage have the same initialization. Whereas in OpenGL, they are two very different things.