diff --git a/engine/AABB.h b/engine/AABB.h index 2562186..2837a3e 100644 --- a/engine/AABB.h +++ b/engine/AABB.h @@ -27,10 +27,10 @@ namespace BlueCore AABB transformed (const Transformation& transformation) { AABB aabb; - + /* Vector3 new_max = transformation.transform (Max); Vector3 new_min = transformation.transform (Min); - + */ return aabb; } }; diff --git a/engine/Application.cpp b/engine/Application.cpp index 862fd7d..bf47f2e 100644 --- a/engine/Application.cpp +++ b/engine/Application.cpp @@ -9,6 +9,9 @@ #include "ScriptSystem_Image.h" #include "ScriptSystem_Math.h" #include "ScriptSystem_RigidBody.h" +#include "ScriptSystem_SceneGraph.h" +#include "ScriptSystem_SceneNode.h" +#include "ScriptSystem_Model.h" namespace BlueCore { @@ -70,6 +73,9 @@ bool Application::initialize() setupScriptSystem_Image(_ScriptSystem, _TextureManager, _Device); setupScriptSystem_Math(_ScriptSystem); setupScriptSystem_RigidBody(_ScriptSystem, _Simulation); + setupScriptSystem_SceneGraph(_ScriptSystem); + setupScriptSystem_SceneNode(_ScriptSystem); + setupScriptSystem_Model(_ScriptSystem, _ModelManager); if (_ScriptSystem->loadScript("main") == false) return false; @@ -108,6 +114,21 @@ void Application::run() _Paused = false; _Running = true; + ref_ptr star_mesh = _MeshManager->loadMesh ("stars.3ds"); + ref_ptr star_texture = _TextureManager->loadTexture ("stars.png", 0); + + GLfloat lightAmbient[] = {0.0f, 0.0f, 0.0f, 1.0f} ; + GLfloat lightDiffuse[] = {1.0f, 1.0f, 1.0f, 1.0f} ; + GLfloat lightSpecular[] = {1.0f, 1.0f, 1.0f, 1.0f} ; + GLfloat lightPosition[] = { 1.0, 0.0, 0.0, 0.0}; + + glLightfv ( GL_LIGHT0, GL_AMBIENT, lightAmbient ) ; + glLightfv ( GL_LIGHT0, GL_DIFFUSE, lightDiffuse ) ; + glLightfv ( GL_LIGHT0, GL_SPECULAR, lightSpecular ) ; + glLightfv ( GL_LIGHT0, GL_POSITION, lightPosition ); + + glEnable ( GL_LIGHT0 ) ; + while (_Window->isOpen() && _Running) { double now = glfwGetTime(); @@ -122,15 +143,20 @@ void Application::run() //camera->setPosition(q.apply(Vector3(0.0, 0.0, _CameraRadius))); //camera->setRotation(q); //pos += 5 * _DeltaTime; + _Device->setAmbientLight(1.0, 1.0, 1.0); + if (_SceneGraph.valid()) { + _RenderQueue->clear(); + _SceneGraph->update(delta); _SceneGraph->queue(_RenderQueue); + _Device->begin3D(_SceneGraph->getCamera()); _RenderQueue->render(_Device); + _Device->end3D(); } /* { _Device->begin3D(_Camera); - _Device->setAmbientLight(1.0, 1.0, 1.0); _RenderQueue->clear(); _RenderQueue->addOpaqueItem(model, Vector3(0.0, 0.0, 0.0), Quaternion()); @@ -139,41 +165,7 @@ void Application::run() } */ /* - device->pushAbsoluteTransformation(Vector3(), camera->getRotation()); - - class RenderState - { - bool _Blending; - GLint _BlendFuncSrc; - GLint _BlendFuncDest; - - 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); @@ -197,6 +189,40 @@ void Application::run() } } + _Device->pushAbsoluteTransformation(Vector3(), _SceneGraph->getCamera()->getRotation()); +/* + _Device->setShaderProgram (0); + glEnable(GL_DEPTH_TEST); + glDepthMask(GL_FALSE); +*/ + glDisable(GL_BLEND); + glEnable(GL_DEPTH_TEST); + _Device->setTexture(0, star_texture, true); + glDisable(GL_LIGHTING); + glColor4d( 1.0f, 1.0f, 1.0f, 1.0f); + star_mesh->render(); + _Device->popTransformation(); + + /* + class RenderState + { + bool _Blending; + GLint _BlendFuncSrc; + GLint _BlendFuncDest; + + 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); +*/ + _Device->begin2D(); _ScriptSystem->callFunction("OnOverlay", delta, now - _StartTime); _Device->end2D(); diff --git a/engine/FileSystem.h b/engine/FileSystem.h deleted file mode 100644 index bdfd283..0000000 --- a/engine/FileSystem.h +++ /dev/null @@ -1,57 +0,0 @@ -#include "Utilities/Referenced.h" - - -class File : public Referenced -{ -public: - bool isOpen (); - unsigned long read (char *buffer, unsigned long size); -}; - - - -class FileSystem : public Referenced -{ -public: - - FileSystem(); - File *openBinary (const std::string& filename); - -private: - - static bool _PhysfsInialized; -}; - - - -FileSystem::FileSystem() -{ - PHYSFS_init (0); - - std::string appdir = PHYSFS_getUserDir(); - appdir += ".bluecore"; - - if( !PHYSFS_setWriteDir(appdir.c_str()) ) - { - if( (PHYSFS_setWriteDir(PHYSFS_getUserDir())) && (PHYSFS_mkdir(".bluecore")) ) - PHYSFS_setWriteDir( appdir.c_str() ); - } - - PHYSFS_addToSearchPath( appdir.c_str(), 0 ); - PHYSFS_addToSearchPath( "data", 1 ); - - char **rc = PHYSFS_enumerateFiles ( "" ); - - for ( char **i = rc; *i != 0; i++ ) - { - std::string filename ( *i ); - - if ( filename.substr ( filename.size() - 4, 4 ) == ".zip" ) - { - PHYSFS_addToSearchPath ( ( "data/" + filename ).c_str(), 1 ); - BlueCore::log << ">>> Using addon: " << filename << endlog; - } - } - - PHYSFS_freeList ( rc ); -} diff --git a/engine/Makefile b/engine/Makefile index 4d026f0..8c3ddf2 100644 --- a/engine/Makefile +++ b/engine/Makefile @@ -42,8 +42,10 @@ else endif SRC = $(wildcard */*.cpp) $(wildcard *.cpp) +INC = $(wildcard */*.h) $(wildcard *.h) OBJ = $(patsubst %.cpp,%.o,$(SRC) ) DEP = $(patsubst %.cpp,%.d,$(SRC) ) +GCH = $(patsubst %.h,%.h.gch,$(INC) ) NAME = redcore$(EXT) @@ -54,7 +56,7 @@ $(NAME): $(OBJ) @g++ -o $(NAME) $(OBJ) $(LDFLAGS) @echo done -obj: $(OBJ) +obj: $(OBJ) @echo done clean: @@ -67,3 +69,7 @@ clean: -@$(RM) $(NAME) -include $(SRC:%.cpp=%.d) + +%.h.gch: %.h + @echo Compiling $@ ... + @$(CXX) $(CXXFLAGS) -o $@ $< diff --git a/engine/Math/Point.h b/engine/Math/Point.h index 7a54762..02718b7 100644 --- a/engine/Math/Point.h +++ b/engine/Math/Point.h @@ -7,8 +7,6 @@ #ifndef BLUECORE_POINT_H #define BLUECORE_POINT_H -#include "rectangle.h" - namespace BlueCore { template @@ -26,30 +24,9 @@ namespace BlueCore Point2DTemplate( type x, type y ) : _x(x), _y(y) { } - - bool in( const Rectangle2DTemplate &rect ); + }; - #include "rectangle.h" - - template - bool Point2DTemplate::in( const Rectangle2DTemplate &rect ) - { - if( _x < rect._x ) - return false; - - if( _x > (rect._x + rect._width) ) - return false; - - if( _y < rect._y ) - return false; - - if( _y > (rect._y + rect._height) ) - return false; - - return true; - } - typedef Point2DTemplate Point2D; }; diff --git a/engine/Math/Ray.h b/engine/Math/Ray.h index c0011ef..9373b57 100644 --- a/engine/Math/Ray.h +++ b/engine/Math/Ray.h @@ -7,8 +7,8 @@ #ifndef BLUECORE_RAY_H #define BLUECORE_RAY_H -#include "scalar.h" -#include "vector.h" +#include "Scalar.h" +#include "Vector.h" namespace BlueCore { @@ -44,7 +44,7 @@ namespace BlueCore * set the point */ template - inline setPoint( const Vector3Template &point ) + inline void setPoint( const Vector3Template &point ) { this->point = point; } @@ -54,7 +54,7 @@ namespace BlueCore * set the direction */ template - inline setDirection( const Vector3Template &direction ) + inline void setDirection( const Vector3Template &direction ) { this->direction = direction.normalized(); } diff --git a/engine/Math/Rectangle.h b/engine/Math/Rectangle.h index beb66e3..86cf4da 100644 --- a/engine/Math/Rectangle.h +++ b/engine/Math/Rectangle.h @@ -7,7 +7,7 @@ #ifndef BLUECORE_RECTANGLE_H #define BLUECORE_RECTANGLE_H -#include "point.h" +#include "Point.h" namespace BlueCore { diff --git a/engine/MeshManager.cpp b/engine/MeshManager.cpp index 1a5c6f2..e0229ca 100644 --- a/engine/MeshManager.cpp +++ b/engine/MeshManager.cpp @@ -14,19 +14,16 @@ using namespace std; namespace BlueCore { -//------------------------------------------------------------------------------ Mesh::Mesh(RenderDevice* device) : _Device(device) { } -//------------------------------------------------------------------------------ Mesh::~Mesh() { clog << ">>> Mesh destructed ..."<< endline; } -//------------------------------------------------------------------------------ void Mesh::render() { @@ -100,20 +97,17 @@ void Mesh::bind() } #endif -//------------------------------------------------------------------------------ MeshManager::MeshManager(RenderDevice* device) : _Device(device) { BlueCore::clog << ">>> MeshManager constructed..." << endlog; } -//------------------------------------------------------------------------------ MeshManager::~MeshManager() { clog << ">>> MeshManager destructed ..." << endlog; } -//------------------------------------------------------------------------------ bool loadTrimesh(const char* filename, Mesh *mesh) { PHYSFS_file* file = PHYSFS_openRead(filename); @@ -193,13 +187,92 @@ bool loadTrimesh(const char* filename, Mesh *mesh) PHYSFS_close(file); + // create normlas, tangents, and binomials + clog << " create normlas..." << endline; + + for (unsigned int i = 0; i < mesh->VertexBuffer.count(); i++) + { + mesh->VertexBuffer[i].normal.zero(); + } + + for (unsigned int i = 0; i < mesh->IndexBuffer.count(); i++) + { + const TriangleIndices& triangle = mesh->IndexBuffer[i]; + + Vector3Template ab = mesh->VertexBuffer[ triangle.a ].point + - mesh->VertexBuffer[ triangle.b ].point; + Vector3Template ac = mesh->VertexBuffer[ triangle.a ].point + - mesh->VertexBuffer[ triangle.c ].point; + Vector3Template n = ab.cross(ac); + + mesh->VertexBuffer[ triangle.a ].normal += n; + mesh->VertexBuffer[ triangle.a ].normal += n; + mesh->VertexBuffer[ triangle.a ].normal += n; + } + + for (unsigned int i = 0; i < mesh->VertexBuffer.count(); i ++) + { + mesh->VertexBuffer[i].normal.normalize(); + } + + clog << " create tangents..." << endline; + + mesh->TangentBuffer.create(mesh->VertexBuffer.count()); + mesh->BitangentBuffer.create(mesh->VertexBuffer.count()); + + for (unsigned int i = 0; i < mesh->TangentBuffer.count(); i ++) + { + mesh->TangentBuffer[i].zero(); + mesh->BitangentBuffer[i].zero(); + } + + for (unsigned int i = 0; i < mesh->IndexBuffer.count(); i ++) + { + const TriangleIndices& triangle = mesh->IndexBuffer[i]; + + const Vertex& v1 = mesh->VertexBuffer[triangle.a]; + const Vertex& v2 = mesh->VertexBuffer[triangle.b]; + const Vertex& v3 = mesh->VertexBuffer[triangle.c]; + + float x1 = v2.point.x - v1.point.x; + float x2 = v3.point.x - v1.point.x; + float y1 = v2.point.y - v1.point.y; + float y2 = v3.point.y - v1.point.y; + float z1 = v2.point.z - v1.point.z; + float z2 = v3.point.z - v1.point.z; + float s1 = v2.u - v1.u; + float s2 = v3.u - v1.u; + float t1 = v2.v - v1.v; + float t2 = v3.v - v1.v; + float r = 1.0f / (s1 * t2 - s2 * t1 ); + + Vector3Float sdir( (t2 * x1 - t1 * x2 ) * r, (t2 * y1 - t1 * y2 ) + * r, (t2 * z1 - t1 * z2 ) * r); + + Vector3Float tdir( (s1 * x2 - s2 * x1 ) * r, (s1 * y2 - s2 * y1 ) + * r, (s1 * z2 - s2 * z1 ) * r); + + mesh->TangentBuffer[triangle.a] += sdir; + mesh->TangentBuffer[triangle.b] += sdir; + mesh->TangentBuffer[triangle.c] += sdir; + + mesh->BitangentBuffer[triangle.a] += tdir; + mesh->BitangentBuffer[triangle.b] += tdir; + mesh->BitangentBuffer[triangle.c] += tdir; + } + + for (unsigned int i = 0; i < mesh->TangentBuffer.count(); i ++) + { + mesh->TangentBuffer[i].normalize(); + mesh->BitangentBuffer[i].normalize(); + } + return 0; } return 1; } -//------------------------------------------------------------------------------ Mesh *MeshManager::loadMesh(const string &filename) { // check if this mesh is already loaded @@ -310,7 +383,6 @@ Mesh *MeshManager::loadMesh(const string &filename) return mesh; } -//------------------------------------------------------------------------------ bool MeshManager::loadFromCache(Mesh *mesh, const std::string &name) { std::string filename = name + ".msc"; @@ -331,25 +403,28 @@ bool MeshManager::loadFromCache(Mesh *mesh, const std::string &name) PHYSFS_read(file, &size, sizeof (size ), 1); mesh->IndexBuffer.create(size); PHYSFS_read(file, mesh->IndexBuffer.data(), mesh->IndexBuffer.size(), 1); -#if 0 - // read tangent buffer - PHYSFS_read ( file, &size, sizeof ( size ), 1 ); - mesh->tangent_buffer.create ( size ); - PHYSFS_read ( file, mesh->tangent_buffer.data(), mesh->tangent_buffer.size(), 1 ); - - // read bitangent buffer - PHYSFS_read ( file, &size, sizeof ( size ), 1 ); - mesh->bitangent_buffer.create ( size ); - PHYSFS_read ( file, mesh->bitangent_buffer.data(), mesh->bitangent_buffer.size(), 1 ); - - // read bounding sphere - PHYSFS_read ( file, &mesh->bounding_sphere, sizeof ( mesh->bounding_sphere ), 1 ); -#endif // read subsets PHYSFS_read(file, &size, sizeof (size ), 1); mesh->SubsetBuffer.create(size); PHYSFS_read(file, mesh->SubsetBuffer.data(), mesh->SubsetBuffer.size(), 1); + + // read tangent buffer + PHYSFS_read(file, &size, sizeof (size ), 1); + mesh->TangentBuffer.create(size); + PHYSFS_read(file, mesh->TangentBuffer.data(), mesh->TangentBuffer.size(), 1); + + // read bitangent buffer + PHYSFS_read(file, &size, sizeof (size ), 1); + mesh->BitangentBuffer.create(size); + PHYSFS_read(file, mesh->BitangentBuffer.data(), + mesh->BitangentBuffer.size(), 1); + +#if 0 + // read bounding sphere + PHYSFS_read ( file, &mesh->bounding_sphere, sizeof ( mesh->bounding_sphere ), 1 ); +#endif + #if 0 // read bitangent buffer PHYSFS_read ( file, &size, sizeof ( size ), 1 ); @@ -362,9 +437,7 @@ bool MeshManager::loadFromCache(Mesh *mesh, const std::string &name) return true; } -//------------------------------------------------------------------------------ bool MeshManager::saveToCache(Mesh *mesh, const std::string &name) - { std::string filename = name + ".msc"; @@ -391,17 +464,19 @@ bool MeshManager::saveToCache(Mesh *mesh, const std::string &name) PHYSFS_write(file, &size, sizeof (size ), 1); PHYSFS_write(file, mesh->SubsetBuffer.data(), mesh->SubsetBuffer.size(), 1); -#if 0 // write tangent_buffer - size = mesh->tangent_buffer.count(); - PHYSFS_write ( file, &size, sizeof ( size ), 1 ); - PHYSFS_write ( file, mesh->tangent_buffer.data(), mesh->tangent_buffer.size(), 1 ); + size = mesh->TangentBuffer.count(); + PHYSFS_write(file, &size, sizeof (size ), 1); + PHYSFS_write(file, mesh->TangentBuffer.data(), mesh->TangentBuffer.size(), + 1); // write bitangent_buffer - size = mesh->bitangent_buffer.count(); - PHYSFS_write ( file, &size, sizeof ( size ), 1 ); - PHYSFS_write ( file, mesh->bitangent_buffer.data(), mesh->bitangent_buffer.size(), 1 ); + size = mesh->BitangentBuffer.count(); + PHYSFS_write(file, &size, sizeof (size ), 1); + PHYSFS_write(file, mesh->BitangentBuffer.data(), + mesh->BitangentBuffer.size(), 1); +#if 0 // write bounding sphere PHYSFS_write ( file, &mesh->bounding_sphere, sizeof ( mesh->bounding_sphere ), 1 ); diff --git a/engine/MeshManager.h b/engine/MeshManager.h index b77491e..24a3a0c 100644 --- a/engine/MeshManager.h +++ b/engine/MeshManager.h @@ -49,8 +49,8 @@ public: Buffer IndexBuffer; Buffer SubsetBuffer; - Buffer tangents; - Buffer bitangents; + Buffer TangentBuffer; + Buffer BitangentBuffer; Buffer shadowfaces; public: diff --git a/engine/ModelManager.cpp b/engine/ModelManager.cpp index 8715fa2..8952ca9 100644 --- a/engine/ModelManager.cpp +++ b/engine/ModelManager.cpp @@ -19,65 +19,57 @@ namespace BlueCore //------------------------------------------------------------------------------ void Model::render(RenderDevice *device) const { - ModelMesh->render(); -/* +// ModelMesh->render(); + glEnable(GL_LIGHTING); glDepthFunc ( GL_LEQUAL ); glEnable ( GL_DEPTH_TEST ); glDepthMask ( GL_TRUE ); -*/ - /* + glEnableClientState (GL_VERTEX_ARRAY ); glEnableClientState (GL_TEXTURE_COORD_ARRAY ); glEnableClientState (GL_NORMAL_ARRAY ); - mesh->vertex_buffer.bind(); - mesh->index_buffer.bind(); - */ -/* - glMaterialfv (GL_FRONT, GL_SHININESS, &pass.Shininess); - glMaterialfv (GL_FRONT, GL_SPECULAR, ( GLfloat * ) &pass.Specular ); + glMaterialfv (GL_FRONT, GL_SHININESS, &LitPass.Shininess); + glMaterialfv (GL_FRONT, GL_SPECULAR, ( GLfloat * ) &LitPass.Specular ); // setup shader - if (pass.Program && ShaderManager::getSingleton()->usingShaders() ) + if (LitPass.Program) { - ShaderManager::getSingleton()->useShaderProgram(pass.Program); + device->setShaderProgram(LitPass.Program); - int textureId = 0; - std::vector::iterator titer; + int textureId = 0; + + std::vector::const_iterator titer; + for (titer = LitPass.Textures.begin(); titer != LitPass.Textures.end(); titer++) + { + device->setTexture(textureId, ( *titer ).mTexture ); + device->setTexture(LitPass.Program, textureId, ( *titer ).mName); + textureId++; + } - for (titer = pass.Textures.begin(); titer != pass.Textures.end(); titer++) - { - Renderer::getSingleton()->bindTexture(( *titer ).mTexture, textureId ); - ShaderManager::getSingleton()->useTexture(pass.Program, textureId, ( *titer ).mName); - textureId++; - } + if (LitPass.Tangents) + { + //device->setTangentBuffer(LitPass.Program, &ModelMesh->TangentBuffer, "tangent"); + //device->setTangentBuffer(LitPass.Program, &ModelMesh->BitangentBuffer, "bitangent"); + } + - if (pass.Tangents) - { - ShaderManager::getSingleton()->useTangentBuffer(pass.Program, &mesh->tangent_buffer, "tangent"); - ShaderManager::getSingleton()->useTangentBuffer(pass.Program, &mesh->bitangent_buffer, - "bitangent"); - } } +/* else { - glActiveTextureARB (GL_TEXTURE2_ARB ); - glDisable (GL_TEXTURE_2D ); - glActiveTextureARB (GL_TEXTURE1_ARB ); - glDisable (GL_TEXTURE_2D ); - int textureId = 0; + int textureId = 0; - std::vector::iterator titer; - for (titer = pass.Textures.begin(); titer != pass.Textures.end(); titer++) - { - Renderer::getSingleton()->bindTexture(( *titer ).mTexture, textureId ); + std::vector::iterator titer; + for (titer = pass.Textures.begin(); titer != pass.Textures.end(); titer++) + { + Renderer::getSingleton()->bindTexture(( *titer ).mTexture, textureId ); ShaderManager::getSingleton()->useTexture(pass.Program, textureId, ( *titer ).mName); textureId++; break; } } - // render subsets SubsetVector::iterator iter; for (iter = mesh->surface_subsets.begin(); iter @@ -86,7 +78,20 @@ void Model::render(RenderDevice *device) const Subset subset = *iter; mesh->index_buffer.draw(subset.first, subset.count); } +*/ + ModelMesh->render(); + + device->setShaderProgram(0); + + glActiveTextureARB (GL_TEXTURE2_ARB ); + glDisable (GL_TEXTURE_2D ); + glActiveTextureARB (GL_TEXTURE1_ARB ); + glDisable (GL_TEXTURE_2D ); + glActiveTextureARB (GL_TEXTURE0_ARB ); + glDisable (GL_TEXTURE_2D ); + + /* // cleanup if (pass.Program && ShaderManager::getSingleton()->usingShaders() ) { @@ -98,6 +103,7 @@ void Model::render(RenderDevice *device) const } } + glPopMatrix(); glActiveTextureARB (GL_TEXTURE2_ARB ); @@ -120,11 +126,13 @@ ModelManager::ModelManager(TextureManager *texturemanager, _TextureManager(texturemanager), _ShaderManager(shadermanager), _MeshManager(meshmanager) { + clog << ">>> ModelManager constructed ..."<< endline; } //------------------------------------------------------------------------------ ModelManager::~ModelManager() { + clog << ">>> ModelManager destructed ..."<< endline; } //------------------------------------------------------------------------------ diff --git a/engine/ModelManager.h b/engine/ModelManager.h index a596aee..1005d14 100644 --- a/engine/ModelManager.h +++ b/engine/ModelManager.h @@ -66,7 +66,7 @@ private: const TiXmlElement* DefinitionElement); void unload(); - + public: ModelManager(TextureManager *texturemanager, ShaderManager* shadermanager, diff --git a/engine/Node.h b/engine/Node.h deleted file mode 100644 index 52512f4..0000000 --- a/engine/Node.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef NODE_H_ -#define NODE_H_ - -class Node -{ - class UpdateListener - { - virtual void update() = 0; - virtual ~UpdateListener() = 0; - }; - typedef std::list NodeContainer; - typedef std::list NodeContainer; - - Node *parent; - NodeContainer children; - - Vector3 getRelativePosition; - Vector3 getAbsolutePosition; - /* ... */ - - bool _Updating; - void update (double time); - void render (Camera *camera, RenderDevice *device); -}; - -#endif /*NODE_H_*/ diff --git a/engine/RenderDevice.cpp b/engine/RenderDevice.cpp index 1380633..f7a056d 100644 --- a/engine/RenderDevice.cpp +++ b/engine/RenderDevice.cpp @@ -190,4 +190,51 @@ void RenderDevice::setTexture(unsigned int unit, Texture* texture, bool last) } } +void RenderDevice::setShaderProgram(ShaderProgram shader_program) +{ + // if ( !_usingShaders) + // return; + + glUseProgramObjectARB(shader_program); +} + +void RenderDevice::setTexture(ShaderProgram shader_program, + unsigned int texture, const std::string &name) +{ + // if ( !_usingShaders) + // return; + + GLint location = glGetUniformLocationARB(shader_program, name.c_str() ); + + //if( location > 0 ) + glUniform1iARB(location, texture); +} +#if 0 +void RenderDevice::setTangentBuffer(ShaderProgram shader_program, + const Buffer& tangent_buffer, const std::string &name) +{ +// if ( !_usingShaders || !tangent_buffer) +// return; + + GLuint location = glGetAttribLocationARB(shader_program, name.c_str() ); + + glEnableVertexAttribArrayARB(location); + + //tangent_buffer->bind(); + + glVertexAttribPointerARB(location, 3, GL_FLOAT, 0, 0, 0); +} + +//------------------------------------------------------------------------------ +void ShaderManager::disableTangentBuffer(ShaderProgram shader_program, + const std::string &name) +{ +// if ( !_usingShaders) +// return; + + GLuint location = glGetAttribLocationARB(shader_program, name.c_str() ); + + glDisableVertexAttribArrayARB(location); +} +#endif } // namespace BlueCore diff --git a/engine/RenderDevice.h b/engine/RenderDevice.h index 3754e3f..37898b6 100644 --- a/engine/RenderDevice.h +++ b/engine/RenderDevice.h @@ -21,9 +21,9 @@ private: ref_ptr _RenderWindow; ref_ptr _Camera; - + unsigned int _ActiveTextures; - + public: RenderDevice(RenderWindow* renderWindow); @@ -45,15 +45,18 @@ 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 setShaderProgram(ShaderProgram shader_program); + void setTexture(ShaderProgram shader_program, unsigned int texture, + const std::string &name); + + void pushAbsoluteTransformation(const Vector3&, const Quaternion&); + void pushTransformation(const Vector3&, const Quaternion&); + void popTransformation(); void setupProjectionMatrix(); void setupViewMatrix(); sigslot::signal0<> DeviceShutdownSignal; - //Buffer &_ShadowFaces; }; diff --git a/engine/SceneGraph.cpp b/engine/SceneGraph.cpp index 0b99715..5e030ed 100644 --- a/engine/SceneGraph.cpp +++ b/engine/SceneGraph.cpp @@ -5,7 +5,7 @@ namespace BlueCore SceneGraph::SceneGraph() { - _RootNode = new SceneNode(); + _RootNode = new SceneNode("root node"); } SceneGraph::~SceneGraph() { @@ -22,6 +22,11 @@ void SceneGraph::setCamera(Camera *camera) _Camera = camera; } +Camera* SceneGraph::getCamera()const +{ + return _Camera.pointer(); +} + void SceneGraph::update(Scalar time) { _RootNode->update (time); diff --git a/engine/SceneGraph.h b/engine/SceneGraph.h index 0bc8d46..70e8b5e 100644 --- a/engine/SceneGraph.h +++ b/engine/SceneGraph.h @@ -18,7 +18,8 @@ public: SceneNode* getRootNode(); void setCamera(Camera *camera); - + Camera* getCamera() const; + void update(Scalar time); void queue (RenderQueue *queue); diff --git a/engine/SceneNode.cpp b/engine/SceneNode.cpp index d1fa48f..e9e4805 100644 --- a/engine/SceneNode.cpp +++ b/engine/SceneNode.cpp @@ -107,8 +107,11 @@ void SceneNode::queue(RenderQueue *queue, Camera *camera) { if (isActive()) { - SceneNodeList::iterator i; + if (_Model.valid()) + queue->addOpaqueItem(_Model, _AbsoluteTransformation.translation, + _AbsoluteTransformation.rotation); + SceneNodeList::iterator i; for (i = _Children.begin(); i != _Children.end(); i++) { ( *i )->queue(queue, camera); @@ -138,13 +141,33 @@ void SceneNode::setRelativeRotation(const Quaternion &rotation) void SceneNode::updateAbsoluteTransformation() { - /* - if (_Parent ) - _AbsoluteTranslation = _Parent->getAbsoluteTranslation() - +_Parent->getAbsoluteRotation().inversed() * _RelativeTranslation; - else - _AbsoluteTranslation = _RelativeTranslation; - */ + if (_Parent) + { + _AbsoluteTransformation.translation = _Parent->getAbsoluteTransformation().translation + +_Parent->getAbsoluteTransformation().rotation.inversed().apply(_RelativeTransformation.translation); + } + else + _AbsoluteTransformation.translation = _RelativeTransformation.translation; +} + +void SceneNode::setRigidBody(RigidBody* body) +{ + _RigidBody = body; +} + +RigidBody* SceneNode::getRigidBody() +{ + return _RigidBody.pointer(); +} + +void SceneNode::setModel(Model* model) +{ + _Model = model; +} + +Model* SceneNode::getModel() +{ + return _Model.pointer(); } } // namespace BlueCore diff --git a/engine/SceneNode.h b/engine/SceneNode.h index 2a78f6b..933703f 100644 --- a/engine/SceneNode.h +++ b/engine/SceneNode.h @@ -32,8 +32,8 @@ class SceneNode : public Referenced, public Named, public Activated Transformation _RelativeTransformation; Transformation _AbsoluteTransformation; - weak_ptr _RigidBody; - weak_ptr _Model; + ref_ptr _RigidBody; + ref_ptr _Model; public: @@ -44,6 +44,12 @@ class SceneNode : public Referenced, public Named, public Activated SceneNode *getParent() const; const SceneNodeList &getChildren() const; + void setRigidBody (RigidBody* body); + RigidBody* getRigidBody (); + + void setModel (Model* body); + Model* getModel (); + void attach(SceneNode *node); void detach(SceneNode *node); void detachAll(); diff --git a/engine/ScriptSystem.cpp b/engine/ScriptSystem.cpp index 64b21ad..e98de94 100644 --- a/engine/ScriptSystem.cpp +++ b/engine/ScriptSystem.cpp @@ -15,10 +15,9 @@ using namespace std; namespace BlueCore { -//-------------------------------------------------------------------------- + static ScriptSystem* gScriptSystem = 0; -//-------------------------------------------------------------------------- void _sq_compiler_error_handler(HSQUIRRELVM vm, const SQChar *error, const SQChar *source, SQInteger line, SQInteger column) { @@ -26,7 +25,6 @@ void _sq_compiler_error_handler(HSQUIRRELVM vm, const SQChar *error, << error << endlog; } -//-------------------------------------------------------------------------- static SQInteger _sq_runtime_error_handler(HSQUIRRELVM vm) { const SQChar *error = 0; @@ -46,7 +44,6 @@ static SQInteger _sq_runtime_error_handler(HSQUIRRELVM vm) return 0; } -//-------------------------------------------------------------------------- void _sq_print(HSQUIRRELVM vm, const SQChar* s, ...) { va_list vl; @@ -55,7 +52,6 @@ void _sq_print(HSQUIRRELVM vm, const SQChar* s, ...) va_end ( vl ); } -//-------------------------------------------------------------------------- static SQInteger _sq_require(HSQUIRRELVM v) { const SQChar *scriptname = 0; @@ -69,7 +65,6 @@ static SQInteger _sq_require(HSQUIRRELVM v) return 0; } -//-------------------------------------------------------------------------- SQInteger _sq_file_reader(SQUserPointer file) { char c; @@ -82,7 +77,6 @@ SQInteger _sq_file_reader(SQUserPointer file) return 0; } -//-------------------------------------------------------------------------- ScriptSystem::ScriptSystem() { gScriptSystem = this; @@ -103,20 +97,17 @@ ScriptSystem::ScriptSystem() clog << ">>> ScriptSystem constructed ..."<< endlog; } -//-------------------------------------------------------------------------- ScriptSystem::~ScriptSystem() { sq_close(_VM); clog << ">>> ScriptSystem destructed ..."<< endlog; } -//-------------------------------------------------------------------------- HSQUIRRELVM ScriptSystem::getVM() { return _VM; } -//-------------------------------------------------------------------------- bool ScriptSystem::loadScript(const std::string &name) { for (unsigned int i = 0; i < _LoadedScripts.size(); i++) @@ -136,7 +127,6 @@ bool ScriptSystem::loadScript(const std::string &name) return false; } -//-------------------------------------------------------------------------- bool ScriptSystem::executeScript(const std::string &name) { string filename = name + ".nut"; @@ -172,7 +162,6 @@ bool ScriptSystem::executeScript(const std::string &name) return true; } -//-------------------------------------------------------------------------- void ScriptSystem::callFunction(const std::string &name) { sq_pushroottable(_VM); @@ -186,7 +175,6 @@ void ScriptSystem::callFunction(const std::string &name) sq_pop(_VM, 1); } -//-------------------------------------------------------------------------- void ScriptSystem::callFunction(const std::string &name, double value) { sq_pushroottable(_VM); @@ -201,7 +189,8 @@ void ScriptSystem::callFunction(const std::string &name, double value) sq_pop(_VM, 1); } -void ScriptSystem::callFunction(const std::string &name, double value1, double value2) +void ScriptSystem::callFunction(const std::string &name, double value1, + double value2) { sq_pushroottable(_VM); sq_pushstring(_VM, name.c_str(), -1); diff --git a/engine/ScriptSystem_Model.cpp b/engine/ScriptSystem_Model.cpp new file mode 100644 index 0000000..e6afc36 --- /dev/null +++ b/engine/ScriptSystem_Model.cpp @@ -0,0 +1,75 @@ +#include "ScriptSystem_Model.h" + +namespace BlueCore +{ + +//------------------------------------------------------------------------------ +static weak_ptr gModelManager; + +//------------------------------------------------------------------------------ +static SQInteger _model_releasehook(SQUserPointer p, SQInteger size) +{ + Model *model = (Model *)p; + + if (model) + model->removeReference(); + + return 1; +} + +//------------------------------------------------------------------------------ +static SQInteger _model_constructor(HSQUIRRELVM vm) +{ + SQInteger argc = sq_gettop(vm); + + Model *model = 0; + + if (argc > 1) + { + const SQChar *name = 0; + + sq_getstring(vm, 2, &name); + + if (gModelManager.valid()) + { + model = gModelManager->loadModel(name); + if (model) + model->addReference(); + } + } + + sq_setinstanceup(vm, 1, (void *)model ); + sq_setreleasehook(vm, 1, _model_releasehook); + + return 0; +} + +//------------------------------------------------------------------------------ +void setupScriptSystem_Model(ScriptSystem* scriptsystem, ModelManager* modelmanager) +{ + if (scriptsystem && modelmanager) + { + HSQUIRRELVM vm = scriptsystem->getVM(); + + gModelManager = modelmanager; + + sq_pushroottable(vm); + + // push class + sq_pushstring(vm, "Model", -1); + + if (SQ_SUCCEEDED(sq_newclass(vm, SQFalse) ) ) + { + // register constructor + sq_pushstring(vm, "constructor", -1); + sq_newclosure(vm, _model_constructor, 0); + sq_newslot(vm, -3, false); + + // create class + sq_newslot(vm, -3, false); + } + + sq_poptop(vm); + } +} +} diff --git a/engine/ScriptSystem_Model.h b/engine/ScriptSystem_Model.h new file mode 100644 index 0000000..4c84837 --- /dev/null +++ b/engine/ScriptSystem_Model.h @@ -0,0 +1,14 @@ +#ifndef BLUECORE_SCRIPTING_MODEL_H +#define BLUECORE_SCRIPTING_MODEL_H + +#include "ModelManager.h" +#include "ScriptSystem.h" + +#include "squirrel.h" + +namespace BlueCore +{ + void setupScriptSystem_Model(ScriptSystem* scriptsystem, ModelManager* modelmanager); +} + +#endif diff --git a/engine/ScriptSystem_SceneGraph.cpp b/engine/ScriptSystem_SceneGraph.cpp index 29c9e81..f88e52d 100644 --- a/engine/ScriptSystem_SceneGraph.cpp +++ b/engine/ScriptSystem_SceneGraph.cpp @@ -48,6 +48,22 @@ static SQInteger _set_camera(HSQUIRRELVM v) return 0; } +static SQInteger _attach_node(HSQUIRRELVM v) +{ + SceneGraph *scenegraph = 0; + + sq_getinstanceup(v, 1, ( void ** ) &scenegraph, 0); + + if (scenegraph) + { + SceneNode *node; + sq_getinstanceup (v, 2, ( void ** ) &node, 0); + scenegraph->getRootNode()->attach(node); + } + + return 0; +} + void setupScriptSystem_SceneGraph(ScriptSystem* scriptsystem) { if (scriptsystem == 0) @@ -71,6 +87,10 @@ void setupScriptSystem_SceneGraph(ScriptSystem* scriptsystem) sq_newclosure(vm, _set_camera, 0); sq_newslot(vm, -3, false); + sq_pushstring(vm, "attachNode", -1); + sq_newclosure(vm, _attach_node, 0); + sq_newslot(vm, -3, false); + // create class sq_newslot(vm, -3, false); } diff --git a/engine/ScriptSystem_SceneGraph.h b/engine/ScriptSystem_SceneGraph.h index 59c0c93..5af5071 100644 --- a/engine/ScriptSystem_SceneGraph.h +++ b/engine/ScriptSystem_SceneGraph.h @@ -1,5 +1,5 @@ -#ifndef BLUECORE_SCRIPTING_CAMERA_H -#define BLUECORE_SCRIPTING_CAMERA_H +#ifndef BLUECORE_SCRIPTING_SCENEGRAPH_H +#define BLUECORE_SCRIPTING_SCENEGRAPH_H #include "ScriptSystem.h" diff --git a/engine/ScriptSystem_SceneNode.cpp b/engine/ScriptSystem_SceneNode.cpp new file mode 100644 index 0000000..771f8c6 --- /dev/null +++ b/engine/ScriptSystem_SceneNode.cpp @@ -0,0 +1,119 @@ +#include "ScriptSystem_SceneNode.h" +#include "ScriptSystem_Math.h" + +#include "SceneNode.h" + +namespace BlueCore +{ + +static SQInteger _scenenode_releasehook(SQUserPointer p, SQInteger size) +{ + SceneNode *node = (SceneNode *)p; + + if (node) + node->removeReference(); + + return 1; +} + +static SQInteger _scenenode_constructor(HSQUIRRELVM vm) +{ + SQInteger argc = sq_gettop(vm); + + SceneNode *node = 0; + + if (argc > 1) + { + const SQChar *name = 0; + + sq_getstring(vm, 2, &name); + + node = new SceneNode (name); + if (node) + node->addReference(); + } + + sq_setinstanceup(vm, 1, (void *)node ); + sq_setreleasehook(vm, 1, _scenenode_releasehook); + + return 0; +} + +static SQInteger _set_model(HSQUIRRELVM v) +{ + SceneNode *node = 0; + + sq_getinstanceup(v, 1, ( void ** ) &node, 0); + + if (node) + { + Model *model; + sq_getinstanceup(v, 2, ( void ** ) &model, 0); + node->setModel(model); + } + + return 0; +} + +static SQInteger _set_position(HSQUIRRELVM v) +{ + SceneNode *node = 0; + + sq_getinstanceup(v, 1, ( void ** ) &node, 0); + + if (node) + { + Vector3 position; + int argc = sq_gettop(v); + + if (argc == 2) + { + _getvectorvalues(v, 2, position.x, position.y, position.z); + } + else if (argc == 4) + { + sq_getfloat(v, 2, &position.x); + sq_getfloat(v, 3, &position.y); + sq_getfloat(v, 4, &position.z); + } + + node->setRelativeTranslation(position); + } + + return 0; +} + +void setupScriptSystem_SceneNode(ScriptSystem* scriptsystem) +{ + if (scriptsystem) + { + HSQUIRRELVM vm = scriptsystem->getVM(); + + sq_pushroottable(vm); + + // push class + sq_pushstring(vm, "SceneNode", -1); + + if (SQ_SUCCEEDED(sq_newclass(vm, SQFalse) ) ) + { + // register constructor + sq_pushstring(vm, "constructor", -1); + sq_newclosure(vm, _scenenode_constructor, 0); + sq_newslot(vm, -3, false); + + sq_pushstring(vm, "setModel", -1); + sq_newclosure(vm, _set_model, 0); + sq_newslot(vm, -3, false); + + sq_pushstring(vm, "setPosition", -1); + sq_newclosure(vm, _set_position, 0); + sq_newslot(vm, -3, false); + + // create class + sq_newslot(vm, -3, false); + } + + sq_poptop(vm); + } +} +} diff --git a/engine/ScriptSystem_SceneNode.h b/engine/ScriptSystem_SceneNode.h new file mode 100644 index 0000000..6212b35 --- /dev/null +++ b/engine/ScriptSystem_SceneNode.h @@ -0,0 +1,13 @@ +#ifndef BLUECORE_SCRIPTING_SCENE_NODE_H +#define BLUECORE_SCRIPTING_SCENE_NODE_H + +#include "ScriptSystem.h" + +#include "squirrel.h" + +namespace BlueCore +{ + void setupScriptSystem_SceneNode(ScriptSystem* scriptsystem); +} + +#endif diff --git a/engine/ShaderManager.cpp b/engine/ShaderManager.cpp index 53d80eb..c2561cc 100644 --- a/engine/ShaderManager.cpp +++ b/engine/ShaderManager.cpp @@ -14,7 +14,7 @@ using namespace std; namespace BlueCore { -//------------------------------------------------------------------------------ + ShaderManager::ShaderManager(RenderWindow* renderwindow) : _usingShaders(true), _RenderWindow(renderwindow) { @@ -33,13 +33,11 @@ ShaderManager::ShaderManager(RenderWindow* renderwindow) : clog << " shaders not supported!"<< endlog; } -//------------------------------------------------------------------------------ ShaderManager::~ShaderManager() { clog << ">>> ShaderManager destructed ..."<< endlog; } -//------------------------------------------------------------------------------ void ShaderManager::shutdown() { clog << ">>> ShaderManager shutting down ..."<< endlog; @@ -66,19 +64,16 @@ void ShaderManager::shutdown() fragment_shader.clear(); } -//------------------------------------------------------------------------------ void ShaderManager::WindowCloseSlot() { shutdown(); } -//------------------------------------------------------------------------------ bool ShaderManager::usingShaders() { return _usingShaders; } -//------------------------------------------------------------------------------ void printInfoLog(GLhandleARB obj) { int infologLength = 0; @@ -100,7 +95,6 @@ void printInfoLog(GLhandleARB obj) } } -//------------------------------------------------------------------------------ VertexShader ShaderManager::loadVertexShader(const string &name) { if ( !_usingShaders) @@ -116,9 +110,9 @@ VertexShader ShaderManager::loadVertexShader(const string &name) return result->second; } - string filename = name + ".vert"; + //string filename = name + ".vert"; - PHYSFS_file *file = PHYSFS_openRead(filename.c_str() ); + PHYSFS_file *file = PHYSFS_openRead(name.c_str() ); if ( !file) { @@ -148,7 +142,6 @@ VertexShader ShaderManager::loadVertexShader(const string &name) return shader; } -//------------------------------------------------------------------------------ FragmentShader ShaderManager::loadFragmentShader(const string &name) { if ( !_usingShaders) @@ -164,9 +157,9 @@ FragmentShader ShaderManager::loadFragmentShader(const string &name) return result->second; } - string filename = name + ".frag"; + //string filename = name + ".frag"; - PHYSFS_file *file = PHYSFS_openRead(filename.c_str() ); + PHYSFS_file *file = PHYSFS_openRead(name.c_str() ); if ( !file) { @@ -198,7 +191,6 @@ FragmentShader ShaderManager::loadFragmentShader(const string &name) return shader; } -//------------------------------------------------------------------------------ ShaderProgram ShaderManager::loadShaderProgram(const std::string &name) { if ( !_usingShaders) @@ -276,7 +268,6 @@ ShaderProgram ShaderManager::loadShaderProgram(const std::string &name) } -//------------------------------------------------------------------------------ void ShaderManager::useShaderProgram(ShaderProgram shader_program) { if ( !_usingShaders) @@ -285,7 +276,6 @@ void ShaderManager::useShaderProgram(ShaderProgram shader_program) glUseProgramObjectARB(shader_program); } -//------------------------------------------------------------------------------ void ShaderManager::useTexture(ShaderProgram shader_program, unsigned int texture, const std::string &name) { @@ -298,7 +288,7 @@ void ShaderManager::useTexture(ShaderProgram shader_program, glUniform1iARB(location, texture); } /* - //------------------------------------------------------------------------------ + void ShaderManager::useTangentBuffer ( ShaderProgram shader_program, TangentBuffer *tangent_buffer, @@ -316,7 +306,7 @@ void ShaderManager::useTexture(ShaderProgram shader_program, glVertexAttribPointerARB ( location, 3, GL_FLOAT, 0, 0, 0 ); } - //------------------------------------------------------------------------------ + void ShaderManager::disableTangentBuffer ( ShaderProgram shader_program, const std::string &name ) diff --git a/engine/Utilities/IniParser.h b/engine/Utilities/IniParser.h deleted file mode 100644 index 5fa21cb..0000000 --- a/engine/Utilities/IniParser.h +++ /dev/null @@ -1,11 +0,0 @@ -#include -#include - -class IniParser -{ - std::map _Pairs; - - void parse (const std::string &text) - { - } -} diff --git a/engine/data/main.nut b/engine/data/main.nut index efebbec..d914312 100644 --- a/engine/data/main.nut +++ b/engine/data/main.nut @@ -81,8 +81,15 @@ function Initialize() ::body = RigidBody(); ::camera = Camera(); ::mainSceneGraph = SceneGraph(); - ::camera.lookAt (0.0, 0.0, 0.0); + // ::camera.lookAt (0.0, 0.0, 0.0); ::mainSceneGraph.setCamera (::camera); + setSceneGraph(::mainSceneGraph); + local model = Model ("combat"); + local node = SceneNode("test"); + node.setModel (model); + node.setPosition (0.0, 0.0, -50.0); + ::mainSceneGraph.attachNode (node); + } function OnFrame (delta, total) @@ -101,10 +108,11 @@ function OnOverlay (delta, total) ::frameCount = 0; ::lastFPS -= 0.1; } - +/* if (total < 2.0) ::logo.draw (0.5, 0.5); else + */ { v <- ::body.getPosition(); ::font.print(10, 40, "Position: " + v.x + ", " + v.y + ", " + v.z, 1, 1 ); @@ -125,7 +133,7 @@ function OnOverlay (delta, total) function OnStep (delta, total) { - ::body.applyLocalForce( Vector(1.0, 0.0, 1.0) ); + ::body.applyLocalForce( Vector(0.0, 0.0, 1.0) ); } function Shutdown() diff --git a/engine/todo.txt b/engine/todo.txt index 1a4c523..29a5627 100644 --- a/engine/todo.txt +++ b/engine/todo.txt @@ -16,7 +16,9 @@ SceneGraph * make ShaderProgram a class * create RenderSet AND/OR RenderQueue -* RenderQueueVisitor +* RenderQueueVisitor +* reimport input class +* use vbos for meshes and tangents #if 0 /*