samedi 18 juin 2016

opengl Framebuffer offscreen to texture, colour error

im trying to use a framebuffer for offscreen-render a cube. Then i try to translate it to a texture using its texture. The problem i get is when i try to put that texture on a plane. The image's colour is full based on one primary colour: Reed, Green or Blue. I dont know where is the problem, maybe a cast problem from float to double? I really dont know what to think or where to explore. Here is a gif of what i get: Cube error

here is the code I use. Its based on Qt but there is no diference from this to glut. I explain it to make easier to understand: 1.-initializeGL: I declare the framebuffer and its attachments 2.-PaintGL: draws the cube without using the FB if i dont "left click" to enable this (first part of the gif, it works perfect). If i left click, then the routine changes and paint the cube on the FrameBuffer, and then i paint its texture on a plane. (second part of the gif, it paints my texture GREEN) 3.- setProyection: set the glOrtho for the normal cube draw.

#include "WidgetOpenGL.h"

WidgetOpenGL::WidgetOpenGL(QWidget *parent) : QOpenGLWidget(parent)
{

}

void WidgetOpenGL::initializeGL()
{
    desplazamientoX = 1;
    desplazamientoY = 0;
    initializeOpenGLFunctions();
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glEnable(GL_DEPTH_TEST);
    buttonpressed = false;
    glGenFramebuffers(1, &buffer);
    glBindFramebuffer(GL_FRAMEBUFFER, buffer);
    glGenTextures(1, &renderedTexture);
    glBindTexture(GL_TEXTURE_2D, renderedTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 768, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glGenRenderbuffers(1, &renderedDepth);
    glBindRenderbuffer(GL_RENDERBUFFER, renderedDepth);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, 1024, 768);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderedDepth);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderedTexture, 0);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    setProyection();
    setModelView();
}

void WidgetOpenGL::paintGL()
{
    if (buttonpressed)
    {
        glBindFramebuffer(GL_FRAMEBUFFER, buffer);
        glViewport(0, 0, 1024, 768);
    }
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    setProyection();
    setModelView();
    glTranslatef(0.0, 0.0, -2.0);
    setRotation();

    glBegin(GL_QUADS);
    // top
    glColor3f(1.0f, 0.0f, 0.0f);
    glNormal3f(0.0f, 1.0f, 0.0f);
    glVertex3f(-0.5f, 0.5f, 0.5f);
    glVertex3f(0.5f, 0.5f, 0.5f);
    glVertex3f(0.5f, 0.5f, -0.5f);
    glVertex3f(-0.5f, 0.5f, -0.5f);

    glEnd();

    glBegin(GL_QUADS);
    // front
    glColor3f(0.0f, 1.0f, 0.0f);
    glNormal3f(0.0f, 0.0f, 1.0f);
    glVertex3f(-0.5f, -0.5f, 0.5f);
    glVertex3f(0.5f, -0.5f, 0.5f);
    glVertex3f(0.5f, 0.5f, 0.5f);
    glVertex3f(-0.5f, 0.5f, 0.5f);

    glEnd();

    glBegin(GL_QUADS);
    // right
    glColor3f(0.0f, 0.0f, 1.0f);
    glNormal3f(1.0f, 0.0f, 0.0f);
    glVertex3f(0.5f, -0.5f, 0.5f);
    glVertex3f(0.5f, -0.5f, -0.5f);
    glVertex3f(0.5f, 0.5f, -0.5f);
    glVertex3f(0.5f, 0.5f, 0.5f);

    glEnd();

    glBegin(GL_QUADS);
    // left
    glColor3f(0.0f, 0.0f, 0.5f);
    glNormal3f(-1.0f, 0.0f, 0.0f);
    glVertex3f(-0.5f, -0.5f, 0.5f);
    glVertex3f(-0.5f, 0.5f, 0.5f);
    glVertex3f(-0.5f, 0.5f, -0.5f);
    glVertex3f(-0.5f, -0.5f, -0.5f);

    glEnd();

    glBegin(GL_QUADS);
    // bottom
    glColor3f(0.5f, 0.0f, 0.0f);
    glNormal3f(0.0f, -1.0f, 0.0f);
    glVertex3f(-0.5f, -0.5f, 0.5f);
    glVertex3f(0.5f, -0.5f, 0.5f);
    glVertex3f(0.5f, -0.5f, -0.5f);
    glVertex3f(-0.5f, -0.5f, -0.5f);

    glEnd();

    glBegin(GL_QUADS);
    // back
    glColor3f(0.0f, 0.5f, 0.0f);
    glNormal3f(0.0f, 0.0f, -1.0f);
    glVertex3f(0.5f, 0.5f, -0.5f);
    glVertex3f(0.5f, -0.5f, -0.5f);
    glVertex3f(-0.5f, -0.5f, -0.5f);
    glVertex3f(-0.5f, 0.5f, -0.5f);

    glEnd();
    glFlush();

    if (buttonpressed)
    {
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        //      QImage img = frameBuffer->toImage();
        //      img.save("Hola.jpg");

        //draw plane with texture
        setViewport();
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, renderedTexture);
        glBegin(GL_QUADS);
        glTexCoord2d(0.0f, 0.0f);
        glVertex3f(-1.0, -1.0, -1.0);
        glTexCoord2d(1.0f, 0.0f);
        glVertex3f(1.0, -1.0, -1);
        glTexCoord2d(1.0f, 1.0f);
        glVertex3f(1.0, 1.0, -1.0);
        glTexCoord2d(0.0f, 1.0f);
        glVertex3f(-1.0, 1.0, -1.0);
        glEnd();
        glBindTexture(GL_TEXTURE_2D, 0);
        glDisable(GL_TEXTURE_2D);
        glFlush();
    }
}

