PDA

View Full Version : unable to display more than one texture



olorin
04-21-2003, 04:36 AM
Greeting to everyone in this forum

I've got a problem with loading more than one texture. My program displays two boxes and I tried to texturemap each box. As long as I use only one texture everything seems to be fine. But when I load a second texture the first one won't be displayed. The box with the first texture displays completly white but the second box is texture mapped with the second loaded texture. Here is the code which i use to initialise OpenGL:



Colormap cmap;
XSetWindowAttributes swa;
GLXContext glxContext;
XVisualInfo *vi;
static int dblBuf[] = {GLX_RGBA, GLX_RED_SIZE, 8, GLX_GREEN_SIZE, 8, GLX_BLUE_SIZE, 8,
GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};

// open connection to X-Server
dpy = XOpenDisplay(NULL);
if (dpy == NULL)
{
cerr << "could not open display" << endl;
exit(1);
}
// check for GLX extension
if (!glXQueryExtension(dpy, NULL, NULL))
{
cerr << "could not find OpenGL GLX extension" << endl;
exit(1);
}
// check for appropriate visual
vi = glXChooseVisual(dpy, DefaultScreen(dpy), dblBuf);
if (vi == NULL)
{
cerr << "could not find appropriate visual" << endl;
exit(1);
}
// create the glx rendering context
glxContext = glXCreateContext(dpy, vi, None, True);
if (glxContext == NULL)
{
cerr << "could not create a rendering context" << endl;
}
// create a colormap
cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone);
swa.colormap = cmap;
swa.border_pixel = 0;
swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask;
win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 320, 240, 0, vi->depth, InputOutput, vi->visual,
CWBorderPixel | CWColormap | CWEventMask, &amp;swa);
XSetStandardProperties(dpy, win, "Engine", "OpenGLEngine", None, argv, argc, NULL);
// bind rendering contex to window
glXMakeCurrent(dpy, win, glxContext);
XMapWindow(dpy, win);
// configure OpenGL context
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
// set the clear-color
// enable Vertex Arrays
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

The code is mainly for the Book "OpenGL Programming for the X Window System" by Mark Kilgard

My drawing loop look somthing like this:


bool OpenGLEngine::redraw()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
glRotatef(xangle, 1.0, 0.0, 0.0);
glRotatef(yangle, 0.0, 1.0, 0.0);
glRotatef(zangle, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

if (objList->drawObjects() != true)
{
cerr << "OpenGLEngine::redraw: Error while drawing the list" << endl;
return false;
}
glXSwapBuffers(dpy, win);
return true;
}

where objList->drawObjects() calls the draw method for each Object (till now only one -> my boxes)


bool OpenGLEngineObject::draw()
{

texture->activateTexture();

glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, textureCoords);
glDrawArrays(GL_QUADS, 0, 16);
texture1->activateTexture();
glTranslatef(0.0, 2.5, 0.0);
glDrawArrays(GL_QUADS, 0, 16);
return true;
}

texture->activateTexture() is implemented like this:


bool OpenGLEngineTexture::activateTexture()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageBuffer);
return true;
}

when enabling the commented glTexImage2D line everything works just fine. But if i get it right the whole texture will copied to video-ram each time I call OpenGLEngineTexture::activateTexture. I thought, that I can bind a texture, copy the image data to video-ram and need only to bind the texture again to use it.

Here is the code i use to load the texture:


// a lot of libpng-function calls to load my texture
// and copy the imagedata to GLubyte * imageBuffer
// which contains the image in RGBA format
cout << "textureNr: " << texture << endl;
// prints 1 for first and 2 for second texture

glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageBuffer);

glEnable(GL_TEXTURE_2D);

if (glIsTexture(texture) == GL_TRUE)
{
cout << "texture bound" << endl; // output for both textures
}
else
{
cout << "texture not bound" << endl;
}


cout << "texture height: " << height << " - width: " << width << endl;
// prints 256x256 for both textures

return true;

the number in textures is generated by calling


glGenTextures(1, texNr);

and texNr is given to the texture

I'm working under linux (LFS - Linux from scratch) with XFree 4.3.0.
Hardware: GForce4600 (driver 41.91)


I hope I've given the most importand informations.

Thanx in advance

olorin

Please excuse my poor english


[This message has been edited by olorin (edited 04-21-2003).]

nomad82
04-21-2003, 05:12 AM
i will admit that i didn't read your code (too tired)...but my guess is that you're overwriting the texture ID of your texture when you load the second image.

------------------- http://nomad.openglforums.com

olorin
04-21-2003, 05:37 AM
Thanks for the quick replay

I displayed the ID for each texture while loading it. The first gives me 1 and the second gives me 2. I also displayed them during the activation and i get 1 and 2. So I don't think this is the problem.