bool mkFluid::loadShader(bool eyeSubmerged, bool refractionMask) { if(refractionMask) { Material *mat = MaterialManager->findMaterialByName("waterRefractionMask"); if(!mat) return false; return mat->bind(); } else if(eyeSubmerged && mDoRefraction) { Material *mat = MaterialManager->findMaterialByName("waterSubmerged"); if(!mat) return false; if(!mat->bind()) return false; Point2I extent = Platform::getWindowSize(); F32 texWidth = getNextPow2((F32)extent.x); F32 texHeight = getNextPow2((F32)extent.y); ShaderManager->setParameter("RefractTexSize", texWidth, texHeight); ShaderManager->setParameter("TexRatio", (F32)extent.x/texWidth, (F32)extent.y/texHeight); return true; } else if(mDoRefraction) { if(mReflectionSize <= 1) return false; Material *mat = MaterialManager->findMaterialByName("waterReflectRefract"); if(!mat) return false; if(!mat->bind()) return false; Point2I extent = Platform::getWindowSize(); F32 texWidth = getNextPow2((F32)extent.x); F32 texHeight = getNextPow2((F32)extent.y); ShaderManager->setParameter("ReflectTexSize", (F32)mReflectionSize); ShaderManager->setParameter("RefractTexSize", texWidth, texHeight); ShaderManager->setParameter("TexRatio", (F32)extent.x/texWidth, (F32)extent.y/texHeight); return true; } else { if(mReflectionSize <= 1) return false; Material *mat = MaterialManager->findMaterialByName("waterReflectOnly"); if(!mat) return false; if(!mat->bind()) return false; ShaderManager->setParameter("ReflectTexSize", (F32)mReflectionSize); return true; } }
void TerrainFile::setSize( U32 newSize, bool clear ) { // Make sure the resolution is a power of two. newSize = getNextPow2( newSize ); // if ( clear ) { mLayerMap.setSize( newSize * newSize ); mLayerMap.compact(); dMemset( mLayerMap.address(), 0, mLayerMap.memSize() ); // Initialize the elevation to something above // zero so that we have room to excavate by default. U16 elev = floatToFixed( 512.0f ); mHeightMap.setSize( newSize * newSize ); mHeightMap.compact(); for ( U32 i = 0; i < mHeightMap.size(); i++ ) mHeightMap[i] = elev; } else { // We're resizing here! } mSize = newSize; _buildGridMap(); }
void mkFluid::setReflectionSize(U32 size) { mReflectionSize = getNextPow2(mClamp(size, 1, getGlobalReflectionSize())); if(mReflectionRT && mReflectionRT->getWidth() != mReflectionSize) { RenderTextureManager->deleteRenderTexture(mReflectionRT); mReflectionRT = NULL; } if(!mReflectionRT && mReflectionSize > 1) { RenderTextureFormat testFormat(RGBA8, Depth24, None, Stencil0, 0, false, false); RenderTextureFormat bestFit; RenderTextureManager->getClosestMatch(testFormat, bestFit); mReflectionRT = RenderTextureManager->createRenderTexture(mReflectionSize, mReflectionSize, bestFit); } }
void MKInit() { AssertFatal( !didInit, "Already initialized the Modernization Kit!"); _ShaderManager::create(); _VBOManager::create(); _MaterialManager::create(); _RenderTextureManager::create(); _DRL::create(); _MKGFX::create(); // Set mkFluid prefs mkFluid::setGlobalReflectionSize(getNextPow2(Con::getIntVariable("$pref::Water::reflectionSize", 1))); mkFluid::enableGlobalRefraction(Con::getBoolVariable("$pref::Water::refract", false)); #ifdef TORQUE_OS_MAC_OSX #ifdef TORQUE_DEBUG //bool testsGood = mkRunTests(); #endif #endif didInit = true; }
void AbstractClassRep::initialize() { AssertFatal(!initialized, "Duplicate call to AbstractClassRep::initialize()!"); Vector<AbstractClassRep *> dynamicTable(__FILE__, __LINE__); AbstractClassRep *walk; // Initialize namespace references... for (walk = classLinkList; walk; walk = walk->nextClass) { walk->mNamespace = Con::lookupNamespace(StringTable->insert(walk->getClassName())); walk->mNamespace->mUsage = walk->getDocString(); walk->mNamespace->mClassRep = walk; } // Initialize field lists... (and perform other console registration). for (walk = classLinkList; walk; walk = walk->nextClass) { // sg_tempFieldList is used as a staging area for field lists // (see addField, addGroup, etc.) sg_tempFieldList.setSize(0); walk->init(); // So if we have things in it, copy it over... if (sg_tempFieldList.size() != 0) walk->mFieldList = sg_tempFieldList; // And of course delete it every round. sg_tempFieldList.clear(); } // Calculate counts and bit sizes for the various NetClasses. for (U32 group = 0; group < NetClassGroupsCount; group++) { U32 groupMask = 1 << group; // Specifically, for each NetClass of each NetGroup... for(U32 type = 0; type < NetClassTypesCount; type++) { // Go through all the classes and find matches... for (walk = classLinkList; walk; walk = walk->nextClass) { if(walk->mClassType == type && walk->mClassGroupMask & groupMask) dynamicTable.push_back(walk); } // Set the count for this NetGroup and NetClass NetClassCount[group][type] = dynamicTable.size(); if(!NetClassCount[group][type]) continue; // If no classes matched, skip to next. // Sort by type and then by name. dQsort((void *) &dynamicTable[0], dynamicTable.size(), sizeof(AbstractClassRep *), ACRCompare); // Allocate storage in the classTable classTable[group][type] = new AbstractClassRep*[NetClassCount[group][type]]; // Fill this in and assign class ids for this group. for(U32 i = 0; i < NetClassCount[group][type];i++) { classTable[group][type][i] = dynamicTable[i]; dynamicTable[i]->mClassId[group] = i; } // And calculate the size of bitfields for this group and type. NetClassBitSize[group][type] = getBinLog2(getNextPow2(NetClassCount[group][type] + 1)); AssertFatal(NetClassCount[group][type] < (1 << NetClassBitSize[group][type]), "NetClassBitSize too small!"); dynamicTable.clear(); } } // Ok, we're golden! initialized = true; }
void TerrainBlock::_updateBaseTexture(bool writeToCache) { if ( !mBaseShader && !_initBaseShader() ) return; // This can sometimes occur outside a begin/end scene. const bool sceneBegun = GFX->canCurrentlyRender(); if ( !sceneBegun ) GFX->beginScene(); GFXDEBUGEVENT_SCOPE( TerrainBlock_UpdateBaseTexture, ColorI::GREEN ); PROFILE_SCOPE( TerrainBlock_UpdateBaseTexture ); GFXTransformSaver saver; const U32 maxTextureSize = GFX->getCardProfiler()->queryProfile( "maxTextureSize", 1024 ); U32 baseTexSize = getNextPow2( mBaseTexSize ); baseTexSize = getMin( maxTextureSize, baseTexSize ); Point2I destSize( baseTexSize, baseTexSize ); // Setup geometry GFXVertexBufferHandle<GFXVertexPT> vb; { F32 copyOffsetX = 2.0f * GFX->getFillConventionOffset() / (F32)destSize.x; F32 copyOffsetY = 2.0f * GFX->getFillConventionOffset() / (F32)destSize.y; GFXVertexPT points[4]; points[0].point = Point3F(1.0 - copyOffsetX, -1.0 + copyOffsetY, 0.0); points[0].texCoord = Point2F(1.0, 1.0f); points[1].point = Point3F(1.0 - copyOffsetX, 1.0 + copyOffsetY, 0.0); points[1].texCoord = Point2F(1.0, 0.0f); points[2].point = Point3F(-1.0 - copyOffsetX, -1.0 + copyOffsetY, 0.0); points[2].texCoord = Point2F(0.0, 1.0f); points[3].point = Point3F(-1.0 - copyOffsetX, 1.0 + copyOffsetY, 0.0); points[3].texCoord = Point2F(0.0, 0.0f); vb.set( GFX, 4, GFXBufferTypeVolatile ); GFXVertexPT *ptr = vb.lock(); if(ptr) { dMemcpy( ptr, points, sizeof(GFXVertexPT) * 4 ); vb.unlock(); } } GFXTexHandle blendTex; // If the base texture is already a valid render target then // use it to render to else we create one. if ( mBaseTex.isValid() && mBaseTex->isRenderTarget() && mBaseTex->getFormat() == GFXFormatR8G8B8A8 && mBaseTex->getWidth() == destSize.x && mBaseTex->getHeight() == destSize.y ) blendTex = mBaseTex; else blendTex.set( destSize.x, destSize.y, GFXFormatR8G8B8A8, &GFXDefaultRenderTargetProfile, "" ); GFX->pushActiveRenderTarget(); // Set our shader stuff GFX->setShader( mBaseShader ); GFX->setShaderConstBuffer( mBaseShaderConsts ); GFX->setStateBlock( mBaseShaderSB ); GFX->setVertexBuffer( vb ); mBaseTarget->attachTexture( GFXTextureTarget::Color0, blendTex ); GFX->setActiveRenderTarget( mBaseTarget ); GFX->clear( GFXClearTarget, ColorI(0,0,0,255), 1.0f, 0 ); GFX->setTexture( 0, mLayerTex ); mBaseShaderConsts->setSafe( mBaseLayerSizeConst, (F32)mLayerTex->getWidth() ); for ( U32 i=0; i < mBaseTextures.size(); i++ ) { GFXTextureObject *tex = mBaseTextures[i]; if ( !tex ) continue; GFX->setTexture( 1, tex ); F32 baseSize = mFile->mMaterials[i]->getDiffuseSize(); F32 scale = 1.0f; if ( !mIsZero( baseSize ) ) scale = getWorldBlockSize() / baseSize; // A mistake early in development means that texture // coords are not flipped correctly. To compensate // we flip the y scale here. mBaseShaderConsts->setSafe( mBaseTexScaleConst, Point2F( scale, -scale ) ); mBaseShaderConsts->setSafe( mBaseTexIdConst, (F32)i ); GFX->drawPrimitive( GFXTriangleStrip, 0, 2 ); } mBaseTarget->resolve(); GFX->setShader( NULL ); //GFX->setStateBlock( NULL ); // WHY NOT? GFX->setShaderConstBuffer( NULL ); GFX->setVertexBuffer( NULL ); GFX->popActiveRenderTarget(); // End it if we begun it... Yeehaw! if ( !sceneBegun ) GFX->endScene(); /// Do we cache this sucker? if (mBaseTexFormat == NONE || !writeToCache) { // We didn't cache the result, so set the base texture // to the render target we updated. This should be good // for realtime painting cases. mBaseTex = blendTex; } else if (mBaseTexFormat == DDS) { String cachePath = _getBaseTexCacheFileName(); FileStream fs; if ( fs.open( _getBaseTexCacheFileName(), Torque::FS::File::Write ) ) { // Read back the render target, dxt compress it, and write it to disk. GBitmap blendBmp( destSize.x, destSize.y, false, GFXFormatR8G8B8A8 ); blendTex.copyToBmp( &blendBmp ); /* // Test code for dumping uncompressed bitmap to disk. { FileStream fs; if ( fs.open( "./basetex.png", Torque::FS::File::Write ) ) { blendBmp.writeBitmap( "png", fs ); fs.close(); } } */ blendBmp.extrudeMipLevels(); DDSFile *blendDDS = DDSFile::createDDSFileFromGBitmap( &blendBmp ); DDSUtil::squishDDS( blendDDS, GFXFormatDXT1 ); // Write result to file stream blendDDS->write( fs ); delete blendDDS; } fs.close(); } else { FileStream stream; if (!stream.open(_getBaseTexCacheFileName(), Torque::FS::File::Write)) { mBaseTex = blendTex; return; } GBitmap bitmap(blendTex->getWidth(), blendTex->getHeight(), false, GFXFormatR8G8B8); blendTex->copyToBmp(&bitmap); bitmap.writeBitmap(formatToExtension(mBaseTexFormat), stream); } }
//----------------------------------------------------------------------------- // innerCreateTexture //----------------------------------------------------------------------------- // This just creates the texture, no info is actually loaded to it. We do that later. void GFXGLTextureManager::innerCreateTexture( GFXGLTextureObject *retTex, U32 height, U32 width, U32 depth, GFXFormat format, GFXTextureProfile *profile, U32 numMipLevels, bool forceMips) { // No 24 bit formats. They trigger various oddities because hardware (and Apple's drivers apparently...) don't natively support them. if(format == GFXFormatR8G8B8) format = GFXFormatR8G8B8A8; retTex->mFormat = format; retTex->mIsZombie = false; retTex->mIsNPoT2 = false; GLenum binding = ( (height == 1 || width == 1) && ( height != width ) ) ? GL_TEXTURE_1D : ( (depth == 0) ? GL_TEXTURE_2D : GL_TEXTURE_3D ); if((profile->testFlag(GFXTextureProfile::RenderTarget) || profile->testFlag(GFXTextureProfile::ZTarget)) && (!isPow2(width) || !isPow2(height)) && !depth) retTex->mIsNPoT2 = true; retTex->mBinding = binding; // Bind it PRESERVE_TEXTURE(binding); glBindTexture(retTex->getBinding(), retTex->getHandle()); // Create it // TODO: Reenable mipmaps on render targets when Apple fixes their drivers if(forceMips && !retTex->mIsNPoT2) { retTex->mMipLevels = numMipLevels > 1 ? numMipLevels : 0; } else if(profile->testFlag(GFXTextureProfile::NoMipmap) || profile->testFlag(GFXTextureProfile::RenderTarget) || numMipLevels == 1 || retTex->mIsNPoT2) { retTex->mMipLevels = 1; } else { retTex->mMipLevels = numMipLevels; } if(!retTex->mIsNPoT2) { if(!isPow2(width)) width = getNextPow2(width); if(!isPow2(height)) height = getNextPow2(height); if(depth && !isPow2(depth)) depth = getNextPow2(depth); } AssertFatal(GFXGLTextureInternalFormat[format] != GL_ZERO, "GFXGLTextureManager::innerCreateTexture - invalid internal format"); AssertFatal(GFXGLTextureFormat[format] != GL_ZERO, "GFXGLTextureManager::innerCreateTexture - invalid format"); AssertFatal(GFXGLTextureType[format] != GL_ZERO, "GFXGLTextureManager::innerCreateTexture - invalid type"); //calculate num mipmaps if(retTex->mMipLevels == 0) retTex->mMipLevels = getMaxMipmaps(width, height, 1); glTexParameteri(binding, GL_TEXTURE_MAX_LEVEL, retTex->mMipLevels-1 ); //If it wasn't for problems on amd drivers this next part could be really simplified and we wouldn't need to go through manually creating our //mipmap pyramid and instead just use glGenerateMipmap if(isCompressedFormat(format)) { AssertFatal(binding == GL_TEXTURE_2D, "GFXGLTextureManager::innerCreateTexture - Only compressed 2D textures are supported"); U32 tempWidth = width; U32 tempHeight = height; U32 size = getCompressedSurfaceSize(format,height,width); //Fill compressed images with 0's U8 *pTemp = (U8*)dMalloc(sizeof(U8)*size); dMemset(pTemp,0,size); for(U32 i=0; i< retTex->mMipLevels; i++) { tempWidth = getMax( U32(1), width >> i ); tempHeight = getMax( U32(1), height >> i ); size = getCompressedSurfaceSize(format,width,height,i); glCompressedTexImage2D(binding,i,GFXGLTextureInternalFormat[format],tempWidth,tempHeight,0,size,pTemp); } dFree(pTemp); } else { if(binding == GL_TEXTURE_2D)
//----------------------------------------------------------------------------- // Verve // Copyright (C) - Violent Tulip //----------------------------------------------------------------------------- #include "VPathNode.h" #include "VPath.h" #include "core/stream/bitStream.h" #include "core/strings/stringUnit.h" #include "sim/netConnection.h" //----------------------------------------------------------------------------- static U32 gOrientationTypeBits = getBinLog2( getNextPow2( VPathNode::k_OrientationTypeSize ) ); //----------------------------------------------------------------------------- VPathNode::VPathNode( void ) : mPath( NULL ), mLocalPosition( Point3F( 0.f, 0.f, 0.f ) ), mLocalRotation( QuatF( 0.f, 0.f, 0.f, 1.f ) ), mWorldPosition( Point3F( 0.f, 0.f, 0.f ) ), mWorldRotation( QuatF( 0.f, 0.f, 0.f, 1.f ) ), mWeight( 10.f ), mLength( 0.f ) { // Init. mOrientationMode.Type = k_OrientationFree; mOrientationMode.Point = Point3F::Zero;
void ProjectedShadow::_renderToTexture( F32 camDist, const TSRenderState &rdata ) { PROFILE_SCOPE( ProjectedShadow_RenderToTexture ); GFXDEBUGEVENT_SCOPE( ProjectedShadow_RenderToTexture, ColorI( 255, 0, 0 ) ); RenderPassManager *renderPass = _getRenderPass(); if ( !renderPass ) return; GFXTransformSaver saver; // NOTE: GFXTransformSaver does not save/restore the frustum // so we must save it here before we modify it. F32 l, r, b, t, n, f; bool ortho; GFX->getFrustum( &l, &r, &b, &t, &n, &f, &ortho ); // Set the orthographic projection // matrix up, to be based on the radius // generated based on our shape. GFX->setOrtho( -mRadius, mRadius, -mRadius, mRadius, 0.001f, (mRadius * 2) * smDepthAdjust, true ); // Set the world to light space // matrix set up in shouldRender(). GFX->setWorldMatrix( mWorldToLight ); // Get the shapebase datablock if we have one. ShapeBaseData *data = NULL; if ( mShapeBase ) data = static_cast<ShapeBaseData*>( mShapeBase->getDataBlock() ); // Init or update the shadow texture size. if ( mShadowTexture.isNull() || ( data && data->shadowSize != mShadowTexture.getWidth() ) ) { U32 texSize = getNextPow2( data ? data->shadowSize : 256 * LightShadowMap::smShadowTexScalar ); mShadowTexture.set( texSize, texSize, GFXFormatR8G8B8A8, &PostFxTargetProfile, "BLShadow" ); } GFX->pushActiveRenderTarget(); if ( !mRenderTarget ) mRenderTarget = GFX->allocRenderToTextureTarget(); mRenderTarget->attachTexture( GFXTextureTarget::DepthStencil, _getDepthTarget( mShadowTexture->getWidth(), mShadowTexture->getHeight() ) ); mRenderTarget->attachTexture( GFXTextureTarget::Color0, mShadowTexture ); GFX->setActiveRenderTarget( mRenderTarget ); GFX->clear( GFXClearZBuffer | GFXClearStencil | GFXClearTarget, ColorI( 0, 0, 0, 0 ), 1.0f, 0 ); const SceneRenderState *diffuseState = rdata.getSceneState(); SceneManager *sceneManager = diffuseState->getSceneManager(); SceneRenderState baseState ( sceneManager, SPT_Shadow, SceneCameraState::fromGFXWithViewport( diffuseState->getViewport() ), renderPass ); baseState.getMaterialDelegate().bind( &ProjectedShadow::_getShadowMaterial ); baseState.setDiffuseCameraTransform( diffuseState->getCameraTransform() ); baseState.setWorldToScreenScale( diffuseState->getWorldToScreenScale() ); baseState.getCullingState().disableZoneCulling( true ); mParentObject->prepRenderImage( &baseState ); renderPass->renderPass( &baseState ); // Delete the SceneRenderState we allocated. mRenderTarget->resolve(); GFX->popActiveRenderTarget(); // If we're close enough then filter the shadow. if ( camDist < BasicLightManager::getShadowFilterDistance() ) { if ( !smShadowFilter ) { PostEffect *filter = NULL; if ( !Sim::findObject( "BL_ShadowFilterPostFx", filter ) ) Con::errorf( "ProjectedShadow::_renderToTexture() - 'BL_ShadowFilterPostFx' not found!" ); smShadowFilter = filter; } if ( smShadowFilter ) smShadowFilter->process( NULL, mShadowTexture ); } // Restore frustum if (!ortho) GFX->setFrustum(l, r, b, t, n, f); else GFX->setOrtho(l, r, b, t, n, f); // Set the last render time. mLastRenderTime = Platform::getVirtualMilliseconds(); // HACK: Will remove in future release! mDecalInstance->mCustomTex = &mShadowTexture; }
void mkFluid::setGlobalReflectionSize(U32 size) { smGlobalReflectionSize = getNextPow2(size); }