是这样的,我要把上千个一样大小位置移动的纹理依次显示,纹理是即时生成的QPixmap::LoadFromData(), 在数据比较小的时候还好,数据大了就会出现空白,就是应该贴纹理的地方一片白,但是不是程序死在那,还能转动,移动,也不是每一片都空白,随机的。。但是最开始几片一般不会。我试过把将要贴上去的十个纹理(QPixmap)放到QCache中(效果一样), 试过把bindTexture()返回的Gluint作为全局变量事先计算(没有用),试过用QTimer控制过一段时间再更新(时间没有办法控制,小数据太长,大数据不够),试过QThread(想不明白在什么地方发出update的信号, 就是我不知道什么地方不对了,因为QPixmap::loadFromData返回的值一直是true).下面是贴图的代码,每次update时调用display()函数, 因为纹理的位置每次都会变化,所以是即时计算的。请帮忙看看什么地方有问题。
ps:贴的纹理不变时,怎么迅速更新都不会出现这种情况,虽然也是差不多把下面代码都执行了一遍。
bool GLWidget::display(sideType type, int position, bool direction)
{
pixmaps[type].loadFromData(dhtIndex->getSliceArrayForBMP(cdpSlice, position));//生成变化的一个纹理,其他纹理都已经在外部生成好了 {
visibleRange[type] = position;
visibleRangePercent[type] = 
(GLfloat)(visibleRange[type])/(dhtIndex->cdpMax - dhtIndex->cdpMin + 1);
visibleRangeDouble[type] = positionToPasteTexture(cdpSlice, visibleRange[type]);
if (type == cdpMinSide && position == visibleRange[cdpMaxSide])
setSingleVisibleRange(cdpMaxSide, dhtIndex->cdpMax);
else if(type == cdpMaxSide && position == visibleRange[cdpMinSide])
setSingleVisibleRange(cdpMinSide, dhtIndex->cdpMin);
//计算要贴图上去的位置visibleRangeDouble【6】
//和对应的纹理位置visibleRangePercent【6】,因为不是
//每次都把全部纹理(0, 0, ~ 1, 1,)贴上去的。
//因为visibleRangeDouble & visibleRangePercent只有一个量变化了,所以
//只计算一个分量
        glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);        
qglColor(QColor(Qt::white)); 
        glEnable(GL_LINE_SMOOTH); 
        glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);  
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); 
GLuint textures[6];
for ( int i = 0; i < 6; i ++)
textures[i] = bindTexture(pixmaps.at(i), GL_TEXTURE_2D);
//每次我要贴六个图上去,但是只有一个纹理内容发生了变化
//六个纹理的位置都发生变化
//在这里,我想先把bindTexture返回的Gluint存起来的,但是不能用。

glClearDepth(1.0f);
GLdouble pos[3];//每个顶点对应的坐标
GLfloat corner[4];//取纹理区域,
glColor4d(1, 1, 1, 1);
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
//tag 1
corner[leftCorner] = visibleRangePercent[timeMinSide];//timeMin
corner[rightCorner] = visibleRangePercent[timeMaxSide];//timeMax
corner[buttomCorner] = visibleRangePercent[lineMaxSide];//lineMax
corner[topCorner] = visibleRangePercent[lineMinSide];//lineM
//计算取纹理的区域,这四个值在0,1之间 //enum{leftCorner = 0, rightCorner = 1, buttomCorner = 2, topeCorner = 3} glBindTexture(GL_TEXTURE_2D, textures[cdpMinSide]);//x0
//这里,我把第二个参数换成外部生成的Gluint就会有问题。 pos[cdpAxis] = visibleRangeDouble[cdpMinSide];
//enum  axisType{cdpAxis = 0, lineAxis = 1, timeAxis = 2}
//相当于xyz轴
glBegin(GL_QUADS);//cdp glTexCoord2f(corner[leftCorner], corner[topCorner]);
pos[lineAxis] = visibleRangeDouble[lineMinSide];
pos[timeAxis] = visibleRangeDouble[timeMinSide];
glVertex3dv(pos); glTexCoord2f(corner[leftCorner], corner[buttomCorner]);
pos[lineAxis] = visibleRangeDouble[lineMaxSide];
pos[timeAxis] = visibleRangeDouble[timeMinSide];
glVertex3dv(pos); glTexCoord2d(corner[rightCorner], corner[buttomCorner]);
pos[lineAxis] = visibleRangeDouble[lineMaxSide];
pos[timeAxis] = visibleRangeDouble[timeMaxSide];
glVertex3dv(pos); glTexCoord2d(corner[rightCorner], corner[topCorner]);
pos[lineAxis] = visibleRangeDouble[lineMinSide];
pos[timeAxis] = visibleRangeDouble[timeMaxSide];
glVertex3dv(pos);
glEnd();
//tag2
//以下省略与tag1 ~ tag2之间类似的五段代码,分别为立方体另外五个面
//纹理位置和内容信息变更。
sliceDisplayInfo.displayingSlicePos += (sliceDisplayInfo.direction)? 1: (-1);
return false;
}

