Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i know how to apply texture to a rectangle made by quad like this
C#
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
C#
glColor3f(1,0,0);
glRecti(startx,starty,startx+size,starty+size);
Posted

According to MSDN glRect(x1, y1, x2, y2) is equivalent to

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


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.

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


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


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. :)
 
Share this answer
 
v3
Comments
Simon Bang Terkildsen 1-Oct-11 10:30am    
my 5
glBindTexture( GL_TEXTURE_2D, texture );
or

glGenTextures( 1, &texture );

or

C++
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;
}
 
Share this answer
 
v2
Comments
Simon Bang Terkildsen 1-Oct-11 10:29am    
Added formating
Espen Harlinn 1-Oct-11 12:22pm    
Good reply :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900