Hello all,
I have recently upgraded to SFML2 because I required the offscreen rendering that RenderImage supports. Sadly, I seem to be doing something wrong.
/*
* main.cpp
*
* Created on: Jan 17, 2011
* Author: renolc
*/
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include <stdlib.h>
#include <stdio.h>
int main() {
// create image
if (!sf::RenderImage::IsAvailable())
return EXIT_FAILURE;
sf::RenderImage image;
if (!image.Create(800, 600, true))
return EXIT_FAILURE;
image.SetActive(true);
// opengl /////////////////////////////////////////////////////////////////
// clear value
glClearDepth(1);
glClearColor(0, 0, 0, 1);
// enable depth
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
// setup perspective
glViewport(0, 0, image.GetWidth(), image.GetHeight());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, (double)image.GetWidth()/(double)image.GetHeight(), 1, 50);
glMatrixMode(GL_MODELVIEW);
// light
GLfloat position[] = {0, 0, 2, 1};
glLightfv(GL_LIGHT0, GL_POSITION, position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glShadeModel(GL_SMOOTH);
// clear screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// draw stuff
glLoadIdentity();
glTranslated(0, 0, -10);
glBegin(GL_QUADS);
glColor3d(1, 0, 0);
glVertex3d(-5, -3, 0.5);
glVertex3d(-5, 0, 0.5);
glVertex3d(-1, 0, -0.5);
glVertex3d(-1, -3, -0.5);
glColor3d(0, 1, 0);
glVertex3d(1, -3, -0.5);
glVertex3d(1, 0, -0.5);
glVertex3d(5, 0, 0.5);
glVertex3d(5, -3, 0.5);
glColor3d(0, 0, 1);
glVertex3d(-2.5, 0.5, 0);
glVertex3d(-2.5, 3.5, 0);
glVertex3d(2.5, 3.5, 0);
glVertex3d(2.5, 0.5, 0);
glEnd();
// \opengl ////////////////////////////////////////////////////////////////
// update image
image.Display();
// save image of rendering
image.GetImage().SaveToFile("test.png");
return EXIT_SUCCESS;
}
If I replace RenderImage with RenderWindow, it works perfectly. But if I try it as written above, it only outputs a completely transparent image.
I suspect it is something I am doing wrong, since I am still relatively new to SFML2 and OpenGL in general, so I would appreciate any help.
Thanks in advance!