Sign in to follow this  
shawk08033

Opengl qt

Recommended Posts

shawk08033    96

I am trying to display a 64x64 in opengl. The texture loads but I cant get it to display in a square and on the top left corner of the screen. Heres my code for the GLWidget in qt 

#include "glwidget.h"

GLWidget::GLWidget(QWidget *parent) :
    QGLWidget(parent)
{
}

void GLWidget::initializeGL(){
    glClearColor(1, 1, 1 , 0);
    gluPerspective(45, 640/480, 1, 500);
    glEnable(GL_MULTISAMPLE);
    glEnable(GL_AMD_performance_monitor);
    glEnable(GL_TEXTURE_2D);
    GLWidget::loadTexture2(":/images/Block.bmp", GLWidget::backgroundimage);
}

void GLWidget::paintGL(){
        glClearColor(0.4f, 0.1f, 0.1f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glBindTexture(GL_TEXTURE_2D, backgroundimage);

        glBegin(GL_QUADS);
            glTexCoord2f(0,1); glVertex2f(0, 0);
            glTexCoord2f(0,0); glVertex2f(0, 64);
            glTexCoord2f(1,0); glVertex2f(64, 0);
            glTexCoord2f(1,1); glVertex2f(64, 64);

        glEnd();
    glFlush();
}

void GLWidget::resizeGL(int w, int h){

}

QImage GLWidget::loadTexture2(char *filename, GLuint &textureID){
    glEnable(GL_TEXTURE_2D); // Enable texturing

    glGenTextures(1, &textureID); // Obtain an id for the texture
    glBindTexture(GL_TEXTURE_2D, textureID); // Set as the current texture

    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

    QImage im(filename);
    QImage tex = QGLWidget::convertToGLFormat(im);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width(), tex.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

    glDisable(GL_TEXTURE_2D);

    return tex;
}

Share this post


Link to post
Share on other sites
BitMaster    8651
You are setting a perspective matrix in whatever matrix state happens to be activated when you call it. You do not set any of the others (I'm unsure if QGLWidget guarantees if they will be set to the identity). Even if that does not cause a problem the near clipping plane is at z=1 while you render at z=0 (note you cannot set the near clipping plane to 0).

Ignoring that, gluPerspective is the completely wrong choice when you want to render in screen coordinates.

However, none of that is really important because you are learning old, deprecated OpenGL. Unless you are needing that knowledge for a very specific legacy project I would strongly advice to learn modern OpenGL instead of something you mostly have to unlearn later on.

Also note that depending on the Qt version you downloaded, you might not even have real OpenGL and instead will be using DirectX through ANGLE. It's not possible to see all the headers involved, but if you included something like GLEW or the system OpenGL you might be trying to a non-existing OpenGL context while ignoring the Qt-provided emulated context. Edited by BitMaster

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this