Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!

We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.

Opengl qt

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 shawk08033   Members   -  Reputation: 96


Posted 26 April 2014 - 03:09 PM

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) :

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

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

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


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

    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());



    return tex;


#2 BitMaster   Crossbones+   -  Reputation: 4233


Posted 28 April 2014 - 01:21 AM

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, 28 April 2014 - 01:21 AM.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.