简体   繁体   中英

OpenGL C++ wont display texture

I just implemented a texture solution based on this solution which compiles correctly, however I don't see any changes to my scene. 在此输入图像描述 . The camera is currently inside a quad I drew if this matters.

Below is my code for mapping the texture.

GLuint LoadTexture( const char * filename )
{

    GLuint texture;

    int width, height;

    unsigned char * data;

    FILE * file;

    file = fopen( filename, "rb" );

    if ( file == NULL ) return 0;
    width = 1024;
    height = 512;
    data = (unsigned char *)malloc( width * height * 3 );
    //int size = fseek(file,);
    fread( data, width * height * 3, 1, file );
    fclose( file );

    for(int i = 0; i < width * height ; ++i)
    {
        int index = i*3;
        unsigned char B,R;
        B = data[index];
        R = data[index+2];

        data[index] = R;
        data[index+2] = B;

    }


    glGenTextures( 1, &texture );
    glBindTexture( GL_TEXTURE_2D, texture );
    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );


    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT );
    gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,GL_RGB, GL_UNSIGNED_BYTE, data );
    free( data );

    return texture;
}

void quad(int a,int b,int c,int d,float scaleX,float scaleY,float scaleZ )
{
    glBegin(GL_QUADS);
        glVertex3f(ver[a][0]*scaleX,ver[a][1]*scaleY,ver[a][2]*scaleZ);
        glVertex3f(ver[b][0]*scaleX,ver[b][1]*scaleY,ver[b][2]*scaleZ);
        glVertex3f(ver[c][0]*scaleX,ver[c][1]*scaleY,ver[c][2]*scaleZ);
        glVertex3f(ver[d][0]*scaleX,ver[d][1]*scaleY,ver[d][2]*scaleZ);
    glEnd();
}

void room1()
{
    GLuint texture;
    texture = LoadTexture( "brick.bmp" );
    glBindTexture (GL_TEXTURE_2D, texture);
    glColor3fv(color[6]);
    quad(0,3,2,1,10,5,11);
    glColor3fv(color[1]);
    quad(2,3,7,6,10,5,11);
    glColor3fv(color[2]);
    quad(0,4,7,3,10,5,11);
    glColor3fv(color[3]);
    quad(1,2,6,5,10,5,11);
    glColor3fv(color[4]);
    quad(4,5,6,7,10,5,11);
    glColor3fv(color[5]);
    quad(0,1,5,4,10,5,11);
}

Try to specify texture coordinanes to vertices you draw like this

void quad(int a,int b,int c,int d,float scaleX,float scaleY,float scaleZ )
{
    glBegin(GL_QUADS);

        glTexCoord2f(0.0f, 0.0f);
        glVertex3f(ver[a][0]*scaleX,ver[a][1]*scaleY,ver[a][2]*scaleZ);

        glTexCoord2f(1.0f, 0.0f);
        glVertex3f(ver[b][0]*scaleX,ver[b][1]*scaleY,ver[b][2]*scaleZ);

        glTexCoord2f(1.0f, 1.0f);
        glVertex3f(ver[c][0]*scaleX,ver[c][1]*scaleY,ver[c][2]*scaleZ);

        glTexCoord2f(0.0f, 1.0f);
        glVertex3f(ver[d][0]*scaleX,ver[d][1]*scaleY,ver[d][2]*scaleZ);
    glEnd();
}

You may modify texture coords values to achive expected texture orientation. And do not forget enable texturing by usage glEnable(GL_TEXTURE_2D); call.

Plus your code will be extra slow because you use

GLuint texture;
texture = LoadTexture( "brick.bmp" );
glBindTexture (GL_TEXTURE_2D, texture);

at each render call (texture loading is very slow operation), main recomendation is to load texture once at some initialization function. And more you didnot release video memory wich you used for texture. Тo deal with it call.

glDeleteTextures(1, &texture);

Texture memory releasing proces must be performed at application exit time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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