| 1 |
|
| 2 |
typedef unsigned char Byte; |
| 3 |
|
| 4 |
#include <math.h> |
| 5 |
|
| 6 |
#include <vector> |
| 7 |
#include <vcg/space/point3.h> |
| 8 |
|
| 9 |
using namespace vcg; |
| 10 |
using namespace std; |
| 11 |
|
| 12 |
#include "CubeMapSamp.h" |
| 13 |
#include "OctaMapSamp.h" |
| 14 |
|
| 15 |
#include "Mol.h" |
| 16 |
#include "AO.h" |
| 17 |
|
| 18 |
const float infty=1000; |
| 19 |
|
| 20 |
vector<bool> tmpbool(32*32,false); |
| 21 |
|
| 22 |
void AO::PrintBuffer(){ |
| 23 |
int target=32; |
| 24 |
int k=0; |
| 25 |
for (int y=0; y<target; y++){ |
| 26 |
for (int x=0; x<target; x++) { |
| 27 |
double b=buf[k]; |
| 28 |
if (b==infty) |
| 29 |
printf("%2s","*"); |
| 30 |
else |
| 31 |
printf("%2s","-"); |
| 32 |
if (tmpbool[k]) printf("'"); else printf(" "); |
| 33 |
|
| 34 |
k++; |
| 35 |
// printf("%7.3f ",buf[k++]); |
| 36 |
} |
| 37 |
printf("\n"); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
void AO::RenderSphere(float cx, float cy, float cz, float rad){ |
| 42 |
printf("Rendering %f %f %f %f...\n",cx,cy,cz,rad); |
| 43 |
/*cx*=bufscale; |
| 44 |
cy*=bufscale; |
| 45 |
rad*=bufscale;*/ |
| 46 |
int cxi=int(cx); |
| 47 |
int cyi=int(cy); |
| 48 |
|
| 49 |
for (int y=-(int)rad; y<=(int)rad; y++) |
| 50 |
for (int x=-(int)rad; x<=(int)rad; x++) |
| 51 |
{ |
| 52 |
float tmp=(-x*x-y*y+rad*rad); |
| 53 |
if (tmp>0) { |
| 54 |
tmp=sqrt(tmp)+cz; |
| 55 |
int i=index(x+cxi,y+cyi); |
| 56 |
if (buf[i]>tmp) buf[i]=tmp; |
| 57 |
//buf[i]=0; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
void AO::CheckAtom(QAtom &a){ |
| 64 |
|
| 65 |
int n=a.s.nsamp(); |
| 66 |
for (int i=0; i<n; i++) { |
| 67 |
Point3f f=a.s.dirrot[i]*a.trr; |
| 68 |
|
| 69 |
if (f[2]>0) { |
| 70 |
|
| 71 |
|
| 72 |
int bx=(int) (a.trp[0]+f[0]); |
| 73 |
int by=(int) (a.trp[1]+f[1]); |
| 74 |
tmpbool[index(bx,by)]=true;; |
| 75 |
if (buf[index(bx,by)] == infty ) { |
| 76 |
a.s.sum[i]+=f[2]; |
| 77 |
}; |
| 78 |
a.s.div[i]+=f[2]; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
AO::AO( Point3f _dir, Mol &m) { |
| 85 |
|
| 86 |
dir=_dir.Normalize(); |
| 87 |
|
| 88 |
// orthonormal basis |
| 89 |
Point3f ax,ay,az=dir; |
| 90 |
ax=az^Point3f(1,0,0); |
| 91 |
if (ax.SquaredNorm()<0.1) ax=az^Point3f(0,1,0); |
| 92 |
ax=ax.Normalize(); |
| 93 |
ay=(az^ax).Normalize(); |
| 94 |
|
| 95 |
// project... |
| 96 |
m.Transform(ax,ay,az); |
| 97 |
|
| 98 |
|
| 99 |
int target=32; // |
| 100 |
bufx=bufy=target; |
| 101 |
float bufscalex=target/(m.tx1-m.tx0); |
| 102 |
float bufscaley=target/(m.ty1-m.ty0); |
| 103 |
bufscale=(bufscalex<bufscaley)?bufscalex:bufscaley; |
| 104 |
|
| 105 |
m.ScaleTransl(bufscale); |
| 106 |
CubeMapSamp::Transform(ax,ay,az); |
| 107 |
|
| 108 |
printf("Scale=%f\n",bufscale); |
| 109 |
|
| 110 |
buf.resize(target*target,infty); |
| 111 |
|
| 112 |
for (int i=0; i < (int)m.atom.size()-1; i++) { |
| 113 |
QAtom &a=m.atom[i]; |
| 114 |
CheckAtom(a); |
| 115 |
RenderSphere( a.trp[0], a.trp[1], a.trp[2], a.trr ); |
| 116 |
PrintBuffer(); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
} |