tags:

views:

289

answers:

5

Evening everyone, I'm going around in circles here but I think I'm looking in the wrong places.

My question is how would I go about loading an image and applying it to a primative in openGL. For example loading a bmp or jpg and applying it to a glutsolidsphere.

Would the solution be limited to one platform or could it work across all?

Thanks for your help

A: 

It might be more complex than you'd expect.

First of all, I'm quite sure you can't texture a sphere drawn using glutSolidSphere, as that function doesn't specify texture coordinates for the vertices it draws. So you'll need to code a method to draw a sphere either vertex by vertex or using a vertex buffer and be sure you specify texture coordinates for each vertex.

Once this is done, you need to load your images. OpenGL does not provide functions to load images from files and, if I remember well, glu and glut do not either. So you'll have to either read the image file yourself, or use a library that does so. DevIL is one of them.

Once you have the pixel data of the image, you need to create a new OpenGL texture using it. Some image loading libraries can do this for you. DevIL can.

I highly recommend googling for some tutorials, but please do not use NeHe tutorials, their quality is generally quite poor.

Trillian
A: 

There are an infinite number of ways to texture map an image onto a sphere. You could look into the gltexgen commands to automatically generate texture coordinates for a glut sphere.

tkerwin
+3  A: 

Well, if you want to, you can write your own bmp loader. Here is the specification and some code. Otherwise, I happen to have a tga loader here. Once you do that, it will return the data in an unsigned character array, otherwise known as GL_UNSIGNED_BYTE.

To make an OpenGL texture from this array, you first define a variable that will serve as a reference to that texture in OpenGL's memory.

GLuint textureid;

Then, you need to tell OpenGL to make space for a new texture:

glGenTextures(1, &textureid);

Then, you need to bind that texture as the currently used texture.

glBindTexture(GL_TEXTURE_2D, textureid);

Finally, you need to tell OpenGL where the data is for the current texture.

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, your_data);

Then, when you render a primitive, you apply it to the primitive by calling glBindTexture again:


glBindTexture(GL_TEXTURE_2D, textureid);
glBegin(GL_QUAD);
     glTexCoord2f(0.0, 0.0);
     glVertex3f(0.0, 0.0, 0.0);
     glTexCoord2f(1.0, 0.0);
     glVertex3f(1.0, 0.0, 0.0);
     glTexCoord2f(1.0, 1.0);
     glVertex3f(1.0, 1.0, 0.0);
     glTexCoord2f(0.0, 1.0);
     glVertex3f(0.0, 1.0, 0.0);
glEnd();

However, any time you apply the texture to a primitive, you need to have texture coordinate data along with vertex data, and glutSolidSphere does not generate texture coordinate data. To texture a sphere, either generate it yourself, or call texgen functions, or use shaders.

Ned
A: 

Ned has described it nicely. You should read tutorial from NeHe, where each step is described in detail. As others have pointed out, texturing quad would be good start and sphere may be bit tricky.

Ketan
A: 

That's funny, TGAs were the first type of image file I was able to load successfully into a texture. Unfortunately OpenGL does not have its own built in API functions to read files directly. So any external libraries or code would be needed. The TGA format is straightforward and Ned has provided some good code for it.

JustChris