From: Charles McGarvey Date: Sat, 30 Jan 2010 05:04:31 +0000 (-0700) Subject: game loop tweaks; shapes hierarchy defined X-Git-Url: https://git.dogcows.com/gitweb?a=commitdiff_plain;h=76b3f4be992514a740ac03cdbdd57844142a0b4c;p=chaz%2Fyoink game loop tweaks; shapes hierarchy defined --- diff --git a/data/yoinkrc b/data/yoinkrc index de65603..bf5bc83 100644 --- a/data/yoinkrc +++ b/data/yoinkrc @@ -56,19 +56,21 @@ resizable = true --videomode = {800, 600} --- Set this to make the cursor remain visible as you mouse over the view of --- the game. +-- Set this to make the cursor remain visible as you mouse over the video +-- output of the game. -showcursor = true +showcursor = false -- Set this to use double-buffering to improve animation quality. You --- should usually leave this as true. +-- really don't want to turn this off. doublebuffer = true -- Set this to sync with the refresh rate of your display. Your framerate --- will be limited to the refresh rate, but you may experience fewer ugly --- "artifacts" caused by the game animation. +-- will be limited to the refresh rate, but you may experience less +-- tearing caused by the display vertical refresh. On the other hand, you +-- might experience worse tearing, depending on your setup. Try it both +-- ways. swapcontrol = true diff --git a/src/Character.cc b/src/Character.cc index 6a7c698..840099c 100644 --- a/src/Character.cc +++ b/src/Character.cc @@ -30,8 +30,7 @@ public: // spring: //mState.force += -15.0 * x - 1.5 * mState.velocity; - force = SCALAR(-10.0) * (mag - d) * (x / mag) - - SCALAR(2.0) * state.velocity; + force = SCALAR(-10.0) * (mag - d) * (x / mag);// - SCALAR(2.0) * state.velocity; return force; } @@ -73,8 +72,8 @@ Character::Character(const std::string& name) : // forces mState.force = Mf::Vector2(0.0, 0.0); - //mState.forces.push_back(SpringForce(Mf::Vector2(20.0, 4.0))); - mState.forces.push_back(ResistanceForce(2.0)); + //mState.forces.push_back(SpringForce(Mf::Vector2(5.0, 4.0))); + //mState.forces.push_back(ResistanceForce(2.0)); //mState.forces.push_back(Mf::LinearState<2>::GravityForce(-9.8)); // starting position diff --git a/src/GameLayer.cc b/src/GameLayer.cc index 3a0ea09..331d04f 100644 --- a/src/GameLayer.cc +++ b/src/GameLayer.cc @@ -114,7 +114,7 @@ GameLayer::GameLayer() : } -void GameLayer::pushedOntoEngine() +void GameLayer::addedToCore() { Mf::core.push(mHud); @@ -123,8 +123,8 @@ void GameLayer::pushedOntoEngine() mLine.a.set(20, 10); mLine.b.set(19, 14); - mSphere.point.set(22, 5); - mSphere.radius = 2; + mCircle.point.set(22, 5); + mCircle.radius = 2; mRayTimer.init(boost::bind(&GameLayer::rayTimer, this), 1.0, Mf::Timer::REPEAT); @@ -155,8 +155,8 @@ void GameLayer::thinkTimer() void GameLayer::rayTimer() { - Mf::Ray<2>::Intersection meh; - std::list::Intersection> hits; + Mf::Ray2::Contact meh; + std::list hits; Mf::Vector2 point; bool bam = mLine.intersectRay(mRay, meh); @@ -166,7 +166,7 @@ void GameLayer::rayTimer() hits.push_back(meh); } - bam = mSphere.intersectRay(mRay, meh); + bam = mCircle.intersectRay(mRay, meh); if (bam) { meh.normal.normalize(); @@ -177,9 +177,9 @@ void GameLayer::rayTimer() { hits.front().normal.normalize(); mRay.solve(point, hits.front().distance); - //Mf::logInfo << "scene: d = " << hits.front().distance << std::endl; - //Mf::logInfo << " P = " << point << std::endl; - //Mf::logInfo << " n = " << hits.front().normal << std::endl; + Mf::logInfo << "scene: d = " << hits.front().distance << std::endl; + Mf::logInfo << " P = " << point << std::endl; + Mf::logInfo << " n = " << hits.front().normal << std::endl; } } @@ -195,12 +195,11 @@ void GameLayer::draw(Mf::Scalar alpha) const glEnableClientState(GL_TEXTURE_COORD_ARRAY); mState.scene->drawIfVisible(alpha, mState.camera.getFrustum()); - mState.heroine->draw(alpha); mRay.draw(); mLine.draw(); - mSphere.draw(); + mCircle.draw(); } bool GameLayer::handleEvent(const Mf::Event& event) diff --git a/src/GameLayer.hh b/src/GameLayer.hh index 885a49d..c537af6 100644 --- a/src/GameLayer.hh +++ b/src/GameLayer.hh @@ -51,7 +51,7 @@ public: return GameLayerP(new GameLayer); } - void pushedOntoEngine(); + void addedToCore(); void update(Mf::Scalar t, Mf::Scalar dt); void draw(Mf::Scalar alpha) const; @@ -75,9 +75,9 @@ private: Mf::SoundStream mMusic; Mf::Sound mPunchSound; - Mf::Ray<2> mRay; - Mf::Line<2> mLine; - Mf::Sphere<2> mSphere; + Mf::Ray2 mRay; + Mf::Line2 mLine; + Mf::Circle mCircle; Mf::Timer mRayTimer; void rayTimer(); diff --git a/src/Hud.cc b/src/Hud.cc index 9e40a60..f4134cf 100644 --- a/src/Hud.cc +++ b/src/Hud.cc @@ -9,7 +9,9 @@ * **************************************************************************/ +#include #include +#include #include #include @@ -30,11 +32,12 @@ ProgressBar::ProgressBar(const Mf::Texture& tilemap, void ProgressBar::resize(const Mf::Rectangle& rect) { + Mf::logInfo << "rect: " << rect.min << ", " << rect.max << std::endl; Mf::Scalar height = rect.max[1] - rect.min[1]; Mf::Scalar halfHeight = height / 2.0; mWidth = rect.max[0] - rect.min[0] - height; - // assert width > 0 + ASSERT(mWidth > 0); mVertices[0] = rect.min; mVertices[1] = Mf::Vector2(rect.min[0] + halfHeight, rect.min[1]); @@ -122,10 +125,8 @@ void Hud::resize(int width, int height) SCALAR(1.0), SCALAR(-1.0), cml::z_clip_neg_one); // position the two progress bars at the top-left of the screen - mBar1.resize(Mf::Rectangle(20, height - 51, - 0.7 * width, height - 3)); - mBar2.resize(Mf::Rectangle(20, height - 28, - 0.7 * width, height - 70)); + mBar1.resize(Mf::Rectangle(20, height - 51, 0.7 * width, height - 3)); + mBar2.resize(Mf::Rectangle(20, height - 28, 0.7 * width, height - 70)); setBar1Progress(0.05); setBar2Progress(0.0); @@ -174,6 +175,7 @@ bool Hud::handleEvent(const Mf::Event& event) { // don't want the hud anymore Mf::core.pop(this); + Mf::logWarning("okay bye bye hud"); return true; } break; diff --git a/src/Hud.hh b/src/Hud.hh index 30b5a42..bc8a313 100644 --- a/src/Hud.hh +++ b/src/Hud.hh @@ -20,12 +20,14 @@ #include #include #include -#include +//#include #include #include "GameState.hh" +class Rectangle; + // TODO this stuff is still just hacked up class ProgressBar : public Mf::Drawable diff --git a/src/Makefile.am b/src/Makefile.am index 45ea7cd..4e8f65d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -12,7 +12,6 @@ noinst_LIBRARIES = libmoof.a libmoof_a_SOURCES = \ - Moof/Aabb.cc \ Moof/Aabb.hh \ Moof/Camera.cc \ Moof/Camera.hh \ @@ -46,8 +45,6 @@ libmoof_a_SOURCES = \ Moof/Plane.cc \ Moof/Plane.hh \ Moof/Ray.hh \ - Moof/Rectangle.cc \ - Moof/Rectangle.hh \ Moof/Resource.cc \ Moof/Resource.hh \ Moof/RigidBody.hh \ diff --git a/src/Moof/Aabb.cc b/src/Moof/Aabb.cc deleted file mode 100644 index 4ef1520..0000000 --- a/src/Moof/Aabb.cc +++ /dev/null @@ -1,71 +0,0 @@ - -/*] Copyright (c) 2009-2010, Charles McGarvey [************************** -**] All rights reserved. -* -* vi:ts=4 sw=4 tw=75 -* -* Distributable under the terms and conditions of the 2-clause BSD license; -* see the file COPYING for a complete text of the license. -* -**************************************************************************/ - -#include "Aabb.hh" -#include "Frustum.hh" -#include "OpenGL.hh" -#include "Texture.hh" - - -namespace Mf { - - - /* -void Aabb::getOctant(Aabb& octant, int num) const -{ - Vector3 mid = getCenter(); - - switch (num) - { - case 0: - octant.init(Vector3(min[0], min[1], mid[2]), - Vector3(mid[0], mid[1], max[2])); - break; - - case 1: - octant.init(Vector3(mid[0], min[1], mid[2]), - Vector3(max[0], mid[1], max[2])); - break; - - case 2: - octant.init(mid, max); - break; - - case 3: - octant.init(Vector3(min[0], mid[1], mid[2]), - Vector3(mid[0], max[1], max[2])); - break; - - case 4: - octant.init(min, mid); - break; - - case 5: - octant.init(Vector3(mid[0], min[1], min[2]), - Vector3(max[0], mid[1], mid[2])); - break; - - case 6: - octant.init(Vector3(mid[0], mid[1], min[2]), - Vector3(max[0], max[1], mid[2])); - break; - - case 7: - octant.init(Vector3(min[0], mid[1], min[2]), - Vector3(mid[0], max[1], mid[2])); - break; - } -} -*/ - - -} // namespace Mf - diff --git a/src/Moof/Aabb.hh b/src/Moof/Aabb.hh index f3e92ab..8fcdbc3 100644 --- a/src/Moof/Aabb.hh +++ b/src/Moof/Aabb.hh @@ -38,6 +38,7 @@ struct Aabb : public Cullable, public Drawable, public Shape Vector min; Vector max; + Aabb() {} Aabb(const Vector& a, const Vector& b) @@ -45,16 +46,48 @@ struct Aabb : public Cullable, public Drawable, public Shape init(a, b); } - Aabb(Scalar ax, Scalar ay, Scalar az, - Scalar bx, Scalar by, Scalar bz) + Aabb(Scalar x1, Scalar y1, Scalar x2, Scalar y2) { - Vector a(ax, ay, az); - Vector b(bx, by, bz); + Vector a(x1, y1); + Vector b(x2, y2); init(a, b); } - void init(const Vector& a, const Vector& b) + Aabb(Scalar x1, Scalar y1, Scalar z1, Scalar x2, Scalar y2, Scalar z2) + { + Vector a(x1, y1, z1); + Vector b(x2, y2, z2); + + init(a, b); + } + + + void init(const Vector2& a, const Vector2& b) + { + if (a[0] < b[0]) + { + min[0] = a[0]; + max[0] = b[0]; + } + else + { + min[0] = b[0]; + max[0] = a[0]; + } + if (a[1] < b[1]) + { + min[1] = a[1]; + max[1] = b[1]; + } + else + { + min[1] = b[1]; + max[1] = a[1]; + } + } + + void init(const Vector3& a, const Vector3& b) { if (a[0] < b[0]) { @@ -88,19 +121,17 @@ struct Aabb : public Cullable, public Drawable, public Shape } } + Vector getCenter() const { - return Vector((min[0] + max[0]) / 2.0, - (min[1] + max[1]) / 2.0, - (min[2] + max[2]) / 2.0); + return (min + max) / 2.0; } - //void getOctant(Aabb& octant, int num) const; Plane getPlaneXY() const { Plane plane; - plane.normal = Vector(0.0, 0.0, 1.0); + plane.normal = Vector3(0.0, 0.0, 1.0); plane.d = cml::dot(-plane.normal, getCenter()); return plane; } @@ -108,7 +139,7 @@ struct Aabb : public Cullable, public Drawable, public Shape Plane getPlaneXZ() const { Plane plane; - plane.normal = Vector(0.0, 1.0, 0.0); + plane.normal = Vector3(0.0, 1.0, 0.0); plane.d = cml::dot(-plane.normal, getCenter()); return plane; } @@ -116,22 +147,21 @@ struct Aabb : public Cullable, public Drawable, public Shape Plane getPlaneYZ() const { Plane plane; - plane.normal = Vector(1.0, 0.0, 0.0); + plane.normal = Vector3(1.0, 0.0, 0.0); plane.d = cml::dot(-plane.normal, getCenter()); return plane; } - /* - void getCorners(Vector3 corners[8]) const; - - void encloseVertices(const Vector3 vertices[], unsigned count); - - void draw(Scalar alpha = 0.0) const; - bool isVisible(const Frustum& frustum) const; - */ + void getCorners(Vector2 corners[4]) const + { + corners[0][0] = min[0]; corners[0][1] = min[1]; + corners[1][0] = max[0]; corners[1][1] = min[1]; + corners[2][0] = max[0]; corners[2][1] = max[1]; + corners[3][0] = min[0]; corners[3][1] = max[1]; + } - void getCorners(Vector corners[8]) const + void getCorners(Vector3 corners[8]) const { corners[0][0] = min[0]; corners[0][1] = min[1]; @@ -175,45 +205,63 @@ struct Aabb : public Cullable, public Drawable, public Shape void draw(Scalar alpha = 0.0) const { - Scalar vertices[] = {min[0], min[1], min[2], - min[0], max[1], min[2], - max[0], max[1], min[2], - max[0], min[1], min[2], - min[0], max[1], max[2], - min[0], min[1], max[2], - max[0], min[1], max[2], - max[0], max[1], max[2]}; - - GLubyte indices[] = {0, 1, 2, 3, - 1, 2, 7, 4, - 3, 0, 5, 6, - 2, 3, 6, 7, - 5, 0, 1, 4, - 4, 5, 6, 7}; - - glEnableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - glVertexPointer(3, GL_SCALAR, 0, vertices); - - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - Texture::resetBind(); - - glDrawElements(GL_QUADS, sizeof(indices), GL_UNSIGNED_BYTE, - indices); - - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - //glDisableClientState(GL_VERTEX_ARRAY); - - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + glRect(min[0], min[1], max[0], max[1]); } bool isVisible(const Frustum& frustum) const { - return frustum.contains(*this); + return true; } }; +template <> +inline void Aabb<3>::draw(Scalar alpha) const +{ + Scalar vertices[] = {min[0], min[1], min[2], + min[0], max[1], min[2], + max[0], max[1], min[2], + max[0], min[1], min[2], + min[0], max[1], max[2], + min[0], min[1], max[2], + max[0], min[1], max[2], + max[0], max[1], max[2]}; + + GLubyte indices[] = {0, 1, 2, 3, + 1, 2, 7, 4, + 3, 0, 5, 6, + 2, 3, 6, 7, + 5, 0, 1, 4, + 4, 5, 6, 7}; + + glEnableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glVertexPointer(3, GL_SCALAR, 0, vertices); + + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + Texture::resetBind(); + + glDrawElements(GL_QUADS, sizeof(indices), GL_UNSIGNED_BYTE, + indices); + + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + //glDisableClientState(GL_VERTEX_ARRAY); + + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); +} + +template <> +inline bool Aabb<3>::isVisible(const Frustum& frustum) const +{ + return frustum.contains(*this); +} + + +typedef Aabb<2> Aabb2; +typedef Aabb2 Rectangle; +typedef Aabb<3> Aabb3; + + } // namespace Mf #endif // _MOOF_AABB_HH_ diff --git a/src/Moof/Core.cc b/src/Moof/Core.cc index b9ad92a..b659f98 100644 --- a/src/Moof/Core.cc +++ b/src/Moof/Core.cc @@ -70,6 +70,7 @@ public: void run() { init(); + ASSERT(video && "cannot run core without a current video context"); Scalar totalTime = 0.0; Scalar ticks = Timer::getTicks(); @@ -79,49 +80,50 @@ public: Scalar nextSecond = ticks + SCALAR(1.0); mFps = 0; - int frames = 0; + int frameCount = 0; - const int MAX_FRAMESKIP = 15; - const Scalar inverseTimestep = SCALAR(1.0) / mTimestep; + const Scalar timestep = mTimestep; + const Scalar framerate = mFramerate; - ASSERT(video && "cannot run core without a current video context"); + const int MAX_FRAMESKIP = 15; + const Scalar inverseTimestep = SCALAR(1.0) / timestep; do { - Timer::fireIfExpired(); - dispatchEvents(); + Timer::fireIfExpired(); // 1. fire timers + dispatchEvents(); // 2. dispatch events int i = 0; while (nextUpdate < Timer::getTicks() && i < MAX_FRAMESKIP) { - totalTime += mTimestep; - update(totalTime, mTimestep); + totalTime += timestep; + update(totalTime, timestep); // 3. update state - nextUpdate += mTimestep; + nextUpdate += timestep; ++i; } if (nextDraw < (ticks = Timer::getTicks())) { - ++frames; - draw((ticks + mTimestep - nextUpdate) * inverseTimestep); - video->swap(); + draw((ticks + timestep - nextUpdate) * inverseTimestep); + video->swap(); // 4. draw state - nextDraw += mFramerate; + nextDraw += framerate; + ++frameCount; - if (mShowFps && nextSecond < ticks) + if (nextSecond < Timer::getTicks()) { - mFps = frames; - frames = 0; + mFps = frameCount; + frameCount = 0; - logInfo << mFps << " fps" << std::endl; + if (mShowFps) logInfo << mFps << " fps" << std::endl; nextSecond += SCALAR(1.0); } } - // be a good citizen and give back what you don't need - Timer::sleep(0.0); + ticks = Timer::getTicks(); // 5. yield timeslice + if (ticks < nextUpdate && ticks < nextDraw) Timer::sleep(0.0); } while (!mStack.empty()); diff --git a/src/Moof/Frustum.hh b/src/Moof/Frustum.hh index 6e93cd6..5710004 100644 --- a/src/Moof/Frustum.hh +++ b/src/Moof/Frustum.hh @@ -44,7 +44,7 @@ public: { init(modelview, fovy, aspect, abutting, distant); } - + void init(const Matrix4& modelview, const Matrix4& projection); void init(const Matrix4& modelview, Scalar fovy, Scalar aspect, Scalar abutting, Scalar distant); diff --git a/src/Moof/Line.hh b/src/Moof/Line.hh index 7902ea9..f8a2b66 100644 --- a/src/Moof/Line.hh +++ b/src/Moof/Line.hh @@ -38,7 +38,7 @@ struct Line : public Drawable, public Shape a(point1), b(point2) {} - bool intersectRay(const Ray<2>& ray, Ray<2>::Intersection& hit) const + bool intersectRay(const Ray<2>& ray, Ray<2>::Contact& hit) const { // solve: Cx + r*Dx = Ax + s(Bx - Ax) // Cy + r*Dy = Ay + s(By - Ay) @@ -79,6 +79,8 @@ struct Line : public Drawable, public Shape hit.distance = -(a[0] * (ray.point[1] - b[1]) + b[0] * (a[1] - ray.point[1]) + ray.point[0] * (b[1] - a[1])) / denom; + + // check if the intersection is behind the ray if (hit.distance < SCALAR(0.0)) return false; Vector normal = cml::perp(a - b); @@ -98,15 +100,41 @@ struct Line : public Drawable, public Shape }; +typedef Line<2> Line2; +typedef Line<3> Line3; + + template -struct Polygon : public Shape +struct Polygon : public Drawable, public Shape { typedef cml::vector< Scalar, cml::fixed > Vector; Vector points[N]; + + Polygon() {} + + bool intersectRay(const Ray& ray, typename Ray::Contact& hit) + { + return false; + } + + void draw(Scalar alpha = 0.0) const + { + Mf::Texture::resetBind(); + glBegin(GL_POLYGON); + for (int i = 0; i < D; ++i) + { + glVertex(points[0]); + } + glEnd(); + } }; +typedef Polygon<2,3> Triangle2; +typedef Polygon<3,3> Triangle3; + + } // namespace Mf #endif // _MOOF_LINE_HH_ diff --git a/src/Moof/OpenGL.hh b/src/Moof/OpenGL.hh index 2fc9197..05d8317 100644 --- a/src/Moof/OpenGL.hh +++ b/src/Moof/OpenGL.hh @@ -97,6 +97,9 @@ OPENGL_ORDINAL_FUNC(void, TexCoord, 2); OPENGL_ORDINAL_FUNC(void, TexCoord, 3); OPENGL_ORDINAL_FUNC(void, TexCoord, 4); +OPENGL_GENERIC_FUNC(void, Rect, S4); +OPENGL_GENERIC_FUNC(void, Rect, V4); + #if USE_DOUBLE_PRECISION inline void glGetScalar(GLenum a, GLscalar* b) { glGetDoublev(a, b); } diff --git a/src/Moof/Plane.hh b/src/Moof/Plane.hh index 262a6bd..108c49f 100644 --- a/src/Moof/Plane.hh +++ b/src/Moof/Plane.hh @@ -49,7 +49,7 @@ struct Plane : public Shape<3> d(scalar) {} - bool intersectRay(const Ray<3>& ray, Ray<3>::Intersection& hit) + bool intersectRay(const Ray<3>& ray, Ray<3>::Contact& hit) { // solve: [(ray.point + t*ray.direction) dot normal] + d = 0 diff --git a/src/Moof/Ray.hh b/src/Moof/Ray.hh index 1564087..6d4705a 100644 --- a/src/Moof/Ray.hh +++ b/src/Moof/Ray.hh @@ -34,12 +34,12 @@ struct Ray : public Drawable Vector point; Vector direction; - struct Intersection + struct Contact { Scalar distance; // distance from the origin to the nearest point - Vector normal; // surface normal at intersection point + Vector normal; // surface normal at contact point - bool operator < (const Intersection& rhs) + bool operator < (const Contact& rhs) { return distance < rhs.distance; } @@ -69,6 +69,10 @@ struct Ray : public Drawable }; +typedef Ray<2> Ray2; +typedef Ray<3> Ray3; + + } // namespace Mf #endif // _MOOF_RAY_HH_ diff --git a/src/Moof/Rectangle.cc b/src/Moof/Rectangle.cc deleted file mode 100644 index 949c22a..0000000 --- a/src/Moof/Rectangle.cc +++ /dev/null @@ -1,32 +0,0 @@ - -/*] Copyright (c) 2009-2010, Charles McGarvey [************************** -**] All rights reserved. -* -* vi:ts=4 sw=4 tw=75 -* -* Distributable under the terms and conditions of the 2-clause BSD license; -* see the file COPYING for a complete text of the license. -* -**************************************************************************/ - -#include "Rectangle.hh" - - -namespace Mf { - - -void Rectangle::getCorners(Vector2 corners[4]) const -{ - corners[0][0] = min[0]; corners[0][1] = min[1]; - corners[1][0] = max[0]; corners[1][1] = min[1]; - corners[2][0] = max[0]; corners[2][1] = max[1]; - corners[3][0] = min[0]; corners[3][1] = max[1]; - corners[4][0] = min[0]; corners[4][1] = min[1]; - corners[5][0] = max[0]; corners[5][1] = min[1]; - corners[6][0] = max[0]; corners[6][1] = max[1]; - corners[7][0] = min[0]; corners[7][1] = max[1]; -} - - -} // namespace Mf - diff --git a/src/Moof/Rectangle.hh b/src/Moof/Rectangle.hh deleted file mode 100644 index e41b879..0000000 --- a/src/Moof/Rectangle.hh +++ /dev/null @@ -1,83 +0,0 @@ - -/*] Copyright (c) 2009-2010, Charles McGarvey [************************** -**] All rights reserved. -* -* vi:ts=4 sw=4 tw=75 -* -* Distributable under the terms and conditions of the 2-clause BSD license; -* see the file COPYING for a complete text of the license. -* -**************************************************************************/ - -#ifndef _MOOF_RECTANGLE_HH_ -#define _MOOF_RECTANGLE_HH_ - -#include - - -namespace Mf { - - -/** - * Axis-aligned Bounding Box - */ - -struct Rectangle -{ - Vector2 min; - Vector2 max; - - - Rectangle() {} - - Rectangle(const Vector2& a, const Vector2& b) - { - init(a, b); - } - - Rectangle(Scalar ax, Scalar ay, Scalar bx, Scalar by) - { - Vector2 a(ax, ay); - Vector2 b(bx, by); - - init(a, b); - } - - void init(const Vector2& a, const Vector2& b) - { - if (a[0] < b[0]) - { - min[0] = a[0]; - max[0] = b[0]; - } - else - { - min[0] = b[0]; - max[0] = a[0]; - } - if (a[1] < b[1]) - { - min[1] = a[1]; - max[1] = b[1]; - } - else - { - min[1] = b[1]; - max[1] = a[1]; - } - } - - Vector2 getCenter() const - { - return Vector2((min[0] + max[0]) / 2.0, - (min[1] + max[1]) / 2.0); - } - - void getCorners(Vector2 corners[4]) const; -}; - - -} // namespace Mf - -#endif // _MOOF_RECTANGLE_HH_ - diff --git a/src/Moof/Shape.hh b/src/Moof/Shape.hh index c972960..c059d46 100644 --- a/src/Moof/Shape.hh +++ b/src/Moof/Shape.hh @@ -19,20 +19,21 @@ // Frustum -// Plane (can construct from Triangle<3>) +// Plane (can construct from Triangle3) // Ray // Shape<> // +- Line<> -// +- Ball<> -// | Circle <- Ball<2> -// | Sphere <- Ball<3> -// +- Box<> -// | Rectangle <- Box<2> -// | Aabb <- Box<3> +// - Line2 Line<2> +// - Line3 Line<3> +// +- Sphere<> +// | Sphere2, Circle Sphere<2> +// | Sphere3 Sphere<3> +// +- Aabb<> +// | Aabb2, Rectangle Aabb<2> +// | Aabb3 Aabb<3> // +- Polygon<> -// | Triangle <- Polygon<3> -// +- Cylinder -// +- Cone +// | Triangle2 Polygon<2,3> +// | Triangle3 Polygon<3,3> namespace Mf { @@ -48,13 +49,13 @@ public: /** * Checks if this shape is intersected by a given ray. If so, returns * the distance from the start of the ray to the shape and information - * about the intersection via the 2nd parameter. A negative value is - * returned if there is no intersection. + * about the contact via the 2nd parameter. A negative value is + * returned if there is no contact. */ virtual bool intersectRay(const Ray& ray, - typename Ray::Intersection& hit) + typename Ray::Contact& hit) { - return SCALAR(-1.0); + return false; } }; diff --git a/src/Moof/Sphere.hh b/src/Moof/Sphere.hh index 3cd1ec7..d42bfd8 100644 --- a/src/Moof/Sphere.hh +++ b/src/Moof/Sphere.hh @@ -61,7 +61,7 @@ struct Sphere : public Cullable, public Drawable, public Shape // a ray inside the sphere will not intersect on its way out - bool intersectRay(const Ray& ray, typename Ray::Intersection& hit) + bool intersectRay(const Ray& ray, typename Ray::Contact& hit) { Vector b = point - ray.point; Scalar z = cml::dot(b, ray.direction); @@ -163,6 +163,11 @@ inline bool checkCollision(const Sphere& a, const Sphere& b) } +typedef Sphere<2> Sphere2; +typedef Sphere2 Circle; +typedef Sphere<3> Sphere3; + + } // namespace Mf #endif // _MOOF_SPHERE_HH_ diff --git a/src/Scene.cc b/src/Scene.cc index 7b23ab6..8b155e7 100644 --- a/src/Scene.cc +++ b/src/Scene.cc @@ -526,14 +526,14 @@ void Scene::drawIfVisible(Mf::Scalar alpha, bool Scene::castRay(const Mf::Ray<2>& ray, - std::list::Intersection>& hits) const + std::list::Contact>& hits) const { std::list< Mf::Line<2> >& lines = mImpl->mLines; std::list< Mf::Line<2> >::const_iterator it; for (it = lines.begin(); it != lines.end(); ++it) { - Mf::Ray<2>::Intersection hit; + Mf::Ray<2>::Contact hit; Mf::Scalar d = (*it).intersectRay(ray, hit); if (d > 0.0) { diff --git a/src/Scene.hh b/src/Scene.hh index 85a6716..07db30d 100644 --- a/src/Scene.hh +++ b/src/Scene.hh @@ -51,7 +51,7 @@ public: Mf::Scalar getZCoord(const Mf::Vector2& position) const; bool castRay(const Mf::Ray<2>& ray, - std::list::Intersection>& hits) const; + std::list::Contact>& hits) const; bool checkForCollision(Character& character); static std::string getPath(const std::string& name);