简体   繁体   English

纹理在缩放而不是在Qt中重复

[英]Texture is scaling instead of repeat in Qt

I just started working on opengl in qt, using texture i am displaying image in quard. 我刚刚开始在qt中使用opengl进行工作,使用的纹理是在quard中显示图像。

then i set gltexture wrap mode to gl_repeat but its not repeating. 然后我将gltexture环绕模式设置为gl_repeat,但不会重复。

I tried even gl_clamp_to_edge still its not working. 我什至尝试了gl_clamp_to_edge仍然无法正常工作。

images size 256*256 图片尺寸256 * 256

code: 码:

GLuint _textureId; //The id of the texture
int _wdth;
int _hight;
void LoadGLTextures( const char * name )
{
    QImage img;

    if(!img.load(name)){
        qDebug() << "ERROR in loading image";
    }

    QImage t = QGLWidget::convertToGLFormat(img);

    _wdth = t.width();
    _hight = t.height();
    qDebug()<<"width = "<<_wdth<<"height ="<<_hight;
    glGenTextures(1, &_textureId);
    glBindTexture(GL_TEXTURE_2D, _textureId);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, t.width(), t.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, t.bits());

    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
}

void GLWin::initializeGL()
{
    qglColor(Qt::black);
    glEnable(GL_CULL_FACE);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_MULTISAMPLE);
    static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
    ///cameraPos = 0;
    glEnable(GL_TEXTURE_2D);
    LoadGLTextures("resources/Green_Dragon.jpeg");
}
void GLWin::paintGL()
{
       glClearColor( 0.0f, 0.0f, 0.0f, 0.0f);
       glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );

       glMatrixMode( GL_MODELVIEW );
       glLoadIdentity();

       glEnable(GL_TEXTURE_2D);
       glColor3f(0.5, 0.5, 0);
       glBindTexture(GL_TEXTURE_2D, _textureId);

       glBegin(GL_QUADS);
       glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.5f, -0.5f);  // vertex 1
       glTexCoord2f(1.0f, 0.0f); glVertex2f(0.5f, -0.5f); // vertex 2
       glTexCoord2f(1.0f, 1.0f); glVertex2f(0.5f, 0.5f); // vertex 3
       glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.5f, 0.5f); // vertex 4
       glEnd();
       glDisable(GL_TEXTURE_2D);
       glFlush();
}
void GLWin::resizeGL(int width, int height)
{
    glViewport(0, 0, (GLint)width, (GLint)height);
    glMatrixMode(GL_PROJECTION);  // To operate on the Projection matrix
    glLoadIdentity();
    glOrtho(-0.5, 0.5, -0.5, 0.5, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
   glTexCoord2f(0.0f, 0.0f); glVertex2f(-0.5f, -0.5f);  // vertex 1
   glTexCoord2f(1.0f, 0.0f); glVertex2f(0.5f, -0.5f); // vertex 2
   glTexCoord2f(1.0f, 1.0f); glVertex2f(0.5f, 0.5f); // vertex 3
   glTexCoord2f(0.0f, 1.0f); glVertex2f(-0.5f, 0.5f); // vertex 4

If those are your texture coordinates, then it's not going to repeat. 如果这些是你的纹理坐标,那么它不会重复。 Texture wrap modes only apply when the texture coordinate exceeds the [0, 1] range. 纹理环绕模式仅在纹理坐标超出[0,1]范围时适用。 Since your texture coordinates are within that range, no repeating will happen. 由于纹理坐标在该范围内,因此不会重复。

You see scaling of the texture because you are scaling the positions of the triangles. 您看到的是缩放比例,因为您正在缩放三角形的位置 And therefore, the texture will be mapped in accord with those scaled positions. 因此,纹理将按照那些缩放位置进行映射。

You could use the texture matrix to do some transformations on the texture coordinates if you want to scale them. 如果要缩放它们,可以使用纹理矩阵对纹理坐标进行一些转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM