Okay, this is a bit complex and messy but I try to explain.
what I'm trying to do is load an FPS Creator level in to my project with no light maps at first. And I decided to create a function to load the light maps only when I need it. I have no problem with that. The FPS Creator level load fine and the light map with the custom shader as well. I have problem when I try to sort of remove the light map and reload the level with no light map again. I'm not deleting the objects, but simply reload all the textures and apply them without light map using the internal shader again.
This is the code I'm using to load the FPSC Level converted by Wizzkid's FPSC-2-AGK tool and apply the internal shader
Function loadFPSCLevel()
If fpscmap_loaded_nolightmap = 0
SetFolder("/fpsc")
//remove any shader files may exported by FPSC-2-AGK as we don't rely on them
If GetFileExists("vertex.vs") then DeleteFile("vertex.vs")
If GetFileExists("pixel.ps") then DeleteFile("pixel.ps")
//load all 3d mesh, all textures and apply textures on each 3D mesh
fpscObjNum = (GetFileCount() - 2)/2
For obj = 1 to fpscObjNum
objID = fpscObjMeshStartID + obj
textID = fpscObjTextureStartID + obj
If fpscmap_loaded_nolightmap = 0 and fpscmap_loaded_withlightmap = 0 //load meshes only if it not already loaded, otherwise reload only the textures
obj$ = "mesh-" + Str(obj) + ".obj"
LoadObject(objID, obj$, 0)
EndIf
For i = 0 to 15
If GetFileExists("texture-" + Str(obj) + "-" + Str(i) + ".jpg")
tex$ = "texture-" + Str(obj) + "-" + Str(i) + ".jpg"
LoadImage(textID,tex$)
SetImageWrapU(textID, 1)
SetImageWrapV(textID, 1)
SetObjectImage(objID, textID, 0)
EndIf
Next i
SetObjectShader(objID,0) //use internal shader
Sync()
Next obj
fpscmap_loaded_nolightmap = 1
fpscmap_loaded_withlightmap = 0
EndIf
EndFunction
And this is the code I'm using to load the light map and apply the custom shader
Function loadFPSCLightMap()
If fpscmap_loaded_withlightmap = 0
SetFolder("/fpsc")
//load shader
SetFolder("/media/shaders")
LoadShader(1, "fpsc_vertex.vs", "fpsc_pixel.ps")
SetFolder("/fpsc")
//load lightmaps
Dim lm[15]
For i = 0 to 15
If GetFileExists(Str(i) + ".png")
lm[i] = LoadImage(Str(i) + ".png")
lmMax = i
EndIf
Next i
For t = 0 to lmMax
SetImageWrapU(lm[t], 1)
SetImageWrapV(lm[t], 1)
Next t
//apply lightmaps and shaders on each object
fpscObjNum = (GetFileCount() - 2) / 2
For obj = 1 to fpscObjNum
objID = fpscObjMeshStartID + obj
For i = 0 to 15
If GetFileExists("texture-" + Str(obj) + "-" + Str(i) + ".jpg")
tex$ = "texture-" + Str(obj) + "-" + Str(i) + ".jpg"
EndIf
Next i
texname$ = Left(tex$, Len(tex$) - 4)
lm$ = Right(texname$, 1)
SetObjectImage(objID, lm[val(lm$)], 1)
SetObjectShader(objID, 1)
Sync()
Next obj
fpscmap_loaded_withlightmap = 1
fpscmap_loaded_nolightmap = 0
EndIf
EndFunction
So the idea is, when you load the FPSC Level for the first time, it is going to load all the objects and textures but not the light map. If you load the FPSC Level the second time after the light map is loaded, it is going to only reload the textures and apply the internal shader to remove the light map and this is the point when I get the error message.
The only way to avoid the error if I delete the whole Level and reload it but I would like to avoid reloading the whole level every time I want to remove the light maps because it slow.
In theory, you can use the functions as is to to load an FPSC Level and light map converted using FPSC-2-AGK, only need to declare some globals. I think this is the ones you need:
global fpscmap_loaded_withlightmap as integer = 0
global fpscmap_loaded_nolightmap as integer = 0
global fpscObjMeshStartID as integer = 1600
global fpscObjTextureStartID as integer = 2000
The shader code:
Pixel shader:
#ifdef GL_ES
precision highp float;
#endif
uniform sampler2D texture0;
uniform sampler2D texture1;
varying vec2 uv0Varying;
varying vec2 uv1Varying;
void main()
{
gl_FragColor = (texture2D(texture1, uv1Varying)+vec4(0.5,0.5,0.5,1.0))*texture2D(texture0, uv0Varying);
}
Vertex shader:
attribute vec3 position;
attribute vec3 normal;
attribute vec2 uv;
attribute vec2 uv1;
varying vec2 uv0Varying;
varying vec2 uv1Varying;
uniform vec4 uvBounds0;
uniform mat4 agk_WorldViewProj;
void main()
{
gl_Position = agk_WorldViewProj * vec4(position,1.0);
uv0Varying = uv * uvBounds0.xy + uvBounds0.zw;
uv1Varying = uv1 * uvBounds0.xy + uvBounds0.zw;
}
I hope it helps.