Are you using CreateShader for each mesh/door? That would cause a lot of compilation of the shader with each instance using up gpu memory.Quickly looking through the c++ source... there are ShadeEntity, ShadeMesh and ShadeSurface functions. I imagine there are equivalent 'Max functions with similar names? If so then they should let you load and compile the shader once and apply the shader as a material ( if I understand OpenB3D correctly ).

#version 130#define MAX_LIGHTS 8#define NUM_LIGHTS 2out vec2 Vertex_UV;out vec3 Vertex_Normal;out vec3 Vertex_LightDir[MAX_LIGHTS];out vec4 Vertex_EyeVec;out vec4 position;void main(){ gl_TexCoord[0] = gl_MultiTexCoord0; // diffuseMap gl_TexCoord[1] = gl_MultiTexCoord1; // normalMap gl_TexCoord[2] = gl_MultiTexCoord2; // lightMap Vertex_Normal = gl_NormalMatrix * gl_Normal; vec4 view_vertex = gl_ModelViewMatrix * gl_Vertex; for (int i = 0; i < NUM_LIGHTS; ++i) { Vertex_LightDir[i] = gl_LightSource[i].position.xyz+vec3(0,0,0) - view_vertex.xyz; } Vertex_EyeVec = -view_vertex; position = gl_ModelViewMatrix * gl_Vertex; gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;}