void WidgetOpenGL::resizeGL(int w, int h)
{
    setViewport();
    setProyection();
    setModelView();
}

void WidgetOpenGL::setViewport()
{
    int ancho = this->width();
    int alto = this->height();
    glViewport(0, 0, ancho, alto);
}

void WidgetOpenGL::setProyection()
{
    int ancho = this->width();
    int alto = this->height();
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 10.0);
    //gluPerspective(90.0f, ancho / (GLdouble)alto, 0.0f, 10.0f);
}

void WidgetOpenGL::setModelView()
{
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void WidgetOpenGL::mouseMoveEvent(QMouseEvent *event)
{
    desplazamientoY += ((event->x() - initX) * 360) / (GLdouble)width();
    desplazamientoX += ((event->y() - initY) * 360) / (GLdouble)height();
    initX = event->x();
    initY = event->y();

    if (((int)desplazamientoX % 360) == 0) desplazamientoX = 0;
    if (((int)desplazamientoY % 360) == 0) desplazamientoY = 0;
    repaint();

}

void WidgetOpenGL::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        initX = event->x();
        initY = event->y();
    }
    else
    {
        buttonpressed = !buttonpressed;
        repaint();
    }
}

void WidgetOpenGL::setRotation()
{
    glRotatef(desplazamientoX, 1.0, 0.0, 0.0);
    glRotatef(desplazamientoY, 0.0, 1.0, 0.0);
}

and here is the header file if needed:

#ifndef WIDGETOPENGL_H
#define WIDGETOPENGL_H
#include <QtWidgets/QMainWindow>
#include <QtWidgets/qopenglwidget.h>
#include <QtOpenGL>
#include <GL/GLU.h>


class WidgetOpenGL : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT

public:
    WidgetOpenGL(QWidget *parent);

signals:
    void screenClicked();
protected:
    void initializeGL();
    void paintGL();
    void resizeGL(int w, int h);

    void setViewport();
    void setProyection();
    void setModelView();

private:
    int initX;
    int initY;
    QOpenGLFramebufferObject *frameBuffer;
    GLuint buffer;
    GLuint renderedTexture;
    GLuint renderedDepth;
    bool buttonpressed;
    QImage image;
    GLdouble desplazamientoX;
    GLdouble desplazamientoY;
    void setRotation();
    void mouseMoveEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);

};

#endif

thank you for your help :)

Aucun commentaire:

Enregistrer un commentaire