absence_thelab
diff src/3deng/textureman.cpp @ 0:1cffe3409164
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 23 Oct 2014 01:46:07 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/3deng/textureman.cpp Thu Oct 23 01:46:07 2014 +0300 1.3 @@ -0,0 +1,199 @@ 1.4 +#include "textureman.h" 1.5 +#include "3dengine.h" 1.6 +#include "d3dx8.h" 1.7 + 1.8 +using std::string; 1.9 + 1.10 +template <class KeyType> 1.11 +unsigned int Hash(const KeyType &key, unsigned long size) { 1.12 + string str = (string)key; 1.13 + return ((unsigned int)str[0] + (unsigned int)str[1] + (unsigned int)str[2]) % size; 1.14 +} 1.15 + 1.16 +TextureManager::TextureManager(GraphicsContext *gc) { 1.17 + this->gc = gc; 1.18 + notfilecount = 0; 1.19 + 1.20 + textures.SetHashFunction(Hash); 1.21 + 1.22 + CreateStockTextures(); 1.23 +} 1.24 + 1.25 +TextureManager::~TextureManager() {} 1.26 + 1.27 +#define STOCKSIZE 256 1.28 +#include <cassert> 1.29 + 1.30 +void TextureManager::CreateStockTextures() { 1.31 + Surface *surf; 1.32 + 1.33 + // ----- 1/d^2 blob texture ----- 1.34 + Texture *tmp = CreateTexture(STOCKSIZE, STOCKSIZE, 0, true, true); 1.35 + tmp->GetSurfaceLevel(0, &surf); 1.36 + 1.37 + dword *ptr, offs; 1.38 + D3DLOCKED_RECT d3dlock; 1.39 + assert(surf->LockRect(&d3dlock, 0, 0) == D3D_OK); 1.40 + ptr = (dword*)d3dlock.pBits; 1.41 + offs = (d3dlock.Pitch >> 2) - STOCKSIZE; 1.42 + 1.43 + Vector2 Center((float)(STOCKSIZE>>1), (float)(STOCKSIZE>>1)); 1.44 + 1.45 + for(int y=0; y<STOCKSIZE; y++) { 1.46 + for(int x=0; x<STOCKSIZE; x++) { 1.47 + Vector2 pos((float)x, (float)y); 1.48 + float p = 100.0f / (pos - Center).LengthSq(); 1.49 + 1.50 + Color col(p, p, p, p); 1.51 + *ptr++ = col.GetPacked32(); 1.52 + } 1.53 + ptr += offs; 1.54 + } 1.55 + 1.56 + surf->UnlockRect(); 1.57 + 1.58 + Texture *Blob = CreateTexture(STOCKSIZE, STOCKSIZE, 0, true); 1.59 + gc->D3DDevice->UpdateTexture(tmp, Blob); 1.60 + 1.61 + UpdateMipmapChain(Blob); 1.62 + 1.63 + AddTexture(Blob, "STOCKTEX_BLOB"); 1.64 + tmp->Release(); 1.65 + 1.66 + // ----- chessboard texture ----- 1.67 + 1.68 + tmp = CreateTexture(STOCKSIZE, STOCKSIZE, 0, true, true); 1.69 + tmp->GetSurfaceLevel(0, &surf); 1.70 + 1.71 + assert(surf->LockRect(&d3dlock, 0, 0) == D3D_OK); 1.72 + ptr = (dword*)d3dlock.pBits; 1.73 + offs = (d3dlock.Pitch >> 2) - STOCKSIZE; 1.74 + 1.75 + for(int y=0; y<STOCKSIZE; y++) { 1.76 + for(int x=0; x<STOCKSIZE; x++) { 1.77 + int dx = x - (STOCKSIZE>>1); 1.78 + int dy = y - (STOCKSIZE>>1); 1.79 + if((dx > 0 && dy > 0) || (dx < 0 && dy < 0)) { 1.80 + *ptr++ = 0xffffffff; 1.81 + } else { 1.82 + *ptr++ = 0xff000000; 1.83 + } 1.84 + } 1.85 + ptr += offs; 1.86 + } 1.87 + 1.88 + surf->UnlockRect(); 1.89 + 1.90 + Texture *ChessBoard = CreateTexture(STOCKSIZE, STOCKSIZE, 0, true); 1.91 + gc->D3DDevice->UpdateTexture(tmp, ChessBoard); 1.92 + 1.93 + UpdateMipmapChain(ChessBoard); 1.94 + 1.95 + AddTexture(ChessBoard, "STOCKTEX_CHESSBOARD"); 1.96 + tmp->Release(); 1.97 + 1.98 + // ----- grid texture ----- 1.99 + 1.100 + tmp = CreateTexture(STOCKSIZE, STOCKSIZE, 0, true, true); 1.101 + tmp->GetSurfaceLevel(0, &surf); 1.102 + 1.103 + assert(surf->LockRect(&d3dlock, 0, 0) == D3D_OK); 1.104 + ptr = (dword*)d3dlock.pBits; 1.105 + offs = (d3dlock.Pitch >> 2) - STOCKSIZE; 1.106 + 1.107 + for(int y=0; y<STOCKSIZE; y++) { 1.108 + for(int x=0; x<STOCKSIZE; x++) { 1.109 + if(x == 0 || x == STOCKSIZE-1 || y == 0 || y == STOCKSIZE-1) { 1.110 + *ptr++ = 0xffffffff; 1.111 + } else { 1.112 + *ptr++ = 0xff000000; 1.113 + } 1.114 + } 1.115 + ptr += offs; 1.116 + } 1.117 + 1.118 + surf->UnlockRect(); 1.119 + 1.120 + Texture *Grid = CreateTexture(STOCKSIZE, STOCKSIZE, 0, true); 1.121 + gc->D3DDevice->UpdateTexture(tmp, Grid); 1.122 + 1.123 + UpdateMipmapChain(Grid); 1.124 + 1.125 + AddTexture(Grid, "STOCKTEX_GRID"); 1.126 + tmp->Release(); 1.127 + 1.128 + 1.129 + // ---------- biased linear falloff --------- 1.130 + tmp = CreateTexture(STOCKSIZE, STOCKSIZE, 0, true, true); 1.131 + tmp->GetSurfaceLevel(0, &surf); 1.132 + 1.133 + assert(surf->LockRect(&d3dlock, 0, 0) == D3D_OK); 1.134 + ptr = (dword*)d3dlock.pBits; 1.135 + offs = (d3dlock.Pitch >> 2) - STOCKSIZE; 1.136 + 1.137 + Center = Vector2((float)(STOCKSIZE>>1), (float)(STOCKSIZE>>1)); 1.138 + 1.139 + for(int y=0; y<STOCKSIZE; y++) { 1.140 + for(int x=0; x<STOCKSIZE; x++) { 1.141 + Vector2 pos((float)x, (float)y); 1.142 + float dist = (pos - Center).Length(); 1.143 + // f[x] / ((1/b - 2) * (1 - f[x]) + 1) 1.144 + float b = 0.3f; 1.145 + float p = dist ? ((STOCKSIZE - dist) / STOCKSIZE) / ((1/b-2) * (1-((STOCKSIZE - dist) / STOCKSIZE)) + 1) : 1.0f; 1.146 + 1.147 + Color col(p, p, p, p); 1.148 + *ptr++ = col.GetPacked32(); 1.149 + } 1.150 + ptr += offs; 1.151 + } 1.152 + 1.153 + surf->UnlockRect(); 1.154 + 1.155 + Texture *Blofm = CreateTexture(STOCKSIZE, STOCKSIZE, 0, true); 1.156 + gc->D3DDevice->UpdateTexture(tmp, Blofm); 1.157 + 1.158 + UpdateMipmapChain(Blofm); 1.159 + 1.160 + AddTexture(Blofm, "STOCKTEX_BLOFM"); 1.161 + tmp->Release(); 1.162 +} 1.163 + 1.164 +void TextureManager::SetGraphicsContext(GraphicsContext *gc) { 1.165 + this->gc = gc; 1.166 +} 1.167 + 1.168 +Texture *TextureManager::LoadTexture(const char *fname) { 1.169 + if(!gc || !fname) return 0; 1.170 + 1.171 + Texture *tex; 1.172 + 1.173 + Pair<string, Texture*> *p; 1.174 + if(!(p = textures.Find(fname))) { 1.175 + if(D3DXCreateTextureFromFile(gc->D3DDevice, fname, &tex) != D3D_OK) return 0; 1.176 + textures.Insert(fname, tex); 1.177 + } else { 1.178 + tex = p->val; 1.179 + } 1.180 + 1.181 + return tex; 1.182 +} 1.183 + 1.184 +Texture *TextureManager::CreateTexture(dword x, dword y, dword usage, bool mipmaps, bool local) { 1.185 + if(!gc) return 0; 1.186 + Texture *tex; 1.187 + D3DPOOL pool = local ? D3DPOOL_SYSTEMMEM : D3DPOOL_DEFAULT; 1.188 + D3DFORMAT fmt = usage == UsageDepthStencil ? gc->ZFormat : D3DFMT_A8R8G8B8; 1.189 + if(usage == UsageDepthStencil) pool = D3DPOOL_MANAGED; 1.190 + if(gc->D3DDevice->CreateTexture(x, y, (int)(!mipmaps), usage, fmt, pool, &tex) != D3D_OK) return 0; 1.191 + return tex; 1.192 +} 1.193 + 1.194 +Texture *TextureManager::AddTexture(Texture *tex, const char *name) { 1.195 + string fname = name ? name : "not_a_file_" + notfilecount; 1.196 + textures.Insert(fname, tex); 1.197 + return tex; 1.198 +} 1.199 + 1.200 +Texture *TextureManager::AddTexture(const char *fname) { 1.201 + return LoadTexture(fname); 1.202 +} 1.203 \ No newline at end of file