#version 130#define MAX_LIGHTS 8#define NUM_LIGHTS 2precision mediump float;// desaturate RGB constantsconst float r = 0.2126;const float g = 0.7152;const float b = 0.0722;const float specularintensity = 0.5;const float normalhighlight_on = 0.5;const float normalhighlight_off = 0.125;const float colormapintensity = 1.0;const float lightmapintensity = 1.0;const float fogrange = 1024.0;// input texturesuniform sampler2D diffuseMap;uniform sampler2D normalMap;uniform sampler2D lightMap;// input variables from vertex shaderin vec2 Vertex_UV;in vec3 Vertex_Normal;in vec3 Vertex_LightDir[MAX_LIGHTS];in vec4 Vertex_EyeVec;in vec4 position;// input flags from gameuniform int flagFog;uniform int flagTone;uniform int flagSpec;uniform int flagBump;uniform int flagLight;uniform int flagAtt;// input variables from gameuniform float gamma;uniform float lightningshaderintensity;uniform float torchintensity;//uniform int bumpmode;//uniform int fog;//uniform int lightswitch;uniform vec3 fogColor;uniform vec4 baseColor;struct FloatArray { float Float; };uniform FloatArray lightradius[MAX_LIGHTS];uniform vec4 vambient=vec4(0.1,0.1,0.1,1.0);// function: tone mappingvec3 ToneMapping(vec3 color){ float A = 0.15; float B = 0.50; float C = 0.10; float D = 0.20; float E = 0.02; float F = 0.30; float W = 11.2; float exposure = 2.0; color *= exposure; color = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F; float white = ((W * (A * W + C * B) + D * E) / (W * (A * W + B) + D * F)) - E / F; color /= white; color = pow(color, vec3(1. / (1.0 + gamma)))*1.5; return color;}// TBN matrix calculations #1mat3 cotangent_frame(vec3 N, vec3 p, vec2 uv){ vec3 dp1 = dFdx(p); vec3 dp2 = dFdy(p); vec2 duv1 = dFdx(uv); vec2 duv2 = dFdy(uv); vec3 dp2perp = cross(dp2, N); vec3 dp1perp = cross(N, dp1); vec3 T = dp2perp * duv1.x + dp1perp * duv2.x; vec3 B = dp2perp * duv1.y + dp1perp * duv2.y; float invmax = inversesqrt(max(dot(T, T), dot(B, B))); return mat3(T * invmax, B * invmax, N);}// TBN matrix calculations #2vec3 perturb_normal(vec3 N, vec3 V, vec2 texcoord){ vec3 map = texture2D(normalMap, texcoord).xyz * vec3(1.0, 1.0, 1.0); map = map * 255. / 127. - 128. / 127.; mat3 TBN = cotangent_frame(N, -V, texcoord); return normalize(TBN * map);}vec3 DirectIllumination(vec3 P, vec3 N, vec3 lightCentre, float lightRadius, vec3 lightColour, float cutoff){ // calculate normalized light vector and distance to sphere light surface float r = lightRadius; vec3 L = lightCentre - P; float distance = length(L); float d = max(distance - r, 0); L /= distance; // calculate basic attenuation float denom = d / r + 1; float attenuation = 1 / (denom * denom); // scale and bias attenuation such that: // attenuation == 0 at extent of max influence // attenuation == 1 when d == 0 attenuation = (attenuation - cutoff) / (1 - cutoff); attenuation = max(attenuation, 0); float dot = max(dot(L, N), 0); return lightColour * dot * attenuation;}// main fragment shadervoid main(){ float distSqr, att; float specular, lambertTerm; float attenuation; float normalhighlight; vec3 E, R, L; // UV coordinates (1st & second set) vec2 uv1 = gl_TexCoord[0].xy; vec2 uv2 = gl_TexCoord[2].xy; // texture samplers vec4 color = texture(diffuseMap, uv1); vec4 normal = texture(normalMap, uv1); vec4 spec = texture(normalMap, uv1); vec4 light = texture(lightMap, uv2); // mix lightmap with a base color to create a subtle ambient light vec4 amb = (max(light * 1, vec4(0.025, 0.03, 0.035, 1.0)) * color); // normalize normal/view vector and perturbated normal vector vec3 N = normalize(Vertex_Normal.xyz); vec3 V = normalize(Vertex_EyeVec.xyz); vec3 PN = perturb_normal(N, V, uv1); // final color starts with ambient light only //vec4 final_color = vec4(0.0, 1.0, 0.0, 1.0);//amb + amb + amb; vec4 final_color = amb + amb + amb; // distance to frag texel float dist = abs(position.z); for (int i = 0; i < NUM_LIGHTS; ++i) { // calculate light distance vec3 positionToLightSource = vec3(gl_LightSource[i].position - position); vec3 lightDirection = normalize(positionToLightSource); float distance = length(positionToLightSource); float lightdist = gl_LightSource[i].constantAttenuation + (gl_LightSource[i].linearAttenuation * distance) + (gl_LightSource[i].quadraticAttenuation * sqrt(distance)); // spotlight attenuation attenuation = 1.0 / lightdist; if (gl_LightSource[i].spotCutoff <= 90.0) { float clampedCosine = clamp(max(0.0, dot(-lightDirection, gl_LightSource[i].spotDirection)), 0.0, 1.0); // spotlight if (clampedCosine < gl_LightSource[i].spotCosCutoff) { // no light outside spotlight cone attenuation = 0.0; } else { // light intensity inside spotlight cone attenuation = attenuation * pow(clampedCosine, gl_LightSource[i].spotExponent); } } // calculate lambert term distSqr = dot(Vertex_LightDir[i], Vertex_LightDir[i]); att = clamp(1.0 - lightradius[i].Float * sqrt(distSqr), 0.0, 1.0); L = normalize(Vertex_LightDir[i].xyz * inversesqrt(distSqr)); lambertTerm = dot(PN, L); // specularity final_color += gl_LightSource[i].diffuse * gl_FrontMaterial.diffuse * color * att; E = normalize(Vertex_EyeVec.xyz); R = reflect(-L, PN); specular = pow(max(dot(R, E), 0.0), gl_FrontMaterial.shininess)*att* 10.0 / dist; // add specularity to final color final_color += gl_LightSource[i].specular * gl_FrontMaterial.specular * specular * lambertTerm * att; } // decrease attenuation by number of lights attenuation*=1.0/NUM_LIGHTS; // calculate direct illumination vec4 dir = vec4(DirectIllumination(position.xyz, V, vec3(0, 0, 0), 1024, vec3(1.0, 0.9, 0.8), 0.25),1.0); // mix ambient, direct illumination, base color, attenuation with final color final_color += vambient * color*amb; final_color*=dir; final_color*=baseColor; final_color*=attenuation; final_color+=amb; float fogFactor; // fog if (flagFog == 1) { float density = (dist / fogrange); const float e = 2.71828; // calculate fog factor fogFactor = (density * gl_FragCoord.z); fogFactor *= fogFactor; fogFactor = clamp(pow(e, -fogFactor), 0.0, 1.0); // mix fog color with colormap final_color = mix(vec4(fogColor.rgb, 1.0), final_color, fogFactor); } // lightning strike if (lightningshaderintensity > 0.0) { float dist = 100.0 / (distance(position, vec4(0.0, 0.0, 0.0, 1.0))); //final_color = color * lightningshaderintensity * dist; float luminance = r * final_color.r + g * final_color.g + b * final_color.b; final_color.r = max(final_color.r, luminance * lightningshaderintensity * (2.5 * fogFactor)); final_color.g = max(final_color.g, luminance * lightningshaderintensity * (2.5 * fogFactor)); final_color.b = max(final_color.b, luminance * lightningshaderintensity * (2.5 * fogFactor)); //final_color*=lightningshaderintensity; } // apply Tonemapping gl_FragColor.rgb = ToneMapping(final_color.rgb); gl_FragColor.a = color.a; if (flagBump == 1) { gl_FragColor=vec4(color.rgb*attenuation,color.a)+amb; } }

if(flags&2) ApplyAlpha(tex,buffer);