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-01-14 16:25:29 +01:00
|
|
|
void Stats::Init() {
|
|
|
|
ktri = 0;
|
|
|
|
kdisk = 0;
|
|
|
|
if(count == 25) count = 0;
|
|
|
|
if(!count) {
|
|
|
|
fps = 25/watch.Time();
|
|
|
|
watch.Start();
|
|
|
|
}
|
|
|
|
count++;
|
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
|
|
|
}
|
|
|
|
|
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())
|
2004-12-13 01:44:48 +01:00
|
|
|
return false;
|
2004-09-30 02:27:42 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-01-14 16:25:29 +01:00
|
|
|
//void NexusMt::Close() {
|
|
|
|
// prefetch.signal();
|
|
|
|
// prefetch.waitfor();
|
|
|
|
// patches.Close();
|
|
|
|
//}
|
|
|
|
|
|
|
|
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.frustum.GetView();
|
|
|
|
extraction.metric->GetView();
|
|
|
|
extraction.Extract(this);
|
|
|
|
Render(extraction, contest);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NexusMt::Render(Extraction &extraction, DrawContest &contest,
|
|
|
|
Stats *stats) {
|
|
|
|
if(stats) stats->Init();
|
|
|
|
|
|
|
|
preload.post(extraction.selected);
|
2004-09-30 02:27:42 +02:00
|
|
|
|
2005-01-14 16:25:29 +01:00
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
if((signature & NXS_COLORS) && (contest.attrs & DrawContest::COLOR))
|
|
|
|
glEnableClientState(GL_COLOR_ARRAY);
|
|
|
|
if((signature & NXS_NORMALS_SHORT) && (contest.attrs & DrawContest::NORMAL))
|
|
|
|
glEnableClientState(GL_NORMAL_ARRAY);
|
|
|
|
|
|
|
|
vector<unsigned int> skipped;
|
2004-12-13 01:44:48 +01:00
|
|
|
|
2005-01-17 18:35:48 +01:00
|
|
|
for(unsigned int i = 0; i < extraction.draw_size; i++) {
|
|
|
|
unsigned int patch = extraction.selected[i];
|
|
|
|
Entry &entry = operator[](patch);
|
2005-01-14 16:25:29 +01:00
|
|
|
vcg::Sphere3f &sphere = entry.sphere;
|
|
|
|
if(extraction.frustum.IsOutside(sphere.Center(), sphere.Radius()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(stats) stats->ktri += entry.nface;
|
|
|
|
|
|
|
|
if(!entry.patch) {
|
2005-01-17 18:35:48 +01:00
|
|
|
skipped.push_back(patch);
|
2005-01-14 16:25:29 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-01-17 18:35:48 +01:00
|
|
|
Draw(patch, contest);
|
2005-01-14 16:25:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
preload.lock.enter();
|
2005-01-17 18:35:48 +01:00
|
|
|
for(vector<unsigned int>::iterator i = skipped.begin();
|
|
|
|
i != skipped.end(); i++) {
|
2005-01-14 16:25:29 +01:00
|
|
|
GetPatch(*i);
|
|
|
|
Draw(*i, contest);
|
|
|
|
}
|
2005-01-17 18:35:48 +01:00
|
|
|
Flush(false); //in case there are no skipped... :P
|
2005-01-14 16:25:29 +01:00
|
|
|
preload.lock.leave();
|
|
|
|
|
|
|
|
glDisableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glDisableClientState(GL_COLOR_ARRAY);
|
|
|
|
glDisableClientState(GL_NORMAL_ARRAY);
|
2004-10-30 22:17:03 +02:00
|
|
|
}
|
|
|
|
|
2005-01-14 16:25:29 +01:00
|
|
|
void NexusMt::Draw(unsigned int cell, DrawContest &contest) {
|
|
|
|
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-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;
|
|
|
|
cstart = (char *)(sizeof(float) * patch.cstart);
|
|
|
|
nstart = (char *)(sizeof(float) * patch.nstart);
|
|
|
|
} else {
|
|
|
|
fstart = (char *)patch.FaceBegin();
|
|
|
|
vstart = (char *)patch.VertBegin();
|
|
|
|
cstart = (char *)patch.ColorBegin();
|
|
|
|
nstart = (char *)patch.Norm16Begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
glVertexPointer(3, GL_FLOAT, 0, vstart);
|
2005-01-14 16:25:29 +01:00
|
|
|
if(contest.attrs & DrawContest::COLOR)
|
2004-12-14 15:10:22 +01:00
|
|
|
glColorPointer(4, GL_UNSIGNED_BYTE, 0, cstart);
|
2005-01-14 16:25:29 +01:00
|
|
|
if(contest.attrs & DrawContest::NORMAL)
|
2004-12-14 15:10:22 +01:00
|
|
|
glNormalPointer(GL_SHORT, 8, nstart);
|
|
|
|
|
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:
|
2004-12-14 15:10:22 +01:00
|
|
|
glColor3ub((cell * 27)%255, (cell * 37)%255, (cell * 87)%255);
|
2005-01-14 16:25:29 +01:00
|
|
|
case DrawContest::SMOOTH:
|
2004-12-14 15:10:22 +01:00
|
|
|
if(signature & NXS_FACES)
|
|
|
|
glDrawElements(GL_TRIANGLES, patch.nf * 3,
|
|
|
|
GL_UNSIGNED_SHORT, fstart);
|
|
|
|
else if(signature & NXS_STRIP)
|
|
|
|
glDrawElements(GL_TRIANGLE_STRIP, patch.nf,
|
|
|
|
GL_UNSIGNED_SHORT, fstart);
|
|
|
|
break;
|
2005-01-14 16:25:29 +01:00
|
|
|
case DrawContest::FLAT:
|
2004-12-14 15:10:22 +01:00
|
|
|
if(signature & NXS_FACES) {
|
|
|
|
glBegin(GL_TRIANGLES);
|
|
|
|
for(int i = 0; i < patch.nf; i++) {
|
|
|
|
unsigned short *f = patch.Face(i);
|
|
|
|
Point3f &p0 = patch.Vert(f[0]);
|
|
|
|
Point3f &p1 = patch.Vert(f[1]);
|
|
|
|
Point3f &p2 = patch.Vert(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]);
|
2004-10-19 03:23:02 +02:00
|
|
|
}
|
2004-12-14 15:10:22 +01:00
|
|
|
glEnd();
|
|
|
|
} else if(signature & NXS_STRIP) {
|
2004-09-30 02:27:42 +02:00
|
|
|
cerr << "Unsupported rendering mode sorry\n";
|
|
|
|
exit(0);
|
|
|
|
}
|
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-01-14 16:25:29 +01:00
|
|
|
void NexusMt::FlushPatch(unsigned int id) {
|
|
|
|
Entry &entry = operator[](id);
|
|
|
|
if(entry.vbo_element)
|
|
|
|
FlushVbo(entry);
|
|
|
|
|
|
|
|
if(entry.patch->start)
|
|
|
|
delete [](entry.patch->start);
|
|
|
|
delete entry.patch;
|
|
|
|
entry.patch = NULL;
|
|
|
|
ram_used -= entry.ram_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NexusMt::LoadVbo(Entry &entry) {
|
|
|
|
if(entry.vbo_element) return;
|
|
|
|
glGenBuffersARB(1, &entry.vbo_element);
|
|
|
|
assert(entry.vbo_element);
|
|
|
|
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, entry.vbo_element);
|
|
|
|
|
|
|
|
Patch &patch = *entry.patch;
|
|
|
|
unsigned int size = patch.nf * sizeof(unsigned short);
|
|
|
|
if((signature & NXS_FACES) != 0) size *= 3;
|
|
|
|
|
|
|
|
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, size, patch.FaceBegin(),
|
|
|
|
GL_STATIC_DRAW_ARB);
|
|
|
|
vbo_used += size;
|
|
|
|
|
|
|
|
//TODO fix this when we allow data :p
|
|
|
|
size = sizeof(float) * patch.dstart;
|
|
|
|
|
|
|
|
glGenBuffersARB(1, &entry.vbo_array);
|
|
|
|
assert(entry.vbo_array);
|
|
|
|
glBindBufferARB(GL_ARRAY_BUFFER_ARB, entry.vbo_array);
|
|
|
|
|
|
|
|
glBufferDataARB(GL_ARRAY_BUFFER_ARB, size, patch.VertBegin(),
|
|
|
|
GL_STATIC_DRAW_ARB);
|
|
|
|
|
|
|
|
vbo_used += size;
|
|
|
|
delete [](entry.patch->start);
|
|
|
|
entry.patch->start = 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);
|
|
|
|
vbo_used -= sizeof(float) * patch.dstart;
|
|
|
|
}
|
|
|
|
|
2005-01-17 18:35:48 +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
|
|
|
} */
|