vcglib/apps/nexus/nexusmt.cpp

465 lines
13 KiB
C++
Raw Normal View History

2005-02-08 13:43:03 +01:00
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
2005-04-13 16:01:17 +02:00
Revision 1.37 2005/03/02 10:40:17 ponchio
Extraction rewrittten (to fix recusive problems).
Revision 1.36 2005/02/22 10:38:06 ponchio
Debug, cleaning and optimization.
2005-02-22 11:38:17 +01:00
Revision 1.35 2005/02/20 19:49:44 ponchio
cleaning (a bit more).
2005-02-20 20:49:44 +01:00
Revision 1.34 2005/02/20 18:07:01 ponchio
cleaning.
2005-02-20 19:07:01 +01:00
Revision 1.33 2005/02/20 00:43:23 ponchio
Less memory x extraction. (removed frags)
Revision 1.32 2005/02/19 12:06:53 ponchio
Debug...
2005-02-19 13:06:55 +01:00
Revision 1.31 2005/02/19 10:45:04 ponchio
Patch generalized and small fixes.
2005-02-19 11:45:05 +01:00
Revision 1.30 2005/02/17 15:39:44 ponchio
Reorderes statistics a bit.
2005-02-17 16:39:44 +01:00
Revision 1.29 2005/02/14 17:11:07 ponchio
aggiunta delle sphere
2005-02-14 18:11:08 +01:00
Revision 1.28 2005/02/08 12:43:03 ponchio
Added copyright
2005-02-08 13:43:03 +01:00
****************************************************************************/
2005-02-03 13:35:01 +01:00
#ifndef WIN32
#include <fcntl.h>
#endif
2004-09-17 17:25:59 +02:00
#include <map>
#include <queue>
2004-12-14 15:10:22 +01:00
#include <GL/glew.h>
#include <ptypes/pasync.h>
#include "nexusmt.h"
2004-09-30 02:27:42 +02:00
2004-09-17 17:25:59 +02:00
using namespace nxs;
2004-09-30 02:27:42 +02:00
using namespace vcg;
2004-09-17 17:25:59 +02:00
using namespace std;
2005-02-17 16:39:44 +01:00
void Stats::Start() {
2005-04-13 16:01:17 +02:00
tri = extr = disk_tri = 0;
2005-02-17 16:39:44 +01:00
watch.Start();
}
void Stats::Disk(float _disk) {
disk.push_front(_disk);
if(disk.size() > log_size) disk.pop_back();
}
void Stats::Error(float _error) {
error.push_front(_error);
if(error.size() > log_size) error.pop_back();
}
void Stats::Stop() {
time.push_front((float)watch.Time());
if(time.size() > log_size) time.pop_back();
fps = (7*fps + 1/time[0])/8.0f;
2004-09-28 12:26:35 +02:00
}
2005-01-14 16:25:29 +01:00
NexusMt::NexusMt() {
preload.mt = this;
preload.start();
2004-12-13 01:44:48 +01:00
}
2004-11-28 02:23:26 +01:00
2004-12-13 01:44:48 +01:00
NexusMt::~NexusMt() {
2005-01-14 16:25:29 +01:00
preload.signal();
preload.waitfor();
2004-09-30 02:27:42 +02:00
}
2005-02-01 17:42:30 +01:00
void NexusMt::SetPreload(bool on) {
if(on == (preload.get_running() && !preload.get_finished())) return;
if(on) preload.start();
else {
preload.signal();
// preload.waitfor();
}
}
2004-12-13 01:44:48 +01:00
bool NexusMt::Load(const string &filename) {
2005-01-14 16:25:29 +01:00
if(!Nexus::Load(filename, true)) return false;
if(!history.IsQuick() && !history.UpdatesToQuick(*this))
2004-12-13 01:44:48 +01:00
return false;
2005-02-03 13:35:01 +01:00
#ifndef WIN32
//i will read data only once usually.
// for(unsigned int i = 0; i < files.size(); i++) {
// int fd = fileno(files[i]->fp);
// posix_fadvise(fd, 0, 0, POSIX_FADV_NOREUSE);
// }
#endif
2004-09-30 02:27:42 +02:00
return true;
}
2005-01-14 16:25:29 +01:00
bool NexusMt::InitGL(bool vbo) {
use_vbo = vbo;
2004-12-13 01:44:48 +01:00
2004-09-30 02:27:42 +02:00
GLenum ret = glewInit();
if(ret != GLEW_OK) return false;
2005-01-14 16:25:29 +01:00
if(vbo && !GLEW_ARB_vertex_buffer_object) {
2004-10-15 18:45:27 +02:00
cerr << "No vbo available!" << endl;
2005-01-14 16:25:29 +01:00
use_vbo = false;
2004-10-15 18:45:27 +02:00
}
2004-09-30 02:27:42 +02:00
return true;
}
2005-01-14 16:25:29 +01:00
void NexusMt::Render(DrawContest contest) {
Extraction extraction;
extraction.metric->GetView();
extraction.Extract(this);
Render(extraction, contest);
}
2005-02-14 18:11:08 +01:00
2005-01-14 16:25:29 +01:00
void NexusMt::Render(Extraction &extraction, DrawContest &contest,
Stats *stats) {
2005-02-14 18:11:08 +01:00
static ::GLUquadricObj * spr = gluNewQuadric();
2005-01-14 16:25:29 +01:00
//Updating heap errors
map<unsigned int, float> errors;
for(unsigned int i = 0; i < extraction.selected.size(); i++) {
Item &item = extraction.selected[i];
errors[item.id] = item.error;
}
2005-02-03 13:35:01 +01:00
for(unsigned int i = 0; i < heap.size(); i++) {
Item &item = heap[i];
if(!errors.count(item.id)) {
2005-02-03 13:35:01 +01:00
item.error = 1e20;
} else
item.error = errors[item.id];
2005-02-03 13:35:01 +01:00
}
make_heap(heap.begin(), heap.end());
2005-01-14 16:25:29 +01:00
preload.post(extraction.selected);
2004-09-30 02:27:42 +02:00
2005-01-14 16:25:29 +01:00
glEnableClientState(GL_VERTEX_ARRAY);
2005-02-19 11:45:05 +01:00
if(signature.vcolor && (contest.attrs & DrawContest::COLOR))
2005-01-14 16:25:29 +01:00
glEnableClientState(GL_COLOR_ARRAY);
2005-02-19 11:45:05 +01:00
if(signature.vnorm && (contest.attrs & DrawContest::NORMAL))
2005-01-14 16:25:29 +01:00
glEnableClientState(GL_NORMAL_ARRAY);
2005-02-03 13:35:01 +01:00
vector<Item> skipped;
2004-12-13 01:44:48 +01:00
for(unsigned int i = 0; i < extraction.draw_size; i++) {
2005-02-03 13:35:01 +01:00
unsigned int patch = extraction.selected[i].id;
Entry &entry = operator[](patch);
2005-02-19 11:45:05 +01:00
if(stats) stats->extr += 2*entry.nvert;
2005-02-17 16:39:44 +01:00
2005-02-20 19:07:01 +01:00
if(!extraction.Visible(patch))
2005-01-14 16:25:29 +01:00
continue;
2005-02-19 11:45:05 +01:00
if(stats) stats->tri += 2*entry.nvert;
2005-01-14 16:25:29 +01:00
if(!entry.patch) {
2005-02-03 13:35:01 +01:00
skipped.push_back(extraction.selected[i]);
2005-01-14 16:25:29 +01:00
continue;
}
Draw(patch, contest);
2005-01-14 16:25:29 +01:00
}
2005-02-03 13:35:01 +01:00
preload.trigger.reset();
2005-01-14 16:25:29 +01:00
preload.lock.enter();
2005-02-03 13:35:01 +01:00
2005-02-17 16:39:44 +01:00
// if(skipped.size()) cerr << "Skipped: " << skipped.size() << endl;
2005-02-03 13:35:01 +01:00
for(vector<Item>::iterator i = skipped.begin(); i != skipped.end(); i++) {
GetPatch((*i).id, (*i).error);
Draw((*i).id, contest);
2005-01-14 16:25:29 +01:00
}
Flush(false); //in case there are no skipped... :P
2005-02-03 13:35:01 +01:00
2005-02-17 16:39:44 +01:00
if(stats) {
stats->Error(extraction.max_error);
stats->Disk(preload.disk);
2005-04-13 16:01:17 +02:00
stats->disk_tri += preload.disk_tri;
2005-02-17 16:39:44 +01:00
preload.disk = 0;
}
2005-02-03 13:35:01 +01:00
preload.trigger.post();
2005-01-14 16:25:29 +01:00
preload.lock.leave();
2005-02-03 13:35:01 +01:00
2005-01-14 16:25:29 +01:00
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
2004-10-30 22:17:03 +02:00
}
2005-02-17 16:39:44 +01:00
void NexusMt::Draw(unsigned int cell, DrawContest &contest) {
static ::GLUquadricObj * spr = gluNewQuadric();
2005-01-14 16:25:29 +01:00
Entry &entry = operator[](cell);
Patch &patch = *(entry.patch);
2004-12-14 15:10:22 +01:00
char *fstart;
char *vstart;
char *cstart;
char *nstart;
2005-02-17 16:39:44 +01:00
if(contest.attrs & DrawContest::SPHERES){
vcg::Sphere3f &sphere = entry.sphere;
glPushAttrib(GL_POLYGON_BIT);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glPushMatrix();
glTranslatef(sphere.Center().X(),sphere.Center().Y(),sphere.Center().Z());
gluSphere(spr,sphere.Radius(),15,15);
glPopMatrix();
glPopAttrib();
}
2004-12-14 15:10:22 +01:00
2005-01-14 16:25:29 +01:00
if(use_vbo) {
if(!entry.vbo_element)
LoadVbo(entry);
2004-12-14 15:10:22 +01:00
2005-01-14 16:25:29 +01:00
glBindBufferARB(GL_ARRAY_BUFFER_ARB, entry.vbo_array);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, entry.vbo_element);
2004-12-14 15:10:22 +01:00
fstart = NULL;
vstart = NULL;
2005-02-22 11:38:17 +01:00
cstart = (char *)(patch.vstartc - patch.vstart);
nstart = (char *)(patch.vstartn - patch.vstart);
2004-12-14 15:10:22 +01:00
} else {
fstart = (char *)patch.FaceBegin();
2005-02-19 11:45:05 +01:00
vstart = (char *)patch.Vert3fBegin();
cstart = (char *)patch.VColorBegin();
nstart = (char *)patch.VNormBegin();
2004-12-14 15:10:22 +01:00
}
2005-02-19 11:45:05 +01:00
assert(signature.vert == Signature::POINT3F);
2004-12-14 15:10:22 +01:00
glVertexPointer(3, GL_FLOAT, 0, vstart);
2005-02-19 11:45:05 +01:00
if(signature.vcolor && contest.attrs & DrawContest::COLOR) {
assert(signature.vcolor == Encodings::BYTE4);
2004-12-14 15:10:22 +01:00
glColorPointer(4, GL_UNSIGNED_BYTE, 0, cstart);
2005-02-19 11:45:05 +01:00
}
if(signature.vnorm && contest.attrs & DrawContest::NORMAL) {
2005-02-20 20:49:44 +01:00
// assert(signature.vnorm == Encodings::SHORT4);
if(signature.vnorm == Encodings::SHORT4)
glNormalPointer(GL_SHORT, 8, nstart);
else
glNormalPointer(GL_FLOAT, 0, nstart);
2005-02-19 11:45:05 +01:00
}
2005-02-17 16:39:44 +01:00
2004-12-14 15:10:22 +01:00
2005-01-14 16:25:29 +01:00
switch(contest.mode) {
case DrawContest::POINTS:
2004-12-14 15:10:22 +01:00
glDrawArrays(GL_POINTS, 0, patch.nv); break;
2005-01-14 16:25:29 +01:00
case DrawContest::PATCHES:
2005-02-14 18:11:08 +01:00
glColor3ub((cell * 27)%225 + 30, (cell * 37)%225 + 30, (cell * 87)%225 + 30);
2005-01-14 16:25:29 +01:00
case DrawContest::SMOOTH:
2005-02-19 11:45:05 +01:00
if(signature.face == Signature::TRIANGLES)
2004-12-14 15:10:22 +01:00
glDrawElements(GL_TRIANGLES, patch.nf * 3,
GL_UNSIGNED_SHORT, fstart);
2005-02-19 11:45:05 +01:00
else if(signature.face == Signature::STRIPS)
2004-12-14 15:10:22 +01:00
glDrawElements(GL_TRIANGLE_STRIP, patch.nf,
GL_UNSIGNED_SHORT, fstart);
break;
2005-01-14 16:25:29 +01:00
case DrawContest::FLAT:
2005-02-01 17:42:30 +01:00
if(use_vbo) {
2004-09-30 02:27:42 +02:00
cerr << "Unsupported rendering mode sorry\n";
2005-02-19 11:45:05 +01:00
} else {
if(signature.face == Signature::TRIANGLES) {
glBegin(GL_TRIANGLES);
unsigned short *f = patch.Face(0);
for(int i = 0; i < patch.nf; i++) {
Point3f &p0 = patch.Vert3f(f[0]);
Point3f &p1 = patch.Vert3f(f[1]);
Point3f &p2 = patch.Vert3f(f[2]);
Point3f n = ((p1 - p0) ^ (p2 - p0));
glNormal3f(n[0], n[1], n[2]);
glVertex3f(p0[0], p0[1], p0[2]);
glVertex3f(p1[0], p1[1], p1[2]);
glVertex3f(p2[0], p2[1], p2[2]);
f += 3;
}
glEnd();
} else if(signature.face = Signature::STRIPS) {
cerr << "Unsupported rendering mode sorry\n";
}
2004-09-30 02:27:42 +02:00
}
2004-12-14 15:10:22 +01:00
break;
default:
cerr << "Unsupported rendering mode sorry\n";
exit(0);
break;
2004-09-30 02:27:42 +02:00
}
}
2005-02-03 13:35:01 +01:00
Patch &NexusMt::GetPatch(unsigned int patch, float error, bool flush) {
Entry &entry = operator[](patch);
if(entry.patch) return *(entry.patch);
while(flush && ram_used > ram_max) {
if(heap[0].error == 0) break;
unsigned int to_flush = heap[0].id;
pop_heap(heap.begin(), heap.end());
heap.pop_back();
FlushPatch(to_flush);
}
entry.patch = LoadPatch(patch);
heap.push_back(Item(patch, error));
push_heap(heap.begin(), heap.end());
return *(entry.patch);
}
void NexusMt::Flush(bool all) {
if(all) {
for(unsigned int i = 0; i < heap.size(); i++) {
unsigned int patch = heap[i].id;
FlushPatch(patch);
}
heap.clear();
} else {
while(heap.size() && ram_used > ram_max) {
if(heap[0].error == 0) break;
unsigned int to_flush = heap[0].id;
pop_heap(heap.begin(), heap.end());
heap.pop_back();
FlushPatch(to_flush);
}
}
}
bool NexusMt::CanAdd(Item &item) {
if(!heap.size()) return true;
Entry &entry = operator[](item.id);
if(ram_used + entry.ram_size < ram_max)
return true;
return heap[0].error > item.error;
}
2005-01-14 16:25:29 +01:00
void NexusMt::FlushPatch(unsigned int id) {
Entry &entry = operator[](id);
if(entry.vbo_element)
FlushVbo(entry);
2005-02-19 11:45:05 +01:00
if(entry.patch->fstart)
delete [](entry.patch->fstart);
2005-01-14 16:25:29 +01:00
delete entry.patch;
entry.patch = NULL;
ram_used -= entry.ram_size;
}
void NexusMt::LoadVbo(Entry &entry) {
2005-02-01 17:42:30 +01:00
assert(entry.vbo_element == 0);
// if(entry.vbo_element) return;
2005-01-14 16:25:29 +01:00
Patch &patch = *entry.patch;
unsigned int size = patch.nf * sizeof(unsigned short);
2005-02-19 11:45:05 +01:00
if(signature.face == Signature::TRIANGLES)
size *= 3;
else if(signature.face != Signature::STRIPS)
assert(0);
2005-02-01 17:42:30 +01:00
glGenBuffersARB(1, &entry.vbo_element);
assert(entry.vbo_element);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, entry.vbo_element);
2005-01-14 16:25:29 +01:00
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, size, patch.FaceBegin(),
GL_STATIC_DRAW_ARB);
vbo_used += size;
//TODO fix this when we allow data :p
2005-02-22 11:38:17 +01:00
size = patch.vstartd - patch.vstart;
2005-01-14 16:25:29 +01:00
glGenBuffersARB(1, &entry.vbo_array);
assert(entry.vbo_array);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, entry.vbo_array);
2005-02-19 11:45:05 +01:00
glBufferDataARB(GL_ARRAY_BUFFER_ARB, size, patch.Vert3fBegin(),
2005-01-14 16:25:29 +01:00
GL_STATIC_DRAW_ARB);
vbo_used += size;
2005-02-19 11:45:05 +01:00
delete [](entry.patch->fstart);
entry.patch->fstart = NULL;
2004-09-30 02:27:42 +02:00
}
2005-01-14 16:25:29 +01:00
void NexusMt::FlushVbo(Entry &entry) {
if(!entry.vbo_element) return;
glDeleteBuffersARB(1, &entry.vbo_element);
glDeleteBuffersARB(1, &entry.vbo_array);
entry.vbo_element = 0;
entry.vbo_array = 0;
Patch &patch = *entry.patch;
vbo_used -= patch.nf * sizeof(unsigned short);
2005-02-22 11:38:17 +01:00
vbo_used -= sizeof(float) * (patch.vstart - patch.vstartd);
2005-01-14 16:25:29 +01:00
}
//Kept for historical reasons.
2004-10-14 15:42:33 +02:00
/*void NexusMt::ExtractFixed(vector<unsigned int> &selected, float error) {
2004-09-17 17:25:59 +02:00
std::vector<Node>::iterator n;
for(n = nodes.begin(); n != nodes.end(); n++)
(*n).visited = false;
std::queue<Node *> qnodo;
qnodo.push(&nodes[0]);
nodes[0].visited = true;
for( ; !qnodo.empty(); qnodo.pop()) {
Node &node = *qnodo.front();
std::vector<Frag>::iterator fragment;
std::vector<Node *>::iterator on;
for(on = node.out.begin(), fragment = node.frags.begin();
2004-10-14 15:42:33 +02:00
on != node.out.end(); ++on, ++fragment) {
2004-09-17 17:25:59 +02:00
if((*on)->visited) continue;
if(error < (*on)->error) { //need to expand this node.
2004-10-14 15:42:33 +02:00
qnodo.push(*on);
(*on)->visited = 1;
2004-09-17 17:25:59 +02:00
} else {
2004-10-14 15:42:33 +02:00
vector<unsigned int>::iterator cell;
2005-01-14 16:25:29 +01:00
for(cell=(*fragment).begin(); cell != (*fragment).end(); ++cell) selected.push_back(*cell);
2004-09-17 17:25:59 +02:00
}
}
}
2004-10-14 15:42:33 +02:00
} */