load tga image

I’m trying to load a tga file for texture
I’ve downloaded this code
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=33
The number of read pixels is more then necessary

Have someone some source code examples in C to do that?

www.artifactgames.de/Code/Loader.zip

thank you Jan.
now i can load tga file but I can’t see the texture on my figure.
this is the code:

void draw_cube(void){

glRotatef(beta, 1, 0, 0);
glRotatef(alpha, 0, 1, 0);
glColor3f(1, 1, 1);
glTranslatef(translate_x, translate_y, 0);

if(strcmp (_cube->texture_path, "") == 0) 
	glutWireCube(_cube->size);
else
	glutSolidCube(_cube->size);

}

/*
** Function called to update rendering
*/
void DisplayFunc_cube(void)
{

glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

/* Perspective view */
glViewport(0, 0, width / 2, height / 2);
glPushMatrix();
glTranslatef(0, 0, -10);
glRotatef(0, 1, 0, 0);
glRotatef(0, 0, 1, 0);
/* Axis */
glBegin(GL_LINES);
	glColor3f(1, 0, 0); glVertex3f(-1, -1, -1); glVertex3f( 1, -1, -1);
	glColor3f(0, 1, 0); glVertex3f(-1, -1, -1); glVertex3f(-1,  1, -1);
	glColor3f(0, 0, 1); glVertex3f(-1, -1, -1); glVertex3f(-1, -1,  1);
glEnd();
GLfloat light_position[] = { 0, 0, 1, 0};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
draw_cube();
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glPopMatrix();


/* Orthogonal projection */
glMatrixMode(GL_PROJECTION);
/*
** Note how the projection matrix is pushed, to save the perspective
** computed in ReshapeFunc
*/

glPushMatrix();
glLoadIdentity();
glOrtho(-1.2, 1.2, -1.2, 1.2, -1.2, 1.2);
glMatrixMode(GL_MODELVIEW);

/* Right view */
glViewport(0, height / 2 + 1, width / 2 + 1, height / 2);  
glPushMatrix();
glRotatef(90, 0, 1, 0);
/* Axis */
glBegin(GL_LINES);
	glColor3f(1, 0, 0); glVertex3f(-1, -1, -1); glVertex3f( 1, -1, -1);
	glColor3f(0, 1, 0); glVertex3f(-1, -1, -1); glVertex3f(-1,  1, -1);
	glColor3f(0, 0, 1); glVertex3f(-1, -1, -1); glVertex3f(-1, -1,  1);
glEnd();

gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP, image->components, image->width, image->height, image->format, GL_UNSIGNED_BYTE, image->pixels);
glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP_EXT, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	
glEnable(GL_TEXTURE_CUBE_MAP_EXT);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_EXT);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_EXT);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_EXT);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_GEN_R);
draw_cube();
glPopMatrix();

/* Face view */
glViewport(width / 2 + 1, height / 2 + 1, width / 2, height / 2);
glPushMatrix();
/* Axis */
glBegin(GL_LINES);
	glColor3f(1, 0, 0); glVertex3f(-1, -1, -1); glVertex3f( 1, -1, -1);
	glColor3f(0, 1, 0); glVertex3f(-1, -1, -1); glVertex3f(-1,  1, -1);
	glColor3f(0, 0, 1); glVertex3f(-1, -1, -1); glVertex3f(-1, -1,  1);
glEnd();
draw_cube();
glPopMatrix();

/* Top view */
glViewport(width / 2 + 1, 0, width / 2, height / 2);
glPushMatrix();
glRotatef(90, 1, 0, 0);
/* Axis */
glBegin(GL_LINES);
	glColor3f(1, 0, 0); glVertex3f(-1, -1, -1); glVertex3f( 1, -1, -1);
	glColor3f(0, 1, 0); glVertex3f(-1, -1, -1); glVertex3f(-1,  1, -1);
	glColor3f(0, 0, 1); glVertex3f(-1, -1, -1); glVertex3f(-1, -1,  1);
glEnd();
draw_cube();
glPopMatrix();

/* Projection matrix is restaured */
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

/* End */
glFlush();
glutSwapBuffers();

}

void set_opengl_cube_win(Cube *cubo){
glutInit(&argc_, argv_);
_cube = cubo;

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(800, 800);
glutCreateWindow("Visualizzatore figure geometriche");

if(strcmp (_cube->texture_path, "") != 0) {
	FILE *file;

	file = fopen(_cube->texture_path, "rb");
	if (file == NULL) {
		printf("cm_demo: could not open \"%s\"

", _cube->texture_path);
exit(1);
}
image = gliReadTGA(file, _cube->texture_path);
fclose(file);

}
glClearColor(0, 0, 0, 0);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glutDisplayFunc(&DisplayFunc_cube);
glutReshapeFunc(&ReshapeFunc_cube);
glutKeyboardFunc(&KeyboardFunc_cube);
glutMouseFunc(&MouseFunc_cube);
glutMotionFunc(&MotionFunc_cube);

glutMainLoop();

}

I load image and in DisplayFunc_cube i want apply texture to the cube but there is something wrong.

Can you help me?

P.S.: I can use all type of texture in this example I use GL_TEXTURE_CUBE_MAP.

First of all, you are calling

gluBuild2DMipmaps(GL_TEXTURE_CUBE_MAP, image->components, image->width, image->height, image->format, GL_UNSIGNED_BYTE, image->pixels);

EVERY frame! You only need to call that ONCE, after that you can simply bind it every frame, the data is loaded into the GPUs memory.

Second, you are loading the cubemap wrong. When you load a cubemap, you need to load six images, into the six faces of the cubemap, and to load a 2D image into one face you need to use another texture-target (not GL_TEXTURE_CUBE_MAP but GL_TEXTURE_CUBE_MAP_NEG_X, …_POS_X, etc. (don’t know the exact names from memory)).

You can look the proper way up in the specification or in some tutorials. However glGetError should already throw lots of stuff at you because your gluBuild2DMipmaps call is invalid.

For starters i would simply load a 2D texture and display a quad with it, just to check whether texturing is working, at all.

Have fun,
Jan.

I’ve just solved the problem and now it’s all right.
In my project I must apply texture to Sphere, Cone, Torus and so on. I use glutSolidSphere, glutSolidTorus ecc, are there some ways to apply texture similar to the GL_TEXTURE_CUBE_MAP,
for example GL_TEXTURE_SPHERE_MAP? or should I use simple simple GL_TEXTURE_2D??

If i understand you correctly, you want to map the cubemap from all sides on a 3D object, for better texturing than with 2D textures, right?

Such stuff is possible. But using glutSolid* and the fixed function pipeline, i don’t think there is a way.

  1. You can create a torus, sphere, etc yourself and then set your own 3D texture-coordinates (for sampling cubemaps you need 3D texture-coordinates). Of course the difficulty here is to build the meshes.

  2. You can use shaders and then write a vertex/fragment shader that creates proper texture coordinates for sampling the cubemap. This will also work with the glutSolid* objects, but of course you need to get used to shaders.

I would go for 2) because you will need to learn shaders soon anyway, and they are soo powerful, you can do almost anything with them :wink:

Jan.

thanks Jan for your reply.
I don’t know shaders, can you tell me more about it?
or can you link some tutorial?
how can I apply shader to the glutSolid*?

bye

Here’s a start:

http://nehe.gamedev.net/data/articles/article.asp?article=21

But it might not be the best tutorial. Look in the references of the article and search google for “glsl”, you will certainly find lots of information and beginner tutorials.

When you learn what shaders are and how they work, you will know how to apply them to all kind of things, including glutSolids.

Jan.