首页 > 代码库 > 图片测试texture map

图片测试texture map

一,测试方法1:直接使用图片

...

#include <QImage>

...

QImage buf;
buf.load( "D:\\24.bmp" );

tex = QGLWidget::convertToGLFormat( buf ); //QMessageBox::about( 0,"Title","start123" );
GLuint texture[1];
glGenTextures( 1, &texture[0]);
glBindTexture( GL_TEXTURE_2D,texture[0] );
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_RGBA,GL_UNSIGNED_BYTE,tex.bits() );

glBindTexture( GL_TEXTURE_2D,texture[0] );
// Draw image
glBegin(GL_QUADS);
glTexCoord2f( -1.0, 0.0 ); glVertex3f( -1.0, -1.0, -1.0 );
glTexCoord2f( -1.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 );
glTexCoord2f( 0.0, 1.0 ); glVertex3f( 1.0, 1.0, -1.0 );
glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, -1.0, -1.0 );
glEnd();

二,测试方法二:使用char* (采用QImage ( uchar * data, int width, int height, int bytesPerLine, Format)这个构造函数

FILE* g_fp=fopen("D:\\test.bmp","rb");
fseek( g_fp,0L,SEEK_END );
char* pChar=new char[ftell(g_fp)+1];
QImage tex;
QImage buf( (unsigned char*)pChar,WIDTH,HEIGHT,WIDTH*3,QImage::Format_RGB32 );

memset( pChar,0,ftell(g_fp)+1 ); //QMessageBox::about( 0,"Title","start" +QString::number(ftell(g_fp)) );
fseek( g_fp,0,SEEK_SET );
fread( pChar,ftell(g_fp),1,g_fp );


tex = QGLWidget::convertToGLFormat( buf ); //QMessageBox::about( 0,"Title","start123" );
GLuint texture[1];
glGenTextures( 1, &texture[0]);
glBindTexture( GL_TEXTURE_2D,texture[0] );
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,WIDTH,HEIGHT,0,GL_RGBA,GL_UNSIGNED_BYTE,tex.bits() );

//QMessageBox::about( 0,"Title","Now123" );

glBindTexture( GL_TEXTURE_2D,texture[0] );
// Draw image
// 注意opengl中y坐标向上为正,而yuv视频向下为正,需要反转一下
glPushMatrix();
glBegin(GL_QUADS);
glTexCoord2f( -1.0, 0.0 ); glVertex3f( -1.0, -1.0, -1.0 );
glTexCoord2f( -1.0, 1.0 ); glVertex3f( -1.0, 1.0, -1.0 );
glTexCoord2f( 0.0, 1.0 ); glVertex3f( 1.0, 1.0, -1.0 );
glTexCoord2f( 0.0, 0.0 ); glVertex3f( 1.0, -1.0, -1.0 );
glEnd();
glPopMatrix();

glFlush();