00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "xrb_engine2_compound.hpp"
00012
00013 #include "xrb_engine2_polygon.hpp"
00014 #include "xrb_gl.hpp"
00015 #include "xrb_render.hpp"
00016 #include "xrb_rendercontext.hpp"
00017
00018 namespace Xrb
00019 {
00020
00021 Engine2::Compound::~Compound ()
00022 {
00023 DeleteArray(m_vertex_array);
00024 DeleteArray(m_polygon_array);
00025 }
00026
00027
00028
00029
00030
00031
00032 Engine2::Compound *Engine2::Compound::Create (Serializer &serializer)
00033 {
00034 Compound *retval = new Compound();
00035
00036
00037 retval->Object::ReadClassSpecific(serializer);
00038 retval->Compound::ReadClassSpecific(serializer);
00039
00040 return retval;
00041 }
00042
00043 void Engine2::Compound::Write (Serializer &serializer) const
00044 {
00045 WriteObjectType(serializer);
00046
00047 Object::WriteClassSpecific(serializer);
00048 Compound::WriteClassSpecific(serializer);
00049 }
00050
00051 void Engine2::Compound::Draw (
00052 DrawData const &draw_data,
00053 Float const alpha_mask) const
00054 {
00055 if (draw_data.GetRenderContext().MaskAndBiasWouldResultInNoOp())
00056 return;
00057
00058
00059 glMatrixMode(GL_MODELVIEW);
00060
00061
00062
00063 glPushMatrix();
00064
00065
00066
00067 glTranslatef(
00068 Translation()[Dim::X],
00069 Translation()[Dim::Y],
00070 ZDepth());
00071 glRotatef(Angle(), 0.0f, 0.0f, 1.0f);
00072 glScalef(
00073 ScaleFactors()[Dim::X],
00074 ScaleFactors()[Dim::Y],
00075 1.0f);
00076
00077
00078 Color color_bias(draw_data.GetRenderContext().BlendedColorBias(ColorBias()));
00079
00080 Color color_mask(draw_data.GetRenderContext().MaskedColor(ColorMask()));
00081 color_mask[Dim::A] *= alpha_mask;
00082
00083
00084 Render::SetupTextureUnits(GL::GLTexture_OpaqueWhite().Handle(), color_mask, color_bias);
00085
00086
00087 glActiveTexture(GL_TEXTURE0);
00088
00089 for (Uint32 i = 0; i < m_polygon_count; ++i)
00090 m_polygon_array[i].Draw();
00091
00092 glPopMatrix();
00093 }
00094
00095 Engine2::Compound::Compound ()
00096 :
00097 Engine2::Object(OT_COMPOUND)
00098 {
00099 m_vertex_count = 0;
00100 m_vertex_array = NULL;
00101 m_polygon_count = 0;
00102 m_polygon_array = NULL;
00103 }
00104
00105 void Engine2::Compound::ReadClassSpecific (Serializer &serializer)
00106 {
00107 ASSERT1(serializer.GetIODirection() == IOD_READ);
00108 ASSERT1(m_vertex_count == 0);
00109 ASSERT1(m_vertex_array == NULL);
00110
00111 m_vertex_count = serializer.ReadUint32();
00112 ASSERT1(m_vertex_count > 0);
00113 m_vertex_array = new FloatVector2[m_vertex_count];
00114 for (Uint32 i = 0; i < m_vertex_count; ++i)
00115 serializer.ReadFloatVector2(&m_vertex_array[i]);
00116
00117 m_polygon_count = serializer.ReadUint32();
00118 ASSERT1(m_polygon_count > 0);
00119 m_polygon_array = new Polygon[m_polygon_count];
00120 for (Uint32 i = 0; i < m_polygon_count; ++i)
00121 m_polygon_array[i].Read(serializer, m_vertex_array);
00122 }
00123
00124 void Engine2::Compound::WriteClassSpecific (Serializer &serializer) const
00125 {
00126 ASSERT1(serializer.GetIODirection() == IOD_WRITE);
00127 ASSERT1(m_vertex_count > 0);
00128 ASSERT1(m_vertex_array != NULL);
00129 ASSERT1(m_polygon_count > 0);
00130 ASSERT1(m_polygon_array != NULL);
00131
00132 serializer.WriteUint32(m_vertex_count);
00133 for (Uint32 i = 0; i < m_vertex_count; ++i)
00134 serializer.WriteFloatVector2(m_vertex_array[i]);
00135
00136 serializer.WriteUint32(m_polygon_count);
00137 for (Uint32 i = 0; i < m_polygon_count; ++i)
00138 m_polygon_array[i].Write(serializer, m_vertex_array);
00139 }
00140
00141 void Engine2::Compound::CalculateRadius (QuadTreeType quad_tree_type) const
00142 {
00143 ASSERT1(quad_tree_type == QTT_VISIBILITY || quad_tree_type == QTT_PHYSICS_HANDLER);
00144
00145
00146
00147
00148 m_radius[quad_tree_type] = 0.0f;
00149 for (Uint32 i = 0; i < m_vertex_count; ++i)
00150 {
00151 m_radius[quad_tree_type] = Max(
00152 m_radius[quad_tree_type],
00153 (Transformation() * m_vertex_array[i] -
00154 Transformation() * FloatVector2::ms_zero).Length());
00155 }
00156 }
00157
00158 void Engine2::Compound::CloneProperties (Engine2::Object const *const object)
00159 {
00160 ASSERT1(object->GetObjectType() == OT_COMPOUND);
00161 Compound const *compound = DStaticCast<Compound const *>(object);
00162 ASSERT1(compound != NULL);
00163
00164 ASSERT1(m_vertex_count == 0);
00165 ASSERT1(m_vertex_array == NULL);
00166
00167 m_vertex_count = compound->m_vertex_count;
00168 m_vertex_array = new FloatVector2[m_vertex_count];
00169 for (Uint32 i = 0; i < m_vertex_count; ++i)
00170 m_vertex_array[i] = compound->m_vertex_array[i];
00171
00172 m_polygon_count = compound->m_polygon_count;
00173 m_polygon_array = new Polygon[m_polygon_count];
00174 for (Uint32 i = 0; i < m_polygon_count; ++i)
00175 m_polygon_array[i].CloneProperties(
00176 &compound->m_polygon_array[i],
00177 compound->m_vertex_array,
00178 m_vertex_array);
00179 }
00180
00181 }
00182