OpenGL Mode Changes...

OK, I have two questions about the way OpenGL handles textures. The first deals with switching between full-screen and windowed modes. If I start my app full-screen (like Quake for instance) it runs fine and textures perfectly. However, say I want to go windowed, I hit F12 which switches to windowed mode. When I hit F12, it destroys the OpenGL window, then re-creates it windowed, and then calls “InitGL”, my function for setting up culling, rendering, etc etc. When I go windowed however, I see the frame created, then it just exits. I have a feeling it is due to the textures because in the early days of my engine, when I went windowed I would have plain white walls, but going full again made it normal. Am I supposed to somehow clear my texture memory when swapping modes?

This brings me to my next question. Say I load five textures on map A. I then reach the exit and go to map B, which loads ten textures. Is there some way I have to wipe out my five textures first, then use the next set, or am I just supposed to bind to a texture slot I already used and over-write it? Thanks for the help.

Well, I don’t know if i’ve understood your problem correctly, and maybe i’ll just tell you what you already know. This is what I think you’re getting wrong:

Textures are defined inside a gl context. Whenever you swap modes in your app the context gets destroyed (and any textures within). By the time you create another one your textures are invalid.

In other words, try changing your texture loading code to the InitGL function.

On your second question, you can always keep all your textures loaded. The problem is that you’ll probably end up filling all your video memory and performance will suffer. If there is a group of textures that you really don’t need, you should use glDeleteTextures before loading the next group.

There is also a priority mechanism you can use to tell the driver which textures are important and kept in video mem (as your card permits), but I haven’t played with that so I can only tell to look up glPrioritizeTextures in msdn/red book.

OK, glDel* was what I was looking for to clear the memory. As for my first question, the way my functions work is this:

Player hits F12
InitGL() is called, which first calls LoadTextures()
Now InitGL() sets up culling and such

Now why isn’t it working? I can post some source if it will help.

For the 1st problem (that I seemed to forget :4), I’m guessing you’re having sync problems with your messages.

Creating/destroying windows in WM_CHAR/WM_KEY messages isn’t a generally very good thing.

Code would help if you strip it down to the bare essentials and provide the full code so that me (and others) can have a try at home…

OK, here’s a basic example.

bool keys[256];

bool LoadTextures()
{
char TexBuffer[128], Type;
short int Width, Height;
GLubyte ResA[32][32][3], ResB[64][64][3], ResC[96][96][3], ResD[128][128][3], ResE[96][64][3];
FILE *TexFile;

glGenTextures(Area.numTextures, &Texture[0]);

for(short int Loop = 0; Loop < Area.numTextures; Loop++)
{
sprintf(TexBuffer, “Textures\%s”, Area.TexList[Loop]);
if((TexFile = fopen(TexBuffer, “rb”)) == NULL)
return false;

fread(&Width, sizeof(short int), 1, TexFile);
fread(&Height, sizeof(short int), 1, TexFile);
Type = 0;

if(Width == 32 && Height == 32)
  {
  fread(&ResA, sizeof(ResA), 1, TexFile);
  Type = 1;
  }
if(Width == 64 && Height == 64)
  {
  fread(&ResB, sizeof(ResB), 1, TexFile);
  Type = 2;
  }
if(Width == 96 && Height == 96)
  {
  fread(&ResC, sizeof(ResC), 1, TexFile);
  Type = 3;
  }
if(Width == 128 && Height == 128)
  {
  fread(&ResD, sizeof(ResD), 1, TexFile);
  Type = 4;
  }
if(Width == 64 && Height == 96)
  {
  fread(&ResE, sizeof(ResE), 1, TexFile);
  Type = 5;
  }

fclose(TexFile);
if(Type == 0)
  {
  MessageBox(NULL, "Bad texture!", "Debug", MB_OK);
  return false;
  }

glBindTexture(GL_TEXTURE_2D, Texture[Loop]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

if(Type == 1)
  gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, ResA);
if(Type == 2)
  gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, ResB);
if(Type == 3)
  gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, ResC);
if(Type == 4)
  gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, ResD);
if(Type == 5)
  gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, Width, Height, GL_RGB, GL_UNSIGNED_BYTE, ResE);
}

return true;
}

bool InitGL()
{
if(!LoadTextures())
return false;

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

return true;
}

LRESULT CALLBACK MainCalls(…)
{
switch(cbmsg)
{
case WM_KEYDOWN:
{
keys[cbwp] = true;
return 0;
}
case WM_KEYUP:
{
keys[cbwp] = false;
return 0;
}
}
return DefWindowProc(…);
}

int WINAPI WinMain(…)
{
bool done = false;
MSG msg;
WNDCLASS wc;

//Define and register window class
//Load custom settings

while(!done)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
done = true;
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
//Handles calling screen drawing and such
if(keys[VK_F12])
{
DestroyGLWindow();
glDeleteTextures(numTextures, Texture);
CreateGLWindow();
InitGL();
keys[VK_F12] = false;
}
}
}
return msg.wParam;
}

That should be all you need. In my “CreateGLWindow” function I have a check on every command, including pixelformat settings and the actual CreateWindowEx() command. The whole function seems to work, but the app just auto-exits for some reason.

I’m sorry… I tryied but I’m not too good at reading other people’s code .

It would be better to post (give a link, etc) to complete/compilable code. Then some debugging could make a difference.

You don’t have to post your entire app! Just the important stuff. Maybe you find your bug in the process