absence_thelab

view src/3deng/textureman.cpp @ 1:4d5933c261c3

todo and .hgignore
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 23 Oct 2014 02:18:43 +0300
parents
children
line source
1 #include "textureman.h"
2 #include "3dengine.h"
3 #include "d3dx8.h"
5 using std::string;
7 template <class KeyType>
8 unsigned int Hash(const KeyType &key, unsigned long size) {
9 string str = (string)key;
10 return ((unsigned int)str[0] + (unsigned int)str[1] + (unsigned int)str[2]) % size;
11 }
13 TextureManager::TextureManager(GraphicsContext *gc) {
14 this->gc = gc;
15 notfilecount = 0;
17 textures.SetHashFunction(Hash);
19 CreateStockTextures();
20 }
22 TextureManager::~TextureManager() {}
24 #define STOCKSIZE 256
25 #include <cassert>
27 void TextureManager::CreateStockTextures() {
28 Surface *surf;
30 // ----- 1/d^2 blob texture -----
31 Texture *tmp = CreateTexture(STOCKSIZE, STOCKSIZE, 0, true, true);
32 tmp->GetSurfaceLevel(0, &surf);
34 dword *ptr, offs;
35 D3DLOCKED_RECT d3dlock;
36 assert(surf->LockRect(&d3dlock, 0, 0) == D3D_OK);
37 ptr = (dword*)d3dlock.pBits;
38 offs = (d3dlock.Pitch >> 2) - STOCKSIZE;
40 Vector2 Center((float)(STOCKSIZE>>1), (float)(STOCKSIZE>>1));
42 for(int y=0; y<STOCKSIZE; y++) {
43 for(int x=0; x<STOCKSIZE; x++) {
44 Vector2 pos((float)x, (float)y);
45 float p = 100.0f / (pos - Center).LengthSq();
47 Color col(p, p, p, p);
48 *ptr++ = col.GetPacked32();
49 }
50 ptr += offs;
51 }
53 surf->UnlockRect();
55 Texture *Blob = CreateTexture(STOCKSIZE, STOCKSIZE, 0, true);
56 gc->D3DDevice->UpdateTexture(tmp, Blob);
58 UpdateMipmapChain(Blob);
60 AddTexture(Blob, "STOCKTEX_BLOB");
61 tmp->Release();
63 // ----- chessboard texture -----
65 tmp = CreateTexture(STOCKSIZE, STOCKSIZE, 0, true, true);
66 tmp->GetSurfaceLevel(0, &surf);
68 assert(surf->LockRect(&d3dlock, 0, 0) == D3D_OK);
69 ptr = (dword*)d3dlock.pBits;
70 offs = (d3dlock.Pitch >> 2) - STOCKSIZE;
72 for(int y=0; y<STOCKSIZE; y++) {
73 for(int x=0; x<STOCKSIZE; x++) {
74 int dx = x - (STOCKSIZE>>1);
75 int dy = y - (STOCKSIZE>>1);
76 if((dx > 0 && dy > 0) || (dx < 0 && dy < 0)) {
77 *ptr++ = 0xffffffff;
78 } else {
79 *ptr++ = 0xff000000;
80 }
81 }
82 ptr += offs;
83 }
85 surf->UnlockRect();
87 Texture *ChessBoard = CreateTexture(STOCKSIZE, STOCKSIZE, 0, true);
88 gc->D3DDevice->UpdateTexture(tmp, ChessBoard);
90 UpdateMipmapChain(ChessBoard);
92 AddTexture(ChessBoard, "STOCKTEX_CHESSBOARD");
93 tmp->Release();
95 // ----- grid texture -----
97 tmp = CreateTexture(STOCKSIZE, STOCKSIZE, 0, true, true);
98 tmp->GetSurfaceLevel(0, &surf);
100 assert(surf->LockRect(&d3dlock, 0, 0) == D3D_OK);
101 ptr = (dword*)d3dlock.pBits;
102 offs = (d3dlock.Pitch >> 2) - STOCKSIZE;
104 for(int y=0; y<STOCKSIZE; y++) {
105 for(int x=0; x<STOCKSIZE; x++) {
106 if(x == 0 || x == STOCKSIZE-1 || y == 0 || y == STOCKSIZE-1) {
107 *ptr++ = 0xffffffff;
108 } else {
109 *ptr++ = 0xff000000;
110 }
111 }
112 ptr += offs;
113 }
115 surf->UnlockRect();
117 Texture *Grid = CreateTexture(STOCKSIZE, STOCKSIZE, 0, true);
118 gc->D3DDevice->UpdateTexture(tmp, Grid);
120 UpdateMipmapChain(Grid);
122 AddTexture(Grid, "STOCKTEX_GRID");
123 tmp->Release();
126 // ---------- biased linear falloff ---------
127 tmp = CreateTexture(STOCKSIZE, STOCKSIZE, 0, true, true);
128 tmp->GetSurfaceLevel(0, &surf);
130 assert(surf->LockRect(&d3dlock, 0, 0) == D3D_OK);
131 ptr = (dword*)d3dlock.pBits;
132 offs = (d3dlock.Pitch >> 2) - STOCKSIZE;
134 Center = Vector2((float)(STOCKSIZE>>1), (float)(STOCKSIZE>>1));
136 for(int y=0; y<STOCKSIZE; y++) {
137 for(int x=0; x<STOCKSIZE; x++) {
138 Vector2 pos((float)x, (float)y);
139 float dist = (pos - Center).Length();
140 // f[x] / ((1/b - 2) * (1 - f[x]) + 1)
141 float b = 0.3f;
142 float p = dist ? ((STOCKSIZE - dist) / STOCKSIZE) / ((1/b-2) * (1-((STOCKSIZE - dist) / STOCKSIZE)) + 1) : 1.0f;
144 Color col(p, p, p, p);
145 *ptr++ = col.GetPacked32();
146 }
147 ptr += offs;
148 }
150 surf->UnlockRect();
152 Texture *Blofm = CreateTexture(STOCKSIZE, STOCKSIZE, 0, true);
153 gc->D3DDevice->UpdateTexture(tmp, Blofm);
155 UpdateMipmapChain(Blofm);
157 AddTexture(Blofm, "STOCKTEX_BLOFM");
158 tmp->Release();
159 }
161 void TextureManager::SetGraphicsContext(GraphicsContext *gc) {
162 this->gc = gc;
163 }
165 Texture *TextureManager::LoadTexture(const char *fname) {
166 if(!gc || !fname) return 0;
168 Texture *tex;
170 Pair<string, Texture*> *p;
171 if(!(p = textures.Find(fname))) {
172 if(D3DXCreateTextureFromFile(gc->D3DDevice, fname, &tex) != D3D_OK) return 0;
173 textures.Insert(fname, tex);
174 } else {
175 tex = p->val;
176 }
178 return tex;
179 }
181 Texture *TextureManager::CreateTexture(dword x, dword y, dword usage, bool mipmaps, bool local) {
182 if(!gc) return 0;
183 Texture *tex;
184 D3DPOOL pool = local ? D3DPOOL_SYSTEMMEM : D3DPOOL_DEFAULT;
185 D3DFORMAT fmt = usage == UsageDepthStencil ? gc->ZFormat : D3DFMT_A8R8G8B8;
186 if(usage == UsageDepthStencil) pool = D3DPOOL_MANAGED;
187 if(gc->D3DDevice->CreateTexture(x, y, (int)(!mipmaps), usage, fmt, pool, &tex) != D3D_OK) return 0;
188 return tex;
189 }
191 Texture *TextureManager::AddTexture(Texture *tex, const char *name) {
192 string fname = name ? name : "not_a_file_" + notfilecount;
193 textures.Insert(fname, tex);
194 return tex;
195 }
197 Texture *TextureManager::AddTexture(const char *fname) {
198 return LoadTexture(fname);