diff --git a/Makefile b/Makefile index b707a64..44d4b3f 100644 --- a/Makefile +++ b/Makefile @@ -14,3 +14,6 @@ all clean: @$(MAKE) -s -C tinyxml $@ @$(MAKE) -s -C engine $@ @echo done + +obj: + @$(MAKE) -s -C engine $@ diff --git a/engine/Makefile b/engine/Makefile index 22ad915..4d026f0 100644 --- a/engine/Makefile +++ b/engine/Makefile @@ -54,6 +54,9 @@ $(NAME): $(OBJ) @g++ -o $(NAME) $(OBJ) $(LDFLAGS) @echo done +obj: $(OBJ) + @echo done + clean: -@$(RMSUB) *.o -@$(RMSUB) *.d diff --git a/engine/RenderDevice.cpp b/engine/RenderDevice.cpp index ba1c4af..88287c1 100644 --- a/engine/RenderDevice.cpp +++ b/engine/RenderDevice.cpp @@ -152,6 +152,15 @@ void RenderDevice::pushTransformation(const Vector3& position, glMultMatrixd( ( GLdouble * ) &m.m ); } +void RenderDevice::pushAbsoluteTransformation(const Vector3& position, + const Quaternion& orientation) +{ + glMatrixMode( GL_MODELVIEW); + glPushMatrix(); + Matrix4x4 m(orientation, position); + glLoadMatrixd( ( GLdouble * ) &m.m ); +} + void RenderDevice::popTransformation() { glMatrixMode( GL_MODELVIEW); diff --git a/engine/RenderDevice.h b/engine/RenderDevice.h index fc7a151..3754e3f 100644 --- a/engine/RenderDevice.h +++ b/engine/RenderDevice.h @@ -45,6 +45,7 @@ public: void setMaterial(Material *); void setTexture(unsigned int unit, Texture* texture, bool last = false); + void pushAbsoluteTransformation (const Vector3&, const Quaternion&); void pushTransformation (const Vector3&, const Quaternion&); void popTransformation (); void setupProjectionMatrix(); diff --git a/engine/ScriptSystem.cpp b/engine/ScriptSystem.cpp index cd97745..39ff2f6 100644 --- a/engine/ScriptSystem.cpp +++ b/engine/ScriptSystem.cpp @@ -180,8 +180,8 @@ void ScriptSystem::callFunction(const std::string &name) if (SQ_SUCCEEDED(sq_get(_VM, -2)) ) { sq_pushroottable(_VM); - if (sq_call(_VM, 1, SQTrue, SQTrue) ) - sq_pop(_VM, 1); + sq_call(_VM, 1, SQFalse, SQTrue); + sq_pop(_VM, 1); } sq_pop(_VM, 1); } @@ -195,8 +195,8 @@ void ScriptSystem::callFunction(const std::string &name, double value) { sq_pushroottable(_VM); sq_pushfloat(_VM, value); - if (sq_call(_VM, 2, SQTrue, SQTrue) ) - sq_pop(_VM, 1); + sq_call(_VM, 2, SQFalse, SQTrue); + sq_pop(_VM, 1); } sq_pop(_VM, 1); } diff --git a/engine/main.cpp b/engine/main.cpp index b36b45b..e966a57 100644 --- a/engine/main.cpp +++ b/engine/main.cpp @@ -65,101 +65,157 @@ void shutdownPhysfs() PHYSFS_deinit(); } -int main(int argc, char **argv) +class Application : public sigslot::has_slots<> { - initializePhysfs(argv[0]); + bool _Running; + double _DeltaTime; + double _LastTime; - CfgParser cfg; - cfg.parseFile("options.cfg"); - - int width = cfg.get("width", 640); - int height = cfg.get("height", 480); - bool fullscreen = cfg.get("fullscreen", false); - ref_ptr window = new RenderWindow(); - window->create(width, height, 0, 0, 0, fullscreen); - - ref_ptr device = new RenderDevice(window); - ref_ptr fontmanager = new FontManager(device); - ref_ptr meshmanager = new MeshManager(device); - ref_ptr texturemanager = new TextureManager(); - ref_ptr scriptsystem = new ScriptSystem(); - ref_ptr shadermanager = new ShaderManager(window); - ref_ptr simulation = new RigidBodySimulation(scriptsystem); - ref_ptr modelmanager = new ModelManager (texturemanager, shadermanager, meshmanager); - ref_ptr camera = new Camera(); - ref_ptr queue = new RenderQueue(); - - setupScriptSystem_Font(scriptsystem, fontmanager); - setupScriptSystem_Image(scriptsystem, texturemanager, device); - setupScriptSystem_Math(scriptsystem); - setupScriptSystem_RigidBody(scriptsystem, simulation); - - GLfloat mat_specular[] = - { 1.0, 1.0, 1.0, 1.0 }; - GLfloat mat_shininess[] = - { 2.0 }; - glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_specular); - glMaterialfv(GL_FRONT, GL_AMBIENT, mat_specular); - glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); - glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); - - ref_ptr starMesh = meshmanager->loadMesh("stars.3ds"); - ref_ptr starTexture = texturemanager->loadTexture("stars.png", 0); - - ref_ptr nebularMesh = meshmanager->loadMesh("nebular.3ds"); - ref_ptr nebularTexture = texturemanager->loadTexture("nebular.png"); - - if (window.valid() && device.valid()) + void KeySlot(int key, int action) { - scriptsystem->loadScript("main"); + if (key == GLFW_KEY_ESC && action == GLFW_RELEASE) + quit(); + } - camera->setFoV(90.0); - camera->setAspectRatio((double)width/(double)height); - camera->setNearPlane(1.0); - camera->setFarPlane(15000.0); - camera->setPosition(Vector3(0.0, 0.0, 20.0)); + ref_ptr _Window; + ref_ptr _Device; + ref_ptr _FontManager; + ref_ptr _MeshManager; + ref_ptr _TextureManager; + ref_ptr _ScriptSystem; + ref_ptr _ShaderManager; + ref_ptr _Simulation; + ref_ptr _ModelManager; + ref_ptr _Camera; + ref_ptr _RenderQueue; - device->setAmbientLight(1.0, 1.0, 1.0); +public: - //ref_ptr rootnode(new SceneNode("root node")); + bool initialize() + { + CfgParser cfg; + cfg.parseFile("options.cfg"); - double _DeltaTime = 0; - double _LastTime = glfwGetTime(); + int width = cfg.get("width", 640); + int height = cfg.get("height", 480); + bool fullscreen = cfg.get("fullscreen", false); - ref_ptr model = modelmanager->loadModel("combat"); - scriptsystem->callFunction("Initialize"); + _Window = new RenderWindow(); + if (_Window->create(width, height, 0, 0, 0, fullscreen) == false) + return false; + _Device = new RenderDevice(_Window); + _FontManager = new FontManager(_Device); + _MeshManager = new MeshManager(_Device); + _TextureManager = new TextureManager(); + _ScriptSystem = new ScriptSystem(); + _ShaderManager = new ShaderManager(_Window); + _Simulation = new RigidBodySimulation(_ScriptSystem); + _ModelManager = new ModelManager (_TextureManager, _ShaderManager, _MeshManager); + _Camera = new Camera(); + _RenderQueue = new RenderQueue(); + + setupScriptSystem_Font(_ScriptSystem, _FontManager); + setupScriptSystem_Image(_ScriptSystem, _TextureManager, _Device); + setupScriptSystem_Math(_ScriptSystem); + setupScriptSystem_RigidBody(_ScriptSystem, _Simulation); + + _ScriptSystem->loadScript("main"); + _ScriptSystem->callFunction("Initialize"); + + _Window->KeySignal.connect(this, &Application::KeySlot); + + _Running = true; + + return true; + } + /* + if (window.valid() && device.valid()) + { + + camera->setFoV(45.0); + camera->setAspectRatio((double)width/(double)height); + camera->setNearPlane(1.0); + camera->setFarPlane(15000.0); + camera->setPosition(Vector3(0.0, 0.0, 20.0)); + + device->setAmbientLight(1.0, 1.0, 1.0); + + //ref_ptr rootnode(new SceneNode("root node")); + + + ref_ptr model = modelmanager->loadModel("combat"); + + } + */ + + void shutdown() + { + _ScriptSystem->callFunction("Shutdown"); + } + + void quit() + { + _Running = false; + } + + void run() + { clog << "--- starting main loop..."<< endlog; - while (window->isOpen()) + _DeltaTime = 0; + _LastTime = glfwGetTime(); + + while (_Window->isOpen() && _Running) { double time = glfwGetTime(); _DeltaTime = time - _LastTime; _LastTime = time; - device->clear(); - device->begin3D(camera); + _Device->clear(); + /* + camera->setRotation(Quaternion(Vector3(0.0, 1.0, 0.0), fmod(time + /5.0, 6.2))); + device->begin3D(camera); + queue->clear(); + queue->addOpaqueItem(model, Vector3(10.0, 0.0, 0.0), Quaternion()); + queue->render(device); - queue->clear(); - queue->addOpaqueItem(model, Vector3(10.0, 0.0, 0.0), Quaternion(Vector3(0.0,1.0,0.0), fmod(time, 3))); - queue->render(device); + device->pushAbsoluteTransformation(Vector3(), camera->getRotation()); - - glMatrixMode ( GL_MODELVIEW ); - glPushMatrix(); - glLoadIdentity(); - glEnable ( GL_BLEND ); - glBlendFunc ( GL_ONE, GL_ONE ); - glEnable ( GL_DEPTH_TEST ); - glDepthMask ( GL_FALSE ); - glDisable ( GL_LIGHTING ); - glColor4d ( 1.0f, 1.0f, 1.0f, 1.0f ); + class RenderState + { + bool _Blending; + GLint _BlendFuncSrc; + GLint _BlendFuncDest; - device->setTexture(0, starTexture, true); - starMesh->render(); - glDisable ( GL_BLEND ); - - + bool _DepthTest; + bool _DepthMask; + + bool _Lighting; + }; + GLfloat mat_specular[] = + { 1.0, 1.0, 1.0, 1.0 }; + GLfloat mat_shininess[] = + { 2.0 }; + glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_specular); + glMaterialfv(GL_FRONT, GL_AMBIENT, mat_specular); + glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); + glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); + + glEnable(GL_BLEND); + glBlendFunc(GL_ONE, GL_ONE); + glEnable(GL_DEPTH_TEST); + glDepthMask(GL_FALSE); + glDisable(GL_LIGHTING); + glColor4d( 1.0f, 1.0f, 1.0f, 1.0f); + + device->setTexture(0, starTexture, true); + starMesh->render(); + + glDisable(GL_BLEND); + device->popTransformation(); + */ // device->useShader (program); // device->setTexture (stage, name, texture) // device->clearTextures (stage+1); @@ -171,19 +227,31 @@ int main(int argc, char **argv) //device-> //model->render(); - simulation->saveStates(); - simulation->updateSteps(_DeltaTime); - while (simulation->step()) + _Simulation->saveStates(); + _Simulation->updateSteps(_DeltaTime); + while (_Simulation->step()) ; - scriptsystem->callFunction("OnFrame", _DeltaTime); - device->swap(); + _ScriptSystem->callFunction("OnFrame", _DeltaTime); + + _Device->swap(); } - scriptsystem->callFunction("Shutdown"); - clog << "--- main loop finished..."<< endlog; } +}; +int main(int argc, char **argv) +{ + initializePhysfs(argv[0]); + + Application app; + + if (app.initialize()) + { + app.run(); + } + app.shutdown(); + shutdownPhysfs(); }