added enable_blending parameter to objects

git-svn-id: svn://anubis/anaglym/trunk@313 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2010-09-17 16:37:51 +00:00
parent 7ea2aa3644
commit 9ea65f4e65
4 changed files with 88 additions and 44 deletions

View File

@ -276,18 +276,20 @@ bool Engine::fileExists(const string & path)
} }
int Engine::addObject(WFObj * obj, bool is_static, bool is_reference, int Engine::addObject(WFObj * obj, bool is_static, bool is_reference,
float scale) bool enable_blending, float scale)
{ {
Object * o = new Object(is_static, is_reference, m_world, obj, scale); Object * o = new Object(is_static, is_reference, enable_blending,
m_world, obj, scale);
int id = m_objects.add(o); int id = m_objects.add(o);
o->setID(id); o->setID(id);
return id; return id;
} }
int Engine::addObject(bool is_static, bool is_reference, int Engine::addObject(bool is_static, bool is_reference, bool enable_blending,
OdeWorld::GeomType geom_type, refptr< vector<float> > args) OdeWorld::GeomType geom_type, refptr< vector<float> > args)
{ {
Object * o = new Object(is_static, is_reference, m_world, geom_type, args); Object * o = new Object(is_static, is_reference, enable_blending,
m_world, geom_type, args);
int id = m_objects.add(o); int id = m_objects.add(o);
o->setID(id); o->setID(id);
return id; return id;
@ -535,7 +537,7 @@ int Engine::clearEventHandler(lua_State * L)
} }
int Engine::loadModel(const string & name, bool is_static, bool is_reference, int Engine::loadModel(const string & name, bool is_static, bool is_reference,
float scale) bool enable_blending, float scale)
{ {
size_t pos = name.find_first_not_of(FILENAME_SAFE_CHARS); size_t pos = name.find_first_not_of(FILENAME_SAFE_CHARS);
if (pos == string::npos) if (pos == string::npos)
@ -547,7 +549,8 @@ int Engine::loadModel(const string & name, bool is_static, bool is_reference,
if (obj->load(model_path)) if (obj->load(model_path))
{ {
int id = addObject(obj, is_static, is_reference, scale); int id = addObject(obj, is_static, is_reference, enable_blending,
scale);
Engine::Object * engine_obj = getObject(id); Engine::Object * engine_obj = getObject(id);
if (engine_obj != NULL) if (engine_obj != NULL)
{ {
@ -1205,7 +1208,7 @@ void Engine::drawObjects()
/******** Engine::Object functions ********/ /******** Engine::Object functions ********/
/* used for objects loaded directly from model files */ /* used for objects loaded directly from model files */
Engine::Object::Object(bool is_static, bool is_reference, Engine::Object::Object(bool is_static, bool is_reference, bool enable_blending,
OdeWorld & world, WFObj * obj, float scale) OdeWorld & world, WFObj * obj, float scale)
: m_world(world) : m_world(world)
{ {
@ -1220,7 +1223,8 @@ Engine::Object::Object(bool is_static, bool is_reference,
m_ode_object->setUserData(this); m_ode_object->setUserData(this);
} }
m_is_static = is_static; m_is_static = is_static;
m_display_list = obj->render(); m_enable_blending = enable_blending;
m_display_list = obj->render(true, enable_blending);
const float * obj_aabb = obj->getAABB(); const float * obj_aabb = obj->getAABB();
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++)
m_aabb[i] = scale * obj_aabb[i]; m_aabb[i] = scale * obj_aabb[i];
@ -1236,12 +1240,13 @@ Engine::Object::Object(bool is_static, bool is_reference,
} }
/* used for "managed" objects with one geom */ /* used for "managed" objects with one geom */
Engine::Object::Object(bool is_static, bool is_reference, OdeWorld & world, Engine::Object::Object(bool is_static, bool is_reference, bool enable_blending,
OdeWorld::GeomType geom_type, OdeWorld & world, OdeWorld::GeomType geom_type,
refptr< std::vector<float> > args) refptr< std::vector<float> > args)
: m_world(world) : m_world(world)
{ {
m_is_reference = is_reference; m_is_reference = is_reference;
m_enable_blending = enable_blending;
m_is_static = geom_type == OdeWorld::PLANE ? false : is_static; m_is_static = geom_type == OdeWorld::PLANE ? false : is_static;
m_is_visible = true; m_is_visible = true;
m_scale = 1.0f; m_scale = 1.0f;
@ -1278,6 +1283,7 @@ Engine::Object::Object(const Engine::Object & orig)
m_is_reference = false; m_is_reference = false;
m_is_visible = orig.m_is_visible; m_is_visible = orig.m_is_visible;
m_is_static = orig.m_is_static; m_is_static = orig.m_is_static;
m_enable_blending = orig.m_enable_blending;
m_scale = orig.m_scale; m_scale = orig.m_scale;
m_is_scaled = orig.m_is_scaled; m_is_scaled = orig.m_is_scaled;
m_phy = orig.m_phy; m_phy = orig.m_phy;
@ -1417,7 +1423,17 @@ void Engine::Object::render()
} }
checkGLError(); checkGLError();
glNewList(m_display_list, GL_COMPILE); glNewList(m_display_list, GL_COMPILE);
glPushAttrib(GL_ENABLE_BIT); int attrib_flags = GL_ENABLE_BIT;
if (m_enable_blending)
{
attrib_flags |= GL_COLOR_BUFFER_BIT;
}
glPushAttrib(attrib_flags);
if (m_enable_blending)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
if (m_texture != 0) if (m_texture != 0)
{ {
gluQuadricTexture(quad, 1); gluQuadricTexture(quad, 1);

View File

@ -23,10 +23,10 @@ class Engine
class Object class Object
{ {
public: public:
Object(bool is_static, bool is_reference, Object(bool is_static, bool is_reference, bool enable_blending,
OdeWorld & world, WFObj * wfobj, OdeWorld & world, WFObj * wfobj,
float scale = 1.0f); float scale = 1.0f);
Object(bool is_static, bool is_reference, Object(bool is_static, bool is_reference, bool enable_blending,
OdeWorld & world, OdeWorld::GeomType geom_type, OdeWorld & world, OdeWorld::GeomType geom_type,
refptr< std::vector<float> > args); refptr< std::vector<float> > args);
Object(const Object & orig); Object(const Object & orig);
@ -114,6 +114,7 @@ class Engine
bool m_mass_is_set; bool m_mass_is_set;
bool m_gravity_mode; bool m_gravity_mode;
int m_id; int m_id;
bool m_enable_blending;
/* for "pre-loaded" objects */ /* for "pre-loaded" objects */
int * m_display_list_refcnt; int * m_display_list_refcnt;
@ -158,8 +159,8 @@ class Engine
void reportErrors(int status); void reportErrors(int status);
OdeWorld & getWorld() { return m_world; } OdeWorld & getWorld() { return m_world; }
int addObject(WFObj * obj, bool is_static, bool is_reference, int addObject(WFObj * obj, bool is_static, bool is_reference,
float scale = 1.0f); bool enable_blending, float scale = 1.0f);
int addObject(bool is_static, bool is_reference, int addObject(bool is_static, bool is_reference, bool enable_blending,
OdeWorld::GeomType geom_type, OdeWorld::GeomType geom_type,
refptr< std::vector<float> > args); refptr< std::vector<float> > args);
int addSound(refptr<AV::Sound> avs); int addSound(refptr<AV::Sound> avs);
@ -193,7 +194,7 @@ class Engine
void startFrame(); void startFrame();
void endFrame(); void endFrame();
int loadModel(const std::string & name, bool is_static, int loadModel(const std::string & name, bool is_static,
bool is_reference, float scale = 1.0f); bool is_reference, bool enable_blending, float scale = 1.0f);
int loadSound(const std::string & name); int loadSound(const std::string & name);
bool isKeyDown(const std::string & key); bool isKeyDown(const std::string & key);
void exit(); void exit();

51
ag.cc
View File

@ -510,16 +510,18 @@ fail:
bool added = false; bool added = false;
int argc = lua_gettop(L); int argc = lua_gettop(L);
if (argc == 4 if (argc == 5
&& lua_isstring(L, 1) && lua_isstring(L, 1)
&& lua_isnumber(L, 2) && lua_isnumber(L, 2)
&& lua_isboolean(L, 3) && lua_isboolean(L, 3)
&& lua_isboolean(L, 4)) && lua_isboolean(L, 4)
&& lua_isboolean(L, 5))
{ {
int id = g_engine->loadModel( int id = g_engine->loadModel(
lua_tostring(L, 1), /* name */ lua_tostring(L, 1), /* name */
lua_toboolean(L, 3), /* static */ lua_toboolean(L, 3), /* static */
lua_toboolean(L, 4), /* reference */ lua_toboolean(L, 4), /* reference */
lua_toboolean(L, 5), /* enable blending */
lua_tonumber(L, 2)); /* scale */ lua_tonumber(L, 2)); /* scale */
if (id > 0) if (id > 0)
{ {
@ -1030,28 +1032,30 @@ fail:
} }
static void addManagedObject(lua_State * L, bool is_static, static void addManagedObject(lua_State * L, bool is_static,
bool is_reference, bool is_reference, bool enable_blending,
OdeWorld::GeomType geom_type, refptr< vector<float> > args) OdeWorld::GeomType geom_type, refptr< vector<float> > args)
{ {
int id = g_engine->addObject(is_static, is_reference, geom_type, args); int id = g_engine->addObject(is_static, is_reference, enable_blending,
geom_type, args);
createLuaObject(L, id); createLuaObject(L, id);
} }
int createBoxSpecify(lua_State * L) int createBoxSpecify(lua_State * L)
{ {
int argc = lua_gettop(L); int argc = lua_gettop(L);
if (argc == 5 if (argc == 6
&& lua_isnumber(L, 1) && lua_isnumber(L, 1)
&& lua_isnumber(L, 2) && lua_isnumber(L, 2)
&& lua_isnumber(L, 3) && lua_isnumber(L, 3)
&& lua_isboolean(L, 4) && lua_isboolean(L, 4)
&& lua_isboolean(L, 5)) && lua_isboolean(L, 5)
&& lua_isboolean(L, 6))
{ {
refptr< vector<float> > args = new vector<float>(); refptr< vector<float> > args = new vector<float>();
for (int i = 1; i <= 3; i++) for (int i = 1; i <= 3; i++)
args->push_back(lua_tonumber(L, i)); args->push_back(lua_tonumber(L, i));
addManagedObject(L, lua_toboolean(L, 4), lua_toboolean(L, 5), addManagedObject(L, lua_toboolean(L, 4), lua_toboolean(L, 5),
OdeWorld::BOX, args); lua_toboolean(L, 6), OdeWorld::BOX, args);
} }
else else
lua_pushnil(L); lua_pushnil(L);
@ -1061,15 +1065,16 @@ fail:
int createSphereSpecify(lua_State * L) int createSphereSpecify(lua_State * L)
{ {
int argc = lua_gettop(L); int argc = lua_gettop(L);
if (argc == 3 if (argc == 4
&& lua_isnumber(L, 1) && lua_isnumber(L, 1)
&& lua_isboolean(L, 2) && lua_isboolean(L, 2)
&& lua_isboolean(L, 3)) && lua_isboolean(L, 3)
&& lua_isboolean(L, 4))
{ {
refptr< vector<float> > args = new vector<float>(); refptr< vector<float> > args = new vector<float>();
args->push_back(lua_tonumber(L, 1)); args->push_back(lua_tonumber(L, 1));
addManagedObject(L, lua_toboolean(L, 2), lua_toboolean(L, 3), addManagedObject(L, lua_toboolean(L, 2), lua_toboolean(L, 3),
OdeWorld::SPHERE, args); lua_toboolean(L, 4), OdeWorld::SPHERE, args);
} }
else else
lua_pushnil(L); lua_pushnil(L);
@ -1079,10 +1084,10 @@ fail:
int createPlaneSpecify(lua_State * L) int createPlaneSpecify(lua_State * L)
{ {
int argc = lua_gettop(L); int argc = lua_gettop(L);
if (argc == 5 || argc == 7) if (argc == 6 || argc == 8)
{ {
bool valid = lua_isboolean(L, argc); bool valid = true;
for (int i = 1; i < argc; i++) for (int i = 1; i < argc - 1; i++)
{ {
if (!lua_isnumber(L, i)) if (!lua_isnumber(L, i))
{ {
@ -1093,10 +1098,10 @@ fail:
if (valid) if (valid)
{ {
refptr< vector<float> > args = new vector<float>(); refptr< vector<float> > args = new vector<float>();
for (int i = 1; i < argc; i++) for (int i = 1; i < argc - 1; i++)
args->push_back(lua_tonumber(L, i)); args->push_back(lua_tonumber(L, i));
addManagedObject(L, true, lua_toboolean(L, argc), addManagedObject(L, true, lua_toboolean(L, argc - 1),
OdeWorld::PLANE, args); lua_toboolean(L, argc), OdeWorld::PLANE, args);
return 1; return 1;
} }
} }
@ -1107,17 +1112,18 @@ fail:
int createCylinderSpecify(lua_State * L) int createCylinderSpecify(lua_State * L)
{ {
int argc = lua_gettop(L); int argc = lua_gettop(L);
if (argc == 4 if (argc == 5
&& lua_isnumber(L, 1) && lua_isnumber(L, 1)
&& lua_isnumber(L, 2) && lua_isnumber(L, 2)
&& lua_isboolean(L, 3) && lua_isboolean(L, 3)
&& lua_isboolean(L, 4)) && lua_isboolean(L, 4)
&& lua_isboolean(L, 5))
{ {
refptr< vector<float> > args = new vector<float>(); refptr< vector<float> > args = new vector<float>();
args->push_back(lua_tonumber(L, 1)); args->push_back(lua_tonumber(L, 1));
args->push_back(lua_tonumber(L, 2)); args->push_back(lua_tonumber(L, 2));
addManagedObject(L, lua_toboolean(L, 3), lua_toboolean(L, 4), addManagedObject(L, lua_toboolean(L, 3), lua_toboolean(L, 4),
OdeWorld::CYLINDER, args); lua_toboolean(L, 5), OdeWorld::CYLINDER, args);
} }
else else
lua_pushnil(L); lua_pushnil(L);
@ -1127,17 +1133,18 @@ fail:
int createCapsuleSpecify(lua_State * L) int createCapsuleSpecify(lua_State * L)
{ {
int argc = lua_gettop(L); int argc = lua_gettop(L);
if (argc == 4 if (argc == 5
&& lua_isnumber(L, 1) && lua_isnumber(L, 1)
&& lua_isnumber(L, 2) && lua_isnumber(L, 2)
&& lua_isboolean(L, 3) && lua_isboolean(L, 3)
&& lua_isboolean(L, 4)) && lua_isboolean(L, 4)
&& lua_isboolean(L, 5))
{ {
refptr< vector<float> > args = new vector<float>(); refptr< vector<float> > args = new vector<float>();
args->push_back(lua_tonumber(L, 1)); args->push_back(lua_tonumber(L, 1));
args->push_back(lua_tonumber(L, 2)); args->push_back(lua_tonumber(L, 2));
addManagedObject(L, lua_toboolean(L, 3), lua_toboolean(L, 4), addManagedObject(L, lua_toboolean(L, 3), lua_toboolean(L, 4),
OdeWorld::CAPSULE, args); lua_toboolean(L, 5), OdeWorld::CAPSULE, args);
} }
else else
lua_pushnil(L); lua_pushnil(L);

32
ag.lua
View File

@ -111,6 +111,7 @@ ag.loadModel = function(name, args)
local scale = 1.0 local scale = 1.0
local static = false local static = false
local reference = false local reference = false
local blending = false
if (type(args) == "table") then if (type(args) == "table") then
if (type(args.scale) == "number") then if (type(args.scale) == "number") then
scale = args.scale scale = args.scale
@ -121,13 +122,17 @@ ag.loadModel = function(name, args)
if (type(args.reference) == "boolean") then if (type(args.reference) == "boolean") then
reference = args.reference reference = args.reference
end end
if (type(args.enable_blending) == "boolean") then
blending = args.enable_blending
end
end end
return ag.loadModelSpecify(name, scale, static, reference) return ag.loadModelSpecify(name, scale, static, blending, reference)
end end
ag.createBox = function(x, y, z, args) ag.createBox = function(x, y, z, args)
local static = false local static = false
local reference = false local reference = false
local blending = false
if (type(args) == "table") then if (type(args) == "table") then
if (type(args.static) == "boolean") then if (type(args.static) == "boolean") then
static = args.static static = args.static
@ -135,13 +140,17 @@ ag.createBox = function(x, y, z, args)
if (type(args.reference) == "boolean") then if (type(args.reference) == "boolean") then
reference = args.reference reference = args.reference
end end
if (type(args.enable_blending) == "boolean") then
blending = args.enable_blending
end
end end
return ag.createBoxSpecify(x, y, z, static, reference) return ag.createBoxSpecify(x, y, z, static, reference, blending)
end end
ag.createSphere = function(radius, args) ag.createSphere = function(radius, args)
local static = false local static = false
local reference = false local reference = false
local blending = false
if (type(args) == "table") then if (type(args) == "table") then
if (type(args.static) == "boolean") then if (type(args.static) == "boolean") then
static = args.static static = args.static
@ -149,8 +158,11 @@ ag.createSphere = function(radius, args)
if (type(args.reference) == "boolean") then if (type(args.reference) == "boolean") then
reference = args.reference reference = args.reference
end end
if (type(args.enable_blending) == "boolean") then
blending = args.enable_blending
end
end end
return ag.createSphereSpecify(radius, static, reference) return ag.createSphereSpecify(radius, static, reference, blending)
end end
ag.createPlane = function(a, b, c, d, x, y, z) ag.createPlane = function(a, b, c, d, x, y, z)
@ -162,21 +174,26 @@ ag.createPlane = function(a, b, c, d, x, y, z)
t = z t = z
end end
local reference = false local reference = false
local blending = false
if (type(t) == "table") then if (type(t) == "table") then
if (type(t.reference) == "boolean") then if (type(t.reference) == "boolean") then
reference = t.reference reference = t.reference
end end
if (type(args.enable_blending) == "boolean") then
blending = args.enable_blending
end
end end
if (type(y) == "nil") then if (type(y) == "nil") then
return ag.createPlaneSpecify(a, b, c, d, reference) return ag.createPlaneSpecify(a, b, c, d, reference, blending)
else else
return ag.createPlaneSpecify(a, b, c, d, x, y, reference) return ag.createPlaneSpecify(a, b, c, d, x, y, reference, blending)
end end
end end
ag.createCapsule = function(radius, length, args) ag.createCapsule = function(radius, length, args)
local static = false local static = false
local reference = false local reference = false
local blending = false
if (type(args) == "table") then if (type(args) == "table") then
if (type(args.static) == "boolean") then if (type(args.static) == "boolean") then
static = args.static static = args.static
@ -184,6 +201,9 @@ ag.createCapsule = function(radius, length, args)
if (type(args.reference) == "boolean") then if (type(args.reference) == "boolean") then
reference = args.reference reference = args.reference
end end
if (type(args.enable_blending) == "boolean") then
blending = args.enable_blending
end
end end
return ag.createCapsuleSpecify(radius, length, static, reference) return ag.createCapsuleSpecify(radius, length, static, reference, blending)
end end