SOLVED texture trouble in openGL



  • SOLVED! Was a dumbass transposition of numbers!

    Pro file:

    #-------------------------------------------------
    #
    # Project created by QtCreator 2016-06-19T10:09:09
    #
    #-------------------------------------------------
    
    QT  += core gui opengl
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = AnotherTest
    TEMPLATE = app
    
    SOURCES += main.cpp\
            mainwindow.cpp \
        globalfunctions.cpp \
        glwidget.cpp
    
    HEADERS  += mainwindow.h \
        globals.h \
        glwidget.h
    
    RESOURCES += \
        resourcefile.qrc
    
    

    Globals.h:

    #ifndef GLOBALS_H
    #define GLOBALS_H
    
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <stdlib.h>
    #include <vector>
    
    #include <QApplication>
    #include <QDebug>
    #include <QDesktopWidget>
    
    #include <QLayout>
    #include <QMainWindow>
    #include <QMatrix4x4>
    #include <QMouseEvent>
    
    #include <QOpenGLBuffer>
    #include <QOpenGLContext>
    #include <QOpenGLContextGroup>
    #include <QOpenGLDebugLogger>
    #include <QOpenGLDebugMessage>
    #include <QOpenGLFramebufferObject>
    #include <QOpenGLFramebufferObjectFormat>
    #include <QOpenGLFunctions>
    #include <QOpenGLFunctionsPrivate>
    #include <QOpenGLFunctions_4_5_Compatibility>
    #include <QOpenGLFunctions_ES2>
    #include <QOpenGLPaintDevice>
    #include <QOpenGLShader>
    #include <QOpenGLShaderProgram>
    #include <QOpenGLTexture>
    #include <QOpenGLTimeMonitor>
    #include <QOpenGLTimerQuery>
    #include <QOpenGLVersionFunctions>
    #include <QOpenGLVersionProfile>
    #include <QOpenGLVertexArrayObject>
    #include <QOpenGLWidget>
    #include <QOpenGLWindow>
    
    #include <QPushButton>
    
    #include <QVector>
    #include <QVector2D>
    #include <QVector3D>
    #include <QVector4D>
    
    #include <QWheelEvent>
    #include <QWidget>
    #include <QWindow>
    
    #include <QtCore>
    #include <QtGlobal>
    
    #include <QtGui>
    
    using std::cout;
    using std::cin;
    using std::vector;
    
    static const double G_PI = 3.14159265358979323846264338327;
    
    static const double G_E = 2.718281828459045235360287471352;
    
    
    //==================== struct gVertexDataStruct ====================
    
    struct gVertexDataStruct
    {
        QVector4D position;
        QVector4D pixColor;
        QVector2D texCoord;
    };
    
    //==================== functions ====================
    
    void gSizeAndCenterWidget (QWidget* widget, int width, int height);
    
    void gSizeAndCenterWidgetInWidget (QWidget* widget, QWidget* inWidget,
                                                                int sideMargin, int topBotMargin);
    
    #endif // GLOBALS_H
    

    GLWidget.h:

    #ifndef GLWIDGET_H
    #define GLWIDGET_H
    
    
    #ifndef GLOBALS_H
    #include "globals.h"
    #endif
    
    static const int VERTEX_ATTRIBUTE   = 0;
    static const int TEXCOORD_ATTRIBUTE = 1;
    
    //----------
    
    class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions
    {
        Q_OBJECT
    
    public:
        explicit GLWidget(QWidget *parent = 0);
        ~GLWidget();
    
        QSize minimumSizeHint();
        QSize sizeHint();
    
        void setClearColor(const QColor &color);
    
        QVector<gVertexDataStruct>  tStruct;
    
    signals:
        void clicked();
    
    protected:
        void initializeGL();
        void resizeGL(int width, int height);
        void paintGL();
    
        void mousePressEvent(QMouseEvent *event);
        void mouseMoveEvent(QMouseEvent *event);
        void wheelEvent(QWheelEvent *event);
    
    private:
        void makeObject();
    
        QColor clearColor;
    
        double  alpha;
        double  beta;
        double  distance;
        QPoint  lastMousePosition;
    
        QMatrix4x4  pMatrix;
    
        QOpenGLTexture*         texture;
        QOpenGLShaderProgram*   program;
    
    
        QVector<QVector3D>  trnVertexCoords;
        QVector<QVector4D>  trnColorCoords;
        QVector<QVector2D>  trnTextureCoords;
    
        QOpenGLBuffer vbo;
    };
    #endif // GLWIDGET_H
    

    MaindWindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #ifndef GLWIDGET_H
    #include "glwidget.h"
    #endif
    
    #ifndef GLOBALS_H
    #include "globals.h"
    #endif
    
    static const int MW_WIDTH   = 1200;
    static const int MW_HEIGHT  = 800;
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
        void resizeEvent(QResizeEvent* e);
    
        QPushButton* testButt;
    
        GLWidget* glWidget;
    
        QGridLayout*  leftLayout;
        QVBoxLayout*  rightLayout;
        QHBoxLayout*  mainLayout;
        QWidget*      layoutWidget;
    
        bool doProceed;
    };
    
    #endif // MAINWINDOW_H
    

    GlobalFunctions.cpp:

    #ifndef GLOBALS_H
    #include "globals.h"
    #endif
    
    //---------- gSizeAndCenterWidget ---------
    
    void gSizeAndCenterWidget (QWidget* widget, int width, int height) {
        int x,y;
        int screenWidth;
        int screenHeight;
    
        QDesktopWidget* desktop = QApplication::desktop();
    
        screenWidth = desktop->width();
        screenHeight = desktop->height();
    
        x = (screenWidth - width)/2;
        y = (screenHeight - height)/2;
    
        widget->setGeometry (x, y, width, height);
    }//gSizeAndCenterWidget
    
    
    //---------- gSizeAndCenterWidgetInWidget ---------
    
    void gSizeAndCenterWidgetInWidget (QWidget* widget, QWidget* inWidget,
                                                                int sideMargin, int topBotMargin) {
        int x,y;
        int widWidth;
        int widHeight;
        int widX, widY;
        int width, height;
    
        widWidth  = inWidget->width();
        widHeight = inWidget->height();
    
        width  = inWidget->width() - 2*sideMargin;
        height = inWidget->height() - 2*topBotMargin;
    
        widX = inWidget->x();
        widY = inWidget->y();
    
        x = widX + (widWidth - width)/2;
        y = widY + (widHeight - height)/2;
    
        widget->setGeometry (x, y, width, height);
        widget->setFixedSize(width, height);
    }//gSizeAndCenterWidgetInWidget
    

    GLWidget.cpp:

    #include "glwidget.h"
    
    
    //---------- class GLWidget ----------
    
    GLWidget::GLWidget(QWidget *parent) : QOpenGLWidget(parent),
                                              clearColor(Qt::gray),
                                              program(0)
    {
        qDebug()<<"CONSTRUCTOR   GLWidget";
    
        alpha = 90.0;
        beta  = -90.0;
        distance = -8.0;
    }//Constructor
    
    
    //---------- ~GLWidget ----------
    
    GLWidget::~GLWidget()
    {
        qDebug()<<"DESTRUCTOR   GLWidget";
        makeCurrent();
        vbo.destroy();
        delete texture;
        delete program;
        tStruct.clear();
        doneCurrent();
    }//Destructor
    
    //---------- setClearColor ----------
    
    void GLWidget::setClearColor(const QColor &color)
    {
        clearColor = color;
        update();
    }
    
    //---------- initializeGL ----------
    
    void GLWidget::initializeGL()
    {
        initializeOpenGLFunctions();
    
        qDebug()<<"   initializeGL";
    
        makeObject();
    
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_CULL_FACE);
    
        //---------- vertexShader
    
        QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
        const char *vsrc =
    
            "#version 130\n"
            "in vec4 vertex;\n"
            "in vec4 color;\n"
            "in vec2 texCoord;\n"
    
            "out vec2 texc;\n"
            "out vec4 tColor;\n"
    
            "uniform mat4 matrix;\n"
    
            "void main(void)\n"
            "{\n"
            "    gl_Position = matrix * vertex;\n"
            "    texc = texCoord;\n"
            "    tColor = color;\n"
            "}\n";
        vshader->compileSourceCode(vsrc);
    
        //---------- fragmentShader
    
        QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
        const char *fsrc =
    
            "#version 130\n"
    
            "uniform sampler2D texture;\n"
    
            "in vec2 texc;\n"
            "in vec4 tColor;\n"
    
            "void main(void)\n"
            "{\n"
                "gl_FragColor = texture2D(texture, texc);\n"
                //"gl_FragColor = tColor;\n"
            "}\n";
        fshader->compileSourceCode(fsrc);
    
        //----------
    
        program = new QOpenGLShaderProgram;
        program->addShader(vshader);
        program->addShader(fshader);
    
        program->bindAttributeLocation("vertex", VERTEX_ATTRIBUTE);
        program->bindAttributeLocation("color", COLOR_ATTRIBUTE);
        program->bindAttributeLocation("texCoord", TEXCOORD_ATTRIBUTE);
        program->link();
    
        program->bind();
        program->setUniformValue("texture", 0);
    }//initializeGL
    
    
    /*
    struct gVertexDataStruct
    {
        QVector4D position;
        QVector4D pixColor;
        QVector4D texCoord;
    };
    */
    
    
    //---------- paintGL ----------
    
    void GLWidget::paintGL()
    {
        glClearColor(clearColor.redF(), clearColor.greenF(), clearColor.blueF(), clearColor.alphaF());
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        //----------
    
        QMatrix4x4 vMatrix;
        QMatrix4x4  tMatrix;
        QMatrix4x4 cameraTrans;             //Identity matrix
    
        cameraTrans.translate(0.0, 5.0, 0.0);
        cameraTrans.rotate(alpha, 0, 1, 0);
        cameraTrans.rotate(beta, 1, 0, 0);
    
        QVector3D cameraPosition = cameraTrans * QVector3D(0, 0, distance);
        QVector3D cameraUpDirection = cameraTrans * QVector3D(0, 1, 0);
        vMatrix.lookAt(cameraPosition, QVector3D(0, 0, 0), cameraUpDirection);
    
        program->setUniformValue("matrix", pMatrix * vMatrix * tMatrix);
    
        program->enableAttributeArray(VERTEX_ATTRIBUTE);
        program->enableAttributeArray(COLOR_ATTRIBUTE);
        program->enableAttributeArray(TEXCOORD_ATTRIBUTE);
    
        int offset = 0;
    
        int stride = sizeof(gVertexDataStruct);
    
        program->setAttributeBuffer(VERTEX_ATTRIBUTE, GL_FLOAT,  offset,  4,  stride);
    
        offset = offset + sizeof(QVector4D);
    
        program->setAttributeBuffer(COLOR_ATTRIBUTE, GL_FLOAT,  offset,  4,  stride);
    
        offset = offset + sizeof(QVector4D);
    
        program->setAttributeBuffer(TEXCOORD_ATTRIBUTE, GL_FLOAT, offset,  4, stride);
    
        texture->bind();
    
        glDrawArrays(GL_TRIANGLES, 0, 10);
    }
    
    //---------- resizeGL ----------
    
    void GLWidget::resizeGL(int width, int height)
    {
        if (height == 0) {
            height = 1;
        }
        pMatrix.setToIdentity();
        pMatrix.perspective(60.0, (float) width / (float) height, 0.001, 1000);
        glViewport(0, 0, width, height);
    }
    
    //---------- mousePressEvent --------
    
    void GLWidget :: mousePressEvent(QMouseEvent *event)
    {
        lastMousePosition = event->pos();
        event->accept();
    }//mousePressEvent
    
    //---------- mouseMoveEvent --------
    
    void GLWidget :: mouseMoveEvent(QMouseEvent *event)
    {
        int deltaX = event->x() - lastMousePosition.x();
        int deltaY = event->y() - lastMousePosition.y();
    
        if (event->buttons() & Qt::LeftButton) {
            alpha -= deltaX;
            while (alpha < 0) {
                alpha += 360;
            }
    
            while (alpha >= 360) {
                alpha -= 360;
            }
    
            beta -= deltaY;
            if (beta < -90) {
                beta = -90;
            }
    
            if (beta > 90) {
                beta = 90;
            }
            update();
        }//if
    
        lastMousePosition = event->pos();
        event->accept();
    }//mouseMoveEvent
    
    //---------- wheelEvent --------
    
    void GLWidget :: wheelEvent(QWheelEvent *event)
    {
        int delta = event->delta();
    
        if (event->orientation() == Qt::Vertical) {
            if (delta < 0) {
                distance *= 1.1;
            } else if (delta > 0) {
                distance *= 0.9;
            }
            update();
        }//if
    
        event->accept();
    }//wheelEvent
    
    //---------- makeObject ----------
    
    void GLWidget::makeObject()
    {
        texture = new QOpenGLTexture(QImage("Insert Path to an Image Here").mirrored());
    
        //Drawing in the Z-X Plane
    
        gVertexDataStruct vData;
    
        vData.position = {-1, 0, -1, 1};
        vData.pixColor = {0.7, 0.2, 0.0, 1.0};  //Just a test color
        vData.texCoord = {0, 0};
        tStruct.append(vData);
    
        vData.position = {1, 0, -1, 1};
        vData.pixColor = {0.7, 0.2, 0.0, 1.0};
        vData.texCoord = {1, 0};
        tStruct.append(vData);
    
        vData.position = {1, 0, 1, 1};
        vData.pixColor = {0.7, 0.2, 0.0, 1.0};
        vData.texCoord = {1, 1};
        tStruct.append(vData);
    
        //----------
    
        vData.position = {-1, 0, -1, 1};
        vData.pixColor = {0.2, 0.7, 0.0, 1.0};
        vData.texCoord = {0, 0};
        tStruct.append(vData);
    
        vData.position = {1, 0, 1, 1};
        vData.pixColor = {0.2, 0.7, 0.0, 1.0};
        vData.texCoord = {1, 1};
        tStruct.append(vData);
    
        vData.position = {-1, 0, 1, 1};
        vData.pixColor = {0.2, 0.7, 0.0, 1.0};
        vData.texCoord = {0, 1};
        tStruct.append(vData);
    
        //----------
    
        vbo.create();
        vbo.bind();
        int numBytes = tStruct.count() * sizeof(gVertexDataStruct);
        vbo.allocate(tStruct.constData(), numBytes);
    }//makeObject
    
    

    main.cpp:

    #include "mainwindow.h"
    
    int main(int argc, char *argv[])
    {
        int result;
        QApplication a(argc, argv);
        MainWindow* w = new MainWindow();
        gSizeAndCenterWidget (w, MW_WIDTH, MW_HEIGHT);
    
        //---------- Loop
    
        while (w->isVisible()){
            w->update();
            result = a.exec();
        }//while
    
        delete w;
        return result;
    }
    

    Mainwindow.cpp:

    #include "mainwindow.h"
    
    
    //---------- class MainWindow ----------
    
    MainWindow :: MainWindow(QWidget *parent) : QMainWindow(parent)
    {
        qDebug()<<"CONSTRUCTOR   MainWindow";
    
        doProceed = true;
    
        //----------
    
        glWidget = new GLWidget(this);
    
        QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        sizePolicy.setHorizontalStretch(0);
        sizePolicy.setVerticalStretch(0);
        glWidget->setSizePolicy(sizePolicy);
    
        layoutWidget = new QWidget(glWidget);
        layoutWidget->setGeometry(QRect(50, 50, 300, 200));
        layoutWidget->setSizePolicy(sizePolicy);
    
        setCentralWidget(layoutWidget);
    
        mainLayout  = new QHBoxLayout(layoutWidget);
        leftLayout  = new QGridLayout();
        rightLayout = new QVBoxLayout();
    
        leftLayout->addWidget(glWidget, 0, 0);
    
        testButt = new QPushButton("Test", this);
        testButt->setFixedWidth(120);
        testButt->setFixedHeight(24);
    
        QSizePolicy testSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        testSizePolicy.setHorizontalStretch(0);
        testSizePolicy.setVerticalStretch(0);
        testButt->setSizePolicy(testSizePolicy);
    
        rightLayout->addWidget(testButt);
        testButt->show();
    
        mainLayout->addLayout(leftLayout);
        mainLayout->addLayout(rightLayout);
    
        show();
        glWidget->show();
    }//Constructor
    
    //---------- Destructor ----------
    
    MainWindow :: ~MainWindow()
    {
        qDebug()<<"DESTRUCTOR   MainWindow";
    
        leftLayout->~QLayout();
        rightLayout->~QLayout();
    
    }//Destructor
    
    //---------- resizeEvent ----------
    
    void MainWindow :: resizeEvent(QResizeEvent* e){
        //if (glWidget != NULL)
           // glWidget->setGeometry(0, 0, (this->width() - 150), this->height());
    }//resizeEvent
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.