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

Author Topic: [SFML2] RenderImage completely transparent  (Read 2336 times)

0 Members and 1 Guest are viewing this topic.

renolc

  • Newbie
  • *
  • Posts: 1
    • View Profile
[SFML2] RenderImage completely transparent
« 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

Code: [Select]
/*
 * 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!

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
[SFML2] RenderImage completely transparent
« Reply #1 on: January 30, 2011, 07:35:34 am »
I have never used SFML 2.0 or OpenGL but just at a glance... I would say the problem lies here.

Code: [Select]
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.
-Wander

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
[SFML2] RenderImage completely transparent
« Reply #2 on: January 30, 2011, 09:38:38 am »
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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SFML2] RenderImage completely transparent
« Reply #3 on: January 30, 2011, 10:18:39 am »
Quote
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].

Quote
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?
Laurent Gomila - SFML developer

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
[SFML2] RenderImage completely transparent
« Reply #4 on: January 30, 2011, 01:36:46 pm »
Quote from: "Laurent"
Does this code works if you replace sf::RenderImage with sf::RenderWindow?

I'm not the OP, but he said this here:
Quote
If I replace RenderImage with RenderWindow, it works perfectly.

(To help speed things along) :P

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[SFML2] RenderImage completely transparent
« Reply #5 on: January 30, 2011, 05:53:17 pm »
Quote from: "devlin"
Quote from: "Laurent"
Does this code works if you replace sf::RenderImage with sf::RenderWindow?

I'm not the OP, but he said this here:
Quote
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:
Laurent Gomila - SFML developer