static void ComputeLighting( DetailObjectLump_t& prop, int iThread ) { // We're going to take the maximum of the ambient lighting and // the strongest directional light. This works because we're assuming // the props will have built-in faked lighting. Vector directColor[MAX_LIGHTSTYLES]; Vector ambColor[MAX_LIGHTSTYLES]; // Get the max influence of all direct lights ComputeMaxDirectLighting( prop, directColor, iThread ); // Get the ambient lighting + lightstyles ComputeAmbientLighting( prop, ambColor ); // Base lighting Vector totalColor; VectorAdd( directColor[0], ambColor[0], totalColor ); VectorToColorRGBExp32( totalColor, prop.m_Lighting ); bool hasLightstyles = false; prop.m_LightStyleCount = 0; // lightstyles for (int i = 1; i < MAX_LIGHTSTYLES; ++i ) { VectorAdd( directColor[i], ambColor[i], totalColor ); totalColor *= 0.5f; if ((totalColor[0] != 0.0f) || (totalColor[1] != 0.0f) || (totalColor[2] != 0.0f) ) { if (!hasLightstyles) { prop.m_LightStyles = s_pDetailPropLightStyleLump->Size(); hasLightstyles = true; } int j = s_pDetailPropLightStyleLump->AddToTail(); VectorToColorRGBExp32( totalColor, (*s_pDetailPropLightStyleLump)[j].m_Lighting ); (*s_pDetailPropLightStyleLump)[j].m_Style = i; ++prop.m_LightStyleCount; } } }
void ComputePerLeafAmbientLighting() { // Figure out which lights should go in the per-leaf ambient cubes. int nInAmbientCube = 0; int nSurfaceLights = 0; for ( int i=0; i < *pNumworldlights; i++ ) { dworldlight_t *wl = &dworldlights[i]; if ( IsLeafAmbientSurfaceLight( wl ) ) wl->flags |= DWL_FLAGS_INAMBIENTCUBE; else wl->flags &= ~DWL_FLAGS_INAMBIENTCUBE; if ( wl->type == emit_surface ) ++nSurfaceLights; if ( wl->flags & DWL_FLAGS_INAMBIENTCUBE ) ++nInAmbientCube; } Msg( "%d of %d (%d%% of) surface lights went in leaf ambient cubes.\n", nInAmbientCube, nSurfaceLights, nSurfaceLights ? ((nInAmbientCube*100) / nSurfaceLights) : 0 ); g_pLeafAmbientLighting->SetCount( numleafs ); StartPacifier( "ComputePerLeafAmbientLighting: " ); for ( int leafID = 0; leafID < numleafs; leafID++ ) { dleaf_t *pLeaf = &dleafs[leafID]; Vector cube[6]; Vector center = ( Vector( pLeaf->mins[0], pLeaf->mins[1], pLeaf->mins[2] ) + Vector( pLeaf->maxs[0], pLeaf->maxs[1], pLeaf->maxs[2] ) ) * 0.5f; #if MAKE_LIGHT_BELOW_WATER_MATCH_LIGHT_ABOVE_WATER if (pLeaf->contents & CONTENTS_WATER) { center.z=pLeaf->maxs[2]+1; int above_leaf=PointLeafnum( center); dleaf_t *pLeaf = &dleafs[above_leaf]; center = ( Vector( pLeaf->mins[0], pLeaf->mins[1], pLeaf->mins[2] ) + Vector( pLeaf->maxs[0], pLeaf->maxs[1], pLeaf->maxs[2] ) ) * 0.5f; } #endif ComputeAmbientFromSphericalSamples( center, cube ); for ( int i = 0; i < 6; i++ ) { VectorToColorRGBExp32( cube[i], (*g_pLeafAmbientLighting)[leafID].m_Color[i] ); } UpdatePacifier( (float)leafID / numleafs ); } EndPacifier( true ); }
void ComputePerLeafAmbientLighting() { // Figure out which lights should go in the per-leaf ambient cubes. int nInAmbientCube = 0; int nSurfaceLights = 0; for ( int i=0; i < *pNumworldlights; i++ ) { dworldlight_t *wl = &dworldlights[i]; if ( IsLeafAmbientSurfaceLight( wl ) ) wl->flags |= DWL_FLAGS_INAMBIENTCUBE; else wl->flags &= ~DWL_FLAGS_INAMBIENTCUBE; if ( wl->type == emit_surface ) ++nSurfaceLights; if ( wl->flags & DWL_FLAGS_INAMBIENTCUBE ) ++nInAmbientCube; } Msg( "%d of %d (%d%% of) surface lights went in leaf ambient cubes.\n", nInAmbientCube, nSurfaceLights, nSurfaceLights ? ((nInAmbientCube*100) / nSurfaceLights) : 0 ); g_LeafAmbientSamples.SetCount(numleafs); if ( g_bUseMPI ) { // Distribute the work among the workers. VMPI_SetCurrentStage( "ComputeLeafAmbientLighting" ); DistributeWork( numleafs, VMPI_DISTRIBUTEWORK_PACKETID, VMPI_ProcessLeafAmbient, VMPI_ReceiveLeafAmbientResults ); } else { RunThreadsOn(numleafs, true, ThreadComputeLeafAmbient); } // now write out the data Msg("Writing leaf ambient..."); g_pLeafAmbientIndex->RemoveAll(); g_pLeafAmbientLighting->RemoveAll(); g_pLeafAmbientIndex->SetCount( numleafs ); g_pLeafAmbientLighting->EnsureCapacity( numleafs*4 ); for ( int leafID = 0; leafID < numleafs; leafID++ ) { const CUtlVector<ambientsample_t> &list = g_LeafAmbientSamples[leafID]; g_pLeafAmbientIndex->Element(leafID).ambientSampleCount = list.Count(); if ( !list.Count() ) { g_pLeafAmbientIndex->Element(leafID).firstAmbientSample = 0; } else { g_pLeafAmbientIndex->Element(leafID).firstAmbientSample = g_pLeafAmbientLighting->Count(); // compute the samples in disk format. Encode the positions in 8-bits using leaf bounds fractions for ( int i = 0; i < list.Count(); i++ ) { int outIndex = g_pLeafAmbientLighting->AddToTail(); dleafambientlighting_t &light = g_pLeafAmbientLighting->Element(outIndex); light.x = Fixed8Fraction( list[i].pos.x, dleafs[leafID].mins[0], dleafs[leafID].maxs[0] ); light.y = Fixed8Fraction( list[i].pos.y, dleafs[leafID].mins[1], dleafs[leafID].maxs[1] ); light.z = Fixed8Fraction( list[i].pos.z, dleafs[leafID].mins[2], dleafs[leafID].maxs[2] ); light.pad = 0; for ( int side = 0; side < 6; side++ ) { VectorToColorRGBExp32( list[i].cube[side], light.cube.m_Color[side] ); } } } } for ( int i = 0; i < numleafs; i++ ) { // UNDONE: Do this dynamically in the engine instead. This will allow us to sample across leaf // boundaries always which should improve the quality of lighting in general if ( g_pLeafAmbientIndex->Element(i).ambientSampleCount == 0 ) { if ( !(dleafs[i].contents & CONTENTS_SOLID) ) { Msg("Bad leaf ambient for leaf %d\n", i ); } int refLeaf = NearestNeighborWithLight(i); g_pLeafAmbientIndex->Element(i).ambientSampleCount = 0; g_pLeafAmbientIndex->Element(i).firstAmbientSample = refLeaf; } } Msg("done\n"); }
/* ============= FinalLightFace Add the indirect lighting on top of the direct lighting and save into final map format ============= */ void FinalLightFace( int iThread, int facenum ) { dface_t *f; int i, j, k; facelight_t *fl; float minlight; int lightstyles; LightingValue_t lb[NUM_BUMP_VECTS + 1], v[NUM_BUMP_VECTS + 1]; unsigned char *pdata[NUM_BUMP_VECTS + 1]; int bumpSample; radial_t *rad = NULL; radial_t *prad = NULL; f = &g_pFaces[facenum]; // test for non-lit texture if ( texinfo[f->texinfo].flags & TEX_SPECIAL) return; fl = &facelight[facenum]; for (lightstyles=0; lightstyles < MAXLIGHTMAPS; lightstyles++ ) { if ( f->styles[lightstyles] == 255 ) break; } if ( !lightstyles ) return; // // sample the triangulation // minlight = FloatForKey (face_entity[facenum], "_minlight") * 128; bool needsBumpmap = ( texinfo[f->texinfo].flags & SURF_BUMPLIGHT ) ? true : false; int bumpSampleCount = needsBumpmap ? NUM_BUMP_VECTS + 1 : 1; bool bDisp = ( f->dispinfo != -1 ); //#define RANDOM_COLOR #ifdef RANDOM_COLOR unsigned char randomColor[3]; GetRandomColor( randomColor ); #endif // NOTE: I'm using these RB trees to sort all the illumination values // to compute median colors. Turns out that this is a somewhat better // method that using the average; usually if there are surfaces // with a large light intensity variation, the extremely bright regions // have a very small area and tend to influence the average too much. CUtlRBTree< float, int > m_Red( 0, 256, FloatLess ); CUtlRBTree< float, int > m_Green( 0, 256, FloatLess ); CUtlRBTree< float, int > m_Blue( 0, 256, FloatLess ); for (k=0 ; k < lightstyles; k++ ) { m_Red.RemoveAll(); m_Green.RemoveAll(); m_Blue.RemoveAll(); if (!do_fast) { if( !bDisp ) { rad = BuildLuxelRadial( facenum, k ); } else { rad = StaticDispMgr()->BuildLuxelRadial( facenum, k, needsBumpmap ); } } if (numbounce > 0 && k == 0) { // currently only radiosity light non-displacement surfaces! if( !bDisp ) { prad = BuildPatchRadial( facenum ); } else { prad = StaticDispMgr()->BuildPatchRadial( facenum, needsBumpmap ); } } // pack the nonbump texture and the three bump texture for the given // lightstyle right next to each other. // NOTE: Even though it's building positions for all bump-mapped data, // it isn't going to use those positions (see loop over bumpSample below) // The file offset is correctly computed to only store space for 1 set // of light data if we don't have bumped lighting. for( bumpSample = 0; bumpSample < bumpSampleCount; ++bumpSample ) { pdata[bumpSample] = &(*pdlightdata)[f->lightofs + (k * bumpSampleCount + bumpSample) * fl->numluxels*4]; } // Compute the average luxel color, but not for the bump samples Vector avg( 0.0f, 0.0f, 0.0f ); int avgCount = 0; for (j=0 ; j<fl->numluxels; j++) { // garymct - direct lighting bool baseSampleOk = true; if (!do_fast) { if( !bDisp ) { baseSampleOk = SampleRadial( rad, fl->luxel[j], lb, bumpSampleCount ); } else { baseSampleOk = StaticDispMgr()->SampleRadial( facenum, rad, fl->luxel[j], j, lb, bumpSampleCount, false ); } } else { for ( int iBump = 0 ; iBump < bumpSampleCount; iBump++ ) { lb[iBump] = fl->light[0][iBump][j]; } } if (prad) { // garymct - bounced light // v is indirect light that is received on the luxel. if( !bDisp ) { SampleRadial( prad, fl->luxel[j], v, bumpSampleCount ); } else { StaticDispMgr()->SampleRadial( facenum, prad, fl->luxel[j], j, v, bumpSampleCount, true ); } for( bumpSample = 0; bumpSample < bumpSampleCount; ++bumpSample ) { lb[bumpSample].AddLight( v[bumpSample] ); } } if ( bDisp && g_bDumpPatches ) { for( bumpSample = 0; bumpSample < bumpSampleCount; ++bumpSample ) { DumpDispLuxels( facenum, lb[bumpSample].m_vecLighting, j, bumpSample ); } } if (fl->numsamples == 0) { for( i = 0; i < bumpSampleCount; i++ ) { lb[i].Init( 255, 0, 0 ); } baseSampleOk = false; } int bumpSample; for( bumpSample = 0; bumpSample < bumpSampleCount; bumpSample++ ) { // clip from the bottom first // garymct: minlight is a per entity minimum light value? for( i=0; i<3; i++ ) { lb[bumpSample].m_vecLighting[i] = max( lb[bumpSample].m_vecLighting[i], minlight ); } // Do the average light computation, I'm assuming (perhaps incorrectly?) // that all luxels in a particular lightmap have the same area here. // Also, don't bother doing averages for the bump samples. Doing it here // because of the minlight clamp above + the random color testy thingy. // Also have to do it before Vec3toColorRGBExp32 because it // destructively modifies lb[bumpSample] (Feh!) if ((bumpSample == 0) && baseSampleOk) { ++avgCount; ApplyMacroTextures( facenum, fl->luxel[j], lb[0].m_vecLighting ); // For median computation m_Red.Insert( lb[bumpSample].m_vecLighting[0] ); m_Green.Insert( lb[bumpSample].m_vecLighting[1] ); m_Blue.Insert( lb[bumpSample].m_vecLighting[2] ); } #ifdef RANDOM_COLOR pdata[bumpSample][0] = randomColor[0] / ( bumpSample + 1 ); pdata[bumpSample][1] = randomColor[1] / ( bumpSample + 1 ); pdata[bumpSample][2] = randomColor[2] / ( bumpSample + 1 ); pdata[bumpSample][3] = 0; #else // convert to a 4 byte r,g,b,signed exponent format VectorToColorRGBExp32( Vector( lb[bumpSample].m_vecLighting.x, lb[bumpSample].m_vecLighting.y, lb[bumpSample].m_vecLighting.z ), *( ColorRGBExp32 *)pdata[bumpSample] ); #endif pdata[bumpSample] += 4; } } FreeRadial( rad ); if (prad) { FreeRadial( prad ); prad = NULL; } // Compute the median color for this lightstyle // Remember, the data goes *before* the specified light_ofs, in *reverse order* ColorRGBExp32 *pAvgColor = dface_AvgLightColor( f, k ); if (avgCount == 0) { Vector median( 0, 0, 0 ); VectorToColorRGBExp32( median, *pAvgColor ); } else { unsigned int r, g, b; r = m_Red.FirstInorder(); g = m_Green.FirstInorder(); b = m_Blue.FirstInorder(); avgCount >>= 1; while (avgCount > 0) { r = m_Red.NextInorder(r); g = m_Green.NextInorder(g); b = m_Blue.NextInorder(b); --avgCount; } Vector median( m_Red[r], m_Green[g], m_Blue[b] ); VectorToColorRGBExp32( median, *pAvgColor ); } } }
//----------------------------------------------------------------------------- // Write the lighitng to bsp pak lump //----------------------------------------------------------------------------- void CVradStaticPropMgr::SerializeLighting() { char filename[MAX_PATH]; CUtlBuffer utlBuf; // illuminate them all int count = m_StaticProps.Count(); if ( !count ) { // nothing to do return; } char mapName[MAX_PATH]; Q_FileBase( source, mapName, sizeof( mapName ) ); int size; for (int i = 0; i < count; ++i) { sprintf( filename, "sp_%d.vhv", i ); int totalVertexes = 0; for ( int j=0; j<m_StaticProps[i].m_MeshData.Count(); j++ ) { totalVertexes += m_StaticProps[i].m_MeshData[j].m_Verts.Count(); } // allocate a buffer with enough padding for alignment size = sizeof( HardwareVerts::FileHeader_t ) + m_StaticProps[i].m_MeshData.Count()*sizeof(HardwareVerts::MeshHeader_t) + totalVertexes*4 + 2*512; utlBuf.EnsureCapacity( size ); Q_memset( utlBuf.Base(), 0, size ); HardwareVerts::FileHeader_t *pVhvHdr = (HardwareVerts::FileHeader_t *)utlBuf.Base(); // align to start of vertex data unsigned char *pVertexData = (unsigned char *)(sizeof( HardwareVerts::FileHeader_t ) + m_StaticProps[i].m_MeshData.Count()*sizeof(HardwareVerts::MeshHeader_t)); pVertexData = (unsigned char*)pVhvHdr + ALIGN_TO_POW2( (unsigned int)pVertexData, 512 ); // construct header pVhvHdr->m_nVersion = VHV_VERSION; pVhvHdr->m_nChecksum = m_StaticPropDict[m_StaticProps[i].m_ModelIdx].m_pStudioHdr->checksum; pVhvHdr->m_nVertexFlags = VERTEX_COLOR; pVhvHdr->m_nVertexSize = 4; pVhvHdr->m_nVertexes = totalVertexes; pVhvHdr->m_nMeshes = m_StaticProps[i].m_MeshData.Count(); for (int n=0; n<pVhvHdr->m_nMeshes; n++) { // construct mesh dictionary HardwareVerts::MeshHeader_t *pMesh = pVhvHdr->pMesh( n ); pMesh->m_nLod = m_StaticProps[i].m_MeshData[n].m_nLod; pMesh->m_nVertexes = m_StaticProps[i].m_MeshData[n].m_Verts.Count(); pMesh->m_nOffset = (unsigned int)pVertexData - (unsigned int)pVhvHdr; // construct vertexes for (int k=0; k<pMesh->m_nVertexes; k++) { Vector &vector = m_StaticProps[i].m_MeshData[n].m_Verts[k]; ColorRGBExp32 rgbColor; VectorToColorRGBExp32( vector, rgbColor ); unsigned char dstColor[4]; ConvertRGBExp32ToRGBA8888( &rgbColor, dstColor ); // b,g,r,a order pVertexData[0] = dstColor[2]; pVertexData[1] = dstColor[1]; pVertexData[2] = dstColor[0]; pVertexData[3] = dstColor[3]; pVertexData += 4; } } // align to end of file pVertexData = (unsigned char *)((unsigned int)pVertexData - (unsigned int)pVhvHdr); pVertexData = (unsigned char*)pVhvHdr + ALIGN_TO_POW2( (unsigned int)pVertexData, 512 ); AddBufferToPack( filename, (void*)pVhvHdr, pVertexData - (unsigned char*)pVhvHdr, false ); } }