Skip to main content
GameDev.net gamedev.net
🔒 Locked

Opengl qt

Started by shawk08033 Apr 26, 2014 at 9:09 PM 0 replies 2.2k views
Original Post
shawk08033
shawk08033

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;
}

BitMaster
BitMaster
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.