PDA

View Full Version : Problem with textures and gluSphere



div29
12-04-2011, 05:17 AM
Hello everyone,

I'm just starting with opengl. This is my first project, in fact.
I want to map some texture to a quadrilateral in the background and want to draw a sphere using gluSphere in the front and I want to animate this. So, I map the texture first, then draw the sphere in the display function and then call glutPostRedisplay. It does show the texture and the sphere correctly when display is first called. But, as soon as glutPostRedisplay is called, the texture disappears and only the sphere is drawn. I have given my code below. I apologize for using any bad opengl practices.
<code>void display()
{
glClear(GL_COLOR_BUFFER_BIT);
drawTex();
glPushMatrix();
glTranslatef(SIZE/2, SIZE/2, 0);
glutSolidSphere(15.0, 20, 20);
glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();
}
void LoadTextureRAW( const char * filename, int wrap )
{
GLuint texture;
int width, height;
unsigned char * data;
FILE * file;

// open texture data
file = fopen( filename, "rb" );
if ( file == NULL )
{

return;
}
// allocate buffer
cout << "File null" << endl;
width = 1073;
height = 918;
data = (unsigned char *)malloc( width * height * 3 );

// read texture data

fclose( file );

// select our current texture
glBindTexture( GL_TEXTURE_2D, 1 );

// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

// when texture area is small, bilinear filter the closest mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST );
// when texture area is large, bilinear filter the first mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

// if wrap is true, the texture wraps over at the edges (repeat)
// ... false, the texture ends at the edges (clamp)
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 );

// build our texture mipmaps
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
GL_RGB, GL_UNSIGNED_BYTE, data );

// free buffer
free( data );


}
void drawTex()
{
glBegin(GL_QUADS);
glTexCoord2d(0.0,0.0);
glVertex3d(0.0,SIZE/2, -SIZE);
glTexCoord2d(1.0,0.0);
glVertex3d(SIZE, SIZE/2, -SIZE);
glTexCoord2d(1.0,1.0);
glVertex3d(SIZE, SIZE/2, SIZE);
glTexCoord2d(0.0,1.0);
glVertex3d(0.0, SIZE/2, SIZE);
glEnd();
glFlush();
}

void map()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);

glEnable( GL_TEXTURE_2D );
LoadTextureRAW("background.bmp", true);
glBindTexture( GL_TEXTURE_2D, 1 );
drawTex();
}

void init()
{
glClearColor(1, 1, 1, 1);
glClearDepth( SIZE );
glOrtho(0, SIZE, 0, SIZE, -SIZE, SIZE);

GLfloat mat_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { SIZE/2, 0, 1.0, 0.0 };
GLfloat model_ambient[] = { 0.5, 0.5, 0.5, 1.0 };

glClearColor(1.0, 1.0, 1.0, 1.0);

glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient);
//glColor3f(0,0,0);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glRotatef(-40, 1, 0, 0);
glRotatef(-40, 0, 1, 0);
map();
}</code>
Any help would be really appreciated.

BionicBytes
12-04-2011, 12:41 PM
You have misunderstood how to generate texture IDs and bind them.

// select our current texture
glBindTexture( GL_TEXTURE_2D, 1 );

What is 1 doing there? That's not a value you obtained from GL is it?
Use glGenTexture to obtain the ID.

Alfonse Reinheart
12-04-2011, 12:57 PM
Technically, that's perfectly legal (pre-GL 3.1 core). One of the stupider bits of old-school GL objects is that they are required to allow the user to basically pick a GLuint number and use it as an object name. The driver has to play along and create an object for it. You don't have to use glGen* to generate them.

Of course, that doesn't mean that you should do this. Ever.