函数的定义如下
class OSG_EXPORT DrawElementsUInt : public DrawElements, public VectorGLuint
{
public:
typedef VectorGLuint vector_type;
DrawElementsUInt(GLenum mode=0):
DrawElements(DrawElementsUIntPrimitiveType,mode) {}
DrawElementsUInt(const DrawElementsUInt& array,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
DrawElements(array,copyop),
vector_type(array) {}
/**
* \param mode One of osg::PrimitiveSet::Mode. Determines the type of primitives used.
* \param no Number of intended elements. This will be the size of the underlying vector.
*/
DrawElementsUInt(GLenum mode, unsigned int no, const GLuint* ptr, int numInstances=0) :
DrawElements(DrawElementsUIntPrimitiveType,mode,numInstances),
vector_type(ptr,ptr+no) {}
/**
* \param no Number of intended elements. This will be the size of the underlying vector.
*/
DrawElementsUInt(GLenum mode, unsigned int no) :
DrawElements(DrawElementsUIntPrimitiveType,mode),
vector_type(no) {}
template <class InputIterator>
DrawElementsUInt(GLenum mode, InputIterator first,InputIterator last) :
DrawElements(DrawElementsUIntPrimitiveType,mode),
vector_type(first,last) {}
virtual Object* cloneType() const { return new DrawElementsUInt(); }
virtual Object* clone(const CopyOp& copyop) const { return new DrawElementsUInt(*this,copyop); }
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const DrawElementsUInt*>(obj)!=NULL; }
virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const { return "DrawElementsUInt"; }
virtual const GLvoid* getDataPointer() const { return empty()?0:&front(); }
virtual unsigned int getTotalDataSize() const { return 4u*static_cast<unsigned int>(size()); }
virtual bool supportsBufferObject() const { return false; }
virtual void draw(State& state, bool useVertexBufferObjects) const;
virtual void accept(PrimitiveFunctor& functor) const;
virtual void accept(PrimitiveIndexFunctor& functor) const;
virtual unsigned int getNumIndices() const { return static_cast<unsigned int>(size()); }
virtual unsigned int index(unsigned int pos) const { return (*this)[pos]; }
virtual void offsetIndices(int offset);
virtual void reserveElements(unsigned int numIndices) { reserve(numIndices); }
virtual void setElement(unsigned int i, unsigned int v) { (*this)[i] = v; }
virtual unsigned int getElement(unsigned int i) { return (*this)[i]; }
virtual void addElement(unsigned int v) { push_back(GLuint(v)); }
protected:
virtual ~DrawElementsUInt();
};
不知道有谁碰到过这样的问题没有~求解
8 个解决方案
#1
DrawElementsUInt(GLenum mode, unsigned int no) 这个构造函数代码呢?
#2
DrawElementsUInt这个函数式调用了基类实现的吧?
父类DrawElements的构造函数入戏
DrawElements(Type primType=PrimitiveType, GLenum mode=0, int numInstances=0):
PrimitiveSet(primType,mode, numInstances) {}
DrawElements(const DrawElements& copy,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
PrimitiveSet(copy,copyop) {}
virtual DrawElements* getDrawElements() { return this; }
virtual const DrawElements* getDrawElements() const { return this; }
下面是父类PrimitiveSet的代码
class OSG_EXPORT PrimitiveSet : public BufferData
{
public:
enum Type
{
PrimitiveType,
DrawArraysPrimitiveType,
DrawArrayLengthsPrimitiveType,
DrawElementsUBytePrimitiveType,
DrawElementsUShortPrimitiveType,
DrawElementsUIntPrimitiveType
};
enum Mode
{
POINTS = GL_POINTS,
LINES = GL_LINES,
LINE_STRIP = GL_LINE_STRIP,
LINE_LOOP = GL_LINE_LOOP,
TRIANGLES = GL_TRIANGLES,
TRIANGLE_STRIP = GL_TRIANGLE_STRIP,
TRIANGLE_FAN = GL_TRIANGLE_FAN,
QUADS = GL_QUADS,
QUAD_STRIP = GL_QUAD_STRIP,
POLYGON = GL_POLYGON,
LINES_ADJACENCY = GL_LINES_ADJACENCY_EXT,
LINE_STRIP_ADJACENCY = GL_LINE_STRIP_ADJACENCY_EXT,
TRIANGLES_ADJACENCY = GL_TRIANGLES_ADJACENCY_EXT,
TRIANGLE_STRIP_ADJACENCY = GL_TRIANGLE_STRIP_ADJACENCY_EXT,
PATCHES = GL_PATCHES
};
PrimitiveSet(Type primType=PrimitiveType,GLenum mode=0, int numInstances=0):
_primitiveType(primType),
_numInstances(numInstances),
_mode(mode) {}
PrimitiveSet(const PrimitiveSet& prim,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
BufferData(prim,copyop),
_primitiveType(prim._primitiveType),
_numInstances(prim._numInstances),
_mode(prim._mode) {}
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const PrimitiveSet*>(obj)!=NULL; }
virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const { return "PrimitiveSet"; }
Type getType() const { return _primitiveType; }
virtual const GLvoid* getDataPointer() const { return 0; }
virtual unsigned int getTotalDataSize() const { return 0; }
virtual bool supportsBufferObject() const { return false; }
virtual DrawElements* getDrawElements() { return 0; }
virtual const DrawElements* getDrawElements() const { return 0; }
void setNumInstances(int n) { _numInstances = n; }
int getNumInstances() const { return _numInstances; }
void setMode(GLenum mode) { _mode = mode; }
GLenum getMode() const { return _mode; }
virtual void draw(State& state, bool useVertexBufferObjects) const = 0;
virtual void accept(PrimitiveFunctor& functor) const = 0;
virtual void accept(PrimitiveIndexFunctor& functor) const = 0;
virtual unsigned int index(unsigned int pos) const = 0;
virtual unsigned int getNumIndices() const = 0;
virtual void offsetIndices(int offset) = 0;
virtual unsigned int getNumPrimitives() const;
virtual void computeRange() const {}
protected:
virtual ~PrimitiveSet() {}
Type _primitiveType;
int _numInstances;
GLenum _mode;
};
父类DrawElements的构造函数入戏
DrawElements(Type primType=PrimitiveType, GLenum mode=0, int numInstances=0):
PrimitiveSet(primType,mode, numInstances) {}
DrawElements(const DrawElements& copy,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
PrimitiveSet(copy,copyop) {}
virtual DrawElements* getDrawElements() { return this; }
virtual const DrawElements* getDrawElements() const { return this; }
下面是父类PrimitiveSet的代码
class OSG_EXPORT PrimitiveSet : public BufferData
{
public:
enum Type
{
PrimitiveType,
DrawArraysPrimitiveType,
DrawArrayLengthsPrimitiveType,
DrawElementsUBytePrimitiveType,
DrawElementsUShortPrimitiveType,
DrawElementsUIntPrimitiveType
};
enum Mode
{
POINTS = GL_POINTS,
LINES = GL_LINES,
LINE_STRIP = GL_LINE_STRIP,
LINE_LOOP = GL_LINE_LOOP,
TRIANGLES = GL_TRIANGLES,
TRIANGLE_STRIP = GL_TRIANGLE_STRIP,
TRIANGLE_FAN = GL_TRIANGLE_FAN,
QUADS = GL_QUADS,
QUAD_STRIP = GL_QUAD_STRIP,
POLYGON = GL_POLYGON,
LINES_ADJACENCY = GL_LINES_ADJACENCY_EXT,
LINE_STRIP_ADJACENCY = GL_LINE_STRIP_ADJACENCY_EXT,
TRIANGLES_ADJACENCY = GL_TRIANGLES_ADJACENCY_EXT,
TRIANGLE_STRIP_ADJACENCY = GL_TRIANGLE_STRIP_ADJACENCY_EXT,
PATCHES = GL_PATCHES
};
PrimitiveSet(Type primType=PrimitiveType,GLenum mode=0, int numInstances=0):
_primitiveType(primType),
_numInstances(numInstances),
_mode(mode) {}
PrimitiveSet(const PrimitiveSet& prim,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
BufferData(prim,copyop),
_primitiveType(prim._primitiveType),
_numInstances(prim._numInstances),
_mode(prim._mode) {}
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const PrimitiveSet*>(obj)!=NULL; }
virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const { return "PrimitiveSet"; }
Type getType() const { return _primitiveType; }
virtual const GLvoid* getDataPointer() const { return 0; }
virtual unsigned int getTotalDataSize() const { return 0; }
virtual bool supportsBufferObject() const { return false; }
virtual DrawElements* getDrawElements() { return 0; }
virtual const DrawElements* getDrawElements() const { return 0; }
void setNumInstances(int n) { _numInstances = n; }
int getNumInstances() const { return _numInstances; }
void setMode(GLenum mode) { _mode = mode; }
GLenum getMode() const { return _mode; }
virtual void draw(State& state, bool useVertexBufferObjects) const = 0;
virtual void accept(PrimitiveFunctor& functor) const = 0;
virtual void accept(PrimitiveIndexFunctor& functor) const = 0;
virtual unsigned int index(unsigned int pos) const = 0;
virtual unsigned int getNumIndices() const = 0;
virtual void offsetIndices(int offset) = 0;
virtual unsigned int getNumPrimitives() const;
virtual void computeRange() const {}
protected:
virtual ~PrimitiveSet() {}
Type _primitiveType;
int _numInstances;
GLenum _mode;
};
#3
PrimitiveSet(Type primType=PrimitiveType,GLenum mode=0, int numInstances=0):
_primitiveType(primType),
_numInstances(numInstances),
_mode(mode) {}
构造函数在构造列表中应该要调用一个父类构造函数,不能省略。
子类构造函数不调用父类构造函数,应该通不过编译才是,不是道你用的是哪个编译器。
_primitiveType(primType),
_numInstances(numInstances),
_mode(mode) {}
构造函数在构造列表中应该要调用一个父类构造函数,不能省略。
子类构造函数不调用父类构造函数,应该通不过编译才是,不是道你用的是哪个编译器。
#4
我用的vs10,上面的文件会是OSG的开源代码~我在调用语句的时候出错了。楼上能详细点么?或者给我个方向看什么资料能解决问题
#5
class OSG_EXPORT PrimitiveSet : public BufferData
{....
PrimitiveSet(Type primType=PrimitiveType,GLenum mode=0, int numInstances=0):
BufferData(),//这里需要明确调用父类的哪一个构造函数。
_primitiveType(primType),
_numInstances(numInstances),
_mode(mode) {}
}
我没用 VS10 , 不知道为何能通过编译,我甚至怀疑你并没有真正编译该单元。
{....
PrimitiveSet(Type primType=PrimitiveType,GLenum mode=0, int numInstances=0):
BufferData(),//这里需要明确调用父类的哪一个构造函数。
_primitiveType(primType),
_numInstances(numInstances),
_mode(mode) {}
}
我没用 VS10 , 不知道为何能通过编译,我甚至怀疑你并没有真正编译该单元。
#6
你说的感觉有点道理但是还是没能解决问题~昨天在写另外一处代码是又碰到这个问题了
/*ref_ptr<osgViewer::Viewer> view = new osgViewer::Viewer();
view->setSceneData(root);
view->realize();
view->run();*/
osgViewer::Viewer view;
view.setSceneData(root);
view.realize();
view.run();
注释掉的那部分代码会在第二行报同样的错误,但是下面的代码就能运行?这两个具体的区别是什么?类的代码如下
class OSGVIEWER_EXPORT Viewer : public ViewerBase, public osgViewer::View
{
public:
Viewer();
Viewer(osg::ArgumentParser& arguments);
Viewer(const osgViewer::Viewer& viewer, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
virtual ~Viewer();
META_Object(osgViewer,Viewer);
/** Take all the settings, Camera and Slaves from the passed in view(er), leaving it empty. */
virtual void take(osg::View& rhs);
/** Set the Stats object used for collect various frame related timing and scene graph stats.*/
virtual void setViewerStats(osg::Stats* stats) { setStats(stats); }
/** Get the Viewers Stats object.*/
virtual osg::Stats* getViewerStats() { return getStats(); }
/** Get the Viewers Stats object.*/
virtual const osg::Stats* getViewerStats() const { return getStats(); }
/** read the viewer configuration from a configuration file.*/
virtual bool readConfiguration(const std::string& filename);
/** Get whether at least of one of this viewers windows are realized.*/
virtual bool isRealized() const;
/** set up windows and associated threads.*/
virtual void realize();
virtual void setStartTick(osg::Timer_t tick);
void setReferenceTime(double time=0.0);
/** Set the sene graph data that viewer with view.*/
virtual void setSceneData(osg::Node* node);
/** Convenience method for setting up the viewer so it can be used embedded in an external managed window.
* Returns the GraphicsWindowEmbedded that can be used by applications to pass in events to the viewer. */
virtual GraphicsWindowEmbedded* setUpViewerAsEmbeddedInWindow(int x, int y, int width, int height);
virtual double elapsedTime();
virtual osg::FrameStamp* getViewerFrameStamp() { return getFrameStamp(); }
/** Execute a main frame loop.
* Equivalent to while (!viewer.done()) viewer.frame();
* Also calls realize() if the viewer is not already realized,
* and installs trackball manipulator if one is not already assigned.
*/
virtual int run();
/** check to see if the new frame is required, called by run(..) when FrameScheme is set to ON_DEMAND.*/
virtual bool checkNeedToDoFrame();
virtual void advance(double simulationTime=USE_REFERENCE_TIME);
virtual void eventTraversal();
virtual void updateTraversal();
void setCameraWithFocus(osg::Camera* camera) { _cameraWithFocus = camera; }
osg::Camera* getCameraWithFocus() { return _cameraWithFocus.get(); }
const osg::Camera* getCameraWithFocus() const { return _cameraWithFocus.get(); }
virtual void getCameras(Cameras& cameras, bool onlyActive=true);
virtual void getContexts(Contexts& contexts, bool onlyValid=true);
virtual void getAllThreads(Threads& threads, bool onlyActive=true);
virtual void getOperationThreads(OperationThreads& threads, bool onlyActive=true);
virtual void getScenes(Scenes& scenes, bool onlyValid=true);
virtual void getViews(Views& views, bool onlyValid=true);
/** Get the keyboard and mouse usage of this viewer.*/
virtual void getUsage(osg::ApplicationUsage& usage) const;
protected:
void constructorInit();
virtual void viewerInit() { init(); }
osg::observer_ptr<osg::Camera> _cameraWithFocus;
};
/*ref_ptr<osgViewer::Viewer> view = new osgViewer::Viewer();
view->setSceneData(root);
view->realize();
view->run();*/
osgViewer::Viewer view;
view.setSceneData(root);
view.realize();
view.run();
注释掉的那部分代码会在第二行报同样的错误,但是下面的代码就能运行?这两个具体的区别是什么?类的代码如下
class OSGVIEWER_EXPORT Viewer : public ViewerBase, public osgViewer::View
{
public:
Viewer();
Viewer(osg::ArgumentParser& arguments);
Viewer(const osgViewer::Viewer& viewer, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
virtual ~Viewer();
META_Object(osgViewer,Viewer);
/** Take all the settings, Camera and Slaves from the passed in view(er), leaving it empty. */
virtual void take(osg::View& rhs);
/** Set the Stats object used for collect various frame related timing and scene graph stats.*/
virtual void setViewerStats(osg::Stats* stats) { setStats(stats); }
/** Get the Viewers Stats object.*/
virtual osg::Stats* getViewerStats() { return getStats(); }
/** Get the Viewers Stats object.*/
virtual const osg::Stats* getViewerStats() const { return getStats(); }
/** read the viewer configuration from a configuration file.*/
virtual bool readConfiguration(const std::string& filename);
/** Get whether at least of one of this viewers windows are realized.*/
virtual bool isRealized() const;
/** set up windows and associated threads.*/
virtual void realize();
virtual void setStartTick(osg::Timer_t tick);
void setReferenceTime(double time=0.0);
/** Set the sene graph data that viewer with view.*/
virtual void setSceneData(osg::Node* node);
/** Convenience method for setting up the viewer so it can be used embedded in an external managed window.
* Returns the GraphicsWindowEmbedded that can be used by applications to pass in events to the viewer. */
virtual GraphicsWindowEmbedded* setUpViewerAsEmbeddedInWindow(int x, int y, int width, int height);
virtual double elapsedTime();
virtual osg::FrameStamp* getViewerFrameStamp() { return getFrameStamp(); }
/** Execute a main frame loop.
* Equivalent to while (!viewer.done()) viewer.frame();
* Also calls realize() if the viewer is not already realized,
* and installs trackball manipulator if one is not already assigned.
*/
virtual int run();
/** check to see if the new frame is required, called by run(..) when FrameScheme is set to ON_DEMAND.*/
virtual bool checkNeedToDoFrame();
virtual void advance(double simulationTime=USE_REFERENCE_TIME);
virtual void eventTraversal();
virtual void updateTraversal();
void setCameraWithFocus(osg::Camera* camera) { _cameraWithFocus = camera; }
osg::Camera* getCameraWithFocus() { return _cameraWithFocus.get(); }
const osg::Camera* getCameraWithFocus() const { return _cameraWithFocus.get(); }
virtual void getCameras(Cameras& cameras, bool onlyActive=true);
virtual void getContexts(Contexts& contexts, bool onlyValid=true);
virtual void getAllThreads(Threads& threads, bool onlyActive=true);
virtual void getOperationThreads(OperationThreads& threads, bool onlyActive=true);
virtual void getScenes(Scenes& scenes, bool onlyValid=true);
virtual void getViews(Views& views, bool onlyValid=true);
/** Get the keyboard and mouse usage of this viewer.*/
virtual void getUsage(osg::ApplicationUsage& usage) const;
protected:
void constructorInit();
virtual void viewerInit() { init(); }
osg::observer_ptr<osg::Camera> _cameraWithFocus;
};
#7
其实,不用每次都贴那么多代码,看起来累。无关代码可用省略号的。
你的问题是一个有一定规模的系统中出的内存问题,象这类问题,如何你不能找到关键代码,那么是很难得到解决的。特别是看到大量使用多重继承的非纯虚类,这是很令人非常头疼的。
针对你的现象,注意以下几点:
1、大量使用多重继承的非纯虚类,你要确认你的类继承体系不存在问题。为使问题简化,一般设计为:多重继承的父类,均为纯虚类(接口)。在大量的多继承下,你的类体系容易变得复杂化。
2、确保不存在重名文件,如果你要备份,最好备份到U盘,以免因为编译器的搜索路径太多而造成重名问题。存在重名时,编译的可能是修改前的旧文件,编辑时却是新文件,而系统不会有任何提示。确保链接的是最新的 obj 及 lib 库,而不是旧的。
3、跟踪构造函数的执行,看是否正确。如:跟踪构造函数
PrimitiveSet(Type primType=PrimitiveType,GLenum mode=0, int numInstances=0);
看能否跟踪到其父类构造函数。new osgViewer::Viewer();看其类构造过程.
在多继承的情况下,是否每一个父类的构造函数均被执行。如果有夌形继承,还要检查共同的父构造函数是否多执行了一次。
如果你不知道多重继承下 构造函数 的正确执行次序,则该回炉看书。
4、如果上述问题均有保障,则检查每一个类中的指针,是否已经正确初始化,使用未初始化的指针,会产生你说的现象。如果有数组,检查是否越界。
你的问题是一个有一定规模的系统中出的内存问题,象这类问题,如何你不能找到关键代码,那么是很难得到解决的。特别是看到大量使用多重继承的非纯虚类,这是很令人非常头疼的。
针对你的现象,注意以下几点:
1、大量使用多重继承的非纯虚类,你要确认你的类继承体系不存在问题。为使问题简化,一般设计为:多重继承的父类,均为纯虚类(接口)。在大量的多继承下,你的类体系容易变得复杂化。
2、确保不存在重名文件,如果你要备份,最好备份到U盘,以免因为编译器的搜索路径太多而造成重名问题。存在重名时,编译的可能是修改前的旧文件,编辑时却是新文件,而系统不会有任何提示。确保链接的是最新的 obj 及 lib 库,而不是旧的。
3、跟踪构造函数的执行,看是否正确。如:跟踪构造函数
PrimitiveSet(Type primType=PrimitiveType,GLenum mode=0, int numInstances=0);
看能否跟踪到其父类构造函数。new osgViewer::Viewer();看其类构造过程.
在多继承的情况下,是否每一个父类的构造函数均被执行。如果有夌形继承,还要检查共同的父构造函数是否多执行了一次。
如果你不知道多重继承下 构造函数 的正确执行次序,则该回炉看书。
4、如果上述问题均有保障,则检查每一个类中的指针,是否已经正确初始化,使用未初始化的指针,会产生你说的现象。如果有数组,检查是否越界。
#8
就此结贴吧,如果你也在做OSG,而且碰到与楼主同样的问题,那么很高兴告诉你楼主的解决办法就是自己去编译OSG,方法楼主博客里有,方法是笨了点不过对于我这样的菜鸟,实用就行
#1
DrawElementsUInt(GLenum mode, unsigned int no) 这个构造函数代码呢?
#2
DrawElementsUInt这个函数式调用了基类实现的吧?
父类DrawElements的构造函数入戏
DrawElements(Type primType=PrimitiveType, GLenum mode=0, int numInstances=0):
PrimitiveSet(primType,mode, numInstances) {}
DrawElements(const DrawElements& copy,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
PrimitiveSet(copy,copyop) {}
virtual DrawElements* getDrawElements() { return this; }
virtual const DrawElements* getDrawElements() const { return this; }
下面是父类PrimitiveSet的代码
class OSG_EXPORT PrimitiveSet : public BufferData
{
public:
enum Type
{
PrimitiveType,
DrawArraysPrimitiveType,
DrawArrayLengthsPrimitiveType,
DrawElementsUBytePrimitiveType,
DrawElementsUShortPrimitiveType,
DrawElementsUIntPrimitiveType
};
enum Mode
{
POINTS = GL_POINTS,
LINES = GL_LINES,
LINE_STRIP = GL_LINE_STRIP,
LINE_LOOP = GL_LINE_LOOP,
TRIANGLES = GL_TRIANGLES,
TRIANGLE_STRIP = GL_TRIANGLE_STRIP,
TRIANGLE_FAN = GL_TRIANGLE_FAN,
QUADS = GL_QUADS,
QUAD_STRIP = GL_QUAD_STRIP,
POLYGON = GL_POLYGON,
LINES_ADJACENCY = GL_LINES_ADJACENCY_EXT,
LINE_STRIP_ADJACENCY = GL_LINE_STRIP_ADJACENCY_EXT,
TRIANGLES_ADJACENCY = GL_TRIANGLES_ADJACENCY_EXT,
TRIANGLE_STRIP_ADJACENCY = GL_TRIANGLE_STRIP_ADJACENCY_EXT,
PATCHES = GL_PATCHES
};
PrimitiveSet(Type primType=PrimitiveType,GLenum mode=0, int numInstances=0):
_primitiveType(primType),
_numInstances(numInstances),
_mode(mode) {}
PrimitiveSet(const PrimitiveSet& prim,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
BufferData(prim,copyop),
_primitiveType(prim._primitiveType),
_numInstances(prim._numInstances),
_mode(prim._mode) {}
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const PrimitiveSet*>(obj)!=NULL; }
virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const { return "PrimitiveSet"; }
Type getType() const { return _primitiveType; }
virtual const GLvoid* getDataPointer() const { return 0; }
virtual unsigned int getTotalDataSize() const { return 0; }
virtual bool supportsBufferObject() const { return false; }
virtual DrawElements* getDrawElements() { return 0; }
virtual const DrawElements* getDrawElements() const { return 0; }
void setNumInstances(int n) { _numInstances = n; }
int getNumInstances() const { return _numInstances; }
void setMode(GLenum mode) { _mode = mode; }
GLenum getMode() const { return _mode; }
virtual void draw(State& state, bool useVertexBufferObjects) const = 0;
virtual void accept(PrimitiveFunctor& functor) const = 0;
virtual void accept(PrimitiveIndexFunctor& functor) const = 0;
virtual unsigned int index(unsigned int pos) const = 0;
virtual unsigned int getNumIndices() const = 0;
virtual void offsetIndices(int offset) = 0;
virtual unsigned int getNumPrimitives() const;
virtual void computeRange() const {}
protected:
virtual ~PrimitiveSet() {}
Type _primitiveType;
int _numInstances;
GLenum _mode;
};
父类DrawElements的构造函数入戏
DrawElements(Type primType=PrimitiveType, GLenum mode=0, int numInstances=0):
PrimitiveSet(primType,mode, numInstances) {}
DrawElements(const DrawElements& copy,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
PrimitiveSet(copy,copyop) {}
virtual DrawElements* getDrawElements() { return this; }
virtual const DrawElements* getDrawElements() const { return this; }
下面是父类PrimitiveSet的代码
class OSG_EXPORT PrimitiveSet : public BufferData
{
public:
enum Type
{
PrimitiveType,
DrawArraysPrimitiveType,
DrawArrayLengthsPrimitiveType,
DrawElementsUBytePrimitiveType,
DrawElementsUShortPrimitiveType,
DrawElementsUIntPrimitiveType
};
enum Mode
{
POINTS = GL_POINTS,
LINES = GL_LINES,
LINE_STRIP = GL_LINE_STRIP,
LINE_LOOP = GL_LINE_LOOP,
TRIANGLES = GL_TRIANGLES,
TRIANGLE_STRIP = GL_TRIANGLE_STRIP,
TRIANGLE_FAN = GL_TRIANGLE_FAN,
QUADS = GL_QUADS,
QUAD_STRIP = GL_QUAD_STRIP,
POLYGON = GL_POLYGON,
LINES_ADJACENCY = GL_LINES_ADJACENCY_EXT,
LINE_STRIP_ADJACENCY = GL_LINE_STRIP_ADJACENCY_EXT,
TRIANGLES_ADJACENCY = GL_TRIANGLES_ADJACENCY_EXT,
TRIANGLE_STRIP_ADJACENCY = GL_TRIANGLE_STRIP_ADJACENCY_EXT,
PATCHES = GL_PATCHES
};
PrimitiveSet(Type primType=PrimitiveType,GLenum mode=0, int numInstances=0):
_primitiveType(primType),
_numInstances(numInstances),
_mode(mode) {}
PrimitiveSet(const PrimitiveSet& prim,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
BufferData(prim,copyop),
_primitiveType(prim._primitiveType),
_numInstances(prim._numInstances),
_mode(prim._mode) {}
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const PrimitiveSet*>(obj)!=NULL; }
virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const { return "PrimitiveSet"; }
Type getType() const { return _primitiveType; }
virtual const GLvoid* getDataPointer() const { return 0; }
virtual unsigned int getTotalDataSize() const { return 0; }
virtual bool supportsBufferObject() const { return false; }
virtual DrawElements* getDrawElements() { return 0; }
virtual const DrawElements* getDrawElements() const { return 0; }
void setNumInstances(int n) { _numInstances = n; }
int getNumInstances() const { return _numInstances; }
void setMode(GLenum mode) { _mode = mode; }
GLenum getMode() const { return _mode; }
virtual void draw(State& state, bool useVertexBufferObjects) const = 0;
virtual void accept(PrimitiveFunctor& functor) const = 0;
virtual void accept(PrimitiveIndexFunctor& functor) const = 0;
virtual unsigned int index(unsigned int pos) const = 0;
virtual unsigned int getNumIndices() const = 0;
virtual void offsetIndices(int offset) = 0;
virtual unsigned int getNumPrimitives() const;
virtual void computeRange() const {}
protected:
virtual ~PrimitiveSet() {}
Type _primitiveType;
int _numInstances;
GLenum _mode;
};
#3
PrimitiveSet(Type primType=PrimitiveType,GLenum mode=0, int numInstances=0):
_primitiveType(primType),
_numInstances(numInstances),
_mode(mode) {}
构造函数在构造列表中应该要调用一个父类构造函数,不能省略。
子类构造函数不调用父类构造函数,应该通不过编译才是,不是道你用的是哪个编译器。
_primitiveType(primType),
_numInstances(numInstances),
_mode(mode) {}
构造函数在构造列表中应该要调用一个父类构造函数,不能省略。
子类构造函数不调用父类构造函数,应该通不过编译才是,不是道你用的是哪个编译器。
#4
我用的vs10,上面的文件会是OSG的开源代码~我在调用语句的时候出错了。楼上能详细点么?或者给我个方向看什么资料能解决问题
#5
class OSG_EXPORT PrimitiveSet : public BufferData
{....
PrimitiveSet(Type primType=PrimitiveType,GLenum mode=0, int numInstances=0):
BufferData(),//这里需要明确调用父类的哪一个构造函数。
_primitiveType(primType),
_numInstances(numInstances),
_mode(mode) {}
}
我没用 VS10 , 不知道为何能通过编译,我甚至怀疑你并没有真正编译该单元。
{....
PrimitiveSet(Type primType=PrimitiveType,GLenum mode=0, int numInstances=0):
BufferData(),//这里需要明确调用父类的哪一个构造函数。
_primitiveType(primType),
_numInstances(numInstances),
_mode(mode) {}
}
我没用 VS10 , 不知道为何能通过编译,我甚至怀疑你并没有真正编译该单元。
#6
你说的感觉有点道理但是还是没能解决问题~昨天在写另外一处代码是又碰到这个问题了
/*ref_ptr<osgViewer::Viewer> view = new osgViewer::Viewer();
view->setSceneData(root);
view->realize();
view->run();*/
osgViewer::Viewer view;
view.setSceneData(root);
view.realize();
view.run();
注释掉的那部分代码会在第二行报同样的错误,但是下面的代码就能运行?这两个具体的区别是什么?类的代码如下
class OSGVIEWER_EXPORT Viewer : public ViewerBase, public osgViewer::View
{
public:
Viewer();
Viewer(osg::ArgumentParser& arguments);
Viewer(const osgViewer::Viewer& viewer, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
virtual ~Viewer();
META_Object(osgViewer,Viewer);
/** Take all the settings, Camera and Slaves from the passed in view(er), leaving it empty. */
virtual void take(osg::View& rhs);
/** Set the Stats object used for collect various frame related timing and scene graph stats.*/
virtual void setViewerStats(osg::Stats* stats) { setStats(stats); }
/** Get the Viewers Stats object.*/
virtual osg::Stats* getViewerStats() { return getStats(); }
/** Get the Viewers Stats object.*/
virtual const osg::Stats* getViewerStats() const { return getStats(); }
/** read the viewer configuration from a configuration file.*/
virtual bool readConfiguration(const std::string& filename);
/** Get whether at least of one of this viewers windows are realized.*/
virtual bool isRealized() const;
/** set up windows and associated threads.*/
virtual void realize();
virtual void setStartTick(osg::Timer_t tick);
void setReferenceTime(double time=0.0);
/** Set the sene graph data that viewer with view.*/
virtual void setSceneData(osg::Node* node);
/** Convenience method for setting up the viewer so it can be used embedded in an external managed window.
* Returns the GraphicsWindowEmbedded that can be used by applications to pass in events to the viewer. */
virtual GraphicsWindowEmbedded* setUpViewerAsEmbeddedInWindow(int x, int y, int width, int height);
virtual double elapsedTime();
virtual osg::FrameStamp* getViewerFrameStamp() { return getFrameStamp(); }
/** Execute a main frame loop.
* Equivalent to while (!viewer.done()) viewer.frame();
* Also calls realize() if the viewer is not already realized,
* and installs trackball manipulator if one is not already assigned.
*/
virtual int run();
/** check to see if the new frame is required, called by run(..) when FrameScheme is set to ON_DEMAND.*/
virtual bool checkNeedToDoFrame();
virtual void advance(double simulationTime=USE_REFERENCE_TIME);
virtual void eventTraversal();
virtual void updateTraversal();
void setCameraWithFocus(osg::Camera* camera) { _cameraWithFocus = camera; }
osg::Camera* getCameraWithFocus() { return _cameraWithFocus.get(); }
const osg::Camera* getCameraWithFocus() const { return _cameraWithFocus.get(); }
virtual void getCameras(Cameras& cameras, bool onlyActive=true);
virtual void getContexts(Contexts& contexts, bool onlyValid=true);
virtual void getAllThreads(Threads& threads, bool onlyActive=true);
virtual void getOperationThreads(OperationThreads& threads, bool onlyActive=true);
virtual void getScenes(Scenes& scenes, bool onlyValid=true);
virtual void getViews(Views& views, bool onlyValid=true);
/** Get the keyboard and mouse usage of this viewer.*/
virtual void getUsage(osg::ApplicationUsage& usage) const;
protected:
void constructorInit();
virtual void viewerInit() { init(); }
osg::observer_ptr<osg::Camera> _cameraWithFocus;
};
/*ref_ptr<osgViewer::Viewer> view = new osgViewer::Viewer();
view->setSceneData(root);
view->realize();
view->run();*/
osgViewer::Viewer view;
view.setSceneData(root);
view.realize();
view.run();
注释掉的那部分代码会在第二行报同样的错误,但是下面的代码就能运行?这两个具体的区别是什么?类的代码如下
class OSGVIEWER_EXPORT Viewer : public ViewerBase, public osgViewer::View
{
public:
Viewer();
Viewer(osg::ArgumentParser& arguments);
Viewer(const osgViewer::Viewer& viewer, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
virtual ~Viewer();
META_Object(osgViewer,Viewer);
/** Take all the settings, Camera and Slaves from the passed in view(er), leaving it empty. */
virtual void take(osg::View& rhs);
/** Set the Stats object used for collect various frame related timing and scene graph stats.*/
virtual void setViewerStats(osg::Stats* stats) { setStats(stats); }
/** Get the Viewers Stats object.*/
virtual osg::Stats* getViewerStats() { return getStats(); }
/** Get the Viewers Stats object.*/
virtual const osg::Stats* getViewerStats() const { return getStats(); }
/** read the viewer configuration from a configuration file.*/
virtual bool readConfiguration(const std::string& filename);
/** Get whether at least of one of this viewers windows are realized.*/
virtual bool isRealized() const;
/** set up windows and associated threads.*/
virtual void realize();
virtual void setStartTick(osg::Timer_t tick);
void setReferenceTime(double time=0.0);
/** Set the sene graph data that viewer with view.*/
virtual void setSceneData(osg::Node* node);
/** Convenience method for setting up the viewer so it can be used embedded in an external managed window.
* Returns the GraphicsWindowEmbedded that can be used by applications to pass in events to the viewer. */
virtual GraphicsWindowEmbedded* setUpViewerAsEmbeddedInWindow(int x, int y, int width, int height);
virtual double elapsedTime();
virtual osg::FrameStamp* getViewerFrameStamp() { return getFrameStamp(); }
/** Execute a main frame loop.
* Equivalent to while (!viewer.done()) viewer.frame();
* Also calls realize() if the viewer is not already realized,
* and installs trackball manipulator if one is not already assigned.
*/
virtual int run();
/** check to see if the new frame is required, called by run(..) when FrameScheme is set to ON_DEMAND.*/
virtual bool checkNeedToDoFrame();
virtual void advance(double simulationTime=USE_REFERENCE_TIME);
virtual void eventTraversal();
virtual void updateTraversal();
void setCameraWithFocus(osg::Camera* camera) { _cameraWithFocus = camera; }
osg::Camera* getCameraWithFocus() { return _cameraWithFocus.get(); }
const osg::Camera* getCameraWithFocus() const { return _cameraWithFocus.get(); }
virtual void getCameras(Cameras& cameras, bool onlyActive=true);
virtual void getContexts(Contexts& contexts, bool onlyValid=true);
virtual void getAllThreads(Threads& threads, bool onlyActive=true);
virtual void getOperationThreads(OperationThreads& threads, bool onlyActive=true);
virtual void getScenes(Scenes& scenes, bool onlyValid=true);
virtual void getViews(Views& views, bool onlyValid=true);
/** Get the keyboard and mouse usage of this viewer.*/
virtual void getUsage(osg::ApplicationUsage& usage) const;
protected:
void constructorInit();
virtual void viewerInit() { init(); }
osg::observer_ptr<osg::Camera> _cameraWithFocus;
};
#7
其实,不用每次都贴那么多代码,看起来累。无关代码可用省略号的。
你的问题是一个有一定规模的系统中出的内存问题,象这类问题,如何你不能找到关键代码,那么是很难得到解决的。特别是看到大量使用多重继承的非纯虚类,这是很令人非常头疼的。
针对你的现象,注意以下几点:
1、大量使用多重继承的非纯虚类,你要确认你的类继承体系不存在问题。为使问题简化,一般设计为:多重继承的父类,均为纯虚类(接口)。在大量的多继承下,你的类体系容易变得复杂化。
2、确保不存在重名文件,如果你要备份,最好备份到U盘,以免因为编译器的搜索路径太多而造成重名问题。存在重名时,编译的可能是修改前的旧文件,编辑时却是新文件,而系统不会有任何提示。确保链接的是最新的 obj 及 lib 库,而不是旧的。
3、跟踪构造函数的执行,看是否正确。如:跟踪构造函数
PrimitiveSet(Type primType=PrimitiveType,GLenum mode=0, int numInstances=0);
看能否跟踪到其父类构造函数。new osgViewer::Viewer();看其类构造过程.
在多继承的情况下,是否每一个父类的构造函数均被执行。如果有夌形继承,还要检查共同的父构造函数是否多执行了一次。
如果你不知道多重继承下 构造函数 的正确执行次序,则该回炉看书。
4、如果上述问题均有保障,则检查每一个类中的指针,是否已经正确初始化,使用未初始化的指针,会产生你说的现象。如果有数组,检查是否越界。
你的问题是一个有一定规模的系统中出的内存问题,象这类问题,如何你不能找到关键代码,那么是很难得到解决的。特别是看到大量使用多重继承的非纯虚类,这是很令人非常头疼的。
针对你的现象,注意以下几点:
1、大量使用多重继承的非纯虚类,你要确认你的类继承体系不存在问题。为使问题简化,一般设计为:多重继承的父类,均为纯虚类(接口)。在大量的多继承下,你的类体系容易变得复杂化。
2、确保不存在重名文件,如果你要备份,最好备份到U盘,以免因为编译器的搜索路径太多而造成重名问题。存在重名时,编译的可能是修改前的旧文件,编辑时却是新文件,而系统不会有任何提示。确保链接的是最新的 obj 及 lib 库,而不是旧的。
3、跟踪构造函数的执行,看是否正确。如:跟踪构造函数
PrimitiveSet(Type primType=PrimitiveType,GLenum mode=0, int numInstances=0);
看能否跟踪到其父类构造函数。new osgViewer::Viewer();看其类构造过程.
在多继承的情况下,是否每一个父类的构造函数均被执行。如果有夌形继承,还要检查共同的父构造函数是否多执行了一次。
如果你不知道多重继承下 构造函数 的正确执行次序,则该回炉看书。
4、如果上述问题均有保障,则检查每一个类中的指针,是否已经正确初始化,使用未初始化的指针,会产生你说的现象。如果有数组,检查是否越界。
#8
就此结贴吧,如果你也在做OSG,而且碰到与楼主同样的问题,那么很高兴告诉你楼主的解决办法就是自己去编译OSG,方法楼主博客里有,方法是笨了点不过对于我这样的菜鸟,实用就行