Home > OS >  What am i missing that texture isn't working OpenGL
What am i missing that texture isn't working OpenGL

Time:12-20

I'm trying to apply textures to a face for now until i get it to work properly, but everytime the application runs the face is just a white color as it is for default, yet i don't know what is going wrong.

LoadTexture function:

GLuint LoadTexture( const char* texture )
{
    GLuint textureID = SOIL_load_OGL_texture( texture, SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS );

    glBindTexture( GL_TEXTURE_2D, textureID );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
    glBindTexture( GL_TEXTURE_2D, 0 );

    return textureID;
}

Main code:

GLuint tex = LoadTexture("grass.jpg");
int crotate = 0;

void Reshape(int w, int h)
{
    if (h == 0) h = 1;
    float ratio = w * 1.0 / h;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0, 0, w, h);
    gluPerspective(45.0f, ratio, 0.1f, 1000);
    glMatrixMode(GL_MODELVIEW);
}
void Cube()
{
    glTranslatef(0, 0, -5);
    crotate  ;
    glRotatef(crotate, 1,1,0);
    glActiveTexture(tex);
    glEnable(GL_TEXTURE_2D);
    glBindTexture( GL_TEXTURE_2D, tex);
    glBegin(GL_POLYGON);
    glTexCoord2f(0.0, 1.0); glVertex3f(-0.5,-0.5,0);
    glTexCoord2f(0.0, 0.0); glVertex3f(-0.5,0.5,0);
    glTexCoord2f(1.0, 0.0); glVertex3f(0.5,0.5,0);
    glTexCoord2f(1.0, 0.0); glVertex3f(0.5,0.5,0);
    glTexCoord2f(1.0, 1.0); glVertex3f(0.5,-0.5,0);
    glTexCoord2f(0.0, 1.0); glVertex3f(-0.5,-0.5,0);
    glEnd();
    glDisable(GL_TEXTURE_2D);
}

void Display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    Cube();
    glutPostRedisplay();
    glutSwapBuffers();
}

void Init()
{

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    //Lighting
    /*
    glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    GLfloat qaDiffuseLight[] = {0.8, 0.8, 0.8, 1};
    glLightfv(GL_LIGHT0, GL_DIFFUSE, qaDiffuseLight);
    GLfloat qaLightPos[] = {0.5, 0.5, 0, 1.0};
    glLightfv(GL_LIGHT0, GL_POSITION, qaLightPos); */
    gluPerspective(45.5, 1.0f, 0.1f, 1000);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_DEPTH_TEST);

    glClearColor(0, 0.6, 1, 1);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE);
    glutInitWindowSize(1280,720);
    glutCreateWindow("OpenGL Program");
    Init();
    glutReshapeFunc(Reshape);
    glutDisplayFunc(Display);
    glutMainLoop();
    return 0;

}

I have searched many tutorials to texture map but all those i tried still aren't working for me, i'm using GLUT The latest tutorial or rather blog i followed to try adding textures was this https://www.3dgep.com/texturing-and-lighting-in-opengl/

CodePudding user response:

The parameter of glActiveTexture is the texture unit:

`glActiveTexture(tex);

glActiveTexture(GL_TEXTURE0);

The texture object is bound to a texture unit. The current texture unit can be stet with glActiveTexture. The default texture unit is 0 (GL_TEXTURE0).


You cannot execute an OpenGL statement until you have a valid and current OpenGL context. Therefore you have to create the texture object after creating the OpenGL window and context. Call GLuint tex = LoadTexture("grass.jpg"); after the window is crated and the Context is made current.

GLuint tex = 0;
void Init()
{
    tex = LoadTexture("grass.jpg");

    // [...]
}
int main(int argc, char** argv)
{
    // [...]

    glutCreateWindow("OpenGL Program");
    Init();

    // [...]
}
  • Related