54 void append(std::vector<T>& vec,
const T& data,
const int& alloc_step = 1024*10)
56 if (vec.size() == vec.capacity())
57 vec.reserve( vec.size() + alloc_step );
82 std::vector<String> tokens;
83 line.
split(
" \t",tokens,
true);
87 tokens.erase(tokens.begin());
90 for(
int i=0; i<(int)tokens.size(); ++i)
92 if (tokens[i] ==
"-blendu")
98 if (tokens[i] ==
"-blendv")
104 if (tokens[i] ==
"-cc")
106 mCC = tokens[i+1] ==
"on";
110 if (tokens[i] ==
"-clamp")
112 mClamp = tokens[i+1] ==
"on";
116 if (tokens[i] ==
"-mm")
118 mMM_Base = (float)tokens[i+1].toDouble();
119 mMM_Gain = (float)tokens[i+2].toDouble();
123 if (tokens[i] ==
"-o")
125 mO_UVW[0] = (float)tokens[i+1].toDouble();
126 mO_UVW[1] = (float)tokens[i+2].toDouble();
127 mO_UVW[2] = (float)tokens[i+3].toDouble();
131 if (tokens[i] ==
"-s")
133 mS_UVW[0] = (float)tokens[i+1].toDouble();
134 mS_UVW[1] = (float)tokens[i+2].toDouble();
135 mS_UVW[2] = (float)tokens[i+3].toDouble();
139 if (tokens[i] ==
"-t")
141 mT_UVW[0] = (float)tokens[i+1].toDouble();
142 mT_UVW[1] = (float)tokens[i+2].toDouble();
143 mT_UVW[2] = (float)tokens[i+3].toDouble();
147 if (tokens[i] ==
"-texres")
153 if (tokens[i] ==
"-imfchan")
155 mImfchan = (
unsigned char)tokens[i+1][0];
159 if (tokens[i] ==
"-bm")
161 mBM = (float)tokens[i+1].toDouble();
166 if ( i != (
int)tokens.size()-1 )
167 Log::error(
Say(
"Unknown map option '%s' in file '%s'.\n") << tokens[i] << file );
176 Say(
"-blendu %s -blendv %s -cc %s -clamp %s -mm %n %n -o %n %n %n -s %n %n %n -t %n %n %n -texres %n -imfchan '%c' -bm %n %s\n")
179 << (
mCC ?
"on" :
"off")
180 << (
mClamp ?
"on" :
"off")
203 if (line.
empty() || line[0] ==
'#')
224 materials.back().setKa(col);
233 materials.back().setKd(col);
242 materials.back().setKs(col);
251 materials.back().setKe(col);
271 materials.back().setIllum(line.
field(
' ', 1).
toInt());
298 Log::error(
Say(
"Unknown field '%s' in file %s'.\n") << line << file );
307 Log::error(
"loadOBJ() called with NULL argument.\n");
313 Log::error(
Say(
"loadOBJ(): could not open source file.\n") );
326 const int BUF_SIZE = 1024;
330 int f_format_type = 0;
331 std::string object_name;
332 bool starts_new_geom =
true;
334 bool smoothing_group =
false;
337 std::string stdstr_line;
338 while( stream->
readLine(stdstr_line) )
342 if (line.empty() || line[0] ==
'#')
346 while( line[line.length()-1] ==
'\\' && stream->
readLine(stdstr_line) )
350 line[line.length()-1] =
' ';
358 sscanf(line.c_str(),
"%s ", cmd);
363 if (strcmp(cmd,
"v") == 0)
365 float x=0,y=0,z=0,w=1.0f;
366 sscanf(line.c_str()+2,
"%f %f %f", &x, &y, &z);
367 append(mCoords,
fvec4(x,y,z,w));
374 if (strcmp(cmd,
"vt") == 0)
378 sscanf(line.c_str()+3,
"%f %f %f", &x, &y, &z);
379 append(mTexCoords,
fvec3(x,y,z));
385 if (strcmp(cmd,
"vn") == 0)
388 sscanf(line.c_str()+3,
"%f %f %f", &x, &y, &z);
389 append(mNormals,
fvec3(x,y,z));
401 if (strcmp(cmd,
"f") == 0)
408 mMeshes.push_back( cur_mesh );
409 starts_new_geom =
false;
415 while( line[i] ==
' ' || line[i] ==
'\t' ) ++i;
418 while( i < (
int)line.size() && line[i] !=
' ' && line[i] !=
'\t' )
433 if (!slash1 && !slash2)
436 if (slash1 && !slash2)
442 if (slash2 == slash1+1)
451 for(
size_t i=0; i < line.size(); ++i)
458 while( line[i] ==
' ' )
464 int iv=-1,ivt=-1,ivn=-1;
469 switch(f_format_type)
472 sscanf(line.c_str()+i,
"%d", &iv);
473 if (iv>0) --iv;
else iv = (int)mCoords.size() - iv;
477 sscanf(line.c_str()+i,
"%d/%d", &iv,&ivt);
478 if (iv>0) --iv;
else iv = (int)mCoords.size() - iv;
479 if (ivt>0) --ivt;
else ivt = (int)mTexCoords.size() - ivt;
484 sscanf(line.c_str()+i,
"%d//%d", &iv,&ivn);
485 if (iv>0) --iv;
else iv = (int)mCoords.size() - iv;
486 if (ivn>0) --ivn;
else ivn = (int)mNormals.size() - ivn;
491 sscanf(line.c_str()+i,
"%d/%d/%d", &iv,&ivt,&ivn);
492 if (iv>0) --iv;
else iv = (int)mCoords.size() - iv;
493 if (ivt>0) --ivt;
else ivt = (int)mTexCoords.size() - ivt;
494 if (ivn>0) --ivn;
else ivn = (int)mNormals.size() - ivn;
506 append(cur_mesh->
face_type(), face_type);
584 if (strcmp(cmd,
"s") == 0)
589 smoothing_group =
false;
591 smoothing_group =
true;
599 if (strcmp(cmd,
"o") == 0)
601 starts_new_geom =
true;
623 if (strcmp(cmd,
"usemtl") == 0)
625 starts_new_geom =
true;
628 cur_material = mMaterials[mat_name];
631 if (strcmp(cmd,
"mtllib") == 0)
641 std::vector<ObjMaterial> mats;
642 loadObjMaterials(vfile.
get(), mats);
644 for(
size_t i=0; i < mats.size(); ++i)
645 mMaterials[mats[i].objectName()] =
new ObjMaterial(mats[i]);
649 Log::error(
Say(
"Could not find OBJ material file '%s'.\n") << path );
674 std::map< ObjMaterial*, ref<Effect> > material_map;
675 for (std::map< std::string,
ref<ObjMaterial> >::iterator it = mMaterials.begin();
676 it != mMaterials.end();
683 material_map[it->second.get()] = effect;
752 unsigned char* mask_px = mask_img->
pixels();
754 unsigned char* diff_px = diff_img->
pixels() + 3;
755 while( mask_px != mask_end )
757 diff_px[0] = mask_px[0];
777 for(
int imesh=0; imesh<(int)mMeshes.size(); ++imesh)
779 if ( mMeshes[imesh]->facePositionIndex().empty() )
788 for(
int k=0; k<(int)mMeshes[imesh]->face_type().size(); ++k)
789 sum += mMeshes[imesh]->face_type()[k];
790 VL_CHECK( sum == (
int)mMeshes[imesh]->facePositionIndex().size() )
798 if ( !mMeshes[imesh]->faceNormalIndex().empty() && mMeshes[imesh]->faceNormalIndex().size() != mMeshes[imesh]->facePositionIndex().size() )
804 if ( !mMeshes[imesh]->faceTexCoordIndex().empty() && mMeshes[imesh]->faceTexCoordIndex().size() != mMeshes[imesh]->facePositionIndex().size() )
812 int tri_verts_count = 0;
813 for(
int k=0; k<(int)mMeshes[imesh]->face_type().size(); ++k)
814 tri_verts_count += (mMeshes[imesh]->face_type()[k] - 2) * 3;
816 v_coords->resize( tri_verts_count );
817 if ( !mMeshes[imesh]->faceNormalIndex().empty() )
818 n_coords->
resize( tri_verts_count );
819 if ( !mMeshes[imesh]->faceTexCoordIndex().empty() )
820 t_coords2->
resize( tri_verts_count );
824 int src_base_idx = 0;
825 int dst_base_idx = 0;
826 for(
int iface=0; iface<(int)mMeshes[imesh]->face_type().size(); ++iface)
828 int type = mMeshes[imesh]->face_type()[iface];
829 for(
int ivert=2; ivert < type; ++ivert )
831 int a = mMeshes[imesh]->facePositionIndex()[src_base_idx+0];
832 int b = mMeshes[imesh]->facePositionIndex()[src_base_idx+ivert-1];
833 int c = mMeshes[imesh]->facePositionIndex()[src_base_idx+ivert];
842 v_coords->at(dst_base_idx+0) = mCoords[a].xyz();
843 v_coords->at(dst_base_idx+1) = mCoords[b].xyz();
844 v_coords->at(dst_base_idx+2) = mCoords[c].xyz();
846 if (!mMeshes[imesh]->faceNormalIndex().empty())
848 int na = mMeshes[imesh]->faceNormalIndex()[src_base_idx+0];
849 int nb = mMeshes[imesh]->faceNormalIndex()[src_base_idx+ivert-1];
850 int nc = mMeshes[imesh]->faceNormalIndex()[src_base_idx+ivert];
859 n_coords->
at(dst_base_idx+0) = mNormals[na];
860 n_coords->
at(dst_base_idx+1) = mNormals[nb];
861 n_coords->
at(dst_base_idx+2) = mNormals[nc];
865 if (!mMeshes[imesh]->faceTexCoordIndex().empty())
867 int na = mMeshes[imesh]->faceTexCoordIndex()[src_base_idx+0];
868 int nb = mMeshes[imesh]->faceTexCoordIndex()[src_base_idx+ivert-1];
869 int nc = mMeshes[imesh]->faceTexCoordIndex()[src_base_idx+ivert];
874 VL_CHECK( na<(
int)mTexCoords.size() )
875 VL_CHECK( nb<(
int)mTexCoords.size() )
876 VL_CHECK( nc<(
int)mTexCoords.size() )
878 t_coords2->
at(dst_base_idx+0) = mTexCoords[na].st();
879 t_coords2->
at(dst_base_idx+1) = mTexCoords[nb].st();
880 t_coords2->
at(dst_base_idx+2) = mTexCoords[nc].st();
885 src_base_idx += type;
893 if ( mMeshes[imesh]->faceNormalIndex().size() )
895 if ( mMeshes[imesh]->faceTexCoordIndex().size() )
901 ref<Effect> effect = material_map[ mMeshes[imesh]->material() ];
904 effect = material_map[ mMeshes[imesh]->material() ] =
new Effect;
float ns() const
Ns - specular exponent.
Associates a Renderable object to an Effect and Transform.
VLGRAPHICS_EXPORT ref< ResourceDatabase > loadOBJ(const String &path)
Loads a Wavefront OBJ file. See also ObjLoader.
void setEffect(Effect *effect)
Binds an Effect to an Actor.
VLCORE_EXPORT FileSystem * defFileSystem()
Returns the default FileSystem used by VisualizationLibrary.
TexParameter * getTexParameter()
The TexParameter object associated to a Texture.
void setTexture(Texture *texture)
The texture sampler by a texture unit.
String & trim()
Equivalent to trim("\n\r\t\v "), that is, removes all the tabs, spaces and new-lines from the beginni...
void prepareTexture2D(int width, int height, ETextureFormat format, bool border=false)
Prepares for creation an empty 2D texture.
const std::vector< int > & faceTexCoordIndex() const
Index into ObjLoader::texCoordsArray() vector.
Vector3< float > fvec3
A 3 components vector with float precision.
const T_Scalar & b() const
const fvec3 & ka() const
Ka - ambient color.
float tr() const
Tr/d - transparency.
An abstract class representing a file.
Loads a Wavefront OBJ file.
bool mClamp
-clamp on | off
If enabled, do depth comparisons and update the depth buffer; Note that even if the depth buffer exis...
bool readLine(std::string &utf8)
Vector4< float > fvec4
A 4 components vector with float precision.
A simple String formatting class.
const unsigned char * pixels() const
Raw pointer to pixels.
void setObjectName(const char *name)
The name of the object, by default set to the object's class name in debug builds.
static void warning(const String &message)
Use this function to provide information about situations that might lead to errors or loss of data...
TextureSampler * gocTextureSampler(int unit_index)
void loadObjMaterials(VirtualFile *file, std::vector< ObjMaterial > &materials)
Loads a Wavefront MTL file.
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
ref< Image > convertType(EImageType new_type) const
Converts the type() of an image.
const fvec3 & kd() const
Kd - diffuse color.
If enabled, use the current lighting parameters to compute the vertex color; Otherwise, simply associate the current color with each vertex, see also Material, LightModel, and Light.
const std::vector< int > & faceNormalIndex() const
Index into ObjLoader::normalArray() vector.
If enabled, blend the incoming RGBA color values with the values in the color buffers, see also BlendFunc for more information.
void setShininess(float shininess)
String mFileName
Texture file name.
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
VirtualFile * inputFile()
void set(EFunction alphafunc, float ref_value)
void setVertexArray(ArrayAbstract *data)
Conventional vertex array.
bool mBlendU
-blendu on | off
bool mBlendV
-blendv on | off
String extractPath() const
If the String contains a file path the function returns the path with trailing slash, without the file name.
void setSpecular(const fvec4 &color)
Wraps an OpenGL texture object representing and managing all the supported texture types...
virtual ref< VirtualFile > locateFile(const String &full_path, const String &alternate_path=String()) const
Looks for a VirtualFile on the disk and in the currently active FileSystem.
void setNormalArray(ArrayAbstract *data)
Conventional normal array.
AlphaFunc * gocAlphaFunc()
int requiredMemory() const
Returns the number of bytes requested to store the image.
int toInt(bool hex=false) const
Returns the int number represented by the string. The conversion is done using the standard atoi() fu...
VLCORE_EXPORT ref< Image > loadImage(VirtualFile *file)
Loads an image from the specified file.
String & trim(wchar_t ch)
Removes the specified character ch from the beginning and the end of the String.
const ObjTexture & parseLine(const String &line, const String &file)
const String & path() const
Texture file name.
virtual void close()=0
Closes the file.
void disable(EEnable capability)
If enabled, performs alpha testing, see also AlphaFunc for more information.
const T_Scalar & r() const
const String & path() const
Returns the path of the file.
The Geometry class is a Renderable that implements a polygonal mesh made of polygons, lines and points.
float mMM_Gain
-mm base gain
Visualization Library main namespace.
float mTexres_Value
-texres value
double toDouble() const
Returns the double number represented by the string. The conversion is done using the standard atof()...
Represents a Wavefront OBJ mesh. See also ObjLoader.
The TextStream class can be used to conveniently read or parse utf8-encoded text files.
void setDiffuse(const fvec4 &color)
void setMinFilter(ETexParamFilter minfilter)
const fvec3 & ke() const
Ke - emissive color.
void setEmission(const fvec4 &color)
const std::vector< ref< Object > > & resources() const
Represents a Wavefront OBJ texture. See also ObjMaterial and ObjLoader.
float toFloat() const
Returns the float number represented by the string. The conversion is done using the standard atof() ...
static void print(const String &message)
Application message for the user.
bool empty() const
Returns true if length() == 0.
virtual bool open(EOpenMode mode)=0
Opens the file in the specified mode.
ref< ResourceDatabase > loadOBJ(VirtualFile *file)
Loads a Wavefront OBJ file.
void split(wchar_t separator, std::vector< String > &fields, bool remove_empty_fields=false) const
Splits a String into a set of fields. The fields are separated by the specified separator and are ret...
LightModel * gocLightModel()
void setTexCoordArray(int tex_unit, ArrayAbstract *data)
Conventional texture coords arrays.
bool startsWith(const String &str) const
Returns true if a String starts with the specified String str.
void print()
Prints the content of the material. Used for debugging purposes.
const std::vector< int > & face_type() const
Each entry represents a face, the number represents how many vertices the face has.
const ObjTexture & map_d() const
map_d - transparency map
void setAmbient(const fvec4 &color)
Shader * shader(int lodi=0, int pass=0)
Utility function, same as 'lod(lodi)->at(pass);'.
Defines the sequence of Shader objects used to render an Actor.
T_VectorType & at(size_t i)
const std::vector< int > & facePositionIndex() const
Index into ObjLoader::vertexArray() vector.
const std::string & objectName() const
The name of the object, by default set to the object's class name.
char mImfchan
-imfchan r | g | b | m | l | z
ref< Image > convertFormat(EImageFormat new_format) const
Converts the format() of an image.
float mMM_Base
-mm base gain
static std::string trimStdString(const std::string &text)
Remove the spaces before and after an std::string.
The ref<> class is used to reference-count an Object.
const ObjTexture & map_Kd() const
map_Kd - ambient diffuse
std::string toStdString() const
Returns a UTF8 encoded std::string.
Represents a Wavefront OBJ material as loaded from an MTL file. See also ObjLoader.
Wraps the OpenGL function glDrawArrays().
void setMagFilter(ETexParamFilter magfilter)
void setTwoSide(bool twoside)
const fvec3 & ks() const
Ks - specular color.
const T_Scalar & g() const
void setMaterial(ObjMaterial *mat)
The material associated to this mesh.
static int bitsPerPixel(EImageType type, EImageFormat format)
Returns the number of bits used to represents one pixel.
void enable(EEnable capability)
String field(wchar_t separator, int field_index) const
Splits a String into a set of fields based on the specified separator and returns the filed at positi...
The ResourceDatabase class contains and manipulates a set of resources.
Collection< DrawCall > & drawCalls()
Returns the list of DrawCall objects bound to a Geometry.
If enabled, cull polygons based on their winding in window coordinates, see also CullFace.