PDA

View Full Version : Texture mapping non-smoothing



~Lip~
05-18-2011, 12:58 AM
Dears,

I have a question on texture mapping using OpenGL. I have tried to texture map a photo onto a sphere hoping that it could have a more realistic metalic looking.
I mapped each texture to each sphere but there is something looking not smooth just like the first shown picture below.
I hope to have the result like the second picture shown below but I cannot figure out where the problem is. The texture mapping code list here:

//Part of the code: To generate the texture

QPixmap pixmap(materialFileName);
glGenTextures(1, &ID);
glBindTexture(GL_TEXTURE_2D, ID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR_MIPMAP_LINEAR);
QImage tx = this->convertToGLFormat(pixmap.toImage());
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, tx.width(), tx.height(), GL_RGBA, GL_UNSIGNED_BYTE, tx.bits());


//Not continue from above
//Part of the code: To bind the texture
glEnable(GL_TEXTURE_2D); //enable texture mapping
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); //Preset the texture mapping mode
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glBindTexture (GL_TEXTURE_2D, _it->front()->getTextureId());
drawSomething(); //The place to draw the sphere
glDisable(GL_TEXTURE_GEN_S);//disable texture generate
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_2D); //disable texture mapping


Can anyone help and explain?
http://143.89.38.172/Download/Texture1.png
http://143.89.38.172/Download/Texture2.png

Alfonse Reinheart
05-18-2011, 01:53 AM
Can anyone help and explain?

2D textures are squares (technically rectangles). Spheres are... well, spheres. There is no method by which you can map a rectangle onto a sphere with no distortion.

This is why every flat map of the Earth has some distortion relative to an actual globe.

The best you can do is use a cubemap, which is 6 2D images. Now, there will still be some distortion, but it will be very slight compared to what you have now.

~Lip~
05-18-2011, 02:06 AM
Thanks Alfonse, I get your point.

Yet the second picture is obtained from a program written by other person like(Our teacher)=.=.

I was asked to redo the same result by using OpenGL's texture mapping, but the first figure>.<

So Im wondering why that person can but me with similar opengl libraries.

Alfonse Reinheart
05-18-2011, 02:40 AM
So Im wondering why that person can but me with similar opengl libraries.

Probably because they were not applying the texture to the sphere with sphere mapping.

The texture you're using is intended to be a reflection of the surrounding environment, correct? This means that the texture coordinates need to be generated by performing reflection computations based on the camera.

This would not introduce the kinds of distortions you were getting, but nor would it represent an actual reflection. It wouldn't really be a reflection, but it would look kind of like one.

I honestly don't remember what the texture coordinate generation settings for that would be, but you could probably find them on the man pages (http://www.opengl.org/sdk/docs/man/) by looking up the legal values for glTexGeni.