簡體   English   中英

OpenGL紋理不顯示

[英]OpenGL Texture doesn't show

我正在嘗試使用opengl在Qt中顯示紋理,但是運行時它不顯示紋理。

我進行了一些研究,發現需要將紋理的高度和寬度設置為2的冪。我的紋理現在為(1024x1024)。

我還添加了很多glTexParameterf可以解決我的問題,但是仍然沒有運氣。

void WorldView::paintGL ()
{
this->dayOfYear = (this->dayOfYear+1);
this->hourOfDay = (this->hourOfDay+1) % 24;

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

//load texture
QImage img;
if(!img.load(":/files/FloorsCheckerboardSmall.bmp"))
    printf("could not open image");

//try showing texture
glEnable( GL_LIGHTING );
glEnable( GL_COLOR_MATERIAL );
glEnable(GL_TEXTURE_2D);

unsigned int m_textureID;
glGenTextures(1, &m_textureID);
glBindTexture(GL_TEXTURE_2D,m_textureID);
glTexImage2D(GL_TEXTURE_2D,0,(GLint)img.depth(),(GLsizei)img.width(),(GLsizei)img.height(),0,GL_RGB,GL_UNSIGNED_BYTE,img.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_MAG_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);

glColor3d(1.0,0.0,0.0);
glBegin(GL_POLYGON);
    glTexCoord2d(0.0,5.0);
    glVertex2d(0.0,3.0);

    glTexCoord2d(0.0,0.0);
    glVertex2d(0.0,0.0);

    glTexCoord2d(5.0,0.0);
    glVertex2d(3.0,0.0);

    glTexCoord2d(5.0,5.0);
    glVertex2d(3.0,3.0);
    glEnd();
}

EDIT1:可能是我的紋理太大了嗎?

EDIT2:glBindTexture(GL_TEXTURE_2D,m_textureID); 放在glBindTexture之前而不是glColor3d之前

已解決:我的img.depth()返回了一個無效的internalFormat值。 我用有效的interalFormat值GL_RGBA替換了此GLint。 我還將格式從GL_RGB更改為GL_RGBA(請參閱genpfaults答案)

glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,(GLsizei)img.width(),(GLsizei)img.height(),0,GL_RGBA,GL_UNSIGNED_BYTE,img.bits());

你必須打電話

glBindTexture(GL_TEXTURE_2D,m_textureID); 

在調用glTexImage2D(...)之前。 否則OpenGL將不知道您發送的數據在哪里。

                             vvvvvvvvvvvvvvvvvv
glTexImage2D(GL_TEXTURE_2D,0,(GLint)img.depth(),(GLsizei)img.width(),(GLsizei)img.height(),0,GL_RGB,GL_UNSIGNED_BYTE,img.bits());

img.depth()返回32

然后,您需要傳遞一個有效的internalFormat值,而不是img.depth() 嘗試GL_RGBA

您還應該將format設置為GL_RGBA因為img.bits()實際上是四個組成部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM