Browse files

initial import

  • Loading branch information...
0 parents commit 4bbb7e8bd905b7f492ce16cd6e8e6f94402c131e @quadule committed Mar 24, 2012
Showing with 19,463 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +47 −0 autosmiley/addons/ofAddons.h
  3. +33 −0 autosmiley/addons/ofx3DModelLoader/install.xml
  4. +189 −0 autosmiley/addons/ofx3DModelLoader/src/3DS/Vector3DS.h
  5. +614 −0 autosmiley/addons/ofx3DModelLoader/src/3DS/model3DS.cpp
  6. +217 −0 autosmiley/addons/ofx3DModelLoader/src/3DS/model3DS.h
  7. +84 −0 autosmiley/addons/ofx3DModelLoader/src/3DS/texture3DS.cpp
  8. +21 −0 autosmiley/addons/ofx3DModelLoader/src/3DS/texture3DS.h
  9. +16 −0 autosmiley/addons/ofx3DModelLoader/src/ofx3DBaseLoader.h
  10. +26 −0 autosmiley/addons/ofx3DModelLoader/src/ofx3DModelLoader.h
  11. +89 −0 autosmiley/addons/ofx3DModelLoader/src/ofx3dModelLoader.cpp
  12. +46 −0 autosmiley/addons/ofxDirList/doc/Instructions.txt
  13. +26 −0 autosmiley/addons/ofxDirList/install.xml
  14. +145 −0 autosmiley/addons/ofxDirList/src/ofxDirList.cpp
  15. +294 −0 autosmiley/addons/ofxDirList/src/ofxDirList.h
  16. +128 −0 autosmiley/addons/ofxNetwork/docs/Instructions.txt
  17. +38 −0 autosmiley/addons/ofxNetwork/install.xml
  18. +13 −0 autosmiley/addons/ofxNetwork/src/ofxNetwork.h
  19. +232 −0 autosmiley/addons/ofxNetwork/src/ofxTCPClient.cpp
  20. +94 −0 autosmiley/addons/ofxNetwork/src/ofxTCPClient.h
  21. +446 −0 autosmiley/addons/ofxNetwork/src/ofxTCPManager.cpp
  22. +220 −0 autosmiley/addons/ofxNetwork/src/ofxTCPManager.h
  23. +222 −0 autosmiley/addons/ofxNetwork/src/ofxTCPServer.cpp
  24. +87 −0 autosmiley/addons/ofxNetwork/src/ofxTCPServer.h
  25. +484 −0 autosmiley/addons/ofxNetwork/src/ofxUDPManager.cpp
  26. +176 −0 autosmiley/addons/ofxNetwork/src/ofxUDPManager.h
  27. +55 −0 autosmiley/addons/ofxOpenCv/install.xml
  28. +1,467 −0 autosmiley/addons/ofxOpenCv/libs/opencv/include/cv.h
  29. +409 −0 autosmiley/addons/ofxOpenCv/libs/opencv/include/cv.hpp
  30. +1,574 −0 autosmiley/addons/ofxOpenCv/libs/opencv/include/cvaux.h
  31. +144 −0 autosmiley/addons/ofxOpenCv/libs/opencv/include/cvaux.hpp
  32. +1,080 −0 autosmiley/addons/ofxOpenCv/libs/opencv/include/cvcompat.h
  33. +2,326 −0 autosmiley/addons/ofxOpenCv/libs/opencv/include/cvmat.hpp
  34. +93 −0 autosmiley/addons/ofxOpenCv/libs/opencv/include/cvstreams.h
  35. +386 −0 autosmiley/addons/ofxOpenCv/libs/opencv/include/cvtypes.h
  36. +55 −0 autosmiley/addons/ofxOpenCv/libs/opencv/include/cvver.h
  37. +1,324 −0 autosmiley/addons/ofxOpenCv/libs/opencv/include/cvvidsurv.hpp
  38. +621 −0 autosmiley/addons/ofxOpenCv/libs/opencv/include/cvwimage.h
  39. +1,780 −0 autosmiley/addons/ofxOpenCv/libs/opencv/include/cxcore.h
  40. +375 −0 autosmiley/addons/ofxOpenCv/libs/opencv/include/cxcore.hpp
  41. +189 −0 autosmiley/addons/ofxOpenCv/libs/opencv/include/cxerror.h
  42. +918 −0 autosmiley/addons/ofxOpenCv/libs/opencv/include/cxmisc.h
  43. +1,780 −0 autosmiley/addons/ofxOpenCv/libs/opencv/include/cxtypes.h
  44. BIN autosmiley/addons/ofxOpenCv/libs/opencv/lib/osx/openCV.a
  45. +58 −0 autosmiley/addons/ofxOpenCv/src/ofxCvBlob.h
  46. +409 −0 autosmiley/addons/ofxOpenCv/src/ofxCvColorImage.cpp
  47. +138 −0 autosmiley/addons/ofxOpenCv/src/ofxCvColorImage.h
  48. +14 −0 autosmiley/addons/ofxOpenCv/src/ofxCvConstants.h
  49. +206 −0 autosmiley/addons/ofxOpenCv/src/ofxCvContourFinder.cpp
  50. +71 −0 autosmiley/addons/ofxOpenCv/src/ofxCvContourFinder.h
Sorry, we could not display the entire diff because too many files (995) changed.
4 .gitignore
@@ -0,0 +1,4 @@
+*.app
+*.xcuserstate
+*.xcsettings
+autosmiley/apps/AutoSmiley/AutoSmiley-v001/bin
47 autosmiley/addons/ofAddons.h
@@ -0,0 +1,47 @@
+#warning ______________________________________________________________________________
+#warning !!! ofAddons.h is deprecated - from OF 007 and on this file will NOT EXIST !!!
+#warning To see the new usage for using addons in OF please see www.openframeworks.cc/006-Important-Notes
+#warning ______________________________________________________________________________
+
+// these #define are kept around to make sure that old code doesn't break, but from 0.07 we will be using a non #define system for addons.
+// see: http://www.openframeworks.cc/006-Important-Notes
+
+//-------------------------------------------- openCV
+#ifdef OF_ADDON_USING_OFXOPENCV
+ #include "ofxCvMain.h"
+#endif
+
+//-------------------------------------------- dir list
+#ifdef OF_ADDON_USING_OFXDIRLIST
+ #include "ofxDirList.h"
+#endif
+
+//-------------------------------------------- vector math
+#ifdef OF_ADDON_USING_OFXVECTORMATH
+ #include "ofxVectorMath.h"
+#endif
+
+//-------------------------------------------- network
+#ifdef OF_ADDON_USING_OFXNETWORK
+ #include "ofxNetwork.h"
+#endif
+
+//-------------------------------------------- vector graphics
+#ifdef OF_ADDON_USING_OFXVECTORGRAPHICS
+ #include "ofxVectorGraphics.h"
+#endif
+
+//-------------------------------------------- OSC
+#ifdef OF_ADDON_USING_OFXOSC
+ #include "ofxOsc.h"
+#endif
+
+//-------------------------------------------- ofthread
+#ifdef OF_ADDON_USING_OFXTHREAD
+ #include "ofxThread.h"
+#endif
+
+//-------------------------------------------- ofXML
+#ifdef OF_ADDON_USING_OFXXMLSETTINGS
+ #include "ofxXmlSettings.h"
+#endif
33 autosmiley/addons/ofx3DModelLoader/install.xml
@@ -0,0 +1,33 @@
+<install>
+ <name>ofx3DModelLoader</name>
+
+ <version>0.01</version>
+ <author>theo watson</author>
+ <url></url>
+
+ <add>
+ <!-- ===================================================================== -->
+ <!-- ========================== add per project using this addon ========= -->
+ <!-- ===================================================================== -->
+
+ <src>
+ <folder name="addons/ofxDirList/src">
+ <file>../../../addons/ofx3DModelLoader/src/ofx3DBaseLoader.h</file>
+ <file>../../../addons/ofx3DModelLoader/src/ofx3dModelLoader.cpp</file>
+ <file>../../../addons/ofx3DModelLoader/src/ofx3DModelLoader.h</file>
+ <file>../../../addons/ofx3DModelLoader/src/3DS/model3DS.cpp</file>
+ <file>../../../addons/ofx3DModelLoader/src/3DS/model3DS.h</file>
+ <file>../../../addons/ofx3DModelLoader/src/3DS/texture3DS.cpp</file>
+ <file>../../../addons/ofx3DModelLoader/src/3DS/texture3DS.h</file>
+ <file>../../../addons/ofx3DModelLoader/src/3DS/Vector3DS.h</file>
+ </folder>
+ </src>
+
+ <include>
+ <path>../../../addons/ofx3DModelLoader/src/3DS/</path>
+ <path>../../../addons/ofx3DModelLoader/src/</path>
+ </include>
+
+ </add>
+
+</install>
189 autosmiley/addons/ofx3DModelLoader/src/3DS/Vector3DS.h
@@ -0,0 +1,189 @@
+/*
+ Vector3DS class
+ © Keith O'Conor 2004
+ keith.oconor @ {cs.tcd.ie, gmail.com}
+*/
+
+#ifndef VECTOR_H
+#define VECTOR_H
+
+#include <math.h>
+#include <iostream>
+
+class Vector3DS{
+public:
+ float x;
+ float y;
+ float z;
+
+ /*
+ Vector3DS();
+ Vector3DS(float a, float b, float c);
+ Vector3DS(Vector3DS& copy);
+
+ Vector3DS& operator=(const Vector3DS &other);
+ int operator== (const Vector3DS &other);
+ int operator!= (const Vector3DS &other);
+ Vector3DS operator+(const Vector3DS &other);
+ Vector3DS operator-(const Vector3DS &other);
+ Vector3DS operator*(const float &value);
+ Vector3DS operator/(const float &value);
+ Vector3DS& operator+=(const Vector3DS &other);
+ Vector3DS& operator-=(const Vector3DS &other);
+ Vector3DS& operator*=(const float& other);
+ Vector3DS& operator/=(const float& other);
+ float operator[](unsigned i);
+
+ float length();
+ float lengthSq();
+ float dotProduct(const Vector3DS &other);
+ Vector3DS crossProduct(const Vector3DS &other);
+ void normalize();
+ float distance(const Vector3DS &other);
+ float distanceSq(const Vector3DS &other);
+ void set(float newX, float newY, float newZ);
+ void zero();
+ */
+
+ /////////////////
+ // Constructors
+ /////////////////
+
+ inline Vector3DS():x(0),y(0),z(0){}
+ inline Vector3DS(const float a, const float b, const float c):x(a),y(b),z(c){}
+ inline Vector3DS(const Vector3DS& copy):x(copy.x),y(copy.y),z(copy.z){}
+
+ //////////////
+ // Operators
+ //////////////
+
+ inline Vector3DS& operator= (const Vector3DS &other){
+ x=other.x;y=other.y;z=other.z;
+ return *this;
+ }
+
+ inline int operator== (const Vector3DS &other) const{
+ return (x==other.x && y==other.y && z==other.z);
+ }
+ inline int operator!= (const Vector3DS &other) const{
+ return (x!=other.x || y!=other.y || z!=other.z);
+ }
+
+ inline Vector3DS operator+ (const Vector3DS &other) const{
+ return Vector3DS(x+other.x, y+other.y, z+other.z);
+ }
+
+ inline Vector3DS operator- (const Vector3DS &other) const{
+ return Vector3DS(x-other.x, y-other.y, z-other.z);
+ }
+
+ inline Vector3DS operator* (const float &value) const{
+ return Vector3DS(x*value, y*value, z*value);
+ }
+
+ inline Vector3DS operator/ (const float &value) const{
+ return Vector3DS(x/value, y/value, z/value);
+ }
+
+ inline Vector3DS& operator+= (const Vector3DS &other){
+ x+=other.x;
+ y+=other.y;
+ z+=other.z;
+ return *this;
+ }
+ inline Vector3DS& operator-= (const Vector3DS &other){
+ x-=other.x;
+ y-=other.y;
+ z-=other.z;
+ return *this;
+ }
+
+ inline Vector3DS& operator*= (const float &value){
+ x*=value;
+ y*=value;
+ z*=value;
+ return *this;
+ }
+
+ inline Vector3DS& operator/= (const float &value){
+ x/=value;
+ y/=value;
+ z/=value;
+ return *this;
+ }
+
+ inline float operator[] (unsigned i) const{
+ switch(i){
+ case 0:return x;
+ case 1:return y;
+ case 2:return z;
+ }
+ }
+
+ /////////////////////
+ // Other operations
+ /////////////////////
+
+ inline float length() const{
+ float len=(x*x)+(y*y)+(z*z);
+ return (float)sqrt(len);
+ }
+
+ inline float lengthSq() const{
+ return (x*x)+(y*y)+(z*z);
+ }
+
+ inline float dotProduct(const Vector3DS &other) const{
+ //this[dot]other
+ return (x*other.x) + (y*other.y) + (z*other.z);
+ }
+
+ inline Vector3DS crossProduct(const Vector3DS &other) const{
+ //(x1,y1,z1)◊(x2,y2,z2) = (y1z2-y2z1, x2z1-x1z2, x1y2-x2y1).
+ return Vector3DS(
+ (y*other.z) - (z*other.y),
+ (z*other.x) - (x*other.z),
+ (x*other.y) - (y*other.x)
+ );
+ }
+
+ inline void normalize(){
+ float len=length();
+ if(len==0)return;
+ len=1.0f/len;
+ x*=len;
+ y*=len;
+ z*=len;
+ }
+
+ inline float distance(const Vector3DS &other) const{
+ return (Vector3DS(other.x-x,other.y-y,other.z-z)).length();
+ }
+
+ inline float distanceSq(const Vector3DS &other) const{
+ return (Vector3DS(other.x-x,other.y-y,other.z-z)).lengthSq();
+ }
+
+ inline void set(float newX, float newY, float newZ){
+ x=newX;y=newY;z=newZ;
+ }
+
+ inline void zero(){
+ x=y=z=0;
+ }
+
+};
+
+typedef Vector3DS Vertex;
+
+const Vector3DS vZero=Vector3DS(0,0,0);
+
+ /////////////////////////////
+ // Global stream operators
+//////////////////////////////
+inline std::ostream& operator<<(std::ostream &str, const Vector3DS &v){
+ str<<v.x<<", "<<v.y<<", "<<v.z;
+ return str;
+}
+
+#endif //VECTOR_H
614 autosmiley/addons/ofx3DModelLoader/src/3DS/model3DS.cpp
@@ -0,0 +1,614 @@
+/*
+ 3DS model loader
+ � Keith O'Conor 2005
+ keith.oconor @ {cs.tcd.ie, gmail.com}
+*/
+
+#include "model3DS.h"
+
+model3DS::model3DS(){
+ hasTexture = false;
+}
+
+void model3DS::loadModel(string filename, float scale){
+ filename = ofToDataPath(filename);
+ loadModel((const char *)filename.c_str(), scale);
+}
+
+void model3DS::loadModel(const char* filename, float scale){
+
+ m_filename = filename;
+ m_scale = scale;
+
+ std::ifstream *modelFile = new std::ifstream(filename,std::ios::in | std::ios::binary | std::ios::ate);
+
+ if(!modelFile->is_open()){
+ std::cout<<"[3DS] ERROR: Could not open '"<<filename<<"'"<<std::endl;
+ return;
+ }
+ if(int(modelFile->tellg()) == 0){
+ std::cout<<"[3DS] ERROR: Model '"<<filename<<"' is empty"<<std::endl;
+ modelFile->close();
+ return;
+ }
+
+ // Extract path from filename
+ int lastSlashPosition=-1, lastForwardSlash=-1, lastBackslash=-1;
+ lastForwardSlash = (int)m_filename.find_last_of('/');
+ lastBackslash = (int)m_filename.find_last_of('\\');
+ if(lastForwardSlash > lastSlashPosition) lastSlashPosition = lastForwardSlash;
+ if(lastBackslash > lastSlashPosition) lastSlashPosition = lastBackslash;
+ m_filepath = m_filename.substr(0,lastSlashPosition+1);
+ m_filename = m_filename.substr(lastSlashPosition+1);
+
+ // Check to make sure file is valid 3DS format (begins with 0x4D4D)
+ ushort chunkHeader;
+ unsigned int chunkLength;
+
+ modelFile->seekg(0, std::ios::beg);
+ modelFile->read((char*)&chunkHeader,2);
+ modelFile->read((char*)&chunkLength,4);
+
+ if(chunkHeader != CHUNK_MAIN){
+ std::cout<<"[3DS] ERROR: Model '"<<filename<<"' is not a valid 3DS file"<<std::endl;
+ modelFile->close();
+ return;
+ }
+
+ // Detect VBO support
+ std::stringstream extStream((const char*)glGetString(GL_EXTENSIONS));
+ std::string nextToken;
+ bool isVBOSupported=false;
+ while(!extStream.eof()){
+ extStream >> nextToken;
+ if(nextToken == "GL_ARB_vertex_buffer_object"){
+ isVBOSupported=true;
+ break;
+ }
+ }
+ m_drawMode = DRAW_VERTEX_ARRAY;
+
+ // Initialise bounding box to min & max 4-byte float values
+ m_boundingBox.minX = m_boundingBox.minY = m_boundingBox.minZ = 3.4e+38f;
+ m_boundingBox.maxX = m_boundingBox.maxY = m_boundingBox.maxZ = 3.4e-38f;
+
+ // Read all 3DS chunks recursively
+
+ long end;
+ modelFile->seekg (0, ios::end);
+ end = modelFile->tellg();
+ modelFile->seekg (0, ios::beg);
+
+
+ while(!modelFile->eof() && (end != modelFile->tellg())){
+ readChunk(modelFile, modelFile->tellg(), chunkLength);
+ }
+
+ m_centerX = (m_boundingBox.minX + m_boundingBox.maxX) / 2.f;
+ m_centerY = (m_boundingBox.minY + m_boundingBox.maxY) / 2.f;
+ m_centerZ = (m_boundingBox.minZ + m_boundingBox.maxZ) / 2.f;
+
+ // Model loaded, clean up
+ modelFile->close();
+ delete modelFile;
+ std::cout<<"[3DS] Model '"<<filename<<"' loaded"<<std::endl;
+}
+
+void model3DS::readChunk(std::ifstream *modelFile, const int objectStart, const int objectLength){
+ //std::cout<<std::hex<<"readChunk("<<objectStart<<"-"<<(objectStart+objectLength)<<")"<<std::dec<<std::endl;
+
+ ushort chunkHeader;
+ unsigned int chunkLength;
+
+ unsigned long offset;
+ ushort numVertices;
+ ushort usTemp;
+ unsigned int uiTemp;
+ float vertexX,vertexY,vertexZ;
+ int v;
+ std::string name;
+ char currentLetter;
+ unsigned char rgbByte;
+
+ while((modelFile->tellg() < (objectStart + objectLength)) && !modelFile->eof()){
+
+ offset = modelFile->tellg();
+
+ modelFile->read((char*)&chunkHeader, 2);
+ modelFile->read((char*)&chunkLength, 4);
+
+ if(DEBUG_OUTPUT) std::cout<<std::hex<<"["<<offset<<"] chunk: 0x"<<chunkHeader<<" ("<<offset<<"-"<<(offset+chunkLength)<<")"<<std::dec<<std::endl;
+ switch(chunkHeader){
+
+ //////////////////
+ // Main chunks
+ /////////////////
+
+ case CHUNK_MAIN: continue;
+
+ case CHUNK_3D_EDITOR: continue;
+
+ case CHUNK_OBJECT_BLOCK:
+ if(DEBUG_OUTPUT) std::cout<<std::endl<<"[Object block]"<<std::endl;
+
+ m_currentMesh = new mesh3DS(this);
+ m_currentMesh->setDrawMode(m_drawMode);
+
+ // Read object name
+ do{
+ modelFile->read(&currentLetter,1);
+ name += currentLetter;
+ }while(currentLetter!='\0' && name.length()<20);
+ m_currentMesh->setName(name);
+ if(DEBUG_OUTPUT) std::cout<<" Object: "<<name<<std::endl;
+ name.erase();
+
+ // Read object sub-chunks
+ readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));
+
+ if(m_currentMesh->getNumFaces() != 0){
+ m_currentMesh->buildMesh();
+ m_meshes.push_back(*m_currentMesh);
+ }
+ delete m_currentMesh;
+ break;
+
+ /////////////////////
+ // Geometry chunks
+ ////////////////////
+
+ case CHUNK_MESH:continue;
+
+ case CHUNK_VERTICES:
+ modelFile->read((char*)&numVertices,2);
+ for(v=0; v < numVertices*3; v+=3){
+ modelFile->read((char*)&vertexX,4);
+ modelFile->read((char*)&vertexY,4);
+ modelFile->read((char*)&vertexZ,4);
+ // 3DS Max has different axes to OpenGL
+ vertexX *= m_scale;
+ vertexY *= m_scale;
+ vertexZ *= m_scale;
+ m_currentMesh->addVertex(vertexX);// x
+ m_currentMesh->addVertex(vertexZ);// y
+ m_currentMesh->addVertex(-vertexY);// z
+ // Update bounding box
+ if(vertexX < m_boundingBox.minX)m_boundingBox.minX = vertexX;
+ if(vertexZ < m_boundingBox.minY)m_boundingBox.minY = vertexZ;
+ if(-vertexY < m_boundingBox.minZ)m_boundingBox.minZ = -vertexY;
+ if(vertexX > m_boundingBox.maxX)m_boundingBox.maxX = vertexX;
+ if(vertexZ > m_boundingBox.maxY)m_boundingBox.maxY = vertexZ;
+ if(-vertexY > m_boundingBox.maxZ)m_boundingBox.maxZ = -vertexY;
+ }
+ break;
+
+ case CHUNK_TEXCOORDS: // texcoords list
+ modelFile->read((char*)&numVertices,2);
+ for(v=0; v < numVertices*2; v+=2){
+ modelFile->read((char*)&vertexX,4);
+ modelFile->read((char*)&vertexY,4);
+ m_currentMesh->addTexcoord(vertexX);
+ m_currentMesh->addTexcoord(vertexY);
+ }
+ break;
+
+ case CHUNK_FACES:
+ modelFile->read((char*)&m_tempUshort,2);
+
+ for(v=0; v < m_tempUshort*3; v+=3){
+ modelFile->read((char*)&usTemp,2);m_currentMesh->addFaceIndex(usTemp);
+ modelFile->read((char*)&usTemp,2);m_currentMesh->addFaceIndex(usTemp);
+ modelFile->read((char*)&usTemp,2);m_currentMesh->addFaceIndex(usTemp);
+ modelFile->read((char*)&usTemp,2); //face flags
+ }
+
+ // Read face sub-chunks
+ readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));
+ break;
+
+ case CHUNK_SMOOTHING_GROUP:
+ for(v=0; v < m_tempUshort; v++){
+ modelFile->read((char*)&uiTemp,4);
+ m_currentMesh->addFaceSmoothing(uiTemp);
+ //if(DEBUG_OUTPUT) std::cout<<"Smoothing: "<<uiTemp<<std::endl;
+ }
+ break;
+
+ /////////////////////
+ // Material chunks
+ ////////////////////
+
+ case CHUNK_FACE_MATERIAL:
+ // Read material name
+ do{
+ modelFile->read(&currentLetter,1);
+ name += currentLetter;
+ }while(currentLetter!='\0' && name.length()<20);
+
+ modelFile->read((char*)&m_tempUshort,2);
+
+ for(v=0; v < m_tempUshort; v++){
+ modelFile->read((char*)&usTemp,2);
+ m_currentMesh->addMaterialFace(name, usTemp);
+ }
+
+ name.erase();
+
+ break;
+
+ case CHUNK_MATERIAL_BLOCK:
+ if(DEBUG_OUTPUT) std::cout<<std::endl<<"[Material block]"<<std::endl;
+
+ m_currentMaterial = new material3DS();
+
+ // Read material sub-chunks
+ readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));
+
+ m_materials[m_currentMaterial->getName()] = *m_currentMaterial;
+ delete m_currentMaterial;
+ break;
+
+ case CHUNK_MATERIAL_NAME:
+ // Read material name and add to current material
+ do{
+ modelFile->read(&currentLetter,1);
+ name += currentLetter;
+ }while(currentLetter!='\0' && name.length()<20);
+ m_currentMaterial->setName(name);
+ if(DEBUG_OUTPUT) std::cout<<" Material: "<<m_currentMaterial->getName()<<"("<<m_currentMaterial->getName().size()<<")"<<std::endl;
+ name.erase();
+ break;
+
+ case CHUNK_TEXTURE_MAP:
+ case CHUNK_BUMP_MAP:
+ //Read texture name and add to current material
+ readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));
+ m_currentMaterial->loadTexture(m_filepath + m_tempString, chunkHeader);
+ hasTexture = true;
+
+ break;
+
+ case CHUNK_MAP_FILENAME:
+ // Read texture map filename
+ m_tempString.erase();
+ do{
+ modelFile->read(&currentLetter,1);
+ m_tempString += currentLetter;
+ }while(currentLetter!='\0' && m_tempString.length()<20);
+ break;
+
+ case CHUNK_MATERIAL_TWO_SIDED:
+ m_currentMaterial->setTwoSided(true);
+ break;
+
+ case CHUNK_DIFFUSE_COLOR:
+ // Read color sub-chunks
+ readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));
+ m_currentMaterial->setDiffuseColor(m_currentColor);
+
+ break;
+
+ case CHUNK_AMBIENT_COLOR:
+ // Read color sub-chunks
+ readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));
+ m_currentMaterial->setAmbientColor(m_currentColor);
+
+ break;
+
+ case CHUNK_SPECULAR_COLOR:
+ // Read color sub-chunks
+ readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));
+ m_currentMaterial->setSpecularColor(m_currentColor);
+
+ break;
+
+ case CHUNK_SPECULAR_EXPONENT:
+ // Read percent sub-chunk
+ readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));
+ m_currentMaterial->setSpecularExponent(m_tempFloat);
+ break;
+
+ case CHUNK_SHININESS:
+ // Read percent sub-chunk
+ readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));
+ m_currentMaterial->setShininess(m_tempFloat);
+ break;
+
+ case CHUNK_TRANSPARENCY:
+ // Read percent sub-chunk
+ readChunk(modelFile, modelFile->tellg(), chunkLength - (long(modelFile->tellg()) - offset));
+ m_currentMaterial->setOpacity(1.0f - m_tempFloat);
+ break;
+
+ /////////////////////
+ // Global chunks
+ ////////////////////
+
+ case CHUNK_RGB_FLOAT:
+ case CHUNK_RGB_FLOAT_GAMMA:
+ modelFile->read((char*)&m_currentColor[0],4);
+ modelFile->read((char*)&m_currentColor[1],4);
+ modelFile->read((char*)&m_currentColor[2],4);
+ break;
+
+ case CHUNK_RGB_BYTE:
+ case CHUNK_RGB_BYTE_GAMMA:
+ modelFile->read((char*)&rgbByte,1); m_currentColor[0]=float(rgbByte)/255.f;
+ modelFile->read((char*)&rgbByte,1); m_currentColor[1]=float(rgbByte)/255.f;
+ modelFile->read((char*)&rgbByte,1); m_currentColor[2]=float(rgbByte)/255.f;
+ break;
+
+ case CHUNK_PERCENT_INT:
+ modelFile->read((char*)&usTemp,2);
+ m_tempFloat = usTemp / 100.f;
+ break;
+
+ case CHUNK_PERCENT_FLOAT:
+ modelFile->read((char*)&m_tempFloat,4);
+ m_tempFloat /= 100.f;
+ break;
+
+ default:break; // any other chunk
+
+ }
+
+ // Go to the next chunk's header (if any left in object)
+ modelFile->seekg(offset + chunkLength, std::ios::beg);
+ }
+
+}
+
+void material3DS::loadTexture(std::string filename, int chunkType){
+
+ string lowerCaseStr = filename;
+ std::transform(lowerCaseStr.begin(),lowerCaseStr.end(),lowerCaseStr.begin(), ::tolower);
+
+ if((lowerCaseStr.find(".jpg") == std::string::npos) && (lowerCaseStr.find(".png") == std::string::npos) && (lowerCaseStr.find(".tga") == std::string::npos) && (lowerCaseStr.find(".bmp") == std::string::npos)){
+ std::cout<<"[3DS] WARNING: Could not load map '"<<filename<<"'\n[3DS] WARNING: (texture must be TGA, PNG, JPG or BMP)"<<std::endl;
+ return;
+ }
+
+ GLuint newTextureId;
+ glGenTextures(1, &newTextureId);
+
+ texture3DS newTexture(filename, newTextureId);
+
+ switch(chunkType){
+ case CHUNK_TEXTURE_MAP:
+ m_textureMapId = newTextureId;
+ m_hasTextureMap = true;
+ break;
+ case CHUNK_BUMP_MAP:
+ m_bumpMapId = newTextureId;
+ m_hasBumpMap = true;
+ break;
+ }
+
+}
+
+void mesh3DS::buildMesh(){
+ calculateNormals();
+ sortFacesByMaterial();
+
+}
+
+void mesh3DS::calculateNormals(){
+
+ // Doesn't take smoothing groups into account yet
+
+ if(DEBUG_OUTPUT) std::cout<<"Calculating normals... ";
+ m_normals.assign(m_vertices.size(), 0.0f);
+
+ Vertex vtx1, vtx2, vtx3;
+ Vector3DS v1, v2, faceNormal;
+
+ for(int face=0; face < int(m_faces.size()); face+=3){
+ // Calculate face normal
+ vtx1.set(m_vertices[m_faces[face]*3], m_vertices[(m_faces[face]*3)+1], m_vertices[(m_faces[face]*3)+2]);
+ vtx2.set(m_vertices[m_faces[face+1]*3], m_vertices[(m_faces[face+1]*3)+1], m_vertices[(m_faces[face+1]*3)+2]);
+ vtx3.set(m_vertices[m_faces[face+2]*3], m_vertices[(m_faces[face+2]*3)+1], m_vertices[(m_faces[face+2]*3)+2]);
+
+ v1 = vtx2 - vtx1;
+ v2 = vtx3 - vtx1;
+
+ faceNormal = v1.crossProduct(v2);
+
+ // Add normal to all three vertex normals
+ m_normals[m_faces[face]*3] += faceNormal.x;
+ m_normals[(m_faces[face]*3)+1] += faceNormal.y;
+ m_normals[(m_faces[face]*3)+2] += faceNormal.z;
+
+ m_normals[m_faces[face+1]*3] += faceNormal.x;
+ m_normals[(m_faces[face+1]*3)+1] += faceNormal.y;
+ m_normals[(m_faces[face+1]*3)+2] += faceNormal.z;
+
+ m_normals[m_faces[face+2]*3] += faceNormal.x;
+ m_normals[(m_faces[face+2]*3)+1] += faceNormal.y;
+ m_normals[(m_faces[face+2]*3)+2] += faceNormal.z;
+
+ }
+
+ //normalize all normals
+ for(int n=0; n < int(m_normals.size()); n+=3){
+ faceNormal.set(m_normals[n], m_normals[n+1], m_normals[n+2]);
+ faceNormal.normalize();
+ m_normals[n] = faceNormal.x;
+ m_normals[n+1] = faceNormal.y;
+ m_normals[n+2] = faceNormal.z;
+ }
+
+ if(DEBUG_OUTPUT) std::cout<<"done"<<std::endl;
+}
+
+void mesh3DS::sortFacesByMaterial(){
+
+ assert(getNumFaces()!=0);
+ assert(m_parentModel!=NULL);
+
+ std::vector<ushort> newMatFaces;
+
+ // mark each face off as assigned to a material so
+ // we can figure out which faces have no material
+ std::vector<bool> assignedFaces;
+ std::vector<bool>::iterator assignedFacesIter;
+ assignedFaces.assign(m_faces.size() / 3, false);
+
+ // loop over each material
+ std::map<std::string, std::vector<ushort> >::iterator matFacesIter;
+ for(matFacesIter=m_materialFaces.begin(); matFacesIter!=m_materialFaces.end(); ++matFacesIter){
+ //std::cout<<" Faces in material '"<<matFacesIter->first<<"': "<<matFacesIter->second.size()<<std::endl;
+
+ // loop over all the faces with that material
+ std::vector<ushort>::iterator facesIter;
+ for(facesIter=matFacesIter->second.begin(); facesIter!=matFacesIter->second.end(); ++facesIter){
+ newMatFaces.push_back(m_faces[((*facesIter)*3)]);
+ newMatFaces.push_back(m_faces[((*facesIter)*3)+1]);
+ newMatFaces.push_back(m_faces[((*facesIter)*3)+2]);
+ assignedFaces[*facesIter]=true;
+ }
+
+ //replace the material's face indices with the actual face vertex indices
+ m_materialFaces[matFacesIter->first].assign(newMatFaces.begin(),newMatFaces.end());
+ newMatFaces.clear();
+ }
+
+ // Make a default material and assign any unused faces to it
+ int numUnassignedFaces=0;
+ for(assignedFacesIter=assignedFaces.begin(); assignedFacesIter!=assignedFaces.end(); ++assignedFacesIter){
+ if(*assignedFacesIter == false){
+ numUnassignedFaces++;
+ //assign face to default material
+ }
+ }
+ //std::cout<<"numUnassignedFaces: "<<numUnassignedFaces<<std::endl;
+}
+
+void mesh3DS::draw(){
+
+ assert(getNumFaces()!=0);
+
+ int face, numFaces, vertexIndex, texcoordIndex;
+ GLuint materialFaces; //GL_FRONT or GL_FRONT_AND_BACK
+
+ std::map<std::string, std::vector<ushort> >::iterator materialsIter;
+ for(materialsIter=m_materialFaces.begin(); materialsIter!=m_materialFaces.end(); ++materialsIter){
+ const material3DS& currentMaterial = m_parentModel->getMaterial(materialsIter->first);
+
+ // Bind texture map (if any)
+ bool hasTextureMap = currentMaterial.hasTextureMap();
+ if(hasTextureMap) glBindTexture(GL_TEXTURE_2D, currentMaterial.getTextureMapId());
+ else glBindTexture(GL_TEXTURE_2D, 0);
+
+ const GLfloat *specular = currentMaterial.getSpecularColor();
+ float shininess = currentMaterial.getShininess();
+ float adjustedSpecular[4] = {specular[0]*shininess, specular[1]*shininess, specular[2]*shininess, 1};
+
+ glPushAttrib(GL_LIGHTING_BIT);
+ if(currentMaterial.isTwoSided()){
+ glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,1);
+ materialFaces = GL_FRONT_AND_BACK;
+ }
+ else{
+ glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,0);
+ materialFaces = GL_FRONT;
+ }
+
+ // Apply material colors
+ if(glIsEnabled(GL_LIGHTING)){
+ //const GLfloat matZero[4]={0,0,0,0};
+ const GLfloat matOne[4]={1,1,1,1};
+ if(hasTextureMap){ //replace color with texture, but keep lighting contribution
+ glMaterialfv(materialFaces, GL_DIFFUSE, matOne);
+ }
+ else glMaterialfv(materialFaces, GL_DIFFUSE, currentMaterial.getDiffuseColor());
+ glMaterialfv(materialFaces, GL_AMBIENT, currentMaterial.getAmbientColor());
+ glMaterialfv(materialFaces, GL_SPECULAR, adjustedSpecular);
+ glMaterialf(materialFaces, GL_SHININESS, 128.f * currentMaterial.getSpecularExponent());
+ }
+ else glColor3fv(currentMaterial.getDiffuseColor());
+
+ const std::vector<ushort> *currentMatFaces = &(materialsIter->second);
+ numFaces = (int)currentMatFaces->size(); //number of faces in this material
+
+ switch(m_drawMode){
+ case DRAW_IMMEDIATE_MODE:
+
+ glBegin(GL_TRIANGLES);
+ for(face=0; face<numFaces; face+=3){
+ if(hasTextureMap){
+ texcoordIndex = (*currentMatFaces)[face]*2;
+ glTexCoord2f(m_texcoords[texcoordIndex], m_texcoords[texcoordIndex+1]);
+ }
+ vertexIndex = (*currentMatFaces)[face]*3;
+ glNormal3f(m_normals[vertexIndex], m_normals[vertexIndex+1], m_normals[vertexIndex+2]);
+ glVertex3f(m_vertices[vertexIndex], m_vertices[vertexIndex+1], m_vertices[vertexIndex+2]);
+
+ if(hasTextureMap){
+ texcoordIndex = (*currentMatFaces)[face+1]*2;
+ glTexCoord2f(m_texcoords[texcoordIndex], m_texcoords[texcoordIndex+1]);
+ }
+ vertexIndex = (*currentMatFaces)[face+1]*3;
+ glNormal3f(m_normals[vertexIndex], m_normals[vertexIndex+1], m_normals[vertexIndex+2]);
+ glVertex3f(m_vertices[vertexIndex], m_vertices[vertexIndex+1], m_vertices[vertexIndex+2]);
+
+ if(hasTextureMap){
+ texcoordIndex = (*currentMatFaces)[face+2]*2;
+ glTexCoord2f(m_texcoords[texcoordIndex], m_texcoords[texcoordIndex+1]);
+ }
+ vertexIndex = (*currentMatFaces)[face+2]*3;
+ glNormal3f(m_normals[vertexIndex], m_normals[vertexIndex+1], m_normals[vertexIndex+2]);
+ glVertex3f(m_vertices[vertexIndex], m_vertices[vertexIndex+1], m_vertices[vertexIndex+2]);
+ }
+ glEnd();
+
+ break;
+
+ case DRAW_VERTEX_ARRAY:
+
+ glEnableClientState( GL_VERTEX_ARRAY );
+ glEnableClientState( GL_NORMAL_ARRAY );
+
+ if(hasTextureMap){
+ glTexCoordPointer( 2, GL_FLOAT, 0, &m_texcoords[0] );
+ glEnableClientState( GL_TEXTURE_COORD_ARRAY );
+ }
+
+ glVertexPointer( 3, GL_FLOAT, 0, &m_vertices[0] );
+ glNormalPointer(GL_FLOAT, 0, &m_normals[0] );
+ glDrawElements(GL_TRIANGLES, numFaces, GL_UNSIGNED_SHORT, &(materialsIter->second[0]));
+
+ glDisableClientState( GL_VERTEX_ARRAY );
+ glDisableClientState( GL_NORMAL_ARRAY );
+ if(hasTextureMap){
+ glDisableClientState( GL_TEXTURE_COORD_ARRAY );
+ }
+
+ break;
+
+ case DRAW_VBO:
+
+ break;
+
+ default:
+ std::cout<<"[3DS] ERROR: Invalid mesh draw mode specified"<<std::endl;
+ break;
+ }
+
+ glPopAttrib(); // GL_LIGHTING_BIT
+ }
+}
+
+void model3DS::draw(){
+
+
+ std::vector<mesh3DS>::iterator meshIter;
+
+ glPushMatrix();
+ glTranslatef(-m_centerX,-m_centerY,-m_centerZ);
+
+ for(meshIter = m_meshes.begin(); meshIter != m_meshes.end(); meshIter++){
+ meshIter->draw();
+ }
+ glPopMatrix();
+}
+
217 autosmiley/addons/ofx3DModelLoader/src/3DS/model3DS.h
@@ -0,0 +1,217 @@
+/*
+ 3DS model loader
+ � Keith O'Conor 2005
+ keith.oconor @ {cs.tcd.ie, gmail.com}
+*/
+
+#ifndef MODEL3DS_H
+#define MODEL3DS_H
+
+#pragma warning(disable: 4786) //ignore truncated STL debug identifiers warnings
+
+#include "ofMain.h"
+#include "texture3DS.h"
+#include "Vector3DS.h"
+
+#include "ofx3DBaseLoader.h"
+
+#include <iostream>
+#include <fstream>
+#include <vector>
+#include <list>
+#include <map>
+#include <string>
+#include <sstream>
+#include <cctype>
+#include <algorithm>
+#include <functional>
+#include <iostream>
+#include <assert.h>
+
+#define CHUNK_MAIN 0x4D4D
+#define CHUNK_3D_EDITOR 0x3D3D
+#define CHUNK_OBJECT_BLOCK 0x4000
+#define CHUNK_MESH 0x4100
+#define CHUNK_VERTICES 0x4110
+#define CHUNK_FACES 0x4120
+#define CHUNK_FACE_MATERIAL 0x4130
+#define CHUNK_SMOOTHING_GROUP 0x4150
+#define CHUNK_TEXCOORDS 0x4140
+#define CHUNK_MATERIAL_BLOCK 0xAFFF
+#define CHUNK_MATERIAL_NAME 0xA000
+#define CHUNK_AMBIENT_COLOR 0xA010
+#define CHUNK_DIFFUSE_COLOR 0xA020
+#define CHUNK_SPECULAR_COLOR 0xA030
+#define CHUNK_SPECULAR_EXPONENT 0xA040
+#define CHUNK_SHININESS 0xA041
+#define CHUNK_TRANSPARENCY 0xA050
+#define CHUNK_MATERIAL_TWO_SIDED 0xA081
+#define CHUNK_TEXTURE_MAP 0xA200
+#define CHUNK_BUMP_MAP 0xA230
+#define CHUNK_REFLECTION_MAP 0xA220
+#define CHUNK_MAP_FILENAME 0xA300
+#define CHUNK_RGB_FLOAT 0x0010
+#define CHUNK_RGB_BYTE 0x0011
+#define CHUNK_RGB_BYTE_GAMMA 0x0012
+#define CHUNK_RGB_FLOAT_GAMMA 0x0013
+#define CHUNK_PERCENT_INT 0x0030
+#define CHUNK_PERCENT_FLOAT 0x0031
+
+#define DRAW_IMMEDIATE_MODE 0x01
+#define DRAW_VERTEX_ARRAY 0x02
+#define DRAW_VBO 0x03
+
+#define DEBUG_OUTPUT false
+
+class model3DS;
+class mesh3DS;
+class material3DS;
+
+typedef unsigned short ushort;
+
+ ///////////////////////////////////////////////////////
+ // 3DS Material
+ // Describes a material to be used by a mesh3DS
+/////////////////////////////////////////////////////
+
+class material3DS{
+private:
+ std::string m_name;
+ float m_diffuseColor[3];
+ float m_ambientColor[3];
+ float m_specularColor[3];
+ float m_specularExponent;
+ float m_shininess;
+ float m_opacity;
+ GLuint m_textureMapId;
+ GLuint m_bumpMapId;
+ bool m_hasTextureMap;
+ bool m_hasBumpMap;
+ bool m_twoSided;
+
+public:
+ material3DS():m_hasTextureMap(false),m_hasBumpMap(false),m_twoSided(false){}
+ void loadTexture(std::string filename, int mapType);
+
+ std::string getName()const{return m_name;}
+ const float* getDiffuseColor()const{return m_diffuseColor;}
+ const float* getAmbientColor()const{return m_ambientColor;}
+ const float* getSpecularColor()const{return m_specularColor;}
+ float getSpecularExponent()const{return m_specularExponent;}
+ float getShininess()const{return m_shininess;}
+ float getOpacity()const{return m_opacity;}
+ GLuint getTextureMapId()const{return m_textureMapId;}
+ GLuint getBumpMapId()const{return m_bumpMapId;}
+ bool hasTextureMap()const{return m_hasTextureMap;}
+ bool hasBumpMap()const{return m_hasBumpMap;}
+ bool isTwoSided()const{return m_twoSided;}
+
+ void setName(const std::string newName){m_name = newName;}
+ void setDiffuseColor(const float* newColor){memcpy(m_diffuseColor,newColor,sizeof(float)*3);}
+ void setAmbientColor(const float* newColor){memcpy(m_ambientColor,newColor,sizeof(float)*3);}
+ void setSpecularColor(const float* newColor){memcpy(m_specularColor,newColor,sizeof(float)*3);}
+ void setShininess(const float newShininess){m_shininess = newShininess;}
+ void setSpecularExponent(const float newExponent){m_specularExponent = newExponent;}
+ void setOpacity(const float newOpacity){m_opacity = newOpacity;}
+ void setTwoSided(const bool isTwoSided){m_twoSided = isTwoSided;}
+
+};
+
+ ///////////////////////////////////////////////////////
+ // 3DS Mesh
+ // Contains all vertex, face, texcoord & normal data
+///////////////////////////////////////////////////////
+
+class mesh3DS{
+private:
+ std::string m_name;
+ std::string m_materialName;
+ model3DS *m_parentModel;
+
+ std::vector<float> m_vertices;
+ std::vector<float> m_normals;
+ std::vector<ushort> m_faces;
+ std::vector<unsigned int> m_faceSmoothing;
+ std::vector<float> m_texcoords;
+ std::map<std::string, std::vector<ushort> > m_materialFaces;
+
+ //VBO IDs
+ GLuint m_verticesArrayId;
+ GLuint m_normalsArrayId;
+ GLuint m_texcoordsArrayId;
+
+ unsigned int m_drawMode;
+
+public:
+
+ mesh3DS(model3DS *parentModel):m_parentModel(parentModel){}
+
+ void buildMesh();
+ void calculateNormals();
+ void sortFacesByMaterial();
+ void draw();
+
+ void addVertex(const float vertex){m_vertices.push_back(vertex);}
+ void addFaceIndex(const ushort faceIndex){m_faces.push_back(faceIndex);}
+ void addMaterialFace(const std::string &name, const ushort faceIndex){m_materialFaces[name].push_back(faceIndex);}
+ void addFaceSmoothing(const unsigned int smoothing){m_faceSmoothing.push_back(smoothing);}
+ void addTexcoord(const float texcoord){m_texcoords.push_back(texcoord);}
+
+ std::string getMaterial()const{return m_materialName;}
+ std::string getName()const{return m_name;}
+ int getNumFaces()const{return (int)m_faces.size();}
+
+ void setMaterial(const std::string &newMat){m_materialName = newMat;}
+ void setName(const std::string newName){m_name = newName;}
+ void setDrawMode(const unsigned int newDrawMode){m_drawMode = newDrawMode;}
+};
+
+ //////////////////////////////////////////////////////////////////
+ // 3DS Model
+ // Contains all the meshes & materials that make up a 3DS model
+//////////////////////////////////////////////////////////////////
+
+typedef struct{
+ float minX,maxX;
+ float minY,maxY;
+ float minZ,maxZ;
+} boundingBox3DS;
+
+class model3DS : public ofx3DBaseLoader{
+private:
+ std::string m_filename;
+ std::string m_filepath;
+ std::vector<mesh3DS> m_meshes;
+ std::map<std::string, material3DS> m_materials;
+ unsigned int m_drawMode;
+ boundingBox3DS m_boundingBox;
+ float m_scale;
+ float m_centerX, m_centerY, m_centerZ;
+
+ // temporaries used while reading chunks
+ mesh3DS *m_currentMesh;
+ material3DS *m_currentMaterial;
+ std::vector<material3DS> *m_currentMaterialFaces;
+ float m_currentColor[3];
+ ushort m_tempUshort;
+ float m_tempFloat;
+ std::string m_tempString;
+
+ void readChunk(std::ifstream *modelFile, const int objectStart, const int objectLength);
+
+ // Private copy and assignment constructors
+ // to prevent object being copied
+ model3DS(const model3DS &model);
+ model3DS &operator=(const model3DS &model);
+
+public:
+ model3DS();
+ void loadModel(const char* filename, float scale = 1);
+ void loadModel(string filename, float scale);
+ void draw();
+
+ std::string getFilename(){return m_filename;}
+ const material3DS& getMaterial(const std::string &matName){return m_materials[matName];}
+};
+
+#endif //MODEL3DS_H
84 autosmiley/addons/ofx3DModelLoader/src/3DS/texture3DS.cpp
@@ -0,0 +1,84 @@
+/*
+ BMP texture loader
+ � Keith O'Conor 2005
+ keith.oconor @ {cs.tcd.ie, gmail.com}
+
+ adapted for openFrameworks by Theodore Watson - theo@openframeworks.cc
+*/
+
+#include "texture3DS.h"
+
+texture3DS::texture3DS(string filename, const int textureId){
+
+ ofImage img;
+ if( !img.loadImage(filename) ){
+ ofLog(OF_LOG_ERROR, "texture3DS ERROR: Could not open %s", filename.c_str());
+ }
+
+ m_width = img.width;
+ m_height = img.height;
+ m_bpp = img.bpp;
+
+ if( m_width <= 0 || m_height <= 0){
+ ofLog(OF_LOG_ERROR, "texture3DS ERROR: Something wrong with %s - dimensions less than 0", filename.c_str());
+
+ }
+
+ if(m_bpp != 32 && m_bpp != 24){
+ ofLog(OF_LOG_ERROR, "texture3DS ERROR: Invalid texture color depth %s must be uncompressed 24/32bpp png, jpg, bmp, tga", filename.c_str());
+ return;
+ }
+
+ // Determine format
+ int fileFormat, internalFormat;
+ switch(m_bpp){
+ case 24:fileFormat = GL_RGB; internalFormat = GL_RGB; break;
+ case 32:fileFormat = GL_RGBA; internalFormat = GL_RGBA; break;
+ default:
+ ofLog(OF_LOG_ERROR, "texture3DS ERROR: Invalid texture color depth %s must be uncompressed 24/32bpp", filename.c_str());
+
+ return;
+ break;
+ }
+
+
+ // FLIP THE PIXELS
+ //we need to flip the image vertically
+ unsigned char * ptrToPixels = img.getPixels();
+ int bytesPP = (m_bpp / 8);
+
+ //this extra allocation is because of a glu bug - http://osdir.com/ml/video.mesa3d.devel/2005-02/msg00035.html
+ unsigned char * flippedPixels = new unsigned char[bytesPP * m_width * (m_height+1)];
+ unsigned char * ptrToFlipped = flippedPixels;
+
+ int numBytesPerLine = m_width * bytesPP;
+
+ //go to the beginning of the bottom line of pixels of the image
+ ptrToPixels += ( numBytesPerLine * (m_height-1) );
+
+ //then step through line at a time copying the whole line from the original
+ //to the flipped pixels
+ //- the original starts at the bottom and goes up
+ //- the flipped starts at the top and goes down
+ for(int y = 0; y < m_height; y++){
+ memcpy(ptrToFlipped, ptrToPixels, numBytesPerLine * sizeof( char ) );
+ ptrToPixels -= numBytesPerLine;
+ ptrToFlipped += numBytesPerLine;
+ }
+
+
+ // Bind texture ID to load
+ glBindTexture(GL_TEXTURE_2D, textureId);
+
+ // Set texture parameters
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+ glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+
+ // Upload texture to card with bound texture ID
+ gluBuild2DMipmaps(GL_TEXTURE_2D, internalFormat, m_width, m_height, fileFormat, GL_UNSIGNED_BYTE, flippedPixels);
+
+ ofLog(OF_LOG_NOTICE, "texture3DS Texture %s loaded", filename.c_str());
+}
21 autosmiley/addons/ofx3DModelLoader/src/3DS/texture3DS.h
@@ -0,0 +1,21 @@
+#pragma once
+
+#include "ofMain.h"
+
+
+class texture3DS{
+private:
+ int m_width;
+ int m_height;
+ int m_bpp;
+ int m_format;
+
+public:
+ texture3DS(std::string filename, const int textureId);
+
+ int getWidth(){return m_width;}
+ int getHeight(){return m_width;}
+ int getBpp(){return m_bpp;}
+
+};
+
16 autosmiley/addons/ofx3DModelLoader/src/ofx3DBaseLoader.h
@@ -0,0 +1,16 @@
+#pragma once
+
+class ofx3DBaseLoader{
+
+ public:
+ ofx3DBaseLoader(){
+ hasTexture = false;
+ }
+ virtual void loadModel(string model, float scale = 1.0){}
+ virtual void loadModel(string model){}
+ virtual ~ofx3DBaseLoader(){}
+ virtual void draw(){ printf("hello\n"); }
+
+ bool hasTexture;
+
+};
26 autosmiley/addons/ofx3DModelLoader/src/ofx3DModelLoader.h
@@ -0,0 +1,26 @@
+#pragma once
+
+#include "ofMain.h"
+#include "model3DS.h"
+
+class ofx3DModelLoader{
+
+ public:
+ ofx3DModelLoader();
+ ~ofx3DModelLoader();
+
+ void loadModel(string modelName, float scale = 1.0);
+ void setPosition(float x, float y, float z);
+ void setScale(float x, float y, float z);
+ void setRotation(int which, float angle, float rot_x, float rot_y, float r_z);
+ void draw();
+
+ vector <float> rotAngle;
+ vector <ofPoint> rotAxis;
+ ofPoint scale;
+ ofPoint pos;
+
+ int numRotations;
+ ofx3DBaseLoader * model;
+
+};
89 autosmiley/addons/ofx3DModelLoader/src/ofx3dModelLoader.cpp
@@ -0,0 +1,89 @@
+#include "ofx3DModelLoader.h"
+
+//-------------------------------------------
+static string getExtension( string filename ){
+ std::string::size_type idx;
+ idx = filename.rfind('.');
+
+ if(idx != std::string::npos){
+ return filename.substr(idx+1);
+ }
+ else{
+ return "";
+ }
+}
+
+//-------------------------------------------
+ofx3DModelLoader::ofx3DModelLoader(){
+ numRotations = 0;
+ rotAngle.clear();
+ rotAxis.clear();
+ scale = ofPoint(1, 1, 1);
+
+ model = NULL;
+}
+
+//------------------------------------------
+void ofx3DModelLoader::loadModel(string modelName, float scale){
+ if( model != NULL)delete model;
+
+ string ext = getExtension( modelName );
+ if( ext == "3ds" || ext == "3DS" ){
+ model = new model3DS();
+ model->loadModel( modelName.c_str(), scale);
+ }
+
+}
+
+//-------------------------------------------
+ofx3DModelLoader::~ofx3DModelLoader(){
+ if( model != NULL )delete model;
+}
+
+//-------------------------------------------
+void ofx3DModelLoader::setPosition(float x, float y, float z){
+ pos.x = x;
+ pos.y = y;
+ pos.z = z;
+}
+
+void ofx3DModelLoader::setScale(float x, float y, float z){
+ scale.x = x;
+ scale.y = y;
+ scale.z = z;
+}
+
+//-------------------------------------------
+void ofx3DModelLoader::setRotation(int which, float angle, float rot_x, float rot_y, float rot_z){
+
+ if(which + 1 > numRotations){
+ int diff = 1 + (which - numRotations);
+ for(int i = 0; i < diff; i++){
+ rotAngle.push_back(0);
+ rotAxis.push_back(ofPoint());
+ }
+ numRotations = rotAngle.size();
+ }
+
+ rotAngle[which] = angle;
+ rotAxis[which].x = rot_x;
+ rotAxis[which].y = rot_y;
+ rotAxis[which].z = rot_z;
+}
+
+//-------------------------------------------
+void ofx3DModelLoader::draw(){
+
+ glPushMatrix();
+ glTranslatef(pos.x, pos.y, pos.z);
+ glScalef(scale.x, scale.y, scale.z);
+ for(int i = 0; i < numRotations; i++){
+ glRotatef(rotAngle[i], rotAxis[i].x, rotAxis[i].y, rotAxis[i].z);
+ }
+
+ glShadeModel(GL_SMOOTH);
+ if( model->hasTexture)glEnable(GL_TEXTURE_2D);
+ model->draw();
+ if( model->hasTexture)glDisable(GL_TEXTURE_2D);
+ glPopMatrix();
+}
46 autosmiley/addons/ofxDirList/doc/Instructions.txt
@@ -0,0 +1,46 @@
+ofDirList - July 18th 2007 - by Theodore Watson
+
+Installation:
+1 Copy ofDirList.h and ofDirList.cpp to your ofAddons folder.
+2 Add #include "ofDirList.h" to ofAddons.h
+3 Add ofDirList.h and ofDirList.cpp to IDE
+
+Changes:
+1 Now takes string type as argument
+2 Fixed wildcard * for allowing all extensions
+3 Extensions are now case insensitive
+4 reset() function to clear extension list
+
+Example Usage:
+
+////////////////////////////////////////////////
+ofDirList DIR;
+
+//Allow only certain files
+//by specifying extension
+//Extensions are not case sensitive
+//so 'mov' allows both 'mov' and 'MOV'
+//To include all files either don't
+//call allowExt or specify the wildcard
+// '*'
+
+DIR.allowExt("mov");
+DIR.allowExt("jpg");
+
+//if you wish to keep the console quiet
+DIR.setVerbose(false);
+
+//now lets list the directory
+//listDir returns number of files found
+//with the specified extension
+int numFiles = DIR.listDir("images/");
+
+//you can now iterate through the files as you like
+for(int i = 0; i < numFiles; i++){
+ printf("name is %s - path is %s \n", DIR.getName(i), DIR.getPath(i) );
+}
+
+//if you want to reuse ofDirList for different files
+//call reset(); to clear the allowed extensions
+
+
26 autosmiley/addons/ofxDirList/install.xml
@@ -0,0 +1,26 @@
+<install>
+ <name>ofxDirList</name>
+ <version>0.01</version>
+ <author>theo watson</author>
+ <url></url>
+
+ <add>
+
+ <!-- ===================================================================== -->
+ <!-- ========================== add per project using this addon ========= -->
+ <!-- ===================================================================== -->
+
+ <src>
+ <folder name="addons/ofxDirList/src">
+ <file>../../../addons/ofxDirList/src/ofxDirList.h</file>
+ <file>../../../addons/ofxDirList/src/ofxDirList.cpp</file>
+ </folder>
+ </src>
+
+ <include>
+ <path>../../../addons/ofxDirList/src</path>
+ </include>
+
+ </add>
+
+</install>
145 autosmiley/addons/ofxDirList/src/ofxDirList.cpp
@@ -0,0 +1,145 @@
+#include "ofxDirList.h"
+#include <algorithm>
+#include <string>
+
+
+// Handy string functions
+
+static std::string::size_type idx;
+static string getExt(string filename){
+ idx = filename.rfind('.');
+
+ if(idx != std::string::npos){
+ return filename.substr(idx+1);
+ }
+ else{
+ return "";
+ }
+}
+
+static string strToLower(string myStr){
+ transform( myStr.begin(), myStr.end(), myStr.begin(), ::tolower );
+ return myStr;
+}
+
+//----------------------------------------------------------
+ofxDirList::ofxDirList(){
+ reset();
+}
+
+//----------------------------------------------------------
+void ofxDirList::reset(){
+ allowedFileExt.clear();
+ nameArray.clear();
+ pathArray.clear();
+}
+
+//----------------------------------------------------------
+void ofxDirList::setVerbose(bool _verbose){
+ ofLog(OF_LOG_WARNING, "ofxDirList setVerbose is depreciated use ofSetLogLevel instead");
+}
+
+//----------------------------------------------------------
+bool ofxDirList::allowExt(string ext){
+ allowedFileExt.push_back( strToLower(ext) );
+ return true;
+}
+
+//----------------------------------------------------------
+string ofxDirList::getName(int pos){
+ if(pos >= (int)nameArray.size()) return 0;
+ return nameArray[pos];
+}
+
+//----------------------------------------------------------
+string ofxDirList::getPath(int pos){
+ if(pos >= (int)pathArray.size()) return 0;
+ return pathArray[pos];
+}
+
+//----------------------------------------------------------
+int ofxDirList::listDir(string directory){
+ directory = ofToDataPath(directory);
+
+ nameArray.clear();
+ pathArray.clear();
+
+ if(directory.length() <= 0)return 0;
+
+ //if the trailing slash was not added - then add it
+ if( directory[directory.length()-1] != '/'){
+ directory = directory + "/";
+ }
+
+ DIR *dir = NULL;
+ struct dirent *entry;
+
+ //open the directory
+ ofLog(OF_LOG_VERBOSE, "ofxDirList - attempting to open %s", directory.c_str());
+ dir = opendir(directory.c_str());
+
+ if(dir == NULL){
+ ofLog(OF_LOG_ERROR, "ofxDirList - error opening directory");
+ return 0;
+ }else{
+ ofLog(OF_LOG_VERBOSE, "ofxDirList - success opening directory");
+ }
+
+ string entry_name = "";
+ string ext = "";
+ bool skip = false;
+
+ while ((entry = readdir(dir)) != NULL){
+
+ //turn it into a C++ string
+ entry_name = entry->d_name;
+
+ //lets get the length of the string here as we query it again
+ int fileLen = entry_name.length();
+
+ if(fileLen <= 0)continue; //if the name is not existant
+ if(entry_name[0] == '.')continue; //ignore invisible files, ./ and ../
+
+ //by default we don't skip files unless we are checking extensions
+ skip = false;
+
+ if(allowedFileExt.size() > 0){
+ //we will skip this files unless it has an allowed extension
+ skip = true;
+ for(int i = 0; i < (int)allowedFileExt.size(); i++){
+
+ //if the wildecard * has been entered for an ext type then don't check any extensions
+ if( allowedFileExt[i] == "*"){ skip = false; break; }
+
+
+ int extLen = allowedFileExt[i].length();
+
+ //the extension has to be shorter than the filename - simple check
+ if(extLen >= fileLen) continue;
+
+ //lets get the ext as lowercase
+ ext = strToLower( getExt(entry_name) );
+
+ //if no ext - then skip this ext check
+ if( ext == "" )continue;
+
+ //if we find a match then stop checking and approve this file
+ if(ext == allowedFileExt[i]){
+ skip = false;
+ break;
+ }
+ }
+ }
+
+ if(skip) continue;
+
+ //finally we store the result
+ pathArray.push_back(directory + entry_name);
+ nameArray.push_back(entry_name);
+
+ ofLog(OF_LOG_VERBOSE, "ofxDirList - listing %s ", nameArray.back().c_str());
+ }
+
+ ofLog(OF_LOG_VERBOSE, "ofxDirList - listed %i files in %s", nameArray.size(), directory.c_str());
+ return nameArray.size();
+}
294 autosmiley/addons/ofxDirList/src/ofxDirList.h
@@ -0,0 +1,294 @@
+#ifndef OFX_DIRLIST
+#define OFX_DIRLIST
+
+/************************************************************
+
+OpenFrameworks Library
+
+File: ofxDirList.h
+Description: List the contents of a directory
+Notes: Now takes string arguements and starts at the data/ folder level
+ Now all C++ strings! No more arrays.
+
+Last Modified: 2009.03.01
+Creator: Theodore Watson
+
+************************************************************/
+
+
+#include "ofMain.h"
+
+#ifdef TARGET_WIN32
+ #include <stdio.h>
+ #include <iostream>
+ #include <string.h>
+#else
+ #include <dirent.h>
+#endif
+
+
+class ofxDirList{
+
+ public:
+ ofxDirList();
+ void setVerbose(bool _verbose);
+ string getName(int pos);
+ string getPath(int pos);
+ void reset(); // resets extension list
+ bool allowExt(string ext); // returns true if ext is accepted
+ int listDir(string directory); // returns number of files found
+
+ private:
+ vector <string> allowedFileExt;
+ vector <string> nameArray;
+ vector <string> pathArray;
+
+};
+
+
+
+// this is WIN32 dirent included in here for ease of usage
+// since windows doesn't have dirent by default. this is just a wrapper....
+// because we haven't written win32 version of the same code
+
+//---------------------------------------------------------------------------------------
+//---------------------------------------------------------------------------------------
+//---------------------------------------------------------------------------------------
+
+/*
+ * dirent.h - dirent API for Microsoft Visual Studio
+ *
+ * Copyright (C) 2006 Toni Ronkko
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * ``Software''), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifdef TARGET_WIN32
+
+ #include <windows.h>
+ #include <tchar.h>
+ #include <string.h>
+ #include <assert.h>
+
+
+ typedef struct dirent {
+ /* name of current directory entry (a multi-byte character string) */
+ char d_name[MAX_PATH + 1];
+
+ /* file attributes */
+ WIN32_FIND_DATA data;
+ } dirent;
+
+
+ typedef struct DIR {
+ /* current directory entry */
+ dirent current;
+
+ /* is there an un-processed entry in current? */
+ int cached;
+
+ /* file search handle */
+ HANDLE search_handle;
+
+ /* search pattern (3 = zero terminator + pattern "\\*") */
+ TCHAR patt[MAX_PATH + 3];
+ } DIR;
+
+
+ static DIR *opendir (const char *dirname);
+ static struct dirent *readdir (DIR *dirp);
+ static int closedir (DIR *dirp);
+
+
+ /* use the new safe string functions introduced in Visual Studio 2005 */
+ #if defined(_MSC_VER) && _MSC_VER >= 1400
+ # define STRNCPY(dest,src,size) strncpy_s((dest),(size),(src),_TRUNCATE)
+ #else
+ # define STRNCPY(dest,src,size) strncpy((dest),(src),(size))
+ #endif
+
+
+ /*
+ * Open directory stream DIRNAME for read and return a pointer to the
+ * internal working area that is used to retrieve individual directory
+ * entries.
+ */
+ static DIR*
+ opendir(
+ const char *dirname)
+ {
+ DIR *dirp;
+ assert (dirname != NULL);
+ assert (strlen (dirname) < MAX_PATH);
+
+ /* construct new DIR structure */
+ dirp = (DIR*) malloc (sizeof (struct DIR));
+ if (dirp != NULL) {
+ TCHAR *p;
+
+ /* prepare search pattern */
+ #ifdef _UNICODE
+
+ /* convert directory name to wide character string */
+ MultiByteToWideChar(
+ CP_ACP, /* code page */
+ 0, /* conversion flags */
+ dirname, /* mb-string to convert */
+ -1, /* length of mb-string */
+ dirp->patt, /* wc-string to produce */
+ MAX_PATH); /* max length of wc-string */
+ dirp->patt[MAX_PATH] = '\0';
+
+ /* append search pattern to directory name */
+ p = wcschr (dirp->patt, '\0');
+ if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
+ *p++ = '\\';
+ }
+ *p++ = '*';
+ *p = '\0';
+
+ #else /* !_UNICODE */
+
+ /* take directory name... */
+ STRNCPY (dirp->patt, dirname, sizeof(dirp->patt));
+ dirp->patt[MAX_PATH] = '\0';
+
+ /* ... and append search pattern to it */
+ p = strchr (dirp->patt, '\0');
+ if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
+ *p++ = '\\';
+ }
+ *p++ = '*';
+ *p = '\0';
+
+ #endif /* !_UNICODE */
+
+ /* open stream and retrieve first file */
+ dirp->search_handle = FindFirstFile (dirp->patt, &dirp->current.data);
+ if (dirp->search_handle == INVALID_HANDLE_VALUE) {
+ /* invalid search pattern? */
+ free (dirp);
+ return NULL;
+ }
+
+ /* there is an un-processed directory entry in memory now */
+ dirp->cached = 1;
+
+ }
+ return dirp;
+ }
+
+
+ /*
+ * Read a directory entry, and return a pointer to a dirent structure
+ * containing the name of the entry in d_name field. Individual directory
+ * entries returned by this very function include regular files,
+ * sub-directories, pseudo-directories "." and "..", but also volume labels,
+ * hidden files and system files may be returned.
+ */
+ static struct dirent *
+ readdir(
+ DIR *dirp)
+ {
+ assert (dirp != NULL);
+
+ if (dirp->search_handle == INVALID_HANDLE_VALUE) {
+ /* directory stream was opened/rewound incorrectly or it ended normally */
+ return NULL;
+ }
+
+ /* get next directory entry */
+ if (dirp->cached != 0) {
+ /* a valid directory entry already in memory */
+ dirp->cached = 0;
+ } else {
+ /* read next directory entry from disk */
+ if (FindNextFile (dirp->search_handle, &dirp->current.data) == FALSE) {
+ /* the very last file has been processed or an error occured */
+ FindClose (dirp->search_handle);
+ dirp->search_handle = INVALID_HANDLE_VALUE;
+ return NULL;
+ }
+ }
+
+ /* copy directory entry to d_name */
+ #ifdef _UNICODE
+
+ /* convert entry name to multibyte */
+ WideCharToMultiByte(
+ CP_ACP, /* code page */
+ 0, /* conversion flags */
+ dirp->current.data.cFileName, /* wc-string to convert */
+ -1, /* length of wc-string */
+ dirp->current.d_name, /* mb-string to produce */
+ MAX_PATH, /* max length of mb-string */
+ NULL, /* use sys default character */
+ NULL); /* don't care */
+ dirp->current.d_name[MAX_PATH] = '\0';
+
+ #else /* !_UNICODE */
+
+ /* copy as a multibyte character string */
+ STRNCPY (dirp->current.d_name, dirp->current.data.cFileName, sizeof(dirp->current.d_name));
+ dirp->current.d_name[MAX_PATH] = '\0';
+
+ #endif /* !_UNICODE */
+
+ return &dirp->current;
+ }
+
+
+ /*
+ * Close directory stream opened by opendir() function. Close of the
+ * directory stream invalidates the DIR structure as well as any previously
+ * read directory entry.
+ */
+ static int
+ closedir(
+ DIR *dirp)
+ {
+ assert (dirp != NULL);
+
+ /* release search handle */
+ if (dirp->search_handle != INVALID_HANDLE_VALUE) {
+ FindClose (dirp->search_handle);
+ dirp->search_handle = INVALID_HANDLE_VALUE;
+ }
+
+ /* release directory handle */
+ free (dirp);
+ return 0;
+ }
+
+
+#endif
+
+//---------------------------------------------------------------------------------------
+//---------------------------------------------------------------------------------------
+//---------------------------------------------------------------------------------------
+
+
+
+
+
+
+
+
+#endif
128 autosmiley/addons/ofxNetwork/docs/Instructions.txt
@@ -0,0 +1,128 @@
+ofTCPServer & ofTCPClient install instructions
+
+NOTE: ofTCPServer and ofTCPClient require ofThread - please checkout ofThread first and follow the instructions to install ofThread before installing ofTCPClient and ofTCPServer.
+NOTE: Windows users will need to add ws2_32.lib to your project. This is part of the Windows SDK.
+ For Codewarrior it is here: C:/Program Files/Metrowerks/CodeWarrior/Win32-x86 Support/Libraries/Win32SDK/
+ For Visual Studio it is here C:/Program Files/Microsoft Platform SDK for Windows Server 2003 R2/Lib
+ For Dev-C++ it is here C:/Dev-Cpp/lib -- for Dev-C++ it is called libws2_32.a
+
+ It is strongly encourage to copy the lib to your openFrameworks libs/ folder and then add to your project.
+
++++++ Step 0 ++++++
+Copy ofTCPServer.h and .cpp and ofTCPClient.h and .cpp to libs/ofAddons/communication/
+
++++++ Step 1 ++++++
+Duplicate a project twice and rename one tcpServerExample and tcpClientExample replace testApp.h testApp.cpp and main.cpp with the replacement for each example.
+
++++++ Step 2 ++++++
+
+Drag the ofTCPServer and ofTCPClient files from libs/ofAddons/communication/
+into your project. As well as ofAddons.h from libs/ofAddons/
+
+in ofAddons.h you should have:
+
+#ifndef _OF_ADDONS_H_
+#define _OF_ADDONS_H_
+
+//--------------------------
+// utils
+#include "ofThread.h"
+
+//--------------------------
+// communication
+#include "ofTCPServer.h"
+#include "ofTCPClient.h"
+
+//--------------------------
+// graphics
+
+
+//--------------------------
+// app
+
+
+//--------------------------
+// audio
+
+
+//--------------------------
+// video
+
+#endif
+
+
++++++ Step 3 ++++++
+Add the folder tcpUdpManager to your main libs folder.
+Then drag folder into both projects.
+
+
++++++ Step 4 ++++++
+in of Constants.h change the line
+
+// then the the platform specific includes:
+#ifdef TARGET_WIN32
+
+To:
+
+// then the the platform specific includes:
+#ifdef TARGET_WIN32
+ //this is for TryEnterCriticalSection
+ //http://www.zeroc.com/forums/help-center/351-ice-1-2-tryentercriticalsection-problem.html
+ #ifndef _WIN32_WINNT
+ # define _WIN32_WINNT 0x400
+ #endif
+
+
+
++++++ Step 5 ++++++
+To run the demo app make sure in ofConstants.h you have
+
+using namespace std;
+#include <string>
+#include <sstream> //for ostringsream
+#include <iomanip> //for setprecision
+
+
++++++ Step 6 ++++++
+Also to run the demo make sure you have in ofUtils.cpp
+
+//--------------------------------------------------
+string ofToString(double value, int precision){
+ stringstream sstr;
+ sstr << fixed << setprecision(precision) << value;
+ return sstr.str();
+}
+
+//--------------------------------------------------
+string ofToString(int value){
+ stringstream sstr;
+ sstr << value;
+ return sstr.str();
+}
+
+And also in in ofUtils.h:
+
+string ofToString(double value, int precision = 7);
+string ofToString(int value);
+
+
++++++ Step 7 ++++++
+Add ofSleepMillis()
+
+add the line bellow to ofAppRunner.h:
+void ofSleepMillis(int millis);
+
+
+and add the code bellow to ofAppRunner.cpp
+//--------------------------------------
+void ofSleepMillis(int millis){
+ #ifdef TARGET_WIN32
+ Sleep(millis); //windows sleep in milliseconds
+ #else
+ usleep(millis * 1000); //mac sleep in microseconds - cooler :)
+ #endif
+}
+
++++++ Step 8 ++++++
+Make sure you add the fonts needed and specify the corrent path to their location.
+
38 autosmiley/addons/ofxNetwork/install.xml
@@ -0,0 +1,38 @@
+<install>
+ <name>ofxNetwork</name>
+ <version>0.01</version>
+ <author>christian nagelhofer, theo watson</author>
+ <url></url>
+ <requires>ofxThread</requires>
+
+ <add>
+
+ <!-- ===================================================================== -->
+ <!-- ========================== add per project using this addon ========= -->
+ <!-- ===================================================================== -->
+
+ <src>
+ <folder name="addons/ofxNetwork/src">
+ <file>../../../addons/ofxNetwork/src/ofxNetwork.h</file>
+ <file>../../../addons/ofxNetwork/src/ofxTCPClient.cpp</file>
+ <file>../../../addons/ofxNetwork/src/ofxTCPClient.h</file>
+ <file>../../../addons/ofxNetwork/src/ofxTCPManager.cpp</file>
+ <file>../../../addons/ofxNetwork/src/ofxTCPManager.h</file>
+ <file>../../../addons/ofxNetwork/src/ofxTCPServer.cpp</file>
+ <file>../../../addons/ofxNetwork/src/ofxTCPServer.h</file>
+ <file>../../../addons/ofxNetwork/src/ofxUDPManager.cpp</file>
+ <file>../../../addons/ofxNetwork/src/ofxUDPManager.h</file>
+ </folder>
+ </src>
+
+ <include>
+ <path>../../../addons/ofxNetwork/src</path>
+ </include>
+
+ <link>
+ <lib os="win32" compiler="devcpp">wsock32</lib>
+ </link>
+
+ </add>
+
+</install>
13 autosmiley/addons/ofxNetwork/src/ofxNetwork.h
@@ -0,0 +1,13 @@
+#ifndef _OFX_NETWORK_
+#define _OFX_NETWORK_
+
+#include "ofxThread.h"
+#include "ofxTCPClient.h"
+#include "ofxTCPManager.h"
+#include "ofxTCPServer.h"
+#include "ofxUDPManager.h"
+
+#endif
+
+
+
232 autosmiley/addons/ofxNetwork/src/ofxTCPClient.cpp
@@ -0,0 +1,232 @@
+#include "ofxTCPClient.h"
+#include "ofAppRunner.h"
+
+
+//--------------------------
+ofxTCPClient::ofxTCPClient(){
+
+ verbose = false;
+ connected = false;
+ messageSize = 0;
+ port = 0;
+ index = -1;
+ str = "";
+ tmpStr = "";
+ ipAddr ="000.000.000.000";
+
+ partialPrevMsg = "";
+
+ memset(tmpBuff, 0, TCP_MAX_MSG_SIZE+1);
+}
+
+//--------------------------
+ofxTCPClient::~ofxTCPClient(){
+ close();
+}
+
+//--------------------------
+void ofxTCPClient::setVerbose(bool _verbose){
+ verbose = _verbose;
+}
+
+//--------------------------
+bool ofxTCPClient::setup(string ip, int _port, bool blocking){
+
+ if( !TCPClient.Create() ){
+ if(verbose)printf("ofxTCPClient: Create() failed\n");
+ return false;
+ }else if( !TCPClient.Connect((char *)ip.c_str(), _port) ){
+ if(verbose)printf("ofxTCPClient: Connect(%s, %i) failed\n", ip.c_str(), _port);
+ TCPClient.Close(); //we free the connection
+ return false;
+ }
+
+ TCPClient.SetNonBlocking(!blocking);
+
+ port = _port;
+ ipAddr = ip;
+ connected = true;
+ return true;
+}
+
+//don't use this one
+//for server to use internally only!
+//--------------------------
+bool ofxTCPClient::setup(int _index, bool blocking){
+ index = _index;
+
+ //this fetches the port that the server
+ //sets up the connection on
+ //different to the server's listening port
+ InetAddr addr;
+ if( TCPClient.GetRemoteAddr(&addr) ){
+ port = addr.GetPort();
+ ipAddr = addr.DottedDecimal();
+ }
+
+ TCPClient.SetNonBlocking(!blocking);
+ connected = true;
+ return true;
+}
+
+
+//--------------------------
+bool ofxTCPClient::close(){
+ if( connected ){
+
+ if( !TCPClient.Close() ){
+ if(verbose)printf("ofxTCPClient: Close() failed\n");
+ return false;
+ }else{
+ connected = false;
+ return true;
+ }
+ }else{
+ return true;
+ }
+}
+
+//--------------------------
+bool ofxTCPClient::send(string message){
+ // tcp is a stream oriented protocol
+ // so there's no way to be sure were
+ // a message ends without using a terminator
+ // note that you will receive a trailing [/TCP]\0
+ // if sending from here and receiving from receiveRaw or
+ // other applications
+ message = partialPrevMsg + message + STR_END_MSG;
+ message += (char)0; //for flash
+ int ret = TCPClient.SendAll( message.c_str(), message.length() );
+ if( ret == 0 ){
+ if(verbose)printf("ofxTCPClient: other side disconnected\n");
+ close();
+ return false;
+ }else if(ret<0){
+ if(verbose)printf("ofxTCPClient: sendAll() failed\n");
+ return false;
+ }else if(ret<(int)message.length()){
+ // in case of partial send, store the
+ // part that hasn't been sent and send
+ // with the next message to not corrupt
+ // next messages
+ partialPrevMsg=message.substr(ret);
+ return true;
+ }else{
+ partialPrevMsg = "";
+ return true;
+ }
+}
+
+//--------------------------
+bool ofxTCPClient::sendRaw(string message){
+ if( message.length() == 0) return false;
+
+ if( !TCPClient.SendAll(message.c_str(), message.length()) ){
+ if(verbose)printf("ofxTCPClient: sendRawBytes() failed\n");
+ close();
+ return false;
+ }else{
+ return true;
+ }
+}
+
+//--------------------------
+bool ofxTCPClient::sendRawBytes(const char* rawBytes, const int numBytes){
+ if( numBytes <= 0) return false;
+
+ if( !TCPClient.SendAll(rawBytes, numBytes) ){
+ if(verbose)printf("ofxTCPClient: sendRawBytes() failed\n");
+ close();
+ return false;
+ }else{
+ return true;
+ }
+}
+
+
+//this only works after you have called receive
+//--------------------------
+int ofxTCPClient::getNumReceivedBytes(){
+ return messageSize;
+}
+
+//--------------------------
+static void removeZeros(char * buffer, int size){
+ for(int i=0;i<size-1;i++){
+ if(buffer[i]==(char)0){
+ for(int j=i;j<size-1;j++){
+ buffer[j]=buffer[j+1];
+ }
+ buffer[size-1]=(char)0;
+ }
+ }
+}
+
+//--------------------------
+string ofxTCPClient::receive(){
+
+ str = "";
+ int length=-2;
+ //only get data from the buffer if we don't have already some complete message
+ if(tmpStr.find(STR_END_MSG)==string::npos){
+ memset(tmpBuff, 0, TCP_MAX_MSG_SIZE+1); //one more so there's always a \0 at the end for string concat
+ length = TCPClient.Receive(tmpBuff, TCP_MAX_MSG_SIZE);
+ if(length>0){ // don't copy the data if there was an error or disconnection
+ removeZeros(tmpBuff,length);
+ tmpStr += tmpBuff;
+ }
+ }
+
+ // check for connection reset or disconnection
+ if((length==-1 && errno==TCP_CONNRESET ) || length == 0){
+ close();
+ if(tmpStr.length()==0) // return if there's no more data left in the buffer
+ return "";
+ }
+
+ // process any available data
+ if(tmpStr.find(STR_END_MSG)!=string::npos){
+ str=tmpStr.substr(0,tmpStr.find(STR_END_MSG));
+ tmpStr=tmpStr.substr(tmpStr.find(STR_END_MSG)+STR_END_MSG_LEN);
+ }
+ return str;
+}
+
+//--------------------------
+int ofxTCPClient::receiveRawBytes(char * receiveBuffer, int numBytes){
+ messageSize = TCPClient.Receive(receiveBuffer, numBytes);
+ if(messageSize==0){
+ close();
+ }
+ return messageSize;
+}
+
+//--------------------------
+string ofxTCPClient::receiveRaw(){
+ messageSize = TCPClient.Receive(tmpBuff, TCP_MAX_MSG_SIZE);
+ if(messageSize==0){
+ close();
+ }
+ return tmpBuff;
+}
+
+//--------------------------
+bool ofxTCPClient::isConnected(){
+ return connected;
+}
+
+//--------------------------
+int ofxTCPClient::getPort(){
+ return port;
+}
+
+//--------------------------
+string ofxTCPClient::getIP(){
+ return ipAddr;
+}
+
+
+
+
+
+
94 autosmiley/addons/ofxNetwork/src/ofxTCPClient.h
@@ -0,0 +1,94 @@
+#ifndef _OFX_TCP_CLIENT_
+#define _OFX_TCP_CLIENT_
+
+#include "ofConstants.h"
+#include "ofxTCPManager.h"
+
+#define TCP_MAX_MSG_SIZE 512
+#define STR_END_MSG "[/TCP]"
+#define STR_END_MSG_LEN 6
+
+#ifndef TARGET_WIN32
+ #define TCP_CONNRESET ECONNRESET
+#else
+ #define TCP_CONNRESET WSAECONNRESET
+#endif
+
+class ofxTCPClient{
+
+
+ public:
+
+ ofxTCPClient();
+ ~ofxTCPClient();
+
+ void threadedFunction();
+
+ void setVerbose(bool _verbose);
+ bool setup(string ip, int _port, bool blocking = false);
+ bool close();
+
+ //send data as a string - a short message
+ //is added to the end of the string which is
+ //used to indicate the end of the message to
+ //the receiver see: STR_END_MSG (ofxTCPClient.h)
+ bool send(string message);
+
+ //send data as a string without the end message
+ bool sendRaw(string message);
+
+ //the received message length in bytes
+ int getNumReceivedBytes();
+
+ //send and receive raw bytes lets you send and receive
+ //byte (char) arrays without modifiying or appending the data.
+ //Strings terminate on null bytes so this is the better option
+ //if you are trying to send something other than just ascii strings
+ bool sendRawBytes(const char * rawBytes, const int numBytes);
+
+
+ //get the message as a string
+ //this will only work with messages coming via
+ //send() and sendToAll()
+ //or from messages terminating with the STR_END_MSG
+ //which by default is [/TCP]
+ //eg: if you want to send "Hello World" from other
+ //software and want to receive it as a string
+ //sender should send "Hello World[/TCP]"
+ string receive();
+
+ //no terminating string you will need to be sure
+ //you are receiving all the data by using a loop
+ string receiveRaw();
+
+ //pass in buffer to be filled - make sure the buffer
+ //is at least as big as numBytes
+ int receiveRawBytes(char * receiveBytes, int numBytes);
+
+
+ bool isConnected();
+ int getPort();
+ string getIP();
+
+ //don't use this one
+ //for server to use internally only!
+ //--------------------------
+ bool setup(int _index, bool blocking);
+
+
+ ofxTCPManager TCPClient;
+
+protected:
+ char tmpBuff[TCP_MAX_MSG_SIZE+1];
+ string str, tmpStr, ipAddr;
+ int index, messageSize, port;
+ bool connected, verbose;
+ string partialPrevMsg;
+
+
+};
+
+#endif
+
+
+
446 autosmiley/addons/ofxNetwork/src/ofxTCPManager.cpp
@@ -0,0 +1,446 @@
+#include "ofxTCPManager.h"
+#include <stdio.h>
+
+//--------------------------------------------------------------------------------
+bool ofxTCPManager::m_bWinsockInit= false;
+
+//--------------------------------------------------------------------------------
+ofxTCPManager::ofxTCPManager()
+{
+ // was winsock initialized?
+ #ifdef TARGET_WIN32
+ if (!m_bWinsockInit) {
+ unsigned short vr;
+ WSADATA wsaData;
+ vr = MAKEWORD(2, 2);
+ WSAStartup(vr, &wsaData);
+ m_bWinsockInit= true;
+ }
+ #else
+ //this disables the other apps from shutting down if the client
+ //or server disconnects.
+ signal(SIGPIPE,SIG_IGN);
+ signal(EPIPE,SIG_IGN);
+ #endif
+
+ nonBlocking = false;
+ m_hSocket= INVALID_SOCKET;
+ m_dwTimeoutSend= OF_TCP_DEFAULT_TIMEOUT;
+ m_dwTimeoutReceive= OF_TCP_DEFAULT_TIMEOUT;
+ m_dwTimeoutAccept= OF_TCP_DEFAULT_TIMEOUT;
+ m_iListenPort= -1;
+};
+
+//--------------------------------------------------------------------------------
+/// Closes an open socket.
+/// NOTE: A closed socket cannot be reused again without a call to "Create()".
+bool ofxTCPManager::Close()
+{
+ if (m_hSocket == INVALID_SOCKET) return(false);
+
+ #ifdef TARGET_WIN32
+ if(closesocket(m_hSocket) == SOCKET_ERROR)
+ #else
+ if(close(m_hSocket) == SOCKET_ERROR)
+ #endif
+ {
+ return(false);
+ }
+
+ m_hSocket= INVALID_SOCKET;
+
+ #ifdef TARGET_WIN32
+ //This was commented out in the original
+ //WSACleanup();
+ #endif
+ return(true);
+}
+
+void ofxTCPManager::CleanUp() {
+ #ifd