1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
/***************************************************************************
AllegroVisitor.cpp - description
-------------------
begin : Sat Jan 6 2001
copyright : (C) 2001 by Henrik Enqvist
email : henqvist@excite.com
***************************************************************************/
#include "Private.h"
#include "AllegroVisitor.h"
#include "Group.h"
#include "Shape3D.h"
#include "Polygon.h"
#include "BillBoard.h"
#include "Config.h"
AllegroVisitor * AllegroVisitor::p_AllegroVisitor = NULL;
AllegroVisitor::AllegroVisitor(){
#if EM_USE_ALLEGRO
//
for (int a=0; a<256; a++) {
alleg_vtx_p[a] = &alleg_vtx[a];
alleg_clip_p[a] = &alleg_clip[a];
alleg_tmp_p[a] = &alleg_tmp[a];
}
m_iMode = EM_ALLEGRO_GCOL_TEX;
#endif // EM_USE_ALLEGRO
}
AllegroVisitor::~AllegroVisitor() {
p_AllegroVisitor = NULL;
}
AllegroVisitor * AllegroVisitor::getInstance() {
if (p_AllegroVisitor == NULL) {
p_AllegroVisitor = new AllegroVisitor();
}
return p_AllegroVisitor;
}
void AllegroVisitor::empty() {
#if EM_USE_ALLEGRO
switch (m_iMode) {
case EM_ALLEGRO_GCOL_TEX: {
drawing_mode(DRAW_MODE_SOLID, NULL, 0, 0);
} break;
case EM_ALLEGRO_GCOL_TEX_TRANS: {
drawing_mode(DRAW_MODE_SOLID, NULL, 0, 0);
} break;
case EM_ALLEGRO_CLEAN: {
} break;
}
#endif // EM_USE_ALLEGRO
}
#if EM_USE_ALLEGRO
void AllegroVisitor::visit(Group * g) {
int filter = Config::getInstance()->getGLFilter();
switch (m_iMode) {
case EM_ALLEGRO_GCOL_TEX: {
// shapes
vector<Shape3D*>::iterator shapeIter = g->m_vShape3D.begin();
vector<Shape3D*>::iterator shapeEnd = g->m_vShape3D.end();
for ( ; shapeIter != shapeEnd; ++shapeIter) {
if (EM_SHAPE3D_HIDDEN & (*shapeIter)->m_iProperties) continue;
vector<Polygon3D*>::iterator polyIter = (*shapeIter)->m_vPolygon.begin();
vector<Polygon3D*>::iterator polyEnd = (*shapeIter)->m_vPolygon.end();
for ( ; polyIter != polyEnd; polyIter++) {
// if ((*polyIter)->m_iProperties & EM_POLY_HIDDEN) continue;
if ((*polyIter)->m_iProperties & EM_POLY_TRANS) continue;
vector<int>::iterator indexIter = (*polyIter)->m_vIndex.begin();
vector<int>::iterator indexEnd = (*polyIter)->m_vIndex.end();
// 3.4 %
int index=0;
// TODO implement a fast float to int conversion and fast clamp;
// see section 2.1 i Game Prog Gems 2
if ((*shapeIter)->m_Texture == NULL || filter == -1) {
// color polygon
for (; indexIter != indexEnd; ++indexIter, ++index) {
#if 0
alleg_vtx[index].x = (*shapeIter)->m_vVtxAlign[(*indexIter)].x;
alleg_vtx[index].y = (*shapeIter)->m_vVtxAlign[(*indexIter)].y;
alleg_vtx[index].z = (*shapeIter)->m_vVtxAlign[(*indexIter)].z;
alleg_vtx[index].u = 0;
alleg_vtx[index].v = 0;
alleg_vtx[index].c = 0xFFFFFF;
#else
// TODO Fix this slow copying !!!
alleg_vtx[index].x = (*shapeIter)->m_vVtxAlign[(*indexIter)].x;
alleg_vtx[index].y = (*shapeIter)->m_vVtxAlign[(*indexIter)].y;
alleg_vtx[index].z = (*shapeIter)->m_vVtxAlign[(*indexIter)].z;
float r = (*shapeIter)->m_vLitColor[(*indexIter)].r*255.0f;
float g = (*shapeIter)->m_vLitColor[(*indexIter)].g*255.0f;
float b = (*shapeIter)->m_vLitColor[(*indexIter)].b*255.0f;
int ir = (int)r;
int ig = (int)g;
int ib = (int)b;
alleg_vtx[index].c = makecol(EM_MIN(ir,255), EM_MIN(ig,255), EM_MIN(ib,255));
#endif
}
} else {
// textured polygon
for (; indexIter != indexEnd; ++indexIter, ++index) {
// TODO Fix this slow copying !!!
alleg_vtx[index].x = (*shapeIter)->m_vVtxAlign[(*indexIter)].x;
alleg_vtx[index].y = (*shapeIter)->m_vVtxAlign[(*indexIter)].y;
alleg_vtx[index].z = (*shapeIter)->m_vVtxAlign[(*indexIter)].z;
// TODO Not all textures are 256
alleg_vtx[index].u = (int)((*shapeIter)->m_vTexCoord[(*indexIter)].u*255.0f);
alleg_vtx[index].v = (int)((*shapeIter)->m_vTexCoord[(*indexIter)].v*255.0f);
float r = (*shapeIter)->m_vLitColor[(*indexIter)].r*255.0f;
float g = (*shapeIter)->m_vLitColor[(*indexIter)].g*255.0f;
float b = (*shapeIter)->m_vLitColor[(*indexIter)].b*255.0f;
int ir = (int)r;
int ig = (int)g;
int ib = (int)b;
ib = EM_MAX(ir, EM_MAX(ig, ib));
alleg_vtx[index].c = EM_MIN(ib, 255);
}
}
// 52.9 %
if (index < 3) continue;
EmAssert(index < 128, "AllegorVisitor::visit polygonedge amout to large");
// clip
int size = 0;
if ((*shapeIter)->m_Texture == NULL || filter == -1) {
size = clip3d_f(POLYTYPE_GCOL, 0.1, 1000, index,
(AL_CONST V3D_f**)alleg_vtx_p,alleg_clip_p, alleg_tmp_p, int_tmp);
} else {
size = clip3d_f(POLYTYPE_ATEX, 0.1, 1000, index, (AL_CONST V3D_f**)alleg_vtx_p,
alleg_clip_p, alleg_tmp_p, int_tmp);
}
if (size < 3) continue;
// project, if clause moved outside for loop for perfomance
// most likely scenario first
if (!((*shapeIter)->m_iProperties & (EM_SHAPE3D_BEHIND + EM_SHAPE3D_BEHIND2))) {
for (int a=0; a<size; ++a) {
persp_project_f(alleg_clip[a].x, alleg_clip[a].y, alleg_clip[a].z,
&alleg_clip[a].x, &alleg_clip[a].y);
}
} else if ((*shapeIter)->m_iProperties & EM_SHAPE3D_BEHIND) {
for (int a=0; a<size; ++a) {
persp_project_f(alleg_clip[a].x, alleg_clip[a].y, alleg_clip[a].z,
&alleg_clip[a].x, &alleg_clip[a].y);
alleg_clip[a].z += 0.1f;
}
} else if ((*shapeIter)->m_iProperties & EM_SHAPE3D_BEHIND2) {
for (int a=0; a<size; ++a) {
persp_project_f(alleg_clip[a].x, alleg_clip[a].y, alleg_clip[a].z,
&alleg_clip[a].x, &alleg_clip[a].y);
alleg_clip[a].z += 0.2f;
}
}
// cull
if (polygon_z_normal_f(alleg_clip_p[0], alleg_clip_p[1], alleg_clip_p[2]) < 0) continue;
//53.5 %
// render
if ((*shapeIter)->m_Texture == NULL || filter == -1) {
polygon3d_f(backbuffer, POLYTYPE_GCOL | POLYTYPE_ZBUF, NULL, size, alleg_clip_p);
} else {
polygon3d_f(backbuffer, POLYTYPE_ATEX_LIT | POLYTYPE_ZBUF, (*shapeIter)->m_Texture,
size, alleg_clip_p);
}
// 72 % (79%)
}
}
// billboard TODO
BillBoard * b = g->p_BillBoard;
if (b != NULL && b->m_iProperties & EM_BILLBOARD_TRANS) {
EmAssert(b->m_Texture != NULL, "all billboards must have a texture");
EM_COUT_D("AllegroVisitor::visit() BillBoard at " << b->m_vtxAlign.x <<" "<<
b->m_vtxAlign.y <<" "<< b->m_vtxAlign.z, 0);
}
} break;
case EM_ALLEGRO_GCOL_TEX_TRANS: {
// shapes
vector<Shape3D*>::iterator shapeIter = g->m_vShape3D.begin();
vector<Shape3D*>::iterator shapeEnd = g->m_vShape3D.end();
for ( ; shapeIter != shapeEnd; ++shapeIter) {
if (EM_SHAPE3D_HIDDEN & (*shapeIter)->m_iProperties) continue;
if (!(EM_SHAPE3D_USE_TRANS & (*shapeIter)->m_iProperties)) continue;
vector<Polygon3D*>::iterator polyIter = (*shapeIter)->m_vPolygon.begin();
vector<Polygon3D*>::iterator polyEnd = (*shapeIter)->m_vPolygon.end();
for ( ; polyIter != polyEnd; ++polyIter) {
// if ((*polyIter)->m_iProperties & EM_POLY_HIDDEN) continue;
if (!((*polyIter)->m_iProperties & EM_POLY_TRANS)) continue;
vector<int>::iterator indexIter = (*polyIter)->m_vIndex.begin();
vector<int>::iterator indexEnd = (*polyIter)->m_vIndex.end();
int index=0;
if ((*shapeIter)->m_Texture == NULL || filter == -1) {
// color polygon
for (; indexIter != indexEnd; ++indexIter, ++index) {
// TODO Fix this slow copying !!!
alleg_vtx[index].x = (*shapeIter)->m_vVtxAlign[(*indexIter)].x;
alleg_vtx[index].y = (*shapeIter)->m_vVtxAlign[(*indexIter)].y;
alleg_vtx[index].z = (*shapeIter)->m_vVtxAlign[(*indexIter)].z;
// TODO implement a fast float to int conversion and fast clamp;
// see section 2.1 i Game Prog Gems 2
float r = (*shapeIter)->m_vLitColor[(*indexIter)].r*255.0f;
float g = (*shapeIter)->m_vLitColor[(*indexIter)].g*255.0f;
float b = (*shapeIter)->m_vLitColor[(*indexIter)].b*255.0f;
int ir = (int)r;
int ig = (int)g;
int ib = (int)b;
alleg_vtx[index].c = makecol(EM_MIN(ir,255), EM_MIN(ig,255), EM_MIN(ib,255));
}
} else {
// textured polygon
for (; indexIter != indexEnd; ++indexIter, ++index) {
// TODO Fix this slow copying !!!
alleg_vtx[index].x = (*shapeIter)->m_vVtxAlign[(*indexIter)].x;
alleg_vtx[index].y = (*shapeIter)->m_vVtxAlign[(*indexIter)].y;
alleg_vtx[index].z = (*shapeIter)->m_vVtxAlign[(*indexIter)].z;
// TODO Not all textures are 255
alleg_vtx[index].u = (int)((*shapeIter)->m_vTexCoord[(*indexIter)].u*255.0f);
alleg_vtx[index].v = (int)((*shapeIter)->m_vTexCoord[(*indexIter)].v*255.0f);
// TODO implement a fast float to int conversion and fast clamp;
// see section 2.1 i Game Prog Gems 2
float r = (*shapeIter)->m_vLitColor[(*indexIter)].r*255.0f;
float g = (*shapeIter)->m_vLitColor[(*indexIter)].g*255.0f;
float b = (*shapeIter)->m_vLitColor[(*indexIter)].b*255.0f;
int ir = (int)r;
int ig = (int)g;
int ib = (int)b;
ib = EM_MAX(ir, EM_MAX(ig, ib));
alleg_vtx[index].c = EM_MIN(ib,255) ;
}
}
if (index < 3) continue;
EmAssert(index < 128, "AllegorVisitor::visit polygonedge amout to large");
// clip
int size = 0;
if ((*shapeIter)->m_Texture == NULL || filter == -1) {
size = clip3d_f(POLYTYPE_GCOL, 0.1, 1000, index,
(AL_CONST V3D_f**)alleg_vtx_p,alleg_clip_p, alleg_tmp_p, int_tmp);
} else {
size = clip3d_f(POLYTYPE_ATEX, 0.1, 1000, index, (AL_CONST V3D_f**)alleg_vtx_p,
alleg_clip_p, alleg_tmp_p, int_tmp);
}
if (size < 3) continue;
// project, if clause moved outside for loop for perfomance
if ((*shapeIter)->m_iProperties & EM_SHAPE3D_BEHIND) {
for (int a=0; a<size; ++a) {
persp_project_f(alleg_clip[a].x, alleg_clip[a].y, alleg_clip[a].z,
&alleg_clip[a].x, &alleg_clip[a].y);
alleg_clip[a].z += 0.1f;
}
} else if ((*shapeIter)->m_iProperties & EM_SHAPE3D_BEHIND2) {
for (int a=0; a<size; ++a) {
persp_project_f(alleg_clip[a].x, alleg_clip[a].y, alleg_clip[a].z,
&alleg_clip[a].x, &alleg_clip[a].y);
alleg_clip[a].z += 0.2f;
}
} else {
for (int a=0; a<size; ++a) {
persp_project_f(alleg_clip[a].x, alleg_clip[a].y, alleg_clip[a].z,
&alleg_clip[a].x, &alleg_clip[a].y);
}
}
// cull
if (polygon_z_normal_f(alleg_clip_p[0], alleg_clip_p[1], alleg_clip_p[2]) < 0) continue;
// render
if ((*shapeIter)->m_Texture == NULL || filter == -1) {
polygon3d_f(backbuffer, POLYTYPE_GCOL | POLYTYPE_ZBUF, NULL, size, alleg_clip_p);
} else {
polygon3d_f(backbuffer, POLYTYPE_ATEX_MASK_LIT | POLYTYPE_ZBUF,
(*shapeIter)->m_Texture, size, alleg_clip_p);
}
}
}
// billboard TODO
BillBoard * b = g->p_BillBoard;
if (b != NULL && b->m_iProperties & EM_BILLBOARD_TRANS) {
EmAssert(b->m_Texture != NULL, "all billboards must have a texture");
EM_COUT_D("AllegroVisitor::visit() BillBoard at " << b->m_vtxAlign.x <<" "<<
b->m_vtxAlign.y <<" "<< b->m_vtxAlign.z, 0);
}
} break;
}
}
#else
void AllegroVisitor::visit(Group *) {
}
#endif // EM_USE_ALLEGRO
|