Thread: quake2, bsp and lightmaps

  1. #1
    Registered User
    Join Date
    Sep 2013

    quake2, bsp and lightmaps

    hello everyone, new to the forums.

    i was wondering if i could get some help for doing something that seems simple enough, yet it also seems like something i won't be able to figure out by myself this year.

    let me begin by saying that while i'm familiar with coding in C and C#,
    (i have done stuff from simple command line applications to graphical interfaces to 2d finite state machine engines to VERY crude attempts at my own 3d engines)

    this is something i'm not great at, reverse engineering other people's work (im guessing john carmack's) and it's even worse if they're in C++. i know the basic differences and what classes are, but i'm not exactly in my comfort zone if you guys know what i mean. it's been years since i was actually involved with any code, and though i wouldn't be too worried if i was working in my own stuff writing everything from scratch, this is a whole different beast but i believe i can do it if i can get you to point me in the right direction. (after all i always heard Q2 is C++ code written in a very C fashion)

    ok so on to the problem i have:

    i want to be able to change the quake2 so it will be able to load lightmaps as a .TGA file from the hard drive, like in quake3>
    instead of looking them up inside the BSP file itself (the playable map)

    i don't have any worries about the targa image format since i'm using KMquake2 port's source code instead of the old vanilla q2, so it should know what targa is, my problem is that i just can't find the line where quake2 actually loads the lightmap off the BSP file.

    while looking around on the net and other idtech games i found a line in the game enemy territory, inside bsp.c that loads the lightmap off the disc:

    // ydnar: clear lightmaps first
    tr.numLightmaps = tr.maxLightmaps = 0;
    tr.lightmaps = NULL;
    // get number of external lightmaps
    if (tr.worldDir) {
    ri.FS_ListFiles(tr.worldDir, ".tga", &numExternalLightmaps);

    if im thinking correctly i just need to incorporate those bits onto quake2 so it can load the lightmaps off the disc,
    i dont even need to generate the lightmaps myself since i'm using the quake3 compiler to recompile my maps and throwing the -external switch so the lightmaps get baked into a tga, then i just use old quake2 bsp format but hopefully with an externally loaded tga lightmap that i got from q3map2?

    so in theory i wouldn't need to mess with the codes that lump the triangles and makes an array so they can be "uv mapped" as it were..

    if i have the right idea i should be looking in KMQ2's lightmap.c or draw.c or surface.c, but i have no clue which, since they all reference lightmaps and none of them seem to have a lightmap loading routine, or i just cant find it.

    how would i even go about doing such a thing? im at a loss, i thank you for taking the time to help out a C++ newb!
    much appreciated!!!
    Last edited by basteagui; 09-06-2013 at 06:23 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    I suggest the first thing you get is a decent code editor / IDE with lots of indexing / search tools built into it.

    With this, you can easily find declarations / references / implementations of whatever it is you're looking at.

    Also, just running it in a debugger and setting a few breakpoints at interesting points, then stepping the code just to follow where it goes.

    You'll also need lots of paper to sketch out class hierarchies, call sequences, relationships and what not.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2013
    thanks for the suggestions salem. will keep try and get myself set up and organized,

    i think i have narrowed it down to lightmap.c, at the beginnning of the lightmap.c code there's a comment that reads:

    "// r_lightmap.c: lightmap loading and handling functions"

    for a c file it's not huge, but im scared that i may break something related to other parts of the code. so like you said i'm going to have to study the code in detail.

    however - i do have a second idea, though; which may prove a lot easier:

    some time ago i found the source code for the quake2 map compiling tools.
    they're tiny, only kilobytes each, yet they create the BSP file that the engine reads and the lightmap itself (it's baked onto the bsp file)
    sources i found here:

    i also know someone made a quake 2 "map mixer" utility that opens up BSP files and swaps textures and can even recompile the light stage, here:
    Map Mixer for Quake II released!

    so, i guess my next question is, would it be easier to just make a utility that opens the bsp file and extracts/replaces the lightmap directly onto it?
    i'm guessing it would be easier than rewriting quake2's convoluted code to look up the lightmap elsewhere.
    just rewrite the 2mb bsp file to swap out and replace the lightmap in whatever format i can get it to work with (my guess is pcx?)

    does this seem more reasonable and/or feasible for someone like me?

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Didn't we have a member that worked on this and had various maps loading and running? I do not remember who it was. Perhaps someone else remembers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lightmaps vs "regular" lighting
    By psychopath in forum Game Programming
    Replies: 4
    Last Post: 05-19-2005, 10:23 AM