I'm sorry for not adding more information immediately, I was in the process of trying some stuff, but I did not have anything tangential enough that I felt it would be worth showing. I can post the current state of things.
Here are current implementation details for the base class:
GLBaseWidget::GLBaseWidget(int width, int height) : dirty_bit_(true) {
texture_.create(width, height, true);
}
void GLBaseWidget::update() {
if (dirty_bit_) {
texture_.setActive(true);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
float aspect = static_cast<float>(texture_.getTexture().getSize().x) / static_cast<float>(texture_.getTexture().getSize().y);
float fov = 60.f;
float near = 1.f / std::tan(fov / 2.f * (M_PI / 180.f)) / 100.f;
glFrustum(-.01f, .01f, -.01f / aspect, .01f / aspect, near, 100.f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(.0f, .0f, -4.0f);
// TODO: rotation here
renderToTexture();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
texture_.setActive(false);
dirty_bit_ = false;
}
}
void GLBaseWidget::draw(sf::RenderTarget& target, sf::RenderStates states) const {
sf::Sprite sprite(texture_.getTexture());
states.transform *= getTransform();
target.draw(sprite, states);
}
Here is an example implementation that tries to draw just a triangle (PtWidget publicly inherits GLBaseWidget):
PtWidget::PtWidget(int width, int height)
: CubeWidget(width, height) {}
void PtWidget::renderToTexture() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, 0.0f);
glEnd();
}
This is the main function that calls this code:
int main() {
sf::RenderWindow window(sf::VideoMode(512, 256), "Cubetest", sf::Style::Titlebar|sf::Style::Close);
PtWidget pt_widget(256, 256);
pt_widget.setPosition(0, 0);
while (window.isOpen()) {
window.pushGLStates();
window.clear();
window.draw(pt_widget);
window.display();
window.popGLStates();
sf::Event event;
while (window.waitEvent(event)) {
if (event.type == sf::Event::Closed) window.close();
}
pt_widget.update();
}
return 0;
}
When I run it, I get some random textures from memory (from other applications or parts of the OS) in the left half of the window. When I close the window, the application "quits unexpectedly".