本文介绍了如何在C ++中使用opengl将纹理应用到由rect制作的矩形上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何将纹理应用于像这样的四边形制成的矩形

i know how to apply texture to a rectangle made by quad like this

glColor3f(1,1,1);
textures[0].setTexture(1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,1);;
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0);
glVertex2f(startx,starty);
glTexCoord2f(1.0,0);
glVertex2f(startx+size,starty);
glTexCoord2f(1.0,1.0);
glVertex2f(startx+size,starty-size);
glTexCoord2f(0,1.0);
glVertex2f(startx,starty-size);
glEnd();



但我想在此矩形上应用纹理



but i want to apply texture on this rectangle

glColor3f(1,0,0);
glRecti(startx,starty,startx+size,starty+size);

推荐答案

glBegin(GL_POLYGON); 
glVertex2(x1, y1);
glVertex2(x2, y1); 
glVertex2(x2, y2);
glVertex2(x1, y2); 
glEnd( );



这里给出了4个没有纹理坐标的顶点.

我准备了四个角的不同颜色的图像.
然后检查以下代码的输出图像.



Here 4 vertices are given without a texture coordinate.

I prepared an image with different colours in four corners.
Then checked output image of following codes.

glTexCoord2f(0,0);
glRectf(-1,-1,1,1); // provide bottom left color.





glTexCoord2f(1,1);
glRectf(-1,-1,1,1); // provide top right color.



简单地glRect会接受所有四个顶点的单个纹理坐标,并且永远不会创建预期的纹理映射.

我认为很难使用glRect()函数创建整个纹理图像. :)



Simply glRect will accept a single texture coordinate four all vertices, and it will never create the expected texture mapping.

I think its difficult to create entire texture image with glRect() function. :)


GLuint LoadTextureRAW( const char * filename, int wrap )
{
    GLuint texture;
    int width, height;
    BYTE * data;
    FILE * file;

    // open texture data
    file = fopen( filename, "rb" );
    if ( file == NULL ) return 0;

    // allocate buffer
    width = 256;
    height = 256;
    data = malloc( width * height * 3 );

    // read texture data
    fread( data, width * height * 3, 1, file );
    fclose( file );

    // allocate a texture name
    glGenTextures( 1, &texture );

    // select our current texture
    glBindTexture( GL_TEXTURE_2D, texture );

    // select modulate to mix texture with color for shading
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

    // when texture area is small, bilinear filter the closest mipmap
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                     GL_LINEAR_MIPMAP_NEAREST );
    // when texture area is large, bilinear filter the first mipmap
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

    // if wrap is true, the texture wraps over at the edges (repeat)
    //       ... false, the texture ends at the edges (clamp)
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
                     wrap ? GL_REPEAT : GL_CLAMP );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
                     wrap ? GL_REPEAT : GL_CLAMP );

    // build our texture mipmaps
    gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
                       GL_RGB, GL_UNSIGNED_BYTE, data );

    // free buffer
    free( data );

    return texture;
}


这篇关于如何在C ++中使用opengl将纹理应用到由rect制作的矩形上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:39