Sign in to follow this  

Unity BSPv38 - Quake 2 BSP loader lightmap problem

Recommended Posts

hypernewbie    123
Hey guys! I'm trying to write a quake 2 BSP loader, and everything works completely fine except for my lightmap...where I've come to a complete halt, much like this poor person: Is anyone floating around here thats run into the same problem? Half my lightmaps look good the other half of surfaces seem warped and retarded - like I've got the wrong dimensions or something. The general color of the lightmaps seem correct But I've followed and snooped around heaps, but I've only found people with the same problem, not solutions =( Here are comparison screenies: Here quake 2 screenshot with console command gl_lightmap 1: Here is my engine's futil attempt at rendering the lightmap: (with tex filter turned off): Also, it would be of great help if someone can point me to where quake 2 source stores each face's lightmap width/height, as I can just output that and compare. I've been digging through this q2source and keep getting lost o.O

Share this post

Link to post
Share on other sites
make sure you're using bilinear filtering on the lightmap textures, in opengl i think the functions are


Share this post

Link to post
Share on other sites
hypernewbie    123
yes i can easily put bilinear filtering on,

except it still looks "wrong" for some reason - my texture width/height calculations seem off, but I've followed that guide EXACTLY.

I also don't understand the part about lightmap being max 16x16...I'm getting lightmaps that are much larger than 16x16 from the exact formula he has given on the site, the web page contradicts itself o.O


Share this post

Link to post
Share on other sites
hypernewbie    123
Anyway, from looking at other people's projects and randomly modifying random bits of bitsy code, I somehow managed to get the formula right.

The formula for texture width and height are wrong, and the website has no mention of how lightmap texture coordinates are calculated.

I'll post my findings here, just in case anyone comes across this page on google with the exact same problem I ran into:

This info is for BSPv38, in other words Quake 2 BSP files, regarding lightmaps and loading and rendering them properly. I used openGL to do the rendering.

Getting lightmap dimensions:

Face structure gives the lightmap offset, but for some crazy reason, does not give the lightmap dimensions: you must calculate them yourself by going through every vertex, calculating their texture map U V co-ordinates and do stuff to them.

So, first, you go through every vertex of the face, and calculate their UV co-ordinates (use the formulae on the website

u = x * u_axis.x + y * u_axis.y + z * u_axis.z + u_offset
v = x * v_axis.x + y * v_axis.y + z * v_axis.z + v_offset

Then, you get the results in two floats, u and v. FLOOR u and FLOOR v into two integers, floor_u and floor_v. Then, you check floor_u and floor_v against max_u and max_v and min_u and min_v (4 of them all integers), to get the maximum and minimum floor_u and floor_v variables seen so far. DO NOT GET MAXIMUM/MINIMUM u and v. We need maximum/minimum floor_u and floor_v.

Then, you divide the min_u and max_u and min_v and max_v variables by 16, in FLOAT. If you are using C++, this means you will need to specifically typecast those 4 ints into float before dividing by 16.

Then, you floor the two min_ variables and ceil the two max_ variables, get the difference between the two, add 1, and typecast the result back to an int.

Here is my little bit of C++ code that does this step:

_minU = (float)floor((float)(minU / 16.0));
_maxU = (float)ceil((float)(maxU / 16.0));
_minV = (float)floor((float)(minV / 16.0));
_maxV = (float)ceil((float)(maxV / 16.0));
LIGHTMAPW = (int)(_maxU - _minU + 1);
LIGHTMAPH = (int)(_maxV - _minV + 1);

Getting lightmap texture co-ordinates:
This will generate a texture co-ordinate that's in the range 0.0 to 1.0, for opengl glTexCoord2f usage.

Let's say we need to get the texture co-ordinates for a vertex.

we first calculate the u and v for the texture map, the formula given above.
Then, we get LIGHTMAPW and LIGHTMAPH , also given above
We also need the minU and minV and maxU and maxV from above, the max/min floor_u and floor_v for all vertexes.

Then, simply follow this formula:

float MidPolyU = (minU + maxU) / 2.0;
float MidPolyV = (minV + maxV) / 2.0;
float MidTexU = LIGHTMAPW / 2.0;
float MidTexV = LIGHTMAPH / 2.0;
float lightmap_u = MidTexU + (u - MidPolyU) / 16.0;
float lightmap_v = MidTexV + (v - MidPolyV) / 16.0;
glTexCoord2f(lightmap_u/LIGHTMAPW ,lightmap_v/LIGHTMAPH );

As you can see, we get the co-ordinates and make them relative to the middle of the texture. Or something like that.

Hopefully this will solve the problems for people who are trying to load quake2's BSP files and get stuck at trying to load the lightmaps, because nowhere on the face of the internet is there such a documentation of this and quake 2 source code takes a while to excavate into.

Check six,

Share this post

Link to post
Share on other sites
gogiii    104
I know the topic is too old.
But I want to make the answer clear for others, because I spent a lot of time determining the source of wierd/broken/rainbow lightmaps.
So here is how to read lightmaps from quake 2 bsp file.
The next code is a bit simplified and there is no lightmap baking into one single texture here.
Only per-face generation of lightmaps just to be clear of all texture coordinate/dimensions calculations.

1. Just get all the data into memory:
[code]int lightmapDataLength = header.lump[LUMP_LIGHTMAPS].length; // /sizeof(unsigned char);
unsigned char lightmapData = new unsigned char[lightmapDataLength];
fseek(file, header.lump[LUMP_LIGHTMAPS].offset, SEEK_SET); // LUMP_LIGHTMAPS is 7
fread(lightmapData, sizeof(unsigned char), lightmapDataLength, file);[/code]
Alternatively you can also seek for each lightmap while iterating through bsp faces. Current lightmap for current face would be located at [i]header.lump[LUMP_LIGHTMAPS].offset + currentFace.lightmap_offset[/i]

2. Yes, to get lightmap dimensions you must process all faces and get those damn floored min/max of UVs.
First of all you must init the face data like this:
[code]faces[f].fminUV.x = 999999;
faces[f].fminUV.y = 999999;
faces[f].fmaxUV.x = -99999;
faces[f].fmaxUV.y = -99999;[/code]
Then, you calculate the texture coordinates exactly like it's written on all q2bsp specs:
[code]faces[f].texcoords[i*2 + j].x = verts[idx].x * texinfo->u_axis.x + verts[idx].y * texinfo->u_axis.y + verts[idx].z * texinfo->u_axis.z + texinfo->u_offset;
faces[f].texcoords[i*2 + j].y = verts[idx].x * texinfo->v_axis.x + verts[idx].y * texinfo->v_axis.y + verts[idx].z * texinfo->v_axis.z + texinfo->v_offset;[/code]
And only after that you compute min/max values while iterating through faces:
faces[f].fminUV.x = min(floor(faces[f].texcoords[i*2 + j].x), faces[f].fminUV.x);
faces[f].fminUV.y = min(floor(faces[f].texcoords[i*2 + j].y), faces[f].fminUV.y);
faces[f].fmaxUV.x = max(floor(faces[f].texcoords[i*2 + j].x), faces[f].fmaxUV.x);
faces[f].fmaxUV.y = max(floor(faces[f].texcoords[i*2 + j].y), faces[f].fmaxUV.y);[/code]

3. Now you're ready to get lightmap dimensions:
[code]int lightMapWidth = ceil(faces[f].fmaxUV.x/16) - floor(faces[f].fminUV.x/16) +1;
int lightMapHeight = ceil(faces[f].fmaxUV.y/16) - floor(faces[f].fminUV.y/16) +1;[/code]

4. Now the [b]important part[/b] which is still never indicated by anybody, with a little exceptions.
First of all if you choose to offset into lightmap data array directly, like:
[code]unsigned char *lm_data = &lightmapData[face->lightmap_offset];[/code]
And then pass rgb data into opengl you MUST specify alignment!
glTexImage2D(GL_TEXTURE_2D, 0, 3, lightMapWidth, lightMapHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, lm_data);[/code]
Without this all the people get lightmaps which are diagonaly projected or a lot of rainbow colors.
This deceives most developers and they think like: damn the calculations are wrong, there is something wrong with pointers/data/uvs/etc BUT ITS THE DAMN ALIGNMENT (blame Carmack for all this shit!)
The alternative way to read the data is to read it as rgb and add fourth component with value 255 and upload into rgba texture, like this:
[code]static unsigned char lm_data[32*32*4] = {0};
fseek(file, header.lump[LUMP_LIGHTMAPS].offset + face->lightmap_offset, SEEK_SET);
for(int y = 0; y<lightMapHeight; y++) {
for(int x = 0; x<lightMapWidth; x++) {
unsigned char rgba[4];
fread(rgba, sizeof(unsigned char), 3, file); // read rgb
rgba[3] = 255; // set the 4th component to 255
memcpy(&lm_data[x*4+(y*4)*lightMapWidth], rgba, sizeof(unsigned char)*4); // write as rgba
glTexImage2D(GL_TEXTURE_2D, 0, 4, lightMapWidth, lightMapHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, lm_data);[/code]
And don't forget the clamping:

5. Rendering (resulting texture coordinates generation):
[code]u = face->texcoords[j].x;
v = face->texcoords[j].y;
u -= face->fminUV.x;
v -= face->fminUV.y;
u /= (face->fmaxUV.x - face->fminUV.x);
v /= (face->fmaxUV.y - face->fminUV.y);
glTexCoord2f(u, v);

The result... well not totally but almost identical.

Share this post

Link to post
Share on other sites
mhagain    13430
The other alternative is to study the GPL Quake 2 source code and see how that does it (by packing lightmap textures into a 128x128 atlas and adjusting texcoords to match so that alignment isn't actually a problem).

Share this post

Link to post
Share on other sites
gogiii    104
[quote name='mhagain' timestamp='1311940732' post='4842103']
The other alternative is to study the GPL Quake 2 source code and see how that does it (by packing lightmap textures into a 128x128 atlas and adjusting texcoords to match so that alignment isn't actually a problem).
[left]srsly, did you try that yourself?
Why there's a tons of broken specs and such a clever person like you still didn't post any explanation anywhere so people won't be having problems?
Study what?...
A lot of non-commented retarded code with thousands of hax and funny comments like [i]'// sigh'[/i] or [i]'// stupid quake bug'[/i] where static lightmapping mixed with dynamic one with wrapped functions of memory allocation and also complicated with atlasing and hardware specific routines and around 80kb of code (gl_rsurf.c, gl_light.c, gl_model.c) just for this? LOL

Everyone knows that it does pack into atlas. But the alignment is an actual problem ANYWAY [b]even if you will use atlas[/b].
Why? Well, just try to upload lightmaps into your atlas having everything in GL_RGB (hell isn't obvious to use rgb when it's stored in rgb lol) format with TexSubImage and you'll get nice rainbows.
So there is a choice: you either store it in GL_RGBA filling dump alpha channel with 255 or using glPixelStorei with GL_RGB.
[i]It's not because lightmaps are not in atlas, it's because of how the data is stored in binary bsp file...[/i]

That's it and no need to 'study' any code where obvious things wrapped around thousand data structures.
I showed per-face texturing just because it's lot easier to understand. After this you can easily make atlas or whatever you want.
[/left] [/center]

Share this post

Link to post
Share on other sites
mhagain    13430
[quote name='gogiii' timestamp='1311943255' post='4842111']sly, did you try that yourself?[/quote]

As it happens, I did.

Yes - there is deep ugliness in there, but yes - if you want to work with Quake 2 formats then it is the definitive reference. Please note that I'm [i][b]not[/b][/i] sayig "copy/paste this code". What I'm saying is "if you want to know how lightmaps are supposed to work in the Quake 2 engine, this is the place to go".

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By dobbey
      Who are you?
      We call ourselves Dobbey. Our team currently consists of two people.
      We are both early 20's and come from Germany. At the moment this is our hobby.
      What's the name of the game and what is it about?
      The game is called Insane and it is a 2D adventure puzzle game with horror elements.
      Insane is about a scientist who has been doing human experiments for years.
      His most successful experiment is number 73, which seems to be mentally labile.
      This experiment manages to flee one day but can't distinguish between reality and imagination.
      In this game you will find a creepy atmosphere with difficult and varied puzzles.
      What inspired you?
      The style of the game is inspired by Limbo. But we were also inspired by Jump 'n' Run games and movies.
      Of course, we have also our own elements like an inventory system, butterfly effect, the "INSANE" effect, using a QR-Code..
      Which program do you use to develop the game?
      We are programming it with Unity. In our opinion, it is the best program for indie games.
      And of course we are working with Photoshop to create the graphics.
      What platform will Insane be available on?
      For PC.
      How can I support you?

      Here you can see some of our screenshots.


      (You can find the animation on Twitter/Instagram.)



      (You can find the animation on YouTube.)

      What do you think about it?
      Yo can see more screenshots or videos on Twitter, Instagram or YouTube.
    • By Liquid1Phantom
      Hello, my name is Thomas and I am currently starting up a project. 
      The project is going to be a very hardcore, and tactical FPS that is similar to Escape from Tarkov. It will be made within Unity, and will be programmed in C#. Currently, I have a Unity project started, an organized Discord setup, and as well as a very organized Google Drive. The Google Drive has art references, and plenty of folders for organization. Lastly, the payment for this project will be solely rev-share as I have no money.
      I am looking for anyone who can contribute: 2D artists, 3D artists, composers, programmers, etc.
      If you would like to help out and be of use, please email me at:
      Alternatively, add my Discord: Thomas#3788
    • By ilovegames
      Score points by shooting enemies! Before you is one of the best representatives of the simulator sniper games genre, with stunning graphics, quality effects, and realistic ballistics of bullets. It will not be easy to win for sure. Are you ready? Then take a sniper rifle and fight!

  • Popular Now