SFML community forums
Help => Graphics => Topic started by: renolc on January 19, 2011, 07:47:26 pm
-
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. :P
/*
* 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!
-
I have never used SFML 2.0 or OpenGL but just at a glance... I would say the problem lies here.
glClearColor(0, 0, 0, 1);
You are settings the Alpha value to '1' which is nearly 0.
In other words it will render invisible.
EDIT: Try setting the Alpha value to 255.
-
The parameters to glClearColor are GLclampf's, they're clamped to lie between [0,1].
Aren't you supposed to have an open window for the renderimage to work though? Try opening a window first - and then render to the renderimage.
-
You are settings the Alpha value to '1' which is nearly 0.
In other words it will render invisible.
Nop, values are floats in range [0 .. 1].
Aren't you supposed to have an open window for the renderimage to work though? Try opening a window first - and then render to the renderimage.
Nop, the render image has its own OpenGL context.
Does this code works if you replace sf::RenderImage with sf::RenderWindow?
-
Does this code works if you replace sf::RenderImage with sf::RenderWindow?
I'm not the OP, but he said this here:
If I replace RenderImage with RenderWindow, it works perfectly.
(To help speed things along) :P
-
Does this code works if you replace sf::RenderImage with sf::RenderWindow?
I'm not the OP, but he said this here:
If I replace RenderImage with RenderWindow, it works perfectly.
(To help speed things along) :P
:lol: Thank you
I'll test this code as soon as possible, but I already have a huge list of things to test in my todo-list :cry: