1
Graphics / Re: Rendering into a sf::RenderTexture with OpenGL inside a class
« on: July 20, 2014, 07:44:41 pm »
I'm not sure if this is absolutely minimal, since something in here might be causing a side effect. This code does reproduce my error though. It shows a window of 512x256, with the left half being filled with what looks like random areas of video memory.
#include <cmath>
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(512, 256), "Cubetest", sf::Style::Titlebar|sf::Style::Close);
sf::RenderTexture texture;
texture.create(256, 256, true);
texture.setActive(true);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
float fov = 60.f;
float near = 1.f / std::tan(fov / 2.f * (M_PI / 180.f)) / 100.f;
glFrustum(-.01f, .01f, -.01f, .01f, near, 100.f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(.0f, .0f, -4.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, 0.0f);
glEnd();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
texture.setActive(false);
window.pushGLStates();
window.clear();
sf::Sprite sprite(texture.getTexture());
window.draw(sprite);
window.display();
window.popGLStates();
sf::Event event;
while (window.waitEvent(event) && event.type != sf::Event::Closed) {}
window.close();
return 0;
}
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(512, 256), "Cubetest", sf::Style::Titlebar|sf::Style::Close);
sf::RenderTexture texture;
texture.create(256, 256, true);
texture.setActive(true);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
float fov = 60.f;
float near = 1.f / std::tan(fov / 2.f * (M_PI / 180.f)) / 100.f;
glFrustum(-.01f, .01f, -.01f, .01f, near, 100.f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(.0f, .0f, -4.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, 0.0f);
glEnd();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
texture.setActive(false);
window.pushGLStates();
window.clear();
sf::Sprite sprite(texture.getTexture());
window.draw(sprite);
window.display();
window.popGLStates();
sf::Event event;
while (window.waitEvent(event) && event.type != sf::Event::Closed) {}
window.close();
return 0;
}