diff --git a/engine/Material.h b/engine/Material.h new file mode 100644 index 0000000..8634c34 --- /dev/null +++ b/engine/Material.h @@ -0,0 +1,23 @@ +#ifndef BLUECORE_MATERIAL_H +#define BLUECORE_MATERIAL_H + +#include "ShaderManager.h" +#include "TextureManager.h" + +namespace BlueCore +{ + +class Material +{ + typedef ref_ptr TexturePtr; + typedef std::pair TextureUnit; + + std::vector _Textures; + ShaderProgram _ShaderProgram; + bool Tangents; + float Shininess; + float Specular[4]; +}; + +} +#endif /*MATERIAL_H_*/ diff --git a/engine/RenderDevice.cpp b/engine/RenderDevice.cpp index a198821..ba1c4af 100644 --- a/engine/RenderDevice.cpp +++ b/engine/RenderDevice.cpp @@ -17,7 +17,7 @@ void RenderDevice::WindowResizeSlot(int width, int height) } RenderDevice::RenderDevice(RenderWindow* renderWindow) : - _RenderWindow(renderWindow) + _RenderWindow(renderWindow), _ActiveTextures(0) { if (_RenderWindow.valid()) { @@ -32,7 +32,7 @@ RenderDevice::RenderDevice(RenderWindow* renderWindow) : glViewport( 0, 0, _ViewportWidth, _ViewportHeight); } - clog << ">>> RenderDevice constructed..."<< endlog; + clog << ">>> RenderDevice constructed..." << endlog; } RenderDevice::~RenderDevice() @@ -114,6 +114,10 @@ void RenderDevice::begin3D(Camera *camera) setupViewMatrix(); } +void RenderDevice::end3D() +{ +} + void RenderDevice::setupProjectionMatrix() { if (_Camera.valid()) @@ -132,55 +136,47 @@ void RenderDevice::setupProjectionMatrix() void RenderDevice::setupViewMatrix() { - // set the view matrix - glMatrixMode(GL_MODELVIEW); + if (_Camera.valid()) + glMatrixMode(GL_MODELVIEW); glLoadIdentity(); Matrix4x4 m(_Camera->getRotation(), _Camera->getRotation().applyInversed(_Camera->getPosition() * -1.0) ); glMultMatrixd( ( GLdouble * ) &m.m ); - /* - // calculate frustum planes - Vector3 up = q.apply ( Vector3 ( 0.0, 1.0, 0.0 ) ); - Vector3 right = q.apply ( Vector3 ( 1.0, 0.0, 0.0 ) ); - Vector3 d = q.apply ( Vector3 ( 0.0, 0.0, -1.0 ) ); - - Vector3 fc = p + d * _far; - - Vector3 ftl = fc + ( up * _hFar ) - ( right * _wFar ); - Vector3 ftr = fc + ( up * _hFar ) + ( right * _wFar ); - Vector3 fbl = fc - ( up * _hFar ) - ( right * _wFar ); - Vector3 fbr = fc - ( up * _hFar ) + ( right * _wFar ); - - Vector3 nc = p + d * _near; - - Vector3 ntl = nc + ( up * _hNear ) - ( right * _wNear ); - Vector3 ntr = nc + ( up * _hNear ) + ( right * _wNear ); - Vector3 nbl = nc - ( up * _hNear ) - ( right * _wNear ); - Vector3 nbr = nc - ( up * _hNear ) + ( right * _wNear ); - - _frustumPlanes[RightPlane] = Plane ( nbr, fbr, ntr ); - _frustumPlanes[LeftPlane] = Plane ( ntl, fbl, nbl ); - _frustumPlanes[BottomPlane] = Plane ( nbl, fbr, nbr ); - _frustumPlanes[TopPlane] = Plane ( ntr, ftl, ntl ); - _frustumPlanes[FarPlane] = Plane ( ftl, ftr, fbl ); - _frustumPlanes[NearPlane] = Plane ( ntl, nbl, ntr ); - */ } -void RenderDevice::setTransformation (const Vector3& position, const Quaternion& orientation) +void RenderDevice::pushTransformation(const Vector3& position, + const Quaternion& orientation) { - glMatrixMode ( GL_MODELVIEW ); + glMatrixMode( GL_MODELVIEW); glPushMatrix(); - Matrix4x4 m ( orientation, position ); - glMultMatrixd ( ( GLdouble * ) &m.m ); + Matrix4x4 m(orientation, position); + glMultMatrixd( ( GLdouble * ) &m.m ); +} + +void RenderDevice::popTransformation() +{ + glMatrixMode( GL_MODELVIEW); + glPopMatrix(); +} + +void RenderDevice::setTexture(unsigned int unit, Texture* texture, bool last) +{ + glActiveTextureARB(GL_TEXTURE0_ARB + unit); + glBindTexture(GL_TEXTURE_2D, texture->getId()); + glEnable(GL_TEXTURE_2D); + + if (unit > _ActiveTextures) + _ActiveTextures = unit; + + if (last) + { + for (unsigned int i = unit + 1; i < _ActiveTextures; i++) + { + glActiveTextureARB(GL_TEXTURE0_ARB + i); + glDisable( GL_TEXTURE_2D); + } + + _ActiveTextures = unit; + } } -#if 0 -void RenderDevice::setu -// set the view matrix -glMatrixMode (GL_MODELVIEW ); -glLoadIdentity(); -Matrix4x4 m(_Rotation, _Rotation.applyInversed(_Position * -1.0) ); -glMultMatrixd ( ( GLdouble * ) &m.m ); } // namespace BlueCore -#endif -} \ No newline at end of file diff --git a/engine/RenderDevice.h b/engine/RenderDevice.h index 9e7f73d..fc7a151 100644 --- a/engine/RenderDevice.h +++ b/engine/RenderDevice.h @@ -22,6 +22,8 @@ private: ref_ptr _RenderWindow; ref_ptr _Camera; + unsigned int _ActiveTextures; + public: RenderDevice(RenderWindow* renderWindow); @@ -33,15 +35,18 @@ public: void begin2D(); void end2D(); + void begin3D(Camera *); + void end3D(); + void clear(); void swap(); void setAmbientLight(float r, float g, float b); void setMaterial(Material *); - void setTexture(unsigned int unit, unsigned int texture); - void begin3D(Camera *); - void setTransformation (const Vector3&, const Quaternion&); + void setTexture(unsigned int unit, Texture* texture, bool last = false); + void pushTransformation (const Vector3&, const Quaternion&); + void popTransformation (); void setupProjectionMatrix(); void setupViewMatrix(); diff --git a/engine/RenderQueue.cpp b/engine/RenderQueue.cpp index 7f241c3..433f505 100644 --- a/engine/RenderQueue.cpp +++ b/engine/RenderQueue.cpp @@ -37,14 +37,16 @@ void RenderQueue::render(RenderDevice *device) const for (i = _OpaqueItems.begin(); i != _OpaqueItems.end(); i++) { - device->setTransformation((*i).position, (*i).orientation); + device->pushTransformation((*i).position, (*i).orientation); (*i).item->render(device); + device->popTransformation(); } for (i = _TransparentItems.begin(); i != _TransparentItems.end(); i++) { - device->setTransformation((*i).position, (*i).orientation); + device->pushTransformation((*i).position, (*i).orientation); (*i).item->render(device); + device->popTransformation(); } } diff --git a/engine/data/main.nut b/engine/data/main.nut index 768f467..0b066fd 100644 --- a/engine/data/main.nut +++ b/engine/data/main.nut @@ -74,7 +74,7 @@ body <- null; function Initialize() { - ::font = Font ("DejaVuSans.ttf", 24, 1 ); + ::font = Font();//Font ("DejaVuSans.ttf", 24, 1 ); ::logo = Image ("image.png", 0.0, 0.0, 1.0, 1.0); ::body = RigidBody(); diff --git a/engine/data/nebular.zip b/engine/data/nebular.zip new file mode 100644 index 0000000..2418ec1 Binary files /dev/null and b/engine/data/nebular.zip differ diff --git a/engine/data/stars.zip b/engine/data/stars.zip new file mode 100644 index 0000000..7210a9e Binary files /dev/null and b/engine/data/stars.zip differ diff --git a/engine/main.cpp b/engine/main.cpp index 2f66d22..b36b45b 100644 --- a/engine/main.cpp +++ b/engine/main.cpp @@ -89,10 +89,6 @@ int main(int argc, char **argv) ref_ptr camera = new Camera(); ref_ptr queue = new RenderQueue(); - /* - ShaderProgram prog = - shadermanager->loadShaderProgram("ambient_color_emissive"); - */ setupScriptSystem_Font(scriptsystem, fontmanager); setupScriptSystem_Image(scriptsystem, texturemanager, device); setupScriptSystem_Math(scriptsystem); @@ -107,6 +103,12 @@ int main(int argc, char **argv) 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()) { scriptsystem->loadScript("main"); @@ -114,7 +116,7 @@ int main(int argc, char **argv) camera->setFoV(90.0); camera->setAspectRatio((double)width/(double)height); camera->setNearPlane(1.0); - camera->setFarPlane(100.0); + camera->setFarPlane(15000.0); camera->setPosition(Vector3(0.0, 0.0, 20.0)); device->setAmbientLight(1.0, 1.0, 1.0); @@ -142,6 +144,22 @@ int main(int argc, char **argv) queue->addOpaqueItem(model, Vector3(10.0, 0.0, 0.0), Quaternion(Vector3(0.0,1.0,0.0), fmod(time, 3))); queue->render(device); + + 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 ); + + device->setTexture(0, starTexture, true); + starMesh->render(); + glDisable ( GL_BLEND ); + + // device->useShader (program); // device->setTexture (stage, name, texture) // device->clearTextures (stage+1);