
Loading Highres
Textures
|
Copyright info |
|
All
code in this tutorial is protected by the [GPL License]. |
|
Tutorial by: MrG |
||||||||||||||||
|
Mailto: biteme@telefragged.com If you've been
playing around with modding Quake2, you may have come across the fact that it
limits texture sizes to a maximum of 256x256, resizing all images greater
than that to that size. After
implementing the code within this tutorial, youll be able to load images with
dimensions greater than 256 pixels into the engine, providing that your
hardware supports it. Now, onto the
code! All of our
changes are going to take place within the file 'gl_image.c', so open it up
now in your editor of choice. Locate the function named 'GL_Upload32' and
delete it. We are going to rebuild this function, and we are going to do it
right! Now we shall
begin recreating it. Declare the function and its associated variables the
same as they was before:
These are the
variables that we will need, and the same function declaration allows it to
be easily used instead of the old function that we are replacing. Next we shall
reset the uploaded_paletted variable to false so that Quake2 knows that the
textures arent paletted, and then scan the input image data for any
non-opaque pixels.
Now that
that's done, we'll begin on the block of code that will determine the size
that the texture will be uploaded to video memory as. Firstly we'll create a
new scope and declare a couple more variables, query OpenGL for the hardwares
maximum texture size, and reset some variables values:
Now we have
our required variables. The variable 'powers_of_two' is an array of integers
containing the values of valid texture sizes that we can use. Each value is
power of 2, which is what OpenGL will accept for image dimensions. The
highest value in the list is 4096, which is higher = than any realistic
texture size that should be used. The value of max_size is the largest
texture size that the users OpenGL hardware can use. Next we will
loop through each texture dimension, and adjust the size of the image that we
will be uploading. The dimensions that we will use will be one of the values
of the powers_of_two array, the value closest to the input images width and
height.
Now it's
possible that the selected size is larger than what the OpenGL hardware will
accept, so we will check it against the maximum texture size that we
retrieved earlier, and adjust it if needed:
Now that
that's done, we shall check to see if the image needs resizing, and if so,
allocate sufficient memory and resize the texture.
Now we can
begin uploading it to video memory. If the 'mipmap' function parameter has
been set to true by the calling function, we will build mipmaps for the
texture and then upload it, else we will just upload it as it is. Either way,
we are also going to adjust the texture to take into account gamma control.
Finally we shall free the memory used by the scaled texture.
Now that we
have done that, we will inform Quake2 of the sizes that the image was scaled
to and uploaded as. We'll also set the filtering methods for the texture, and
then we will finally return from the function.
Before you can
compile, you'll need to link to the 'glu32.lib' library. Youll have to find
out how to do that for your compiler yourself. After doing that, all that you
need to do is compile. Quake2 is now
able to load textures with dimensions greater than 256x256, unless of course
your hardware doesnt support it. Enjoy. |
Tutorial Originally found at: