turbos
turbos

Reputation: 63

load .png texture using soil, issue

Hey guys I'm new in OpenGL, and I have an issue while I try to display a texture.Every texture I try to display in .png format it just gives it's color, for example if I try to display a red brick it just appears the red, only the color. The code I use is below.Where should I bind the texture or what's wrong with code? I can't aunderstand.

GLuint LoadTexture(const char * filename, int width, int height){
GLuint texture_id;
unsigned char *data;

    texture_id = SOIL_load_OGL_texture(filename,
    4, 0, SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MIPMAPS |   SOIL_FLAG_INVERT_Y);

if (texture_id == 0)
{
    printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
}


glBindTexture(GL_TEXTURE_2D, texture_id);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_DECAL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_DECAL);
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);
}

void display()
{
 glClearColor(0, 0, 0, 1);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glEnable(GL_TEXTURE_2D);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 int w = glutGet(GLUT_WINDOW_WIDTH);
 int h = glutGet(GLUT_WINDOW_HEIGHT);
 gluPerspective(60, w / h, 0.1, 100);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(3, 3, 3,
          0, 0, 0,
          0, 0, 1);

glRotatef(rotate_x, 1.0, 0.0, 0.0);
glRotatef(rotate_y, 0.0, 1.0, 0.0);

glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_TEXTURE_2D);
mycube();
glutSwapBuffers();
}

int main(int argc, char **argv)
{

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(640, 480);
glutCreateWindow("CUBE");
texture = LoadTexture("texture.png", 256, 256);
glutDisplayFunc(display);
glutSpecialFunc(specialKeys);
glEnable(GLUT_DEPTH);


glutMainLoop();
FreeTexture(texture);

return 0;
}

Upvotes: 0

Views: 596

Answers (1)

Francis Cugler
Francis Cugler

Reputation: 7905

I do not see the source of your LoadTexture() function. One thing that can be done is to create a structure that holds the id value that OpenGL sets to a texture when it is bound to a renderable object, its width & height in pixels, color bit depth information, and the color data, if it contains transparencies, should be wrapped or repeated, and the mipmap quality being used. I do not know what you are using to open, read and parse a png file, but within my own projects I happen to use libpng.

Upvotes: 1

Related Questions