我刚开始在qt中使用opengl,使用纹理我在quard中显示图像.
然后我将gltexture包装模式设置为gl_repeat但不重复.
我甚至试过gl_clamp_to_edge仍然无法正常工作.
图像大小256*256
码:
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(); }
Nicol Bolas.. 6
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
如果这些是你的纹理坐标,那么它不打算重复.纹理包裹模式仅在纹理坐标超过[0,1]范围时应用.由于纹理坐标在该范围内,因此不会发生重复.
您可以看到纹理缩放,因为您正在缩放三角形的位置.因此,纹理将根据那些缩放位置进行映射.
如果要缩放纹理坐标,可以使用纹理矩阵对纹理坐标进行一些变换.
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
如果这些是你的纹理坐标,那么它不打算重复.纹理包裹模式仅在纹理坐标超过[0,1]范围时应用.由于纹理坐标在该范围内,因此不会发生重复.
您可以看到纹理缩放,因为您正在缩放三角形的位置.因此,纹理将根据那些缩放位置进行映射.
如果要缩放纹理坐标,可以使用纹理矩阵对纹理坐标进行一些变换.