PDA

View Full Version : Texturing Sphere



AhmedSabry
04-21-2012, 04:43 AM
hello all
i need an know how to texture a sphere, i am using glutSolidSphere function,

can you give me an example

V-man
04-21-2012, 04:49 AM
http://www.opengl.org/wiki/Texturing_a_Sphere

AhmedSabry
04-21-2012, 04:55 AM
many thanks for your quick response, but this is a task that required to be done using glu, glut, if you have an example that uses these lib's , please send it

BionicBytes
04-21-2012, 07:27 AM
When you created your glu object you could ask it for texture coordinates.

AhmedSabry
04-21-2012, 08:01 AM
When you created your glu object you could ask it for texture coordinates.

i need an example

BionicBytes
04-21-2012, 09:09 AM
Plenty on the net.
Try this:


GLUquadricObj *sphere=NULL;
sphere = gluNewQuadric();
gluQuadricDrawStyle(sphere, GLU_FILL);
gluQuadricTexture(sphere, TRUE);
gluQuadricNormals(sphere, GLU_SMOOTH);
//Making a display list
mysphereID = glGenLists(1);
glNewList(mysphereID, GL_COMPILE);
gluSphere(sphere, 1.0, 20, 20);
glEndList();
gluDeleteQuadric(sphere);
//-----------------
//and whenever you want to render, call glCallList(mysphereID)
//to kill the display list, glDeleteLists(mysphereID, 1);

AhmedSabry
04-21-2012, 10:09 AM
Plenty on the net.
Try this:


GLUquadricObj *sphere=NULL;
sphere = gluNewQuadric();
gluQuadricDrawStyle(sphere, GLU_FILL);
gluQuadricTexture(sphere, TRUE);
gluQuadricNormals(sphere, GLU_SMOOTH);
//Making a display list
mysphereID = glGenLists(1);
glNewList(mysphereID, GL_COMPILE);
gluSphere(sphere, 1.0, 20, 20);
glEndList();
gluDeleteQuadric(sphere);
//-----------------
//and whenever you want to render, call glCallList(mysphereID)
//to kill the display list, glDeleteLists(mysphereID, 1);


nothing but a black screen, i don't know what is wrong
here is my draw function

void Draw()
{
GLuint texture = LoadTextureRAW("EarthMap.bmp",1);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -12.0f);
GLUquadricObj *sphere=NULL;
sphere = gluNewQuadric();
gluQuadricDrawStyle(sphere, GLU_FILL);
gluQuadricTexture(sphere, TRUE);
gluQuadricNormals(sphere, GLU_SMOOTH);
texture = glGenLists(1);
glNewList(texture, GL_COMPILE);
gluSphere(sphere, 1.0, 20, 20);
glEndList();
gluDeleteQuadric(sphere);
glutSwapBuffers();
glDisable(GL_TEXTURE_2D);
}

here is the loading texture function


GLuint LoadTextureRAW( const char * filename, int wrap )
{
GLuint texture;
int width, height;
BYTE * data;
FILE * file;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
file = fopen( filename, "r" );
if ( file == NULL )return 0;
width = 256;
height = 256;
data = (byte*) malloc( width * height * 3 );
fread( data, width * height * 3, 1, file );
fclose( file );
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP );
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height, GL_BGR_EXT, GL_UNSIGNED_BYTE, data );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glEnable( GL_TEXTURE_2D );
return texture;
}

V-man
04-21-2012, 10:54 AM
Did you notice this line?

//and whenever you want to render, call glCallList(mysphereID)

Also, read this
http://www.opengl.org/wiki/Common_Mistakes#glGenTextures_in_render_function

BionicBytes
04-21-2012, 01:25 PM
You are not following any tutorials are you?
You have not yet grasped the basics at all.
You are creating textures and glushpere each and every frame. The creation of these objects should be done exactly once. When you render you need to bind the texture then call the displaylist for the sphere. Also be sure to translate further into the Z because the sphere will be clipped against the near plane otherwise