| 1 |
typedef unsigned char Byte; |
| 2 |
typedef unsigned int uint; |
| 3 |
|
| 4 |
#include <GL/glew.h> |
| 5 |
#include "CgUtil.h" |
| 6 |
|
| 7 |
//#include <GL/gl.h> |
| 8 |
#include <math.h> |
| 9 |
//#include <GL/glu.h> |
| 10 |
|
| 11 |
#include <vector> |
| 12 |
|
| 13 |
#include <vcg/space/point3.h> |
| 14 |
#include <vcg/space/color4.h> |
| 15 |
|
| 16 |
#include "gen_normal.h" |
| 17 |
#include <wrap/gui/trackball.h> |
| 18 |
|
| 19 |
using namespace vcg; |
| 20 |
using namespace std; |
| 21 |
|
| 22 |
#include "CubeMapSamp.h" |
| 23 |
#include "OctaMapSamp.h" |
| 24 |
#include "Mol.h" |
| 25 |
//#include "AOgpu.h" |
| 26 |
|
| 27 |
#include "Common.h" |
| 28 |
#include "HardSettings.h" |
| 29 |
#include "MyCanvas.h" |
| 30 |
|
| 31 |
#include "ShadowMap.h" |
| 32 |
|
| 33 |
extern CgUtil shadowSettings; |
| 34 |
extern CgUtil shadowSettingsAcc; |
| 35 |
|
| 36 |
GeoSettings geoSettings; // singleton |
| 37 |
|
| 38 |
|
| 39 |
extern Mol mol; |
| 40 |
|
| 41 |
double aniStep=-1; |
| 42 |
|
| 43 |
void setAniStep(double step){ |
| 44 |
aniStep=step; |
| 45 |
} |
| 46 |
|
| 47 |
void stopAni(){ |
| 48 |
aniStep=-1; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
void GeoSettings::Apply(){ |
| 53 |
if (mol.IsReady()) { |
| 54 |
if (mode==BALL_N_STICKS) { |
| 55 |
mol.SetBallAndSticks(stickRadius); |
| 56 |
} |
| 57 |
if (mode==LICORICE) { |
| 58 |
mol.SetLicorice(licoRadius); |
| 59 |
} |
| 60 |
if (mode==SPACE_FILL) mol.SetSpaceFill(); |
| 61 |
} |
| 62 |
|
| 63 |
cgSettings.setGeoSettings(*this); |
| 64 |
cgSettings.UpdateShaders(); |
| 65 |
|
| 66 |
UpdateShadowmap(); |
| 67 |
|
| 68 |
mol.ResetAO(); |
| 69 |
} |
| 70 |
|
| 71 |
void SetColMode(float mode){ |
| 72 |
mol.SetColMode(mode); |
| 73 |
} |
| 74 |
|
| 75 |
void ResetColMode(){ |
| 76 |
float tmp=mol.colMode; |
| 77 |
mol.colMode=-1; |
| 78 |
mol.SetColMode(tmp); |
| 79 |
} |
| 80 |
|
| 81 |
float GetColMode(){ |
| 82 |
return mol.colMode; |
| 83 |
} |
| 84 |
|
| 85 |
void GeoSettings::ApplyColor(){ |
| 86 |
cgSettings.setGeoSettings(*this); |
| 87 |
cgSettings.UpdateShaders(); |
| 88 |
} |
| 89 |
|
| 90 |
using namespace vcg; |
| 91 |
using namespace std; |
| 92 |
|
| 93 |
|
| 94 |
// VIS06 presentaiton modes: |
| 95 |
bool draw_balls=true; |
| 96 |
bool draw_sticks=true; |
| 97 |
bool draw_wireframe_sticks=false; |
| 98 |
bool draw_wireframe_balls=false; |
| 99 |
|
| 100 |
int winx=512,winy=winx; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
int CSIZE; // size (texels) per atom patches |
| 105 |
int BSIZE; // size (texels) per pond patches |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
vcg::Trackball track; |
| 110 |
vcg::Trackball lightTrack; |
| 111 |
bool MovingLightMode=false; |
| 112 |
|
| 113 |
|
| 114 |
Mol mol; |
| 115 |
ShadowMap shadowmap(mol); |
| 116 |
CgUtil cgSettings; |
| 117 |
|
| 118 |
int GetCurrentHetatm(){ |
| 119 |
return mol.nhetatm; |
| 120 |
} |
| 121 |
|
| 122 |
int GetCurrentAtm(){ |
| 123 |
return mol.natm; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
//void DrawTmp(); |
| 128 |
|
| 129 |
void UpdateShadowmap(){ |
| 130 |
ShadowMap::Update(); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
void drawFrame(); // def later... |
| 135 |
|
| 136 |
Byte* GetSnapshot(int sx, int sy, bool alpha){ |
| 137 |
|
| 138 |
|
| 139 |
//static uint textureSnap = 666; |
| 140 |
//static uint frameSnap; |
| 141 |
|
| 142 |
// set offline rendering |
| 143 |
mainCanvas.RedirectToMemory(); |
| 144 |
mainCanvas.SetRes(sx); |
| 145 |
//hardsx=mainCanvas.GetHardRes(); |
| 146 |
|
| 147 |
if (!mainCanvas.SetAsOutput()) return NULL; |
| 148 |
|
| 149 |
drawFrame(); |
| 150 |
|
| 151 |
// capture frame |
| 152 |
sx=sy=mainCanvas.GetHardRes(); |
| 153 |
Byte* res=new(Byte[sx*sy*4]); |
| 154 |
glReadPixels(0,0,sx,sy,alpha?GL_RGBA:GL_RGB,GL_UNSIGNED_BYTE,res); |
| 155 |
|
| 156 |
|
| 157 |
mainCanvas.RedirectToVideo(); |
| 158 |
mainCanvas.SetAsOutput(); |
| 159 |
|
| 160 |
return res; |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
void setProjection(int res){ |
| 166 |
|
| 167 |
int winx,winy; |
| 168 |
winx=winy=res; |
| 169 |
|
| 170 |
glMatrixMode(GL_PROJECTION); |
| 171 |
glLoadIdentity(); |
| 172 |
GLfloat nearPlane = 0.01; |
| 173 |
GLfloat farPlane = 10000; |
| 174 |
float size=1.2f; |
| 175 |
float ratio=size*(float)winx / winy; |
| 176 |
if (cgSettings.projmode==CgUtil::PERSPECTIVE) { |
| 177 |
gluPerspective(60.0, ratio, nearPlane, farPlane); |
| 178 |
} |
| 179 |
else |
| 180 |
// if(winx<winy) glOrtho(-1,+1,-(float)winy / winx,+(float)winy / winx, 1, 201); |
| 181 |
// else glOrtho(-(float)winx / winy,+(float)winx / winy,-1,+1, 1, 201); |
| 182 |
// if(winx<winy) glOrtho(-size,+size,-ratio,+ratio, 1, 201); |
| 183 |
// else glOrtho(-ratio,+ratio,-size,+size, 1, 201); |
| 184 |
if(winx<winy) glOrtho(-1,+1,-(float)winy / winx,+(float)winy / winx, 1, 201); |
| 185 |
else glOrtho(-(float)winx / winy,+(float)winx / winy,-1,+1, 40-2, 40+200); |
| 186 |
|
| 187 |
glViewport(0, 0, winx,winy); |
| 188 |
|
| 189 |
glMatrixMode(GL_MODELVIEW); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
void MakeHiqualityScreen(int quality){ |
| 196 |
int curres=mainCanvas.GetSoftRes(); |
| 197 |
|
| 198 |
mainCanvas.RedirectToMemory(); |
| 199 |
|
| 200 |
mainCanvas.SetRes(curres * quality / 100 ); |
| 201 |
|
| 202 |
if (!mainCanvas.SetAsOutput()) { |
| 203 |
// something went wrong. Fall-back: do a normal screenshot |
| 204 |
mainCanvas.RedirectToVideo(); |
| 205 |
mainCanvas.SetAsOutput(); |
| 206 |
drawFrame(); |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
float HSratio = float(mainCanvas.GetSoftRes()) / mainCanvas.GetHardRes(); |
| 211 |
|
| 212 |
drawFrame(); |
| 213 |
|
| 214 |
mainCanvas.RedirectToVideo(); |
| 215 |
mainCanvas.SetAsOutput(); |
| 216 |
|
| 217 |
glActiveTextureARB(GL_TEXTURE1_ARB); |
| 218 |
glDisable(GL_TEXTURE_2D); |
| 219 |
|
| 220 |
glActiveTextureARB(GL_TEXTURE0_ARB); |
| 221 |
mainCanvas.SetAsTexture(); |
| 222 |
|
| 223 |
glDisable(GL_FRAGMENT_PROGRAM_ARB); |
| 224 |
glDisable(GL_VERTEX_PROGRAM_ARB); |
| 225 |
|
| 226 |
setProjection(mainCanvas.GetSoftRes()); |
| 227 |
|
| 228 |
|
| 229 |
glMatrixMode(GL_MODELVIEW); |
| 230 |
//glPushMatrix(); |
| 231 |
glLoadIdentity(); |
| 232 |
|
| 233 |
//glClearColor(1,1,1,1); |
| 234 |
//glClear(GL_COLOR_BUFFER_BIT); |
| 235 |
|
| 236 |
glEnable(GL_TEXTURE_2D); |
| 237 |
glDisable(GL_DEPTH_TEST); |
| 238 |
|
| 239 |
|
| 240 |
//glColor3f(1,0.75,0.75); // <= TEST HQ |
| 241 |
glColor3f(1,1,1); |
| 242 |
|
| 243 |
float z=-45; |
| 244 |
|
| 245 |
glBegin(GL_QUADS); |
| 246 |
|
| 247 |
glTexCoord2f(0,0);glVertex3f(-1,-1, z); |
| 248 |
glTexCoord2f(HSratio,0);glVertex3f(+1,-1, z); |
| 249 |
glTexCoord2f(HSratio,HSratio);glVertex3f(+1,+1, z); |
| 250 |
glTexCoord2f(0,HSratio);glVertex3f(-1,+1, z); |
| 251 |
glEnd(); |
| 252 |
|
| 253 |
glEnable(GL_FRAGMENT_PROGRAM_ARB); |
| 254 |
glEnable(GL_VERTEX_PROGRAM_ARB); |
| 255 |
glEnable(GL_DEPTH_TEST); |
| 256 |
|
| 257 |
//glMatrixMode(GL_MODELVIEW);glPopMatrix(); |
| 258 |
//glMatrixMode(GL_PROJECTION);glPopMatrix(); |
| 259 |
|
| 260 |
|
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
void drawFrame(int quality){ |
| 265 |
if (quality==100) { |
| 266 |
drawFrame(); |
| 267 |
} else MakeHiqualityScreen(quality); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
// pos camera, coordinate polari |
| 272 |
double dist=3.0, alpha=0.0, beta=0.0; |
| 273 |
|
| 274 |
//vector<Byte> texture(hardSettigs.TSIZE*hardSettigs.TSIZE*3,128); |
| 275 |
|
| 276 |
void QAtom::DrawHalo(){ |
| 277 |
|
| 278 |
if ((!geoSettings.showHetatm)&&(hetatomFlag)) return; |
| 279 |
|
| 280 |
float s=cgSettings.P_halo_size * 2.5; |
| 281 |
|
| 282 |
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, r+s, (r+s)*(r+s) / (s*s+2*r*s)); |
| 283 |
|
| 284 |
glTexCoord2f(+1,+1); |
| 285 |
glVertex3f(px,py,pz); |
| 286 |
|
| 287 |
glTexCoord2f(-1,+1); |
| 288 |
glVertex3f(px,py,pz); |
| 289 |
|
| 290 |
glTexCoord2f(-1,-1); |
| 291 |
glVertex3f(px,py,pz); |
| 292 |
|
| 293 |
glTexCoord2f(+1,-1); |
| 294 |
glVertex3f(px,py,pz); |
| 295 |
|
| 296 |
} |
| 297 |
|
| 298 |
void QAtom::Draw(){ |
| 299 |
|
| 300 |
if ((!geoSettings.showHetatm)&&(hetatomFlag)) return; |
| 301 |
|
| 302 |
glColor3f(cr,cg,cb); |
| 303 |
glTexCoord2f(tx/float(moltextureCanvas.GetHardRes()),ty/float(moltextureCanvas.GetHardRes())); |
| 304 |
|
| 305 |
glNormal3f(+1,+1, r); |
| 306 |
glVertex3f(px,py,pz); |
| 307 |
|
| 308 |
glNormal3f(-1,+1, r); |
| 309 |
glVertex3f(px,py,pz); |
| 310 |
|
| 311 |
glNormal3f(-1,-1, r); |
| 312 |
glVertex3f(px,py,pz); |
| 313 |
|
| 314 |
glNormal3f(+1,-1, r); |
| 315 |
glVertex3f(px,py,pz); |
| 316 |
} |
| 317 |
|
| 318 |
void Bond::DrawHalo(){ |
| 319 |
|
| 320 |
if ((!geoSettings.showHetatm)&&(hetatomFlag)) return; |
| 321 |
|
| 322 |
glTexCoord4f(dir[0],dir[1],dir[2], 1.0/lenght); |
| 323 |
|
| 324 |
glNormal3f(+1,+1,0); |
| 325 |
glVertex3f(a[0],a[1],a[2]); |
| 326 |
glNormal3f(-1,+1,0); |
| 327 |
glVertex3f(a[0],a[1],a[2]); |
| 328 |
|
| 329 |
glNormal3f(-1,-1,0); |
| 330 |
glVertex3f(b[0],b[1],b[2]); |
| 331 |
glNormal3f(+1,-1,0); |
| 332 |
glVertex3f(b[0],b[1],b[2]); |
| 333 |
|
| 334 |
} |
| 335 |
|
| 336 |
void Bond::Draw(){ |
| 337 |
|
| 338 |
if ((!geoSettings.showHetatm)&&(hetatomFlag)) return; |
| 339 |
|
| 340 |
// glColor3f(cr,cg,cb); |
| 341 |
// glTexCoord2f(tx/float(TSIZE),ty/float(TSIZE)); |
| 342 |
|
| 343 |
glTexCoord4f(dir[0],dir[1],dir[2], 1.0/lenght); |
| 344 |
//glMultiTexCoord3fARB(GL_TEXTURE3_ARB, col1[0],col1[1],col1[2] ); |
| 345 |
glColor3fv(&(col1[0])); |
| 346 |
glSecondaryColor3fv(&col2[0]); |
| 347 |
//glMultiTexCoord3fARB(GL_TEXTURE4_ARB, col2[0],col2[1],col2[2] ); |
| 348 |
glMultiTexCoord3fARB(GL_TEXTURE1_ARB, startp[0],startp[1],startp[2] ); |
| 349 |
glMultiTexCoord2fARB(GL_TEXTURE2_ARB, tx/float(moltextureCanvas.GetHardRes()),ty/float(moltextureCanvas.GetHardRes())); |
| 350 |
|
| 351 |
glNormal3f(+1,+1,0); |
| 352 |
glVertex3f(a[0],a[1],a[2]); |
| 353 |
glNormal3f(-1,+1,0); |
| 354 |
glVertex3f(a[0],a[1],a[2]); |
| 355 |
|
| 356 |
glNormal3f(-1,-1,0); |
| 357 |
glVertex3f(b[0],b[1],b[2]); |
| 358 |
glNormal3f(+1,-1,0); |
| 359 |
glVertex3f(b[0],b[1],b[2]); |
| 360 |
|
| 361 |
} |
| 362 |
|
| 363 |
void QAtom::DrawShadowmap(){ |
| 364 |
|
| 365 |
if ((!geoSettings.showHetatm)&&(hetatomFlag)) return; |
| 366 |
|
| 367 |
glNormal3f(+1,+1, r); |
| 368 |
glVertex3f(px,py,pz); |
| 369 |
|
| 370 |
glNormal3f(-1,+1, r); |
| 371 |
glVertex3f(px,py,pz); |
| 372 |
|
| 373 |
glNormal3f(-1,-1, r); |
| 374 |
glVertex3f(px,py,pz); |
| 375 |
|
| 376 |
glNormal3f(+1,-1, r); |
| 377 |
glVertex3f(px,py,pz); |
| 378 |
} |
| 379 |
|
| 380 |
void Bond::DrawShadowmap(){ |
| 381 |
|
| 382 |
if ((!geoSettings.showHetatm)&&(hetatomFlag)) return; |
| 383 |
|
| 384 |
glTexCoord4f(dir[0],dir[1],dir[2], 1.0/lenght); |
| 385 |
|
| 386 |
glNormal3f(+1,+1,0); |
| 387 |
glVertex3f(a[0],a[1],a[2]); |
| 388 |
glNormal3f(-1,+1,0); |
| 389 |
glVertex3f(a[0],a[1],a[2]); |
| 390 |
|
| 391 |
glNormal3f(-1,-1,0); |
| 392 |
glVertex3f(b[0],b[1],b[2]); |
| 393 |
glNormal3f(+1,-1,0); |
| 394 |
glVertex3f(b[0],b[1],b[2]); |
| 395 |
|
| 396 |
} |
| 397 |
|
| 398 |
float extractCurrentScaleFactor(float x[4][4]){ |
| 399 |
float det= x[0][0]*x[1][1]*x[2][2] |
| 400 |
-x[0][0]*x[1][2]*x[2][1] |
| 401 |
+x[1][0]*x[2][1]*x[0][2] |
| 402 |
-x[1][0]*x[0][1]*x[2][2] |
| 403 |
+x[2][0]*x[0][1]*x[1][2] |
| 404 |
-x[2][0]*x[1][1]*x[0][2]; |
| 405 |
return pow(fabs(det), 1.0f/3.0f); |
| 406 |
} |
| 407 |
|
| 408 |
float extractCurrentScaleFactor(){ |
| 409 |
float x[4][4]; |
| 410 |
glGetFloatv(GL_MODELVIEW_MATRIX, &(x[0][0])); |
| 411 |
return extractCurrentScaleFactor(x); |
| 412 |
} |
| 413 |
|
| 414 |
void prepareDepthTextureForCurrentViewpoint(); |
| 415 |
|
| 416 |
|
| 417 |
void Mol::DrawHalos(){ |
| 418 |
|
| 419 |
// let's try to aviod THIS! |
| 420 |
prepareDepthTextureForCurrentViewpoint(); // hum, unavoidable. |
| 421 |
|
| 422 |
glPushMatrix(); |
| 423 |
glScalef(1/r,1/r,1/r); |
| 424 |
glTranslatef(-px,-py,-pz); |
| 425 |
|
| 426 |
float x[4][4], scalef; |
| 427 |
glGetFloatv(GL_MODELVIEW_MATRIX, &(x[0][0])); |
| 428 |
|
| 429 |
glProgramEnvParameter4fARB(GL_VERTEX_PROGRAM_ARB, 0, |
| 430 |
scalef=extractCurrentScaleFactor(x), 0,0,0 |
| 431 |
); |
| 432 |
|
| 433 |
glEnable(GL_VERTEX_PROGRAM_ARB); |
| 434 |
glEnable(GL_FRAGMENT_PROGRAM_ARB); |
| 435 |
|
| 436 |
glDepthMask(false); |
| 437 |
glEnable(GL_BLEND); |
| 438 |
|
| 439 |
if (cgSettings.doingAlphaSnapshot) |
| 440 |
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); |
| 441 |
else |
| 442 |
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 443 |
|
| 444 |
cgSettings.BindHaloShader( haloCanvas.GetResPow2() ); |
| 445 |
|
| 446 |
glProgramEnvParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0, |
| 447 |
(100.0+cgSettings.P_halo_aware*1300.0)/scalef/mol.r, 0,0,0 |
| 448 |
); |
| 449 |
|
| 450 |
glBegin(GL_QUADS); |
| 451 |
for (int i=0; i < (int)atom.size(); i++) atom[i].DrawHalo(); |
| 452 |
glEnd(); |
| 453 |
|
| 454 |
/* |
| 455 |
glBegin(GL_QUADS); |
| 456 |
for (int i=0; i<bond.size(); i++) bond[i].DrawHalo(); |
| 457 |
glEnd(); |
| 458 |
*/ |
| 459 |
|
| 460 |
glDisable(GL_BLEND); |
| 461 |
cgSettings.BindShaders(); |
| 462 |
|
| 463 |
glDepthMask(true); |
| 464 |
|
| 465 |
glPopMatrix(); |
| 466 |
|
| 467 |
|
| 468 |
glDisable(GL_VERTEX_PROGRAM_ARB); |
| 469 |
glDisable(GL_FRAGMENT_PROGRAM_ARB); |
| 470 |
} |
| 471 |
|
| 472 |
void Mol::DrawShadowmap(bool invert){ |
| 473 |
glPushMatrix(); |
| 474 |
glScalef(1/r,1/r,1/r); |
| 475 |
glTranslatef(-px,-py,-pz); |
| 476 |
|
| 477 |
float x[4][4], scalef; |
| 478 |
glGetFloatv(GL_MODELVIEW_MATRIX, &(x[0][0])); |
| 479 |
|
| 480 |
glProgramEnvParameter4fARB(GL_VERTEX_PROGRAM_ARB, 0, |
| 481 |
scalef=extractCurrentScaleFactor(x),0,0,0 |
| 482 |
); |
| 483 |
|
| 484 |
/*if (invert) { |
| 485 |
glScalef(1,1,-1); |
| 486 |
}*/ |
| 487 |
|
| 488 |
glEnable(GL_VERTEX_PROGRAM_ARB); |
| 489 |
glEnable(GL_FRAGMENT_PROGRAM_ARB); |
| 490 |
|
| 491 |
glActiveTextureARB(GL_TEXTURE0_ARB); |
| 492 |
glDisable(GL_TEXTURE_2D); |
| 493 |
glActiveTextureARB(GL_TEXTURE1_ARB); |
| 494 |
glDisable(GL_TEXTURE_2D); |
| 495 |
|
| 496 |
|
| 497 |
shadowSettings.BindShaders(); |
| 498 |
glBegin(GL_QUADS); |
| 499 |
for (int i=0; i < (int)atom.size(); i++) atom[i].DrawShadowmap(); |
| 500 |
glEnd(); |
| 501 |
|
| 502 |
if (sticks) { |
| 503 |
shadowSettings.BindStickShaders(); |
| 504 |
|
| 505 |
glProgramEnvParameter4fARB(GL_VERTEX_PROGRAM_ARB, 0, |
| 506 |
scalef, stick_radius,stick_radius*2,0 ); |
| 507 |
|
| 508 |
|
| 509 |
glBegin(GL_QUADS); |
| 510 |
for (int i=0; i < (int)bond.size(); i++) bond[i].DrawShadowmap(); |
| 511 |
glEnd(); |
| 512 |
} |
| 513 |
glPopMatrix(); |
| 514 |
|
| 515 |
} |
| 516 |
|
| 517 |
void Mol::Draw(){ |
| 518 |
|
| 519 |
glPushMatrix(); |
| 520 |
glScalef(1/r,1/r,1/r); |
| 521 |
glTranslatef(-px,-py,-pz); |
| 522 |
|
| 523 |
|
| 524 |
float x[4][4], scalef; |
| 525 |
glGetFloatv(GL_MODELVIEW_MATRIX, &(x[0][0])); |
| 526 |
|
| 527 |
glProgramEnvParameter4fARB(GL_VERTEX_PROGRAM_ARB, 0, |
| 528 |
scalef=extractCurrentScaleFactor(x),0,0,0 |
| 529 |
); |
| 530 |
|
| 531 |
/* |
| 532 |
glProgramEnvParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 6, |
| 533 |
1.0 / ( scalef*scalef) , 0,0,0 |
| 534 |
);*/ |
| 535 |
/* printf("ScaleFactor= %f, ShadowScaleFactor= %f\n", |
| 536 |
scalef, shadowScaleFactor );*/ |
| 537 |
|
| 538 |
|
| 539 |
glEnable(GL_VERTEX_PROGRAM_ARB); |
| 540 |
glEnable(GL_TEXTURE_2D); |
| 541 |
|
| 542 |
glActiveTextureARB(GL_TEXTURE0_ARB); |
| 543 |
moltextureCanvas.SetAsTexture(); |
| 544 |
|
| 545 |
if (cgSettings.P_shadowstrenght>0) { |
| 546 |
ShadowMap::GetCurrentPVMatrix(); |
| 547 |
ShadowMap::FeedParameters(); |
| 548 |
} |
| 549 |
|
| 550 |
for (int i=0; i<3; i++) |
| 551 |
glProgramEnvParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, i, |
| 552 |
x[i][0],x[i][1],x[i][2],0 |
| 553 |
); |
| 554 |
|
| 555 |
glProgramEnvParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 6, |
| 556 |
mol.PredictAO(),0,0,0 |
| 557 |
); |
| 558 |
|
| 559 |
if (draw_balls) { |
| 560 |
if (draw_wireframe_balls) { |
| 561 |
glDisable(GL_FRAGMENT_PROGRAM_ARB); |
| 562 |
for (int i=0; i < (int)atom.size(); i++) { |
| 563 |
glColor3f(0,0,1); |
| 564 |
glBegin(GL_LINE_LOOP); |
| 565 |
atom[i].Draw(); |
| 566 |
glEnd(); |
| 567 |
} |
| 568 |
} else { |
| 569 |
glEnable(GL_VERTEX_PROGRAM_ARB); |
| 570 |
glEnable(GL_FRAGMENT_PROGRAM_ARB); |
| 571 |
//if (DL_atoms==666) glGenD |
| 572 |
|
| 573 |
glBegin(GL_QUADS); |
| 574 |
for (int i=0; i < (int)atom.size(); i++) atom[i].Draw(); |
| 575 |
glEnd(); |
| 576 |
|
| 577 |
glDisable(GL_VERTEX_PROGRAM_ARB); |
| 578 |
glDisable(GL_FRAGMENT_PROGRAM_ARB); |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
if (draw_sticks) |
| 584 |
if (sticks) { |
| 585 |
glEnable(GL_VERTEX_PROGRAM_ARB); |
| 586 |
glEnable(GL_FRAGMENT_PROGRAM_ARB); |
| 587 |
cgSettings.BindStickShaders(); |
| 588 |
ShadowMap::FeedParameters(); |
| 589 |
for (int i=0; i<3; i++) |
| 590 |
glProgramEnvParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, i, |
| 591 |
x[i][0],x[i][1],x[i][2],0 |
| 592 |
); |
| 593 |
|
| 594 |
/* |
| 595 |
glProgramEnvParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 6, |
| 596 |
1.0 / ( scalef*scalef) , stick_radius,1.0 / ( scalef) ,0 |
| 597 |
);*/ |
| 598 |
|
| 599 |
glEnable(GL_TEXTURE_2D); |
| 600 |
|
| 601 |
glProgramEnvParameter4fARB(GL_VERTEX_PROGRAM_ARB, 0, |
| 602 |
scalef, stick_radius,stick_radius*2,0 ); |
| 603 |
|
| 604 |
glColor3f(1,1,1); |
| 605 |
if (draw_wireframe_sticks) { |
| 606 |
glDisable(GL_FRAGMENT_PROGRAM_ARB); |
| 607 |
for (int i=0; i < (int)bond.size(); i++) { |
| 608 |
glColor3f(0,0.6,0.3); |
| 609 |
glBegin(GL_LINE_LOOP); |
| 610 |
bond[i].Draw(); |
| 611 |
glEnd(); |
| 612 |
} |
| 613 |
} |
| 614 |
else { |
| 615 |
glBegin(GL_QUADS); |
| 616 |
for (int i=0; i < (int)bond.size(); i++) bond[i].Draw(); |
| 617 |
glEnd(); |
| 618 |
} |
| 619 |
cgSettings.BindShaders(); |
| 620 |
} |
| 621 |
|
| 622 |
glPopMatrix(); |
| 623 |
} |
| 624 |
|
| 625 |
int random(int max){ |
| 626 |
return rand()%max; |
| 627 |
} |
| 628 |
|
| 629 |
/* |
| 630 |
void FillRandomTexture(){ |
| 631 |
int k=0; |
| 632 |
//for (int y=0; y<hardSettigs.TSIZE; y++) |
| 633 |
//for (int x=0; x<hardSettigs.TSIZE; x++) { |
| 634 |
// texture[k++]=128+random(127); |
| 635 |
// texture[k++]=128+random(127); |
| 636 |
// texture[k++]=128+random(127); |
| 637 |
// random(127); |
| 638 |
// |
| 639 |
//} |
| 640 |
} |
| 641 |
|
| 642 |
void FillRedTexture(){ |
| 643 |
int k=0; |
| 644 |
for (int y=0; y<hardSettigs.TSIZE; y++) |
| 645 |
for (int x=0; x<hardSettigs.TSIZE; x++) { |
| 646 |
texture[k++]=255; |
| 647 |
texture[k++]=0; |
| 648 |
texture[k++]=120; |
| 649 |
} |
| 650 |
}*/ |
| 651 |
|
| 652 |
void FillShadedTexture(){ |
| 653 |
// OctaMapSamp s(CSIZE); |
| 654 |
|
| 655 |
// CubeMapSamp::SetSize(CSIZE); |
| 656 |
// s.Shade(); |
| 657 |
// s.FillTexture(texture,TSIZE); |
| 658 |
} |
| 659 |
|
| 660 |
void drawQuad(float x, float y, float z, float r){ |
| 661 |
|
| 662 |
glNormal3f(+1,+1, r); |
| 663 |
glVertex3f(x,y,z); |
| 664 |
|
| 665 |
glNormal3f(-1,+1, r); |
| 666 |
glVertex3f(x,y,z); |
| 667 |
|
| 668 |
glNormal3f(-1,-1, r); |
| 669 |
glVertex3f(x,y,z); |
| 670 |
|
| 671 |
glNormal3f(+1,-1, r); |
| 672 |
glVertex3f(x,y,z); |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
Point3f getGlLightPos(){ |
| 678 |
float pos[4]; |
| 679 |
glGetLightfv(GL_LIGHT0, GL_POSITION, pos); |
| 680 |
|
| 681 |
Point3f L=Point3f(pos); |
| 682 |
float x[4][4]; |
| 683 |
glGetFloatv(GL_MODELVIEW_MATRIX, &(x[0][0])); |
| 684 |
Point3f res( |
| 685 |
L * Point3f(x[0]), |
| 686 |
L * Point3f(x[1]), |
| 687 |
L * Point3f(x[2]) |
| 688 |
); |
| 689 |
res.Normalize(); |
| 690 |
|
| 691 |
return -res; |
| 692 |
} |
| 693 |
|
| 694 |
vector<Point3f> DirV; |
| 695 |
|
| 696 |
void Mol::ResetAO(){ |
| 697 |
AOready=false; |
| 698 |
AOstarted=false; |
| 699 |
AOdoneLvl=0; |
| 700 |
} |
| 701 |
|
| 702 |
bool Mol::DoingAO(){ |
| 703 |
if (!ready) return false; |
| 704 |
if (cgSettings.P_texture==0) return false; |
| 705 |
if (DirV.size()==0) return true; |
| 706 |
return AOdoneLvl< (int)DirV.size(); |
| 707 |
} |
| 708 |
|
| 709 |
bool Mol::DecentAO(){ |
| 710 |
float k=1; |
| 711 |
if (AOdoneLvl >= (int)DirV.size()) return true; |
| 712 |
if (atom.size()<10) return (AOdoneLvl>6*k); |
| 713 |
if (atom.size()<100) return (AOdoneLvl>4*k); |
| 714 |
if (atom.size()<1000) return (AOdoneLvl>2*k); |
| 715 |
if (atom.size()<10000) return (AOdoneLvl>1*k); |
| 716 |
return true; |
| 717 |
} |
| 718 |
|
| 719 |
float Mol::PredictAO(){ |
| 720 |
// Additive prediction |
| 721 |
//return 0.6*(DirV.size()-AOdoneLvl)/float(DirV.size()); |
| 722 |
|
| 723 |
// multiplicative prediction |
| 724 |
if (DirV.size()==0) return 1.0; else { |
| 725 |
float coeff=0.25+(AOdoneLvl-1)/20.0; |
| 726 |
if (coeff>1.0f) coeff=1.0f; |
| 727 |
return |
| 728 |
coeff*float(DirV.size()*1.0f) / (AOdoneLvl/*+DirV.size()*0.3f*/); |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
|
| 733 |
void Mol::PrepareAOstart(){ |
| 734 |
AOdoneLvl=0; |
| 735 |
AOgpu2::Reset(*this); |
| 736 |
|
| 737 |
AOstarted=true; |
| 738 |
if (DirV.size()==0) { |
| 739 |
// genreate probe views |
| 740 |
GenNormal<float>::Uniform(hardSettings.N_VIEW_DIR,DirV); |
| 741 |
// mix them! |
| 742 |
int N=DirV.size(); |
| 743 |
for (int k=0; k<N/2; k++) { |
| 744 |
int i=rand()%N; |
| 745 |
int j=rand()%N; |
| 746 |
Point3f tmp=DirV[i]; |
| 747 |
DirV[i]=DirV[j]; |
| 748 |
DirV[j]=tmp; |
| 749 |
} |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
// for testing purposes |
| 754 |
void Mol::PrepareAOSingleView(){ |
| 755 |
static int i=0; |
| 756 |
PrepareAOstart(); |
| 757 |
AOgpu2::Bind(); |
| 758 |
AOgpu2 ao(DirV[i], *this, 4 ); |
| 759 |
i++; if (i > (int)DirV.size()) i=0; |
| 760 |
AOgpu2::UnBind(); |
| 761 |
AOdoneLvl=DirV.size(); |
| 762 |
} |
| 763 |
|
| 764 |
bool Mol::PrepareAOstep(int nsteps){ |
| 765 |
|
| 766 |
if (!DoingAO()) return true; |
| 767 |
if (!AOstarted) PrepareAOstart(); |
| 768 |
|
| 769 |
AOgpu2::Bind(); |
| 770 |
if (validView(DirV[AOdoneLvl])) AOgpu2 ao(DirV[AOdoneLvl], *this, DirV.size()); |
| 771 |
AOgpu2::UnBind(); |
| 772 |
|
| 773 |
|
| 774 |
AOdoneLvl++; |
| 775 |
return (AOdoneLvl >= (int)DirV.size()) ; |
| 776 |
} |
| 777 |
|
| 778 |
void Mol::PrepareAOallAtOnce(){ |
| 779 |
if (AOready) return; |
| 780 |
|
| 781 |
StartTime(); |
| 782 |
|
| 783 |
while (!PrepareAOstep(1)); |
| 784 |
|
| 785 |
//AOgpu2::GetFinalTexture(texture,*this); |
| 786 |
//refresh(); |
| 787 |
|
| 788 |
FILE *f = fopen("res.txt", "w"); |
| 789 |
long int w=TakeTime(f, (char *)"sampled"); |
| 790 |
|
| 791 |
fprintf(f," %d views done in %ld msec (%.2f views x sec), with %d atoms & %d sticks.\n", |
| 792 |
(int)DirV.size(), w, |
| 793 |
(int)DirV.size()*1000.0/w, (int)this->atom.size(), |
| 794 |
(int)(this->sticks?(this->bond.size()):0) ); |
| 795 |
fclose(f); |
| 796 |
|
| 797 |
AOready=true; |
| 798 |
} |
| 799 |
|
| 800 |
void clearFrame() { |
| 801 |
glClearColor( cgSettings.P_bg_color_R, cgSettings.P_bg_color_G, cgSettings.P_bg_color_B, 0.0f); |
| 802 |
|
| 803 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 804 |
} |
| 805 |
|
| 806 |
void setLightDir(Point3f d){ |
| 807 |
float f[4]; |
| 808 |
f[0]=d[0]; |
| 809 |
f[1]=d[1]; |
| 810 |
f[2]=d[2]; |
| 811 |
f[3]=0; |
| 812 |
glLightfv(GL_LIGHT0, GL_POSITION, f ); |
| 813 |
} |
| 814 |
|
| 815 |
Point3f getDirFromTrackball(vcg::Trackball &tb){ |
| 816 |
glPushMatrix(); |
| 817 |
gluLookAt(1,-3,-5, 0,0,0, 0,1,0); |
| 818 |
|
| 819 |
tb.center=Point3f(0, 0, 0); |
| 820 |
tb.radius= 1; |
| 821 |
|
| 822 |
tb.GetView(); |
| 823 |
tb.Apply(false); |
| 824 |
|
| 825 |
//float pos[4]={0.0f,0.0f,-1.0f,0.0f}; |
| 826 |
float d[16]; |
| 827 |
glGetFloatv(GL_MODELVIEW_MATRIX,d); |
| 828 |
glPopMatrix(); |
| 829 |
|
| 830 |
Point3f res(-d[8],-d[9],-d[10]); |
| 831 |
res.Normalize(); |
| 832 |
return res; |
| 833 |
|
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
void drawLightDir() |
| 838 |
{ |
| 839 |
glPushMatrix(); |
| 840 |
lightTrack.GetView(); |
| 841 |
lightTrack.Apply(false); |
| 842 |
#if 0 |
| 843 |
glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); |
| 844 |
glColor3f(1,1,0); |
| 845 |
glDisable(GL_LIGHTING); |
| 846 |
const int lineNum=3; |
| 847 |
glBegin(GL_LINES); |
| 848 |
for(unsigned int i=0;i<=lineNum;++i) |
| 849 |
for(unsigned int j=0;j<=lineNum;++j) { |
| 850 |
glVertex3f(-1.0f+i*2.0/lineNum,-1.0f+j*2.0/lineNum,-2); |
| 851 |
glVertex3f(-1.0f+i*2.0/lineNum,-1.0f+j*2.0/lineNum, 2); |
| 852 |
} |
| 853 |
glEnd(); |
| 854 |
glPopAttrib(); |
| 855 |
#endif |
| 856 |
|
| 857 |
glPopMatrix(); |
| 858 |
} |
| 859 |
|
| 860 |
void drawFrame() { |
| 861 |
|
| 862 |
cgSettings.MakeShaders(); |
| 863 |
|
| 864 |
if (mol.DoingAO()) { |
| 865 |
// do at least one more step per rendering |
| 866 |
mol.PrepareAOstep(1); |
| 867 |
// continue until decent |
| 868 |
while (!mol.DecentAO()) mol.PrepareAOstep(1); |
| 869 |
} |
| 870 |
|
| 871 |
/*if (cgSettings.UseHalo()>0) { |
| 872 |
// write depth in HaloTexture |
| 873 |
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, haloFrame); |
| 874 |
} else { |
| 875 |
// write depth in depthbuffer |
| 876 |
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); |
| 877 |
}*/ |
| 878 |
|
| 879 |
|
| 880 |
mainCanvas.SetAsOutput(); |
| 881 |
|
| 882 |
if (cgSettings.doingAlphaSnapshot) |
| 883 |
glClearColor( cgSettings.P_halo_col, cgSettings.P_halo_col, cgSettings.P_halo_col, 0.0f); |
| 884 |
else |
| 885 |
glClearColor( cgSettings.P_bg_color_R, cgSettings.P_bg_color_G, cgSettings.P_bg_color_B, 0.0f); |
| 886 |
|
| 887 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 888 |
|
| 889 |
|
| 890 |
glMatrixMode(GL_MODELVIEW); |
| 891 |
glLoadIdentity(); |
| 892 |
|
| 893 |
|
| 894 |
Point3f lightDir; |
| 895 |
|
| 896 |
if ( cgSettings.P_sem_effect // fixed light dir sem effect |
| 897 |
// || mol.sticks // quick Hack: fixed light dir when bonds |
| 898 |
) |
| 899 |
lightDir= Point3f(0,0,1); |
| 900 |
else |
| 901 |
lightDir=getDirFromTrackball(lightTrack); |
| 902 |
|
| 903 |
setLightDir( lightDir ); |
| 904 |
|
| 905 |
// gluLookAt(0,0,-3, 0,0,0, 0,1,0); original |
| 906 |
// gluLookAt(0,0,-3, 0,0,0, 0,1,0); ok for tra |
| 907 |
|
| 908 |
gluLookAt(0,0,-40, 0,0,0, 0,1,0); |
| 909 |
if(MovingLightMode) drawLightDir(); |
| 910 |
glColor3f(1,1,1); |
| 911 |
if (1) { |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
//track.center=Point3f(0, 0, 0); |
| 916 |
//track.radius= 1; |
| 917 |
//setProjection(); |
| 918 |
|
| 919 |
if (aniStep>=0) |
| 920 |
{ |
| 921 |
double extraRot=360.0*aniStep; |
| 922 |
|
| 923 |
// set extra rotation for GIF animation: |
| 924 |
switch (hardSettings.GIF_ANIMATION_MODE) { |
| 925 |
default: |
| 926 |
case 0: |
| 927 |
glRotated(-hardSettings.GIF_ROT_SIDEVIEW_ANGLE,1,0,0); |
| 928 |
glRotated(extraRot,0,1,0); |
| 929 |
break; |
| 930 |
case 1: |
| 931 |
glRotated(-extraRot,0,0,1); |
| 932 |
glRotated(hardSettings.GIF_INSP_ANGLE,0,1,0); |
| 933 |
glRotated(extraRot,0,0,1); |
| 934 |
break; |
| 935 |
case 2:{ |
| 936 |
double substep[6]; |
| 937 |
for (int i=0; i<6; i++) { |
| 938 |
substep[i]=(aniStep*6-i)*90.0; |
| 939 |
if (substep[i]<0) substep[i]=0; |
| 940 |
if (substep[i]>90.0) substep[i]=90.0; |
| 941 |
} |
| 942 |
glRotated(-substep[5], 1,0,0); |
| 943 |
glRotated(-substep[4], 0,1,0); |
| 944 |
glRotated(-substep[3], 0,1,0); |
| 945 |
glRotated(-substep[2], 1,0,0); |
| 946 |
glRotated(-substep[1], 0,1,0); |
| 947 |
glRotated(-substep[0], 0,1,0); |
| 948 |
}break; |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
setProjection( mainCanvas.GetVideoSize() ); |
| 953 |
track.GetView(); |
| 954 |
track.Apply(false); |
| 955 |
setProjection( mainCanvas.GetSoftRes() ); |
| 956 |
|
| 957 |
|
| 958 |
if (cgSettings.P_use_shadowmap()) { |
| 959 |
shadowmap.computeAsTexture( getGlLightPos() , cgSettings.do_use_doubleshadow(), shadowmapCanvas); |
| 960 |
//shadowmap.computeAsTexture( Point3f(0,1,0) ); |
| 961 |
} |
| 962 |
|
| 963 |
cgSettings.BindShaders(); |
| 964 |
|
| 965 |
glEnable(GL_TEXTURE_2D); |
| 966 |
//glBindTexture(GL_TEXTURE_2D, molTexture); |
| 967 |
|
| 968 |
glActiveTextureARB(GL_TEXTURE1_ARB); |
| 969 |
shadowmapCanvas.SetAsTexture(); |
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
mol.Draw(); |
| 974 |
// shadowmap.m.DrawShadowmap(false); |
| 975 |
|
| 976 |
|
| 977 |
glDisable(GL_VERTEX_PROGRAM_ARB); |
| 978 |
glDisable(GL_FRAGMENT_PROGRAM_ARB); |
| 979 |
glDisable(GL_BLEND); |
| 980 |
|
| 981 |
if (cgSettings.UseHalo()>0) mol.DrawHalos(); |
| 982 |
} |
| 983 |
|
| 984 |
} |
| 985 |
|
| 986 |
void SetTextureAccess(bool bilinear){ |
| 987 |
glActiveTextureARB(GL_TEXTURE0_ARB); |
| 988 |
moltextureCanvas.SetAsTexture(); |
| 989 |
|
| 990 |
|
| 991 |
if (bilinear) { |
| 992 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 993 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 994 |
} |
| 995 |
else { |
| 996 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 997 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 998 |
} |
| 999 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 1000 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 1001 |
} |
| 1002 |
|
| 1003 |
/* |
| 1004 |
void ReloadTexture(vector<Byte> t, bool bilinear){ |
| 1005 |
// glEnable(GL_TEXTURE); |
| 1006 |
glEnable(GL_TEXTURE_2D); |
| 1007 |
|
| 1008 |
|
| 1009 |
glActiveTextureARB(GL_TEXTURE0_ARB); |
| 1010 |
glBindTexture(GL_TEXTURE_2D, molTexture); |
| 1011 |
|
| 1012 |
glTexImage2D( |
| 1013 |
GL_TEXTURE_2D, |
| 1014 |
0, |
| 1015 |
GL_RGB, |
| 1016 |
//TSIZE,TSIZE, |
| 1017 |
0, |
| 1018 |
GL_RGB, |
| 1019 |
GL_UNSIGNED_BYTE, |
| 1020 |
(void*)&(t[0]) |
| 1021 |
); |
| 1022 |
|
| 1023 |
|
| 1024 |
SetTextureAccess(bilinear); |
| 1025 |
}*/ |
| 1026 |
|
| 1027 |
int initGl(){ |
| 1028 |
|
| 1029 |
int res=0; |
| 1030 |
glClearDepth(1.0); |
| 1031 |
glDepthFunc(GL_LESS); |
| 1032 |
glEnable(GL_DEPTH_TEST); |
| 1033 |
glShadeModel(GL_SMOOTH); |
| 1034 |
|
| 1035 |
float pos[4]={0.0f,0.8f,0.6f,0.0f}; |
| 1036 |
glLightfv(GL_LIGHT0, GL_POSITION, pos); |
| 1037 |
|
| 1038 |
glEnable(GL_VERTEX_PROGRAM_ARB); |
| 1039 |
glEnable(GL_FRAGMENT_PROGRAM_ARB); |
| 1040 |
|
| 1041 |
if (!CgUtil::init()) res|=ERRGL_NO_GLEW; |
| 1042 |
if(!GLEW_ARB_vertex_program ) res|=ERRGL_NO_VS; |
| 1043 |
if(!GLEW_ARB_fragment_program ) res|=ERRGL_NO_FS; |
| 1044 |
|
| 1045 |
|
| 1046 |
if (!shadowmap.init()) res|=ERRGL_NO_FBO_SHADOWMAP; |
| 1047 |
if (!shadowmap.initHalo()) res|=ERRGL_NO_FBO_HALO; |
| 1048 |
|
| 1049 |
if (! AOgpu2::init()) res|=ERRGL_NO_FBO_AO; |
| 1050 |
|
| 1051 |
cgSettings.UpdateShaders(); |
| 1052 |
|
| 1053 |
//ReloadTexture(texture, true); |
| 1054 |
return res; |
| 1055 |
|
| 1056 |
} |
| 1057 |
|
| 1058 |
Point3f RandomUnitVec(){ |
| 1059 |
Point3f k; |
| 1060 |
do { |
| 1061 |
k=Point3f( |
| 1062 |
(random(200)-100)*0.01, |
| 1063 |
(random(200)-100)*0.01, |
| 1064 |
(random(200)-100)*0.01 |
| 1065 |
); |
| 1066 |
} while (k.SquaredNorm()>1.0); |
| 1067 |
|
| 1068 |
return k.Normalize(); |
| 1069 |
} |
| 1070 |
|
| 1071 |
long int globaltime,startingtime; |
| 1072 |
|
| 1073 |
void StartTime();/*{ |
| 1074 |
startingtime=globaltime=getTicks(); |
| 1075 |
}*/ |
| 1076 |
|
| 1077 |
long int TakeTime(FILE *f , char *st);/*{ |
| 1078 |
long int timen=getTicks(), delta=timen-globaltime; |
| 1079 |
fprintf(f,"%5dmsec: %s\n",delta,st); |
| 1080 |
globaltime=timen; |
| 1081 |
return delta; |
| 1082 |
}*/ |
| 1083 |
/*long int TakeTotalTime(){ |
| 1084 |
long int timen=getTicks(), delta=timen-startingtime; |
| 1085 |
printf("------------------\nTotal time: %5dmsec\n",delta); |
| 1086 |
globaltime=timen; |
| 1087 |
return delta; |
| 1088 |
}*/ |
| 1089 |
|
| 1090 |
float myfabs(float a){ |
| 1091 |
return (a<0)?-a:a; |
| 1092 |
} |
| 1093 |
|
| 1094 |
void Cycle( float &c, float min, float max, float step){ |
| 1095 |
if (myfabs(c-max)<0.02) c=min; |
| 1096 |
else { |
| 1097 |
c+=step; |
| 1098 |
if (c>max) c=max; |
| 1099 |
} |
| 1100 |
} |
| 1101 |
|
| 1102 |
int InitQuteMol(const char * filename) |
| 1103 |
{ |
| 1104 |
|
| 1105 |
CubeMapSamp::SetSize(CSIZE); |
| 1106 |
OctaMapSamp::SetSize(CSIZE); |
| 1107 |
|
| 1108 |
if (filename==NULL) filename="porin.pdb"; |
| 1109 |
|
| 1110 |
mol.ReadPdb(filename); |
| 1111 |
cgSettings.SetDefaults(); |
| 1112 |
// initGl gets called from the GL canvas on startup. It's not |
| 1113 |
// legal to call it before that time because the OpenGL context |
| 1114 |
// does not exist yet. |
| 1115 |
|
| 1116 |
/*if (!initGl()) { |
| 1117 |
printf("failed to initialize! :(\n"); |
| 1118 |
return 0; |
| 1119 |
}*/ |
| 1120 |
|
| 1121 |
//FillRandomTexture(); |
| 1122 |
//mol.DuplicateTexels(texture, TSIZE); |
| 1123 |
//ReloadTexture(texture, bilinear); |
| 1124 |
return 1; |
| 1125 |
} |
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
bool SaveImagePPM( const char * filename , const Byte *im, int sizex, int sizey) |
| 1130 |
{ |
| 1131 |
FILE * fp = fopen(filename,"wb"); |
| 1132 |
if(fp==0) return false; |
| 1133 |
|
| 1134 |
|
| 1135 |
fprintf(fp,"P6\n%d %d\n255\n",sizex,sizey); |
| 1136 |
|
| 1137 |
int k=0; |
| 1138 |
for(int i=0;i<sizex*sizey;++i) |
| 1139 |
{ |
| 1140 |
fwrite(&(im[k++]),1,1,fp); |
| 1141 |
fwrite(&(im[k++]),1,1,fp); |
| 1142 |
fwrite(&(im[k++]),1,1,fp); |
| 1143 |
//k++; |
| 1144 |
} |
| 1145 |
|
| 1146 |
fclose(fp); |
| 1147 |
return true; |
| 1148 |
} |
| 1149 |
|
| 1150 |
bool SaveImagePPM( const char * filename , const vector<Byte> &im, int sizex, int sizey){ |
| 1151 |
return |
| 1152 |
SaveImagePPM( filename , (Byte*)(&im[0]), sizex, sizey); |
| 1153 |
} |
| 1154 |
|
| 1155 |
bool LoadImagePPM( const char * filename , vector<Byte> &im) |
| 1156 |
{ |
| 1157 |
FILE * fp = fopen(filename,"rb"); |
| 1158 |
if(fp==0) return false; |
| 1159 |
|
| 1160 |
int sizex, sizey, res; |
| 1161 |
res=fscanf(fp,"P6\n%d %d\n255\n",&sizex,&sizey); |
| 1162 |
printf("Loading %s (res=%d)...\n",filename, res); |
| 1163 |
if (res!=2) return false; |
| 1164 |
printf(" - size= (%d %d) ...\n",sizex, sizey); |
| 1165 |
|
| 1166 |
int k=0; |
| 1167 |
for(int i=0;i<sizex*sizey;++i) |
| 1168 |
{ |
| 1169 |
fread(&(im[k++]),1,1,fp); |
| 1170 |
fread(&(im[k++]),1,1,fp); |
| 1171 |
fread(&(im[k++]),1,1,fp); |
| 1172 |
} |
| 1173 |
|
| 1174 |
fclose(fp); |
| 1175 |
return true; |
| 1176 |
} |