M
M
maniii2014-04-21 20:46:09
Qt
maniii, 2014-04-21 20:46:09

Why can't the texture be rendered?

OS Windows7 64
QT 5.2
GF640m

#include "mainwindow.h"
#include <malloc.h>
#include <QtOpenGL>

unsigned int Img;

MainWindow::MainWindow(QWidget *parent): QGLWidget(parent)
{
    GraphicsWidth=800;
    GraphicsHeight=600;

    setFormat(QGLFormat(QGL::Rgba));
    glOrtho(0.0,GraphicsWidth,GraphicsHeight,0.0,1.0,-1.0);

    char *img=(char*)malloc(128*128*3);

 int y=0;
 int x=0;

 for(y=0; y<128; y++)
 {
  for( x=0; x<128; x++ )
  {
   img[y*128*3+x*3]=0;
   img[y*128*3+x*3+1]=0;
   img[y*128*3+x*3+2]=255;
  }
 }

 glGenTextures( 1, &Img );
 glBindTexture(GL_TEXTURE_2D,Img);
 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
 glTexImage2D( GL_TEXTURE_2D, 0, 3, 128, 128, 0, GL_RGB, GL_UNSIGNED_BYTE, img );

    QTimer *timer=new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(updateGL()));
    timer->start(1);
}

MainWindow::~MainWindow()
{

}

void MainWindow::initializeGL()
{
}

void MainWindow::resizeGL( int w, int h )
{
    GraphicsWidth=w;
    GraphicsHeight=h;

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0,GraphicsWidth,GraphicsHeight,0.0,1.0,-1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glViewport(0,0,GraphicsWidth,GraphicsHeight);

    paintGL();
}

void MainWindow::paintGL()
{
    glClearColor(0,0,0,1);
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3ub( 255, 255, 255 );
    glBindTexture( GL_TEXTURE_2D, Img );
    glEnable( GL_TEXTURE_2D );
        glBegin( GL_QUADS );
            glTexCoord2f( 0, 0 ); glVertex2i( 100, 100 );
            glTexCoord2f( 0, 1 ); glVertex2i( 200, 100 );
            glTexCoord2f( 1, 1 ); glVertex2i( 200, 200 );
            glTexCoord2f( 1, 0 ); glVertex2i( 100, 200 );
        glEnd();
    glDisable( GL_TEXTURE_2D );

    glFinish();
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question