00001
00008 #ifndef _SCENE_H_
00009 #define _SCENE_H_
00010
00011 #include "globals.h"
00012 #include "object.h"
00013 #include "light.h"
00014 #include "camera.h"
00015
00016
00023 class TScene
00024 {
00025 protected:
00027 map<string,TObject> objects;
00029 map<string,TObject>::iterator io;
00031 map<string,TMaterial> materials;
00033 map<string,TMaterial>::iterator im;
00035 vector<TLight> lights;
00037 vector<TLight>::iterator il;
00038
00042 map<string,GLuint> tex_cache;
00044 map<string,GLuint>::iterator it;
00046 map<string,VBO>::iterator iob;
00048 map<string,VBO> obj_cache;
00049
00050
00052 Texture font2D_tex;
00054 Texture font2D_bkg;
00056 GLuint font2D;
00057
00058
00059
00061 GLint resx;
00063 GLint resy;
00065 GLfloat near_p;
00067 Glfloat far_p;
00069 GLfloat fovy;
00070
00072 TCamera cam;
00074 bool custom_cam;
00076 bool use_gshader_ref;
00078 int msamples;
00079
00081 TMatrix viewMatrix;
00083 TMatrix tmpMatrix;
00084
00085
00086
00088 bool useHDR;
00090 bool useSSAO;
00092 bool useShadows;
00093
00095 GLuint f_buffer;
00097 GLuint r_buffer_color;
00099 GLuint r_buffer_depth;
00100
00102 GLuint msaa_f_buffer;
00104 GLuint msaa_r_buffer_color;
00106 GLuint msaa_r_buffer_depth;
00107
00109 int RT_resX;
00111 int RT_resY;
00112
00114 GLuint render_texture;
00116 GLuint bloom_texture;
00118 GLuint normal_texture;
00120 GLuint blur_texture;
00121
00123 int load_list;
00125 int load_actual;
00127 string testname;
00128
00129 public:
00130
00131 TScene();
00132
00133 ~TScene();
00134
00135
00136 bool PreInit(GLint _resx, GLint _resy, GLfloat near, GLfloat far, GLfloat fovy, int msamples, bool cust_cam = false, bool load_font = true);
00137 bool PostInit();
00138
00139 void Resize(GLint _resx, GLint _resy);
00140
00141 void Redraw(bool delete_buffer = true);
00142
00143 void LoadScreen(bool swap = true);
00144
00145 void Destroy(bool delete_cache = true);
00147 void SetLoadList(int count)
00148 { load_list = count; load_actual = count; }
00150 void UpdateLoadList(int count)
00151 { load_list += count; load_actual += count; }
00153 void TestName(string name)
00154 { testname = "Current test: "; testname += name; }
00155
00156
00157 void LoadScene(const char* file, bool load_materials = true, bool load_lights = true);
00158
00160
00162 void MoveCamera(GLfloat wx, GLfloat wy, GLfloat wz)
00163 { viewMatrix = cam.Move(wx,wy,wz); }
00165 void MoveCameraAbs(GLfloat wx, GLfloat wy, GLfloat wz)
00166 { viewMatrix = cam.MoveAbs(wx,wy,wz); }
00168 void LookCameraAt(GLfloat wx, GLfloat wy, GLfloat wz)
00169 { viewMatrix = cam.LookAt(wx,wy,wz); }
00170
00172 void RotateCamera(GLfloat angle, GLint axis)
00173 { viewMatrix = cam.Rotate(angle,axis); }
00174
00176 void RotateCameraAbs(GLfloat angle, GLint axis)
00177 { viewMatrix = cam.RotateAbs(angle,axis); }
00178
00180 void SwitchCamera()
00181 { custom_cam = !custom_cam; }
00182
00184 void PrintCamera()
00185 { cout<<"POS: "<<cam.GetPos().x<<","<<cam.GetPos().y<<","<<cam.GetPos().z<<"\n"
00186 <<"ROT: "<<cam.GetRot().x<<","<<cam.GetRot().y<<","<<cam.GetRot().z<<"\n"; }
00188 TVector GetCameraPos()
00189 { return cam.GetPos(); }
00190
00191
00192
00193
00194
00196
00198 void AddLight(GLint _lights, TVector amb, TVector diff, TVector spec, TVector lpos, float radius = 1000.0)
00199 { lights.push_back( TLight(_lights, amb, diff, spec, lpos, radius) ); }
00200
00203 void RemoveLight()
00204 { lights.clear(); }
00205
00207 void MoveLight(GLint light, TVector w)
00208 {
00209 if(light < 0 || (unsigned)light > lights.size()) cerr<<"WARNING: no light with index "<<light<<"\n";
00210 else lights[light].Move(w);
00211 }
00212
00215 void ChangeLightColor(GLint light, GLint component, TVector color)
00216 {
00217 if(light < 0 || (unsigned)light > lights.size()) cerr<<"WARNING: no light with index "<<light<<"\n";
00218 else lights[light].ChangeColor(component,color);
00219 }
00220
00222 int GetLightCount()
00223 { return lights.size(); }
00224
00226 TVector GetLightPos(int light)
00227 {
00228 if(light < 0 || (unsigned)light > lights.size()) { cerr<<"WARNING: no light with index "<<light<<"\n"; return TVector(); }
00229 else return lights[light].GetPos();
00230 }
00231
00233 void UpdateLightCount()
00234 {
00235 for(im = materials.begin(); im != materials.end(); im++)
00236 im->second.SetLights(lights.size());
00237 }
00238
00240 void SetLightRadius(int light, float radius)
00241 {
00242 if(light < 0 || (unsigned)light > lights.size()) cerr<<"WARNING: no light with index "<<light<<"\n";
00243 else lights[light].SetRadius(radius);
00244 }
00245
00247
00249 void AddObject(const char *name, int primitive, GLfloat size = 0.0, GLfloat height = 0.0, GLint sliceX = 1, GLint sliceY = 1)
00250 {
00251 objects[name].Create(name, primitive, size, height, sliceX, sliceY);
00252 LoadScreen();
00253 }
00255 void AddObjectInstance(const char *ref_name, const char *inst_name)
00256 {
00257 if(objects.find(ref_name) == objects.end()) cerr<<"WARNING (AddObjectInstance): no reference object with name "<<ref_name<<"\n";
00258 else objects[inst_name].CreateInstance(objects[ref_name]);
00259 }
00260
00261 void AddObject(const char *name, const char* file);
00262
00264 void MoveObj(const char* name, GLfloat wx, GLfloat wy, GLfloat wz)
00265 { objects[name].Move(wx,wy,wz); }
00267 void MoveObjAbs(const char* name, GLfloat wx, GLfloat wy, GLfloat wz)
00268 { objects[name].MoveAbs(wx,wy,wz); }
00269
00272 void RotateObj(const char* name, GLfloat angle, GLint axis)
00273 { objects[name].Rotate(angle,axis); }
00276 void RotateObjAbs(const char* name, GLfloat angle, GLint axis)
00277 { objects[name].RotateAbs(angle,axis); }
00278
00280 void ResizeObj(const char* name, GLfloat sx, GLfloat sy, GLfloat sz)
00281 { objects[name].Resize(sx,sy,sz); }
00283 GLint GetVertexBuffer(const char* name)
00284 { return objects[name].GetVertexBuffer(); }
00285
00286
00288 TVector GetObjPosition(const char *name)
00289 { return objects[name].GetPosition(); }
00290
00292 void DrawObject(const char* obj_name, bool flag)
00293 { objects[obj_name].DrawObject(flag); }
00294
00296 void SetGInstances(const char* obj_name, int count)
00297 { objects[obj_name].SetGInstances(count); }
00298
00299
00300
00302
00304 void AddMaterial(const char* _name, TVector amb = black, TVector diff = silver, TVector spec = white,
00305 GLfloat shin = 64.0, GLfloat reflect = 0.0, GLfloat transp = 0.0, GLint lm = PHONG)
00306 {
00307 materials[_name] = TMaterial(_name, amb, diff, spec, shin, reflect, transp, lm);
00308 LoadScreen();
00309 }
00310
00311 void AddTexture(const char *name, const char *file, GLint textype = BASE, GLint texmode = MODULATE,
00312 GLfloat intensity = 1.0, GLfloat tileX = 1.0, GLfloat tileY = 1.0, bool mipmap = true, bool aniso = false);
00313
00314
00315 void AddTexture(const char *name, const char **files, GLint textype = CUBEMAP, GLint texmode = MODULATE,
00316 GLfloat intensity = 1.0, GLfloat tileX = 1.0, GLfloat tileY = 1.0, bool aniso = false);
00317
00318
00319 void SetMaterial(const char* obj_name, const char *mat_name);
00320
00323 void CustomShader(const char *name, const char* vert_source, const char* geom_source, const char* frag_source,
00324 const char *vert_defines = NULL, const char *frag_defines = NULL, const char *geom_defines = NULL)
00325 {
00326 if(materials.find(name) == materials.end())
00327 cerr<<"WARNING (CustomShader): no material with name "<<name<<"\n";
00328 else
00329 if(!materials[name].CustomShader(vert_source, geom_source, frag_source,vert_defines, frag_defines, geom_defines))
00330 throw ERR;
00331 }
00332
00334 void SetUniform(const char* m_name, const char* v_name, float value)
00335 { materials[m_name].SetUniform(v_name,value); }
00337 void SetUniform(const char* m_name, const char* v_name, double value)
00338 { materials[m_name].SetUniform(v_name,value); }
00340 void SetUniform(const char* m_name, const char* v_name, int value)
00341 { materials[m_name].SetUniform(v_name,value); }
00343 void SetUniforms(const char* m_name, const char* v_name, float *value, int values)
00344 { materials[m_name].SetUniforms(v_name,value,values); }
00345
00347 void BakeAllMaterials()
00348 { for(im = materials.begin(); im != materials.end(); im++) im->second.BakeMaterial(); }
00349
00350
00351
00353
00354
00355 bool CreateShadowMap(vector<TLight>::iterator ii);
00356
00357 void RenderShadowMap(TLight l);
00358
00361 void SetShadow(GLint lightNum, GLint shadow_size = 2048, GLfloat _shadow_intensity = 0.5, bool shadow = true)
00362 {
00363 if(lightNum < 0 || (unsigned)lightNum > lights.size()) cerr<<"WARNING: no light with index "<<lightNum<<"\n";
00364 else lights[lightNum].SetShadow(shadow_size,_shadow_intensity,shadow);
00365 }
00366
00368 void CastShadow(const char *obj_name, bool flag)
00369 { objects[obj_name].CastShadow(flag); }
00370
00372 void ReceiveShadow(const char *mat_name, bool flag)
00373 { materials[mat_name].ReceiveShadow(flag); }
00374
00376 void UseShadows(bool flag = true)
00377 { useShadows = flag; }
00378
00379
00381
00382
00383 void CreateRenderTarget(int resX = -1, int resY = -1, GLint tex_format = GL_RGBA16F, GLenum tex_type = GL_FLOAT);
00384
00385 void ResizeRenderTarget(int resX, int resY, GLint tex_format = GL_RGBA16F, GLenum tex_type = GL_FLOAT);
00386
00387 bool CreateRenderCubeMap(TObject *obj);
00388
00389 void RenderToCubeMap(TObject *obj);
00390
00392 void SetReflectionSize(const char* obj_name, int value)
00393 { objects[obj_name].refl_size = value; }
00394
00395
00396 void UseDynReflections(bool use_gs = false);
00398 void UseHDR(bool flag = true)
00399 { useHDR = flag; }
00401 void UseSSAO(bool flag = true)
00402 { useSSAO = flag; }
00403
00404
00406
00407
00408 void DrawScreenText(const char *s, float x, float y, float size = 1.0);
00409 void BuildFont();
00410
00412
00414 int GetResX() { return resx; }
00416 int GetResY() { return resy; }
00417 };
00418 #endif