解决方案 »

  1.   

    项目经理跟我说用OpenGL的双缓存,我是在Qt的QGLWidget中绘制的,Qt是自动实现双缓存的,要是我想在后台绘制好再交换前后两张画布的话怎么操作呢?就是后台绘制什么东西由我自己控制,而不是Qt控制的话。
      

  2.   

    opengl里有个swapbuffer()函数,是用来交换双缓存的,不过还是不能自己控制,我只知道你要是想立即画出某个东西就用glflush()
      

  3.   

    啊.我在这里要立即画出就用update() or updtaeGL(),是Qt的函数.不是每一次都出现空白,同样的纹理,有时候正常显示,有时候就没了,现在添加了手动播放,就像一摞扑克牌,顺次显示每一张,同样位置,同样纹理(纹理生成没有问题,返回都是true),播放速度快就会出现问题,慢慢播放就没有问题. 期待高手出现解决啊.
      

  4.   

    和3楼一样看法。你好好看看OpenGL红宝书上关于纹理的那一章,里面应该有比较好的处理大量的纹理载入的办法。
      

  5.   

    to10楼(不知道为什么引用不了了..):
    原来使用qt的绑定纹理方式绑定纹理的,(bindTexture(..)), 改成用glBindTexture(..)好一些了, 切片播放什么没有问题, 还没有完全改好. 想知道要使用加速或者压缩算法的话具体应该看红宝书那一部分, 我这有一本, 目录如下, 麻烦指点一下:), 这是texture mapping 那一章的 major section:
    "An Overview and an Example" gives a brief, broad look at the steps required to perform texture mapping. It also
    presents a relatively simple example of texture mapping.
    l   
    "Specifying the Texture" explains how to specify one- or two-dimensional textures. It also discusses how to use a
    texture's borders, how to supply a series of related textures of different sizes, and how to control the filtering
    methods used to determine how an applied texture is mapped to screen coordinates.
    l   
    "Filtering" details how textures are either magnified or minified as they are applied to the pixels of polygons.
    Minification using special mipmap textures is also explained.
    l   
    "Texture Objects" describes how to put texture images into objects so that you can control several textures at one
    time. With texture objects, you may be able to create a working set of high-performance textures, which are said
    to be resident. You may also prioritize texture objects to increase or decrease the likelihood that a texture object is
    resident.
    l   
    "Texture Functions" discusses the methods used for painting a texture onto a surface. You can choose to have the
    texture color values replace those that would be used if texturing wasn't in effect, or you can have the final color
    be a combination of the two.
    l   
    "Assigning Texture Coordinates" describes how to compute and assign appropriate texture coordinates to the
    vertices of an object. It also explains how to control the behavior of coordinates that lie outside the default range -
    that is, how to repeat or clamp textures across a surface.
    l   
    "Automatic Texture-Coordinate Generation" shows how to have OpenGL automatically generate texture
    coordinates so that you can achieve such effects as contour and environment maps.
    l   
    l   "Advanced Features" explains how to manipulate the texture matrix stack and how to use the q texture coordinate
      

  6.   

    我今天就辞职了, 这个没有做完有点遗憾, 我把bindTexture(..)(QGLwidget::bindTexture())替换成OpenGL自己绑定纹理的函数就没什么问题.
    结贴吧