| 1 |
typedef unsigned char Byte; |
| 2 |
|
| 3 |
#include <GL/glew.h> |
| 4 |
#include "CgUtil.h" |
| 5 |
|
| 6 |
//#include <GL/gl.h> |
| 7 |
#include <math.h> |
| 8 |
//#include <GL/glu.h> |
| 9 |
|
| 10 |
#include <vector> |
| 11 |
|
| 12 |
#include <vcg/space/point3.h> |
| 13 |
#include <vcg/space/color4.h> |
| 14 |
|
| 15 |
using namespace vcg; |
| 16 |
using namespace std; |
| 17 |
|
| 18 |
#include "CubeMapSamp.h" |
| 19 |
#include "OctaMapSamp.h" |
| 20 |
|
| 21 |
#include "Mol.h" |
| 22 |
#include "AOgpu.h" |
| 23 |
#include "HardSettings.h" |
| 24 |
|
| 25 |
extern int CSIZE; |
| 26 |
extern int used_mapping; |
| 27 |
|
| 28 |
bool SaveImagePPM( const char * filename , const Byte *im, int sizex, int sizey); |
| 29 |
void ReloadTexture(vector<Byte> t, bool bilinear); |
| 30 |
float extractCurrentScaleFactor(); |
| 31 |
|
| 32 |
|
| 33 |
CgUtil mySettings; |
| 34 |
|
| 35 |
vector<Byte> AOgpu::fakeTexture; |
| 36 |
vector<unsigned int> AOgpu::snap; |
| 37 |
vector<int> AOgpu::sum; |
| 38 |
unsigned int AOgpu::div; |
| 39 |
float AOgpu::areas; |
| 40 |
unsigned int AOgpu::mask=0; |
| 41 |
|
| 42 |
void swapbuffers(); |
| 43 |
|
| 44 |
void AOgpu::OpenGLSnap() |
| 45 |
{ |
| 46 |
GLint vp[4]; |
| 47 |
glGetIntegerv( GL_VIEWPORT,vp ); // Lettura viewport |
| 48 |
glPixelStorei( GL_PACK_ROW_LENGTH, 0); |
| 49 |
glPixelStorei( GL_PACK_ALIGNMENT, 1); |
| 50 |
snapx = vp[2]; |
| 51 |
snapy = vp[3]; |
| 52 |
|
| 53 |
//Create(tx,ty); |
| 54 |
if (snap.size()!=snapx*snapy+1) snap.resize(snapx*snapy+1); |
| 55 |
|
| 56 |
GLenum mtype = 0; |
| 57 |
|
| 58 |
int format=0; |
| 59 |
if(format==0) { |
| 60 |
format = GL_RGBA; |
| 61 |
mtype = GL_UNSIGNED_BYTE; |
| 62 |
} |
| 63 |
if(format==GL_DEPTH_COMPONENT) { |
| 64 |
format = GL_DEPTH_COMPONENT; |
| 65 |
mtype = GL_FLOAT; |
| 66 |
} |
| 67 |
glReadPixels(vp[0],vp[1],vp[2],vp[3],format,mtype,(GLvoid *)&snap[0]); |
| 68 |
//SaveImagePPM("test.ppm" ,(Byte*)(&snap[0]), vp[2],vp[3]); |
| 69 |
|
| 70 |
//swapbuffers(); |
| 71 |
} |
| 72 |
|
| 73 |
AOgpu::AOgpu( Point3f _dir, Mol &m) { |
| 74 |
|
| 75 |
int out=hardSettings.TSIZE*hardSettings.TSIZE; |
| 76 |
glClearColor((out&&255)/255.0 , ((out>>8)&&255)/255.0, (out>>16)/255.0, 0 ); |
| 77 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 78 |
dir=_dir.Normalize(); |
| 79 |
|
| 80 |
// orthonormal basis |
| 81 |
Point3f ax,ay,az=dir; |
| 82 |
ax=az^Point3f(1,0,0); |
| 83 |
if (ax.SquaredNorm()<0.1) ax=az^Point3f(0,1,0); |
| 84 |
ax=ax.Normalize(); |
| 85 |
ay=(az^ax).Normalize(); |
| 86 |
|
| 87 |
//CubeMapSamp::Transform(ax,ay,az); |
| 88 |
|
| 89 |
// make a snapshot! |
| 90 |
|
| 91 |
glMatrixMode(GL_PROJECTION); |
| 92 |
glPushMatrix(); |
| 93 |
glLoadIdentity(); |
| 94 |
GLfloat nearPlane = 0.1; |
| 95 |
GLfloat farPlane = 200; |
| 96 |
glOrtho(-1,+1,-1,+1, nearPlane, farPlane); |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
glMatrixMode(GL_MODELVIEW); |
| 101 |
glPushMatrix(); |
| 102 |
glLoadIdentity(); |
| 103 |
gluLookAt( az[0],az[1],az[2], 0,0,0, ay[0], ay[1], ay[2] ); |
| 104 |
|
| 105 |
if (areas<0) { |
| 106 |
// only once: compute areas |
| 107 |
GLint vp[4]; |
| 108 |
glGetIntegerv( GL_VIEWPORT,vp ); |
| 109 |
float sc=extractCurrentScaleFactor()*(1/m.r); |
| 110 |
areas=vp[2]*vp[3]*sc*sc; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
mySettings.BindShaders(); |
| 115 |
|
| 116 |
m.Draw(); |
| 117 |
OpenGLSnap(); |
| 118 |
|
| 119 |
// interpret spanshot |
| 120 |
int k=0,h=0; |
| 121 |
|
| 122 |
//int* snapi=(int*)(&snap[0]); |
| 123 |
int max=snapy*snapx; |
| 124 |
int maxt=sum.size(); |
| 125 |
|
| 126 |
/*static*/ |
| 127 |
// vector<int> found(sum.size(), false ); |
| 128 |
|
| 129 |
for (int i=0; i<max; i++){ |
| 130 |
//if (snap[i]==0) printf(" "); |
| 131 |
//sum[snap[i]&0x00ffffff]++; |
| 132 |
//if (sum[snap[i]&0x00ffffff] ) |=mask; |
| 133 |
/* |
| 134 |
int dec=snap[i]&0x00ffffff; |
| 135 |
if (!found[dec]) { |
| 136 |
found[dec]=true; |
| 137 |
sum[ dec ] += snap[i]>>24; |
| 138 |
printf("[%d]",snap[i]>>24); |
| 139 |
}*/ |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
if (sum[ snap[i]&0x00ffffff ]>>24 !=div) { |
| 144 |
sum[ snap[i]&0x00ffffff ] = |
| 145 |
(sum[ snap[i]&0x00ffffff ]+(snap[i]>>24))&0x00ffffff | (div<<24); |
| 146 |
//printf("[%d]",snap[i]>>24); |
| 147 |
} |
| 148 |
|
| 149 |
/* |
| 150 |
sum[ snap[i]&0x00ffffff ] = |
| 151 |
( |
| 152 |
( |
| 153 |
(sum[ snap[i]&0x00ffffff ] + |
| 154 |
((sum[ snap[i]&0x00ffffff ]>>24)!=div)*(snap[i]>>24) |
| 155 |
)&0x00ffffff |
| 156 |
) |
| 157 |
| (div<<24) |
| 158 |
); */ |
| 159 |
|
| 160 |
} |
| 161 |
/* |
| 162 |
static Point3f sumv=Point3f(0,0,0); |
| 163 |
sumv+=az; |
| 164 |
printf("%d (%f %f %f) (%f %f %f)\n", div, az[0],az[1],az[2], sumv[0],sumv[1],sumv[2]); |
| 165 |
*/ |
| 166 |
|
| 167 |
mask<<=1; |
| 168 |
div++; |
| 169 |
glMatrixMode(GL_PROJECTION); |
| 170 |
glPopMatrix(); |
| 171 |
glMatrixMode(GL_MODELVIEW); |
| 172 |
glPopMatrix(); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
void AOgpu::Reset(Mol &m){ |
| 177 |
|
| 178 |
mySettings.SetForOffLine(); |
| 179 |
|
| 180 |
if (fakeTexture.size()==0) { |
| 181 |
// Prepare And Load fake texture |
| 182 |
fakeTexture.resize(hardSettings.TSIZE*hardSettings.TSIZE*3); |
| 183 |
int k=0, i=0; |
| 184 |
for (int x=0; x<hardSettings.TSIZE; x++) |
| 185 |
for (int y=0; y<hardSettings.TSIZE; y++) { |
| 186 |
fakeTexture[i++]=k&255; |
| 187 |
fakeTexture[i++]=(k>>8)&255; |
| 188 |
fakeTexture[i++]=(k>>16)&255; |
| 189 |
k++; |
| 190 |
} |
| 191 |
m.DuplicateTexels(fakeTexture, hardSettings.TSIZE); |
| 192 |
} |
| 193 |
|
| 194 |
glActiveTextureARB(GL_TEXTURE0_ARB); |
| 195 |
//glBindTexture(GL_TEXTURE_2D, molTexture); |
| 196 |
//ReloadTexture(fakeTexture, false); |
| 197 |
|
| 198 |
div=0; |
| 199 |
if (sum.size()!=hardSettings.TSIZE*hardSettings.TSIZE) { |
| 200 |
sum.resize(hardSettings.TSIZE*hardSettings.TSIZE,0); |
| 201 |
} else { |
| 202 |
sum.clear(); |
| 203 |
sum.resize(hardSettings.TSIZE*hardSettings.TSIZE,0); |
| 204 |
} |
| 205 |
mask=1; |
| 206 |
areas=-1; |
| 207 |
|
| 208 |
mySettings.BindShaders(); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
inline unsigned int BitCount(unsigned int x){ |
| 213 |
x=(((x>>1)&x)&(0x88888888)) + ((x<<1)^x)&(0xAAAAAAAA) ; |
| 214 |
x = ((x>>2)&(0x33333333)) + ( x &(0x33333333)); |
| 215 |
x = ((x>>4)&(0x0F0F0F0F)) + ( x &(0x0F0F0F0F)); |
| 216 |
x = ((x>>8)&(0x00FF00FF)) + ( x &(0x00FF00FF)); |
| 217 |
return (x&0x0000FFFF)+(x>>16); |
| 218 |
} |
| 219 |
|
| 220 |
void AOgpu::GetFinalTexture(vector<Byte> &text,Mol &m){ |
| 221 |
int k=0,i=0; |
| 222 |
|
| 223 |
//m.SmoothTexture( sum, hardSettings.TSIZE ); |
| 224 |
int maxt=sum.size(); |
| 225 |
/*for (int i=0; i<maxt; i++){ |
| 226 |
sum[i]=BitCount(sum[i]); |
| 227 |
}*/ |
| 228 |
for (int i=0; i<maxt; i++){ |
| 229 |
sum[i]&=0x00ffffff; |
| 230 |
} |
| 231 |
|
| 232 |
m.FillTexture( text, sum, hardSettings.TSIZE, 4.0/float(div) /*8*2*255 / (div * areas )*/ ); |
| 233 |
m.DuplicateTexels(text, hardSettings.TSIZE); |
| 234 |
} |
| 235 |
|
| 236 |
|