PDA

View Full Version : OpenGL Mode Changes...



Sephiroth
09-24-2002, 07:31 PM
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.

t0y
09-24-2002, 07:50 PM
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.

Sephiroth
09-25-2002, 08:21 AM
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.

t0y
09-25-2002, 04:55 PM
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..

http://www.opengl.org/discussion_boards/ubb/wink.gif

Sephiroth
09-25-2002, 07:25 PM
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(&amp;Width, sizeof(short int), 1, TexFile);
fread(&amp;Height, sizeof(short int), 1, TexFile);
Type = 0;

if(Width == 32 &amp;&amp; Height == 32)
{
fread(&amp;ResA, sizeof(ResA), 1, TexFile);
Type = 1;
}
if(Width == 64 &amp;&amp; Height == 64)
{
fread(&amp;ResB, sizeof(ResB), 1, TexFile);
Type = 2;
}
if(Width == 96 &amp;&amp; Height == 96)
{
fread(&amp;ResC, sizeof(ResC), 1, TexFile);
Type = 3;
}
if(Width == 128 &amp;&amp; Height == 128)
{
fread(&amp;ResD, sizeof(ResD), 1, TexFile);
Type = 4;
}
if(Width == 64 &amp;&amp; Height == 96)
{
fread(&amp;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(&amp;msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
done = true;
else
{
TranslateMessage(&amp;msg);
DispatchMessage(&amp;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.

t0y
09-25-2002, 09:25 PM
I'm sorry... I tryied but I'm not too good at reading other people's code http://www.opengl.org/discussion_boards/ubb/biggrin.gif.

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 http://www.opengl.org/discussion_boards/ubb/smile.gif