• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By ThunderTwonk
      Hello everyone, I am working on a game idea and since I am still in the process of learning C# and the features available in unity I was hoping some of you might be able to offer me a little insight on things in general for getting started.
      I guess the basic components of what I'm wanting to create would be a Multi-levels management/city builder/rpg.
      The goal is to provide a framework for players to interact with, build in and affect the world both from a 3rd person action RPG as well as a zoomed out 4x style view (This would be something unlocked through gameplay)
      As for my questions go I was wondering if anyone had resources that could help me learn.  I've been on youtube as well as enrolled in an online course for basic unity and C# and will continue those but if anyone has any words of advice, a place that has good information and tutorials etc.
      Thanks for your time.
    • By Cahit Karahan

      Hi, I'm new in this forum. It is honorable to see such communities exist. I would like to share my new game. I did for android with unity. I know the game is a little awkward , but you have to know that this game is from the time when Unity's name is Unity3D  I have made my first game when I was 12. Now I am 22.  I have taken a lot of experience in this process and I can make better games nowadays. I have published this game nowadays but actually this game is very old but also it is very special for me :))
      I have just wanted to retouch and share this game, because it has a very important place for me.

      It's a special free 3D horror adventure action game for the halloween. Fun with scary sound effects and musics, 3D realistic graphics, you will feel the horror in the deep of your heart. Use your reflex. Totally free adventure. Totally scary horror game. 

      Tamarra, she is a beast from our world. She needs to consume souls from innocent people to stay alive. Story begins, the old Elaris tribe had lost their everything because of this beast who lived in the well. Araknas was the most powerful warrior of the tribe. One day, Araknas's mother was killed by the servant beasts of Tamarra. That's how Araknas's journey to the well begins. Tamara's well is guarded by horrible beasts. Araknas has to pass all servant beasts until he reaches Tamarra.

      Even death at the end is worth the revenge. 
      Are you brave enough to jump into Tamarra's well?

      Survive from witch attacks, clown attacks and many scary creature.

      - Realistic 3D graphics.
      - Scary sounds.
      - Scary musics.
      - Best experience with headphones.
      - A demon cage where you can imprison all the demons one by one
      - The witches do not like help, but they love blood stone. Witch store where you can develop your abilities and get new abilities.
      - Countless beasts.
      - At the end of the well there is a hidden surprise for you.

      *We do not recommend this game to people with clown phobia, spider phobia, or panic attacks.*

      **!!!**Note : This game is an early-access game, we are upgrading new features every day, new beasts, new improvements, as an example online 1vs1 fall on the list, so stay on connect and follow Halloween : Horror Well on Google Play.**!!!**

    • By INFRA
      SCAN. DRILL. SURVIVE.   ISOLATED Release in May 1st 2018   https://store.steampowered.com/app/805950/Isolated/   A game by Jérémie Bertrand Music & Sound Design by Pierrick Querolle *** Our solar system has been invaded by strangers. For the purpose of a possible negotiation, a team of astronauts is sent to the moon. Alas, they are shot before even arriving on the scene. Only one astronaut survives the crash and his only goal will be to go home...   GAMEPLAY   Shoot enemy ships to avoid being invaded. Be precise in your movements, because it's better to lose a bit of life at the top than to lose it all at the bottom. Take out your drill to destroy the stones in your path. Validate your identity to cross the different laboratories. Reach the flag before losing your three lives.   And all that... at the same time! Will you be able to go home? If the answer is yes, how long will it take?
    • By BigJiggly
      Hello! So, I've been the leader of BJP for a while now. I'm a bit bored of taking the role I always take, leader. I was hoping someone out there is looking to forge a team maybe and needs a programmer. 
      I have experience mainly in the Unity engine(C# intermediate) and I have a very small amount of knowledge on Shaders, as well as experience on developing games(usually end up stuck in dev hell) and leading experience from my last team which at one point reached 11 people. I personally love the Unity engine and prefer to use it as it's the development environment I'm comfortable with. 
      I have used Unity for over a year and a few months, I'd consider myself an intermediate at the Engine, but to this day Unity still surprises me. 
      I live in the United Kingdom, I find it a bit strange to work with other programmers as the ones I've worked with tend to leave their code heavily unoptimised and I'm a on the go optimise kind of guy, I also like to get things done quickly.
      If you're a new team and need a programmer that has high levels of ambition and strives to maintain the motivation throughout the team, then I'm your guy. I don't care if you're just beginning because I'm all for helping people learn!
      To finish this off: I like to get things done and I like to get them done right the first time, if I fail I will do it again and again, etc, until I loose all motivation. So if you're a modeller or an artist, please don't leave me to do all the modelling/art as well as the programming and sound. I do have experience in all those areas but my main power is in programming and I'd prefer to keep it that way.
      [If this was posted in the wrong forum, sorry, I don't really know the layout of this website yet]
    • By Alexia
      Hi all,
      A small team are currently undertaking a fairly large game project involving multi-playing, taming and other complex game mechanisms.
      We are looking for someone who is experienced in programming a wide range of mechanisms, more information is on our discord server. It is also a learning experience and we wouldn't expect you to know how to do everything we wanted, but just ask that you would be willing to learn how to.
      If you are interested in joining the (rather long term project) just message me and i'll add you on discord.
      Thanks for your time,
  • Advertisement
  • Advertisement
Sign in to follow this  

Unity BSPv38 - Quake 2 BSP loader lightmap problem

This topic is 2461 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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: http://www.gamedev.net/community/forums/topic.asp?topic_id=17243 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 http://www.flipcode.com/archives/Quake_2_BSP_File_Format.shtml 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: http://img523.imageshack.us/my.php?image=lightmap2.jpg Here is my engine's futil attempt at rendering the lightmap: (with tex filter turned off): http://img523.imageshack.us/my.php?image=lightmap1.jpg 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
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
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 http://www.flipcode.com/archives/Quake_2_BSP_File_Format.shtml).

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
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:
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);

Alternatively you can also seek for each lightmap while iterating through bsp faces. Current lightmap for current face would be located at header.lump[LUMP_LIGHTMAPS].offset + currentFace.lightmap_offset

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:
faces[f].fminUV.x = 999999;
faces[f].fminUV.y = 999999;
faces[f].fmaxUV.x = -99999;
faces[f].fmaxUV.y = -99999;

Then, you calculate the texture coordinates exactly like it's written on all q2bsp specs:
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;

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);

3. Now you're ready to get lightmap dimensions:
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;

4. Now the important part 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:
unsigned char *lm_data = &lightmapData[face->lightmap_offset];
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);

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:
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);

And don't forget the clamping:

5. Rendering (resulting texture coordinates generation):
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
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

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).


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 '// sigh' or '// stupid quake bug' 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 even if you will use atlas.
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.
It's not because lightmaps are not in atlas, it's because of how the data is stored in binary bsp file...

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.

Share this post

Link to post
Share on other sites
sly, did you try that yourself?

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 not 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
Sign in to follow this  

  • Advertisement