3

I just implemented a texture solution based on this solution which compiles correctly, however I don't see any changes to my scene. enter image description here. 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);
}
Curious Cat
  • 309
  • 1
  • 3
  • 14
  • do you need a fragment shader change in order for the texture to have an effect? What does your fragment shader look like? – dboals Dec 19 '15 at 05:18
  • 5
    Probably not worth an answer since it's so basic: You need texture coordinates. Any tutorial or example on OpenGL texturing will show you how to specify and use texture coordinates. – Reto Koradi Dec 19 '15 at 05:24
  • 1
    ["hmm, note this only works on images without headers"](https://stackoverflow.com/questions/12518111/how-to-load-a-bmp-on-glut-to-use-it-as-a-texture/12524013#comment37079712_12524013) – genpfault Dec 28 '15 at 22:01
  • @genpfault: I think its some kind of specific image format developed by OP) – Mykola Dec 29 '15 at 01:25

1 Answers1

1

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.

Mykola
  • 3,343
  • 6
  • 23
  • 39