C++ Qt OpenGL纹理为黑色
我需要加载一个纹理到一个正方形上,但每当我启动te程序,我都看不到任何纹理,只有一个黑色正方形 代码如下:C++ Qt OpenGL纹理为黑色,c++,qt,opengl,textures,C++,Qt,Opengl,Textures,我需要加载一个纹理到一个正方形上,但每当我启动te程序,我都看不到任何纹理,只有一个黑色正方形 代码如下: GLuint texture; interactiveScenes::interactiveScenes(QWidget *parent) : QGLWidget(parent), ui(new Ui::interactiveScenes) { ui->setupUi(this); timer = new QTimer(); connect( ti
GLuint texture;
interactiveScenes::interactiveScenes(QWidget *parent) :
QGLWidget(parent), ui(new Ui::interactiveScenes)
{
ui->setupUi(this);
timer = new QTimer();
connect( timer, SIGNAL(timeout()), this, SLOT(updateGL()) );
setFocusPolicy(Qt::StrongFocus);
lockMouse = true;
camPosx = 2.0, camPosy = 2.0, camPosz = 25.0;
camViewx = 2.0, camViewy = 2.0, camViewz = 0.0;
camUpx = 0.0, camUpy = 1.0, camUpz = 0.0;
mouseX = QCursor::pos().x();
mouseY = QCursor::pos().y();
setMouseTracking(true);
}
interactiveScenes::~interactiveScenes()
{
delete ui;
}
void interactiveScenes::initializeGL()
{
// Initialize QGLWidget (parent)
QGLWidget::initializeGL();
glShadeModel(GL_SMOOTH);
// White canvas
glClearColor(1.0f,1.0f,1.0f,0.0f);
// Place light
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glEnable(GL_DEPTH_TEST);
GLfloat light0_position [] = {0.1f, 0.1f, 5.0f, 0.0f};
GLfloat light_diffuse []={ 1.0, 1.0, 1.0, 1.0 };
glLightfv ( GL_LIGHT0, GL_POSITION, light0_position );
glLightfv ( GL_LIGHT0, GL_DIFFUSE, light_diffuse );
glEnable(GL_TEXTURE_2D);
img = new QImage(":/images/images.jpg", "JPG");
if (img->isNull())
std::cout << "error" <<std::endl;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
QImage tex = QGLWidget::convertToGLFormat(*img);
if (tex.isNull())
std::cout << "error" <<std::endl;
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA,
tex.width(), tex.height(),
0, GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glDisable(GL_TEXTURE_2D);
timer->start(50);
}
void interactiveScenes::resizeGL(GLint width, GLint height)
{
if ((width<=0) || (height<=0))
return;
//set viewport
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//set persepective
GLdouble aspect_ratio=(GLdouble)width/(GLdouble)height;
gluPerspective(45.0f, aspect_ratio, 0.1, 40.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void interactiveScenes::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// store current matrix
glMatrixMode( GL_MODELVIEW );
glPushMatrix( );
gluLookAt(camPosx ,camPosy ,camPosz,
camViewx,camViewy,camViewz,
camUpx, camUpy, camUpz );
//Draw Axes
glDisable( GL_LIGHTING );
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(10.0, 0.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 10.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 10.0);
glEnd();
glEnable( GL_LIGHTING );
glEnable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glColor3f( 1.0, 0.0, 0.0 );
glBegin(GL_POLYGON);
glTexCoord2d( 0.0, 5.0 );
glVertex3f( 0.0f, 3.0f, 0.0f );
glTexCoord2d( 0.0, 0.0);
glVertex3f( 0.0f, 0.0f, 0.0f );
glTexCoord2d( 5.0, 0.0);
glVertex3f( 3.0f, 0.0f, 0.0f );
glTexCoord2d( 5.0, 5.0);
glVertex3f( 3.0f, 3.0f, 0.0f );
glEnd();
glDisable(GL_TEXTURE_2D);
glPushMatrix();
glTranslated(2.0, 2.0 ,-4);
GLfloat shin[] = { 12.8f };
GLfloat amb[] = { 0.135f, 0.2225f, 0.1575f, 0.95f };
GLfloat diff2 [] = { 0.54f , 0.89f , 0.63f, 0.95f };
GLfloat specular[] = { 0.316228f, 0.316228f, 0.316228f, 0.95f };
glMaterialfv ( GL_FRONT, GL_SPECULAR, specular);
glMaterialfv ( GL_FRONT, GL_SHININESS, shin);
glMaterialfv ( GL_FRONT, GL_AMBIENT, amb);
glMaterialfv ( GL_FRONT, GL_DIFFUSE, diff2 );
solidSphere(2, 25, 25);
glPopMatrix();
// restore current matrix
glMatrixMode( GL_MODELVIEW );
glPopMatrix( );
}
GLuint纹理;
interactiveScenes::interactiveScenes(QWidget*父项):
QGLWidget(父级)、ui(新ui::interactiveScenes)
{
用户界面->设置用户界面(此);
计时器=新的QTimer();
连接(计时器、信号(timeout())、此、插槽(updateGL());
setFocusPolicy(Qt::StrongFocus);
鼠标=真;
camPosx=2.0,camPosy=2.0,camPosz=25.0;
camViewx=2.0,camViewy=2.0,camViewz=0.0;
camUpx=0.0,camUpy=1.0,camUpz=0.0;
mouseX=QCursor::pos().x();
mouseY=QCursor::pos().y();
setMouseTracking(真);
}
interactiveScenes::~interactiveScenes()
{
删除用户界面;
}
void interactiveScenes::initializeGL()
{
//初始化QGLWidget(父级)
QGLWidget::initializeGL();
glShadeModel(GL_平滑);
//白色帆布
glClearColor(1.0f、1.0f、1.0f、0.0f);
//点灯
glEnable(德国劳埃德大学照明);
glEnable(GL_LIGHT0);
glEnable(GLU深度试验);
GLfloat light0_位置[]={0.1f、0.1f、5.0f、0.0f};
GLfloat light_diffuse[]={1.0,1.0,1.0,1.0};
glLightfv(GLU LIGHT0、GLU位置、LIGHT0位置);
glLightfv(GL_LIGHT0,GL_漫反射,light_漫反射);
glEnable(GL_纹理_2D);
img=newqimage(:/images/images.jpg,“jpg”);
如果(img->isNull())
多亏了agrum,我发现了问题,我测试了正方形是否会显示任何颜色,但没有。
在应用纹理/颜色之前,我没有禁用照明。
在应用纹理时禁用照明后,一切正常!您是否先尝试将颜色应用到网格以查看其是否位于平截体中,而不是显示纹理?此外,我建议您花一些时间更新您的openGL知识(或者如果从openGL开始,而不是学习传统的知识).使用固定的图形管道不会让你走得更远。谢谢你帮我找到解决方案!还有关于学习新的openGL,我必须在大学里以这种方式学习,所以我不得不学习它是如何工作的。