From 9234eeb1d2954a3dae6c84ffa8ed8643953272c5 Mon Sep 17 00:00:00 2001 From: xolatile Date: Tue, 5 Aug 2025 15:27:57 +0200 Subject: Experimental MD3 to OBJ to IQM converter and code removal, shit compiles... --- src/engine/lightmap.cpp | 286 ++++-------------------------------------------- 1 file changed, 19 insertions(+), 267 deletions(-) (limited to 'src/engine/lightmap.cpp') diff --git a/src/engine/lightmap.cpp b/src/engine/lightmap.cpp index da1a5df..e705e4b 100644 --- a/src/engine/lightmap.cpp +++ b/src/engine/lightmap.cpp @@ -22,7 +22,6 @@ struct lightmapworker Slot *slot; vector lights; ShadowRayCache *shadowraycache; - BlendMapCache *blendmapcache; bool needspace, doneworking; SDL_cond *spacecond; SDL_Thread *thread; @@ -424,55 +423,6 @@ static bool packlightmap(lightmapinfo &l, layoutinfo &surface) return true; } -static void updatelightmap(const layoutinfo &surface) -{ - if(max(LM_PACKW, LM_PACKH) > hwtexsize || !lightmaps.inrange(surface.lmid-LMID_RESERVED)) return; - - LightMap &lm = lightmaps[surface.lmid-LMID_RESERVED]; - if(lm.tex < 0) - { - lm.offsetx = lm.offsety = 0; - lm.tex = lightmaptexs.length(); - LightMapTexture &tex = lightmaptexs.add(); - tex.type = lm.type; - tex.w = LM_PACKW; - tex.h = LM_PACKH; - tex.unlitx = lm.unlitx; - tex.unlity = lm.unlity; - glGenTextures(1, &tex.id); - createtexture(tex.id, tex.w, tex.h, NULL, 3, 1, tex.type&LM_ALPHA ? GL_RGBA : GL_RGB); - if((lm.type&LM_TYPE)==LM_BUMPMAP0 && lightmaps.inrange(surface.lmid+1-LMID_RESERVED)) - { - LightMap &lm2 = lightmaps[surface.lmid+1-LMID_RESERVED]; - lm2.offsetx = lm2.offsety = 0; - lm2.tex = lightmaptexs.length(); - LightMapTexture &tex2 = lightmaptexs.add(); - tex2.type = (lm.type&~LM_TYPE) | LM_BUMPMAP0; - tex2.w = LM_PACKW; - tex2.h = LM_PACKH; - tex2.unlitx = lm2.unlitx; - tex2.unlity = lm2.unlity; - glGenTextures(1, &tex2.id); - createtexture(tex2.id, tex2.w, tex2.h, NULL, 3, 1, GL_RGB); - } - } - - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glPixelStorei(GL_UNPACK_ROW_LENGTH, LM_PACKW); - - glBindTexture(GL_TEXTURE_2D, lightmaptexs[lm.tex].id); - glTexSubImage2D(GL_TEXTURE_2D, 0, lm.offsetx + surface.x, lm.offsety + surface.y, surface.w, surface.h, lm.type&LM_ALPHA ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, &lm.data[(surface.y*LM_PACKW + surface.x)*lm.bpp]); - if((lm.type&LM_TYPE)==LM_BUMPMAP0 && lightmaps.inrange(surface.lmid+1-LMID_RESERVED)) - { - LightMap &lm2 = lightmaps[surface.lmid+1-LMID_RESERVED]; - glBindTexture(GL_TEXTURE_2D, lightmaptexs[lm2.tex].id); - glTexSubImage2D(GL_TEXTURE_2D, 0, lm2.offsetx + surface.x, lm2.offsety + surface.y, surface.w, surface.h, GL_RGB, GL_UNSIGNED_BYTE, &lm2.data[(surface.y*LM_PACKW + surface.x)*3]); - } - - glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); -} - - static uint generatelumel(lightmapworker *w, const float tolerance, uint lightmask, const vector &lights, const vec &target, const vec &normal, vec &sample, int x, int y) { vec avgray(0, 0, 0); @@ -560,7 +510,7 @@ static bool lumelsample(const vec &sample, int aasample, int stride) static inline void generatealpha(lightmapworker *w, float tolerance, const vec &pos, uchar &alpha) { - alpha = lookupblendmap(w->blendmapcache, pos); + alpha = 0; if(w->slot->layermask) { static const int sdim[] = { 1, 0, 0 }, tdim[] = { 2, 2, 1 }; @@ -628,28 +578,28 @@ static bool generatelightmap(lightmapworker *w, float lpu, const lerpvert *lv, i } if((w->type&LM_TYPE) == LM_BUMPMAP0) memclear(w->raydata, (LM_MAXW + 4)*(LM_MAXH + 4)); - origin1.sub(vec(ystep1).add(xstep1).mul(blurlms)); - origin2.sub(vec(ystep2).add(xstep2).mul(blurlms)); + origin1.sub(vec(ystep1).add(xstep1).mul(0)); + origin2.sub(vec(ystep2).add(xstep2).mul(0)); int aasample = min(1 << lmaa, 4); int stride = aasample*(w->w+1); vec *sample = w->colordata; uchar *amb = w->ambient; lerpbounds start, end; - initlerpbounds(-blurlms, -blurlms, lv, numv, start, end); - float sidex = side0 + blurlms*sidestep; + initlerpbounds(-0, -0, lv, numv, start, end); + float sidex = side0 + 0*sidestep; for(int y = 0; y < w->h; ++y, sidex += sidestep) { vec normal, nstep; - lerpnormal(-blurlms, y - blurlms, lv, numv, start, end, normal, nstep); + lerpnormal(-0, y - 0, lv, numv, start, end, normal, nstep); for(int x = 0; x < w->w; ++x, normal.add(nstep), amb += w->bpp) { #define EDGE_TOLERANCE(x, y) \ - (x < blurlms \ - || x+1 > w->w - blurlms \ - || y < blurlms \ - || y+1 > w->h - blurlms \ + (x < 0 \ + || x+1 > w->w - 0 \ + || y < 0 \ + || y+1 > w->h - 0 \ ? edgetolerance : 1) float t = EDGE_TOLERANCE(x, y) * tolerance; vec u = x < sidex ? vec(xstep1).mul(x).add(vec(ystep1).mul(y)).add(origin1) : vec(xstep2).mul(x).add(vec(ystep2).mul(y)).add(origin2); @@ -660,12 +610,12 @@ static bool generatelightmap(lightmapworker *w, float lpu, const lerpvert *lv, i } if(adaptivesample > 1 && min(w->w, w->h) >= 2) lightmask = ~lightused; sample = w->colordata; - initlerpbounds(-blurlms, -blurlms, lv, numv, start, end); - sidex = side0 + blurlms*sidestep; + initlerpbounds(-0, -0, lv, numv, start, end); + sidex = side0 + 0*sidestep; for(int y = 0; y < w->h; ++y, sidex += sidestep) { vec normal, nstep; - lerpnormal(-blurlms, y - blurlms, lv, numv, start, end, normal, nstep); + lerpnormal(-0, y - 0, lv, numv, start, end, normal, nstep); for(int x = 0; x < w->w; ++x, normal.add(nstep)) { @@ -707,7 +657,7 @@ static bool generatelightmap(lightmapworker *w, float lpu, const lerpvert *lv, i if(aasample > 1) { vec normal, nstep; - lerpnormal(-blurlms, w->h - blurlms, lv, numv, start, end, normal, nstep); + lerpnormal(-0, w->h - 0, lv, numv, start, end, normal, nstep); for(int x = 0; x <= w->w; ++x, normal.add(nstep)) { @@ -730,9 +680,9 @@ static int finishlightmap(lightmapworker *w) float weight = 1.0f / (1.0f + 4.0f*lmaa), cweight = weight * (lmaa == 3 ? 5.0f : 1.0f); vec *ray = w->raydata; - uchar *dstcolor = blurlms && (w->w > 1 || w->h > 1) ? w->blur : w->colorbuf; + uchar *dstcolor = 0 && (w->w > 1 || w->h > 1) ? w->blur : w->colorbuf; uchar mincolor[4] = { 255, 255, 255, 255 }, maxcolor[4] = { 0, 0, 0, 0 }; - bvec *dstray = blurlms && (w->w > 1 || w->h > 1) ? (bvec *)w->raydata : w->raybuf; + bvec *dstray = 0 && (w->w > 1 || w->h > 1) ? (bvec *)w->raydata : w->raybuf; bvec minray(255, 255, 255), maxray(0, 0, 0); loop(y, w->h) { @@ -768,7 +718,7 @@ static int finishlightmap(lightmapworker *w) } if(w->type&LM_ALPHA) { - dstcolor[3] = ambientcolor[3]; + dstcolor[3] = 127;///TODO mincolor[3] = min(mincolor[3], dstcolor[3]); maxcolor[3] = max(maxcolor[3], dstcolor[3]); } @@ -823,13 +773,6 @@ static int finishlightmap(lightmapworker *w) w->lastlightmap->h = w->h = 1; } } - if(blurlms && (w->w>1 || w->h>1)) - { - blurtexture(blurlms, w->bpp, w->w, w->h, w->colorbuf, w->blur, blurlms); - if((w->type&LM_TYPE) == LM_BUMPMAP0) blurnormals(blurlms, w->w, w->h, w->raybuf, (const bvec *)w->raydata, blurlms); - w->lastlightmap->w = (w->w -= 2*blurlms); - w->lastlightmap->h = (w->h -= 2*blurlms); - } if(mincolor[3]==255) return SURFACE_LIGHTMAP_TOP; else if(maxcolor[3]==0) return SURFACE_LIGHTMAP_BOTTOM; else return SURFACE_LIGHTMAP_BLEND; @@ -1019,7 +962,7 @@ static bool findlights(lightmapworker *w, int cx, int cy, int cz, int size, cons case ET_LIGHT: addlight(w, light, cx, cy, cz, size, v, n, numv); break; } } - if(vslot.layer && (setblendmaporigin(w->blendmapcache, ivec(cx, cy, cz), size) || slot.layermask)) return true; + if(vslot.layer) return true; return w->lights.length(); } @@ -1228,11 +1171,6 @@ static int setupsurface(lightmapworker *w, plane planes[2], int numplanes, const int lw = clamp(int(ceil((cmax.x - cmin.x + 1)*lpu)), LM_MINW, LM_MAXW), lh = clamp(int(ceil((cmax.y - cmin.y + 1)*lpu)), LM_MINH, LM_MAXH); w->w = lw; w->h = lh; - if(!preview) - { - w->w += 2*blurlms; - w->h += 2*blurlms; - } if(!alloclightmap(w)) return NO_SURFACE; vec2 cscale = vec2(cmax).sub(cmin).div(vec2(lw-1, lh-1)), @@ -1393,7 +1331,7 @@ static lightmapinfo *setupsurfaces(lightmapworker *w, lightmaptask &task) for(int k = 3; k < numverts; k++) findnormal(pos[k], planes[1], n[k]); } - if(shadertype&(SHADER_NORMALSLMS | SHADER_ENVMAP)) + if(shadertype&SHADER_NORMALSLMS) { loopk(numverts) curlitverts[k].norm = encodenormal(n[k]); if(!(surf.numverts&MAXFACEVERTS)) @@ -1646,189 +1584,6 @@ static void generatelightmaps(cube *c, const ivec &co, int size) } } -static bool previewblends(lightmapworker *w, cube &c, const ivec &co, int size) -{ - if(isempty(c) || c.material&MAT_ALPHA) return false; - - int usefacemask = 0; - loopi(6) if(lookupvslot(c.texture[i], false).layer) - usefacemask |= visibletris(c, i, co, size)<<(4*i); - if(!usefacemask) return false; - - if(!setblendmaporigin(w->blendmapcache, co, size)) - { - if(!c.ext) return false; - bool blends = false; - loopi(6) if(c.ext->surfaces[i].numverts&LAYER_BOTTOM) - { - c.ext->surfaces[i].brighten(); - blends = true; - } - return blends; - } - - w->firstlightmap = w->lastlightmap = w->curlightmaps = NULL; - w->bufstart = w->bufused = 0; - w->c = &c; - - surfaceinfo surfaces[6]; - vertinfo litverts[6*2*MAXFACEVERTS]; - int numlitverts = 0; - memcpy(surfaces, c.ext ? c.ext->surfaces : brightsurfaces, sizeof(surfaces)); - loopi(6) - { - int usefaces = usefacemask&0xF; - usefacemask >>= 4; - if(!usefaces) - { - surfaceinfo &surf = surfaces[i]; - int numverts = surf.totalverts(); - if(numverts) - { - memcpy(&litverts[numlitverts], c.ext->verts() + surf.verts, numverts*sizeof(vertinfo)); - surf.verts = numlitverts; - numlitverts += numverts; - } - continue; - } - - VSlot &vslot = lookupvslot(c.texture[i], false), - &layer = lookupvslot(vslot.layer, false); - Shader *shader = vslot.slot->shader; - int shadertype = shader->type | layer.slot->shader->type; - - vertinfo *curlitverts = &litverts[numlitverts]; - int numverts = 0; - ivec v[4]; - genfaceverts(c, i, v); - int convex = flataxisface(c, i) ? 0 : faceconvexity(v), - order = usefaces&4 || convex < 0 ? 1 : 0; - ivec vo = ivec(co).mask(0xFFF).shl(3); - curlitverts[numverts++].set(v[order].mul(size).add(vo)); - if(usefaces&1) curlitverts[numverts++].set(v[order+1].mul(size).add(vo)); - curlitverts[numverts++].set(v[order+2].mul(size).add(vo)); - if(usefaces&2) curlitverts[numverts++].set(v[(order+3)&3].mul(size).add(vo)); - - vec pos[4], n[4], po(ivec(co).mask(~0xFFF)); - loopj(numverts) pos[j] = vec(curlitverts[j].getxyz()).mul(1.0f/8).add(po); - - plane planes[2]; - int numplanes = 0; - planes[numplanes++].toplane(pos[0], pos[1], pos[2]); - if(numverts < 4 || !convex) loopk(numverts) n[k] = planes[0]; - else - { - planes[numplanes++].toplane(pos[0], pos[2], pos[3]); - vec avg = vec(planes[0]).add(planes[1]).normalize(); - n[0] = avg; - n[1] = planes[0]; - n[2] = avg; - for(int k = 3; k < numverts; k++) n[k] = planes[1]; - } - - surfaceinfo &surf = surfaces[i]; - w->slot = vslot.slot; - w->vslot = &vslot; - w->type = shadertype&SHADER_NORMALSLMS ? LM_BUMPMAP0|LM_ALPHA : LM_DIFFUSE|LM_ALPHA; - w->bpp = 4; - w->orient = i; - w->rotate = vslot.rotation; - int surftype = setupsurface(w, planes, numplanes, pos, n, numverts, curlitverts, true); - switch(surftype) - { - case SURFACE_AMBIENT_TOP: - surf = brightsurface; - continue; - - case SURFACE_AMBIENT_BOTTOM: - surf = brightbottomsurface; - continue; - - case SURFACE_LIGHTMAP_BLEND: - { - if(surf.numverts == (LAYER_BLEND|numverts) && - surf.lmid[0] == surf.lmid[1] && - (surf.numverts&MAXFACEVERTS) == numverts && - !memcmp(curlitverts, c.ext->verts() + surf.verts, numverts*sizeof(vertinfo)) && - lightmaps.inrange(surf.lmid[0]-LMID_RESERVED) && - lightmaps[surf.lmid[0]-LMID_RESERVED].type==w->type) - { - vertinfo *oldverts = c.ext->verts() + surf.verts; - layoutinfo layout; - layout.w = w->w; - layout.h = w->h; - layout.x = (oldverts[0].x - curlitverts[0].x)/((USHRT_MAX+1)/LM_PACKW); - layout.y = (oldverts[0].y - curlitverts[0].y)/((USHRT_MAX+1)/LM_PACKH); - if(LM_PACKW - layout.x >= w->w && LM_PACKH - layout.y >= w->h) - { - layout.lmid = surf.lmid[0]; - copylightmap(*w->lastlightmap, layout); - updatelightmap(layout); - surf.verts = numlitverts; - numlitverts += numverts; - continue; - } - } - - surf.verts = numlitverts; - surf.numverts = LAYER_BLEND|numverts; - numlitverts += numverts; - layoutinfo layout; - if(packlightmap(*w->lastlightmap, layout)) updatelightmap(layout); - surf.lmid[0] = surf.lmid[1] = layout.lmid; - ushort offsetx = layout.x*((USHRT_MAX+1)/LM_PACKW), offsety = layout.y*((USHRT_MAX+1)/LM_PACKH); - loopk(numverts) - { - vertinfo &v = curlitverts[k]; - v.u += offsetx; - v.v += offsety; - } - continue; - } - } - } - - setsurfaces(c, surfaces, litverts, numlitverts); - return true; -} - -static bool previewblends(lightmapworker *w, cube *c, const ivec &co, int size, const ivec &bo, const ivec &bs) -{ - bool changed = false; - loopoctabox(co, size, bo, bs) - { - ivec o(i, co, size); - cubeext *ext = c[i].ext; - if(ext && ext->va && ext->va->hasmerges) - { - changed = true; - destroyva(ext->va); - ext->va = NULL; - invalidatemerges(c[i], co, size, true); - } - if(c[i].children ? previewblends(w, c[i].children, o, size/2, bo, bs) : previewblends(w, c[i], o, size)) - { - changed = true; - ext = c[i].ext; - if(ext && ext->va) - { - destroyva(ext->va); - ext->va = NULL; - } - } - } - return changed; -} - -void previewblends(const ivec &bo, const ivec &bs) -{ - loadlayermasks(); - if(lightmapworkers.empty()) lightmapworkers.add(new lightmapworker); - lightmapworkers[0]->reset(); - if(previewblends(lightmapworkers[0], worldroot, ivec(0, 0, 0), worldsize/2, bo, bs)) - commitchanges(true); -} - void cleanuplightmaps() { loopv(lightmaps) @@ -1860,7 +1615,6 @@ lightmapworker::lightmapworker() colordata = new vec[4*(LM_MAXW+1 + 4)*(LM_MAXH+1 + 4)]; raydata = new vec[(LM_MAXW + 4)*(LM_MAXH + 4)]; shadowraycache = newshadowraycache(); - blendmapcache = newblendmapcache(); needspace = doneworking = false; spacecond = NULL; thread = NULL; @@ -1875,7 +1629,6 @@ lightmapworker::~lightmapworker() delete[] colordata; delete[] raydata; freeshadowraycache(shadowraycache); - freeblendmapcache(blendmapcache); } void lightmapworker::cleanupthread() @@ -1996,7 +1749,6 @@ void calclight(int *quality) } renderbackground("computing lightmaps... (esc to abort)"); mpremip(true); - optimizeblendmap(); loadlayermasks(); int numthreads = lightthreads > 0 ? lightthreads : numcpus; if(numthreads > 1) preloadusedmapmodels(false, true); -- cgit v1.2.3