From 35e423a6422d2b0faa0f6c12684072168cdf589a Mon Sep 17 00:00:00 2001 From: ponchio Date: Wed, 23 Jun 2004 00:10:38 +0000 Subject: [PATCH] Created --- apps/nexus/crudeview/crudeview.cpp | 186 +++++++++++++++++++++++++++++ apps/nexus/ply2crude/ply2crude.cpp | 146 ++++++++++++++++++++++ 2 files changed, 332 insertions(+) create mode 100644 apps/nexus/crudeview/crudeview.cpp create mode 100644 apps/nexus/ply2crude/ply2crude.cpp diff --git a/apps/nexus/crudeview/crudeview.cpp b/apps/nexus/crudeview/crudeview.cpp new file mode 100644 index 00000000..60a700aa --- /dev/null +++ b/apps/nexus/crudeview/crudeview.cpp @@ -0,0 +1,186 @@ +/**************************************************************************** +* 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 $ + +****************************************************************************/ + +#include +using namespace std; + +#include + +#include +#include + +#include +using namespace vcg; +using namespace nxs; + +bool fullscreen = false; +int width =1024; +int height = 768; + +//TrackHand hand; + +SDL_Surface *screen = NULL; + +bool init() { + + if(SDL_Init(SDL_INIT_VIDEO) != 0) { + return false; + } + + const SDL_VideoInfo *info = SDL_GetVideoInfo(); + int bpp = info->vfmt->BitsPerPixel; + + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + int flags = SDL_OPENGL; + if(fullscreen) + flags |= SDL_FULLSCREEN; + + screen = SDL_SetVideoMode(width, height, bpp, flags); + if(!screen) { + return false; + } + + SDL_WM_SetIcon(SDL_LoadBMP("inspector.bmp"), NULL); + SDL_WM_SetCaption(" Inspector", "Inspector"); + + + glDisable(GL_DITHER); + glShadeModel(GL_SMOOTH); + glHint( GL_FOG_HINT, GL_NICEST ); + glEnable(GL_DEPTH_TEST); + glDepthFunc( GL_LEQUAL ); + glDisable(GL_LIGHTING); + + glEnableClientState(GL_VERTEX_ARRAY); + return true; +} + + + + +int main(int argc, char *argv[]) { + char file[64]; + if(argc < 2) { + cerr << "Usage: " << argv[0] << " \n"; + return -1; + } + + Crude crude; + if(!crude.Load(argv[1])) { + cerr << "Could not load crude file: " << argv[1] << endl; + return -1; + } + Box3f box = crude.GetBox(); + + if(!init()) { + cerr << "Could not init SDL window\n"; + return -1; + } + + glClearColor(0, 0, 0, 0); + int quit = 0; + SDL_Event event; + int x, y; + float alpha = 0; + while( !quit ) { + while( SDL_PollEvent( &event ) ){ + switch( event.type ) { + case SDL_QUIT: quit = 1; break; + case SDL_KEYDOWN: + switch(event.key.keysym.sym) { + case SDLK_q: exit(0); break; + } + //quit = 1; + //error++; + //if(error == 5) error = 0; + //render.setMaxError(error/10.0); + break; + case SDL_MOUSEBUTTONDOWN: + x = event.button.x; + y = event.button.y; + // hand.buttonDown(x, y, 1); + break; + case SDL_MOUSEBUTTONUP: + x = event.button.x; + y = event.button.y; + // hand.buttonUp(x, y); + break; + case SDL_MOUSEMOTION: + while(SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_MOUSEMOTIONMASK)); + x = event.motion.x; + y = event.motion.y; + // hand.mouseMove(x, y); + break; + default: break; + } + } + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(60, 1, 0.1, 100); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + gluLookAt(0,0,3, 0,0,0, 0,1,0); + // hand.glTransform(); + // hand.glDraw(ColorUB(255, 100, 100, 200), 3); + + float scale = 3/box.Diag(); + //glScalef(0.4, 0.4, 0.4); + glRotatef(alpha, 0, 1, 0); + alpha++; + if(alpha > 360) alpha = 0; + glScalef(scale, scale, scale); + Point3f center = box.Center(); + glTranslatef(-center[0], -center[1], -center[2]); +// render.render(); + glColor3f(0, 1, 0); + + glBegin(GL_TRIANGLES); + + for(unsigned int i = 0;i < crude.Faces(); i++) { + Crude::Face &face = crude.GetFace(i); + for(int k = 0; k < 3; k++) { + Point3f &p = crude.GetVertex(face[k]); + glVertex3f(p[0], p[1], p[2]); + } + } + glEnd(); + + SDL_GL_SwapBuffers(); + } + + // Clean up + + SDL_Quit(); + return -1; +} + + diff --git a/apps/nexus/ply2crude/ply2crude.cpp b/apps/nexus/ply2crude/ply2crude.cpp new file mode 100644 index 00000000..05795868 --- /dev/null +++ b/apps/nexus/ply2crude/ply2crude.cpp @@ -0,0 +1,146 @@ +/**************************************************************************** +* 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 $ + +****************************************************************************/ + +///TODO: allow other kinds of ply to be readed. + +#include +#include +#include +#include +using namespace std; +using namespace vcg; +using namespace vcg::ply; +using namespace nxs; + +struct PlyVertex { + float v[3]; +}; + +struct PlyFace { + unsigned int f[3]; + unsigned char flags; +}; + +PropDescriptor plyprop1[3]= { + {"vertex","x",T_FLOAT,T_FLOAT,offsetof(PlyVertex,v[0]),0,0,0,0,0}, + {"vertex","y",T_FLOAT,T_FLOAT,offsetof(PlyVertex,v[1]),0,0,0,0,0}, + {"vertex","z",T_FLOAT,T_FLOAT,offsetof(PlyVertex,v[2]),0,0,0,0,0} +}; + +PropDescriptor plyprop2[1]= { + {"face", "vertex_indices",T_INT,T_UINT,offsetof(PlyFace,f[0]), + 1,0,T_UCHAR,T_UCHAR,offsetof(PlyFace,flags) } +}; + +int main(int argc, char *argv[]) { + if(argc <= 2) { + cerr << "Usage: " << argv[0] << " <...> \n"; + return 0; + } + string output = argv[argc-1]; + //test last one is not a ply + if(output.substr(output.size()-4, output.size()) == ".ply") { + cerr << "Last argument is output (so not a .ply)\n"; + return -1; + } + Crude crude; + if(!crude.Create(output, 0, 0)) { + cerr << "Could not create crude output\n"; + return -1; + } + Box3f box; + box.SetNull(); + for(int k = 1; k < argc-1; k++) { + PlyFile pf; + //Opening ply file + int val = pf.Open(argv[k], PlyFile::MODE_READ); + if(val == -1) { + cerr << "Could not open file '" << argv[k] << "'\n"; + return false; + } + + //testing for required vertex fields. + if( pf.AddToRead(plyprop1[0])==-1 || + pf.AddToRead(plyprop1[1])==-1 || + pf.AddToRead(plyprop1[2])==-1) { + cerr << "Error Ply file has not one of the required elements :" + << "xyz coords\n"; + return false; + } + + //testing for required face fields. + if( pf.AddToRead(plyprop2[0])==-1 ) { + cerr << "Error Ply file has not one of the required elements:" + << "faces\n"; + return false; + } + + for(unsigned int i = 0; i < pf.elements.size(); i++) { + if(!strcmp( pf.ElemName(i),"vertex")) { + unsigned int n_vertices = pf.ElemNumber(i); + unsigned int offset = crude.Vertices(); + crude.Resize(offset + n_vertices, crude.Faces()); + + cerr << "Adding " << n_vertices << " n_vertices" << endl; + pf.SetCurElement(i); + PlyVertex vertex; + Point3f p; + for(unsigned v = offset; v < offset + n_vertices; v++) { + pf.Read((void *) &vertex); + p[0] = vertex.v[0]; + p[1] = vertex.v[1]; + p[2] = vertex.v[2]; + box.Add(p); + crude.SetVertex(v, vertex.v); + } + + } else if( !strcmp( pf.ElemName(i),"face") ) { + unsigned int n_faces = pf.ElemNumber(i); + unsigned int offset = crude.Faces(); + crude.Resize(crude.Vertices(), offset + n_faces); + + cerr << "Adding " << n_faces << " n_faces" << endl; + pf.SetCurElement(i); + PlyFace face; + for(unsigned v = offset; v < offset + n_faces; v++) { + pf.Read((void *) &face); + assert(face.f[0] < crude.Vertices() && + face.f[1] < crude.Vertices() && + face.f[2] < crude.Vertices()); + + crude.SetFace(v, face.f); + } + } + } + pf.Destroy(); + } + crude.GetBox() = box; + + return 0; +}