/********************************************************************/ /* Copyright (c) 2017 System fugen G.K. and Yuzi Mizuno */ /* All rights reserved. */ /********************************************************************/ #if !defined(_MGVBO__INCLUDED_) #define _MGVBO__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include "MGCLStdAfx.h" #include "mg/Pvector.h" #include "mg/drawParam.h" #include "mgGL/glslprogram.h" #include "mgGL/StaticGLAttrib.h" #include "mgGL/Color.h" #include "mgGL/GLAttrib.h" #include "mgGL/VBOElement.h" #include "mgGL/VBOLeafBuilder.h" class MGPosition; class MGBox; class MGBPointSeq; class MGSPointSeq; class MGCurve; class MGSPointSeq; class MGCellBase; class mgTL2Triangles; class MGStl; class MGColor; class MGAttribedGel; class mgVBO; class MGComplex; class MGImage; /** @file */ /** @addtogroup DisplayHandling * @{ */ ///////////////////////////////////////////////////////////////////////////// // Vertex Buffer Object Class. /// OpenGL 4 用描画のためのクラス, in other words, display list. /// 基本的な描画のためのツールとOpenGL4を使用した描画機構を提供する。 /// ///******利用上の手順****** ///(1) MGAttribedGel用のmgVBOは、mgVBO(const MGAttribedGel&)のconstructorを利用して構築し、 /// 必要に応じてdrawXXXまたはBegin-EndによりmgVBOに追加のmgVBOLeafを作成しm_gelの描画データを作成する。 ///  非MGAttribedGelのmgVBOはdrawXXX, begin()-end()を利用して描画データ作成処理(make_display_list())を行う。 /// 作成が終了したらsetDirty(false)により、作成完了を宣言する。 /// mgVBOはsetDirty(false)により描画データ作成完了宣言されていない場合にdraw()が呼ばれたとき、 /// 必ずmake_dipslay_list()を呼び出す。 ///(2) draw()により描画処理をする。 /// MGAttribedGel用のmgVBOに対してはmake_display_list()をせず、いきなりdraw()を呼び出してもよい。 /// setDirty(false)が発信されていない場合、mgVBOはmake_display_list()を発信し、描画データ /// 作成処理を行ったのち、drawを行う。 /// /// draw()/make_display_list()を呼ぶ前には種類の異なる複数の図形の作成を行っても /// よい(drawXXX, begin()-end())が、draw()/make_display_list()までに作成した図形は /// draw()/make_display_list()によりひとまとめの扱いとなる。 /// 描画関数draw()はメンバーデータの各mgVBOElementに対してdraw()を発信する。 /// /// Begin()-Bnd()で囲まれた場合、必ずひとつのmgVBOLeafが作成される。ひとつであることは /// 保障される。すべてのdrawXXXは、一つまたは一つ以上のmgVBOLeafをdraw()のために /// 作成する(make_display_list)だけで実際の描画は行わない。 /// ///mgVBOはmgVBOLeafとmgVBO自身(VBOPointer)をelementとして保持する。 ///mgVBOをelementとするのはMGGroupまたはMGShellのメンバーデータの表示情報を ///保持するため。これ以外のMGObjectはmgVBOLeaf(一般には複数)をelementとして保持する class MG_DLL_DECLR mgVBO:public mgVBOElement{ friend class MGDNameControl; friend class MGAttribedGel; friend class mgVBOPointer; // オペレーション public: enum ELEMENT_TARGET{ WIRE=0, SHADING=1, BOTH }; typedef MGPvector container_type; typedef container_type::iterator iterator; typedef container_type::const_iterator const_iterator; ///非MGAttribedGel用のconstructor. mgVBO(); ///MGAttribedGel用のconstructor. mgVBO(const MGAttribedGel& gel); ///MGAttribedGel用のconstructor. mgVBO(const mgVBO& vbo); ///Assignment. mgVBO& operator=(const mgVBO& vbo); virtual ~mgVBO(); ///VBO dataを初期化する。 ///(1) display modeを表示にする ///(2) viewModeに従い、clearElements()をする ///(3) clearStaticAttributes()発信 ///(4) gel用のvboであれば、drawAttrib()発信 void initializeVBO(MGCL::VIEWMODE viewMode=MGCL::DONTCARE); ///Build VBO hierarchy. ///Let n=vbos.size(), then ///vbos[i] includes vbos[i+1] as mgVBOPointer for i=0,...,n-2. ///vbos[0]=&parent, and vbos[n-1] = this mgVBO. ///Function's return value is true if found, false if not. bool buildVBOHierarchy( mgVBO& parent,///& vbos///back();}; mgVBOElement* front()const{return m_target_elements->front();}; iterator begin_element(){return m_target_elements->begin();}; iterator end_element(){return m_target_elements->end();}; const_iterator begin_element()const{return m_target_elements->begin();}; const_iterator end_element()const{return m_target_elements->end();}; int elementNumber()const{return (int)m_target_elements->size();}; void pop_back_element(){m_target_elements->pop_back();}; void push_back_element(mgVBOElement* elm){m_target_elements->push_back(elm);}; ///Obtain display list name. ///0(null) はこのmgVBOElementはmgVBOLeafであり、名前をもたないことを示す。 ///名前はmgVBOだけが持つ unsigned getDName()const{return m_dname;}; MGAttribedGel* gel()const{return m_gel;}; ///Selectionに設定する名前を求める。=0のとき、名前の設定処理をしない。 ///0(null) はこのmgVBOElementはmgVBOLeafであり、名前をもたないことを示す。 ///名前はmgVBOだけが持つ virtual GLuint getSelectionName()const; ///このmgVBOElementがnull(いまだdraw/make_display_list()処理されていない)かを判定 ///mgVBOはm_gelに対するmake_display_list()処理がなされていないときfalseが返される virtual bool is_made(MGCL::VIEWMODE viewMode=MGCL::DONTCARE); ///現在Begin()されているかどうかを調べる。Begin()され、End()の前であれば ///trueを返す bool is_InBegin(); ///typeのmgVBOLeafをひとつ(Begin-Endでひとつ)作成して追加する。 ///targetはBegin()-End()で作成されるmgVBOLeafをどのelementに入れるかを示す。 ///target=WIRE:Wire(highlight用のelement(m_elements) ///target=SHADING:Shading用のelement(m_elementsShade). ///typeは次のものを可とする(GL_xxxxのxxxxを示す) ///POINTS,LINES,LINE_STRIP,LINE_LOOP,TRIANGLE_FAN,TRIANGLE_STRIP,QUAD_STRIP. ///Begin()--End()でひとつのmgVBOLeafとされることが保証される。 ///ここで作成されるmgVBOLeafのstaticAttributesは現在このmgVBOに設定されている ///属性とされる。 virtual void Begin(GLenum type, ELEMENT_TARGET target=WIRE); ///Begin()で始めたVBOをひとつのmgVBOLeafとして作成、追加する。 ///polygonModeはBegin()のtypeがGL_QUAD_STRIP, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN ///の際にのみ有効で、そのPolygonMode(GL_POINT, GL_LINE, GL_FILL)を指定する。 ///作成されたmgVBOElement*が返されるがこのmgVBOElementはmgVBOが所有している。 ///nullが返却されたとき、作成に失敗。 virtual mgVBOLeaf* End(GLenum polygonMode=GL_FILL); ///Set dirty flag(s) of m_elements or m_elementsShade. ///is_dirty is true if dirty and need to remake. void setDirty(bool is_dirty=true, mgVBO::ELEMENT_TARGET target=BOTH)const; ///Function IDをセットする. ///setFunctionID()はBegin()-End()の間でのみ有効。 //void setFunctionID(int functionID); void setDrawType(mgGLSL::DrawType drawType); ///Textureをset/getする。 ///textureは参照するのみ ///setTexture()はBegin()-End()の間でのみ有効。 void setTexture(mgTexture* texture); ///頂点ごとの色を指定する void Color(const MGColor& colr); void Color3fv(const float colr[3]); void Color3dv(const double colr[3]); void Color4fv(const float colr[4]); void Color4ubv(const unsigned char rgba[4]); ///頂点ごとのnormalを指定する void Normal(const MGVector& norml); void Normal(float x, float y, float z); void Normal3d(double x, double y, double z); void Normal3fv(const float norml[3]); void Normal3dv(const double norml[3]); ///頂点の座標値を指定する virtual void Vertex(const MGPosition& v); virtual void Vertex(float x, float y, float z=0.0f); virtual void Vertex3d(double x, double y, double z=0.0); virtual void Vertex2fv(const float v[2]); virtual void Vertex3fv(const float v[3]); virtual void Vertex2dv(const double v[2]); virtual void Vertex3dv(const double v[3]); ///頂点ごとのTextureの座標値を指定する void TexCoord(const MGPosition& v); void TexCoord(float x, float y); void TexCoord2d(double x, double y); void TexCoord2fv(const float v[2]); void TexCoord2dv(const double v[2]); ///Static attributeを設定する。 ///begin()-end()の間であればそのmgVBOLeafに対してだけ適用され、 ///begin()-end()の外であればその後に作成される(setStaticAttribXXXをうちに持たない) ///begin()-end()のmgVBOLeafすべてに適用される。 void setStaticAttribColor(const MGColor& color);//colorにはundefinedのものも許される void setStaticAttribColor(const float color[4]); void setStaticAttribColor(float r, float g, float b); void setStaticAttribLineWidth(GLfloat size);///size<=0. はundefinedを示す void setStaticAttribPointSize(GLfloat size);///size<=0. はundefinedを示す ///Line stipple属性をセットする。 ///When factor=0 is input, line pattern is disabled. 実線となる ///When factor<0, the stipple attribute is undefined. This means the attribute ///is defined by the environment. ///When factor<=0, pattern is unnecessary. void setLineStipple(short int factor, GLushort pattern=0); void LineWidth(GLfloat size){setStaticAttribLineWidth(size);}; ///Line patternをdisableにして実線とする void disableLinePattern(); ///以下のdrawXXXはすべてひとつまたは一つ以上のmgVBOLeafをdraw()のために ///作成するだけで実際の描画は行わない。 ///gelのmgVBOPointerを作成しmgVBOElementとして追加する。 ///gel must be valid when draw event happens since gel is referenced at that time. void drawGel(const MGAttribedGel& gel); ///gelのmgVBOLeafPointerを作成しmgVBOElementとして追加する。 void drawVBOLeaf( const mgVBOLeaf& leaf,///& ipos, double size=-1. ); ///Draw a polyline using openGL functions. ///The last argument must end with nullptr. void drawOpenPolyline(const MGPosition* P0, ...); ///Draw a polyline using openGL functions. ///The last argument must end with nullptr. void drawClosedPolyline(const MGPosition* P0, ...); ///Draw a polyline using openGL functions. ///When clodes=true, 1st and last points will be connected. void drawPolyline(const MGBPointSeq& line, bool closed=false); ///Draw a polyline using openGL functions. ///When cloded=true, 1st and last points will be connected. void drawPolyline(const std::vector& line, bool closed=false); ///Draw a polyline using openGL functions. ///When cloded=true, 1st and last points are connected. void drawPolyline(size_t nPoints, const MGPosition line[], bool closed=false); ///Draw a line from start to end. void drawStraight(const MGPosition& end, const MGPosition& start); //Draw an object in its parameter space(MGDraw_in_parameter_space). //This is valid only for Surface, Face, Loop, Edge. void drawObjInParameterSpace(const MGObject& obj); ///Draw the rectangle of a box. void drawRectangle( const MGBox& box //Box to draw. ); ///OpenGL display for the tessellation lines drawn in world view. //void drawWTess( // const mgTLData& tld //tessellation data. //); ///OpenGL display for the tessellation lines drawn in parameter view. //void drawPTess( // const mgTLData& tld //tessellation data. //); /// Renders curvatures mapping that comes into colorful image. /// A point whose curvature is within [lower, upper], the color varies. void drawSurfaceCurvature( const mgTL2Triangles& tld, MGCL::SURFACE_CURVATURE_KIND kind, double lower, double upper //minimum and maximum value of the curvatures of the kind. ); /// Renders curvatures mapping that comes into colorful image. /// A point whose curvature is within [lower, upper], the color varies. void drawSurfaceCurvature( const std::vector& tldvec,///& trisVector,///* m_target_elements; mutable bool m_elementsDirty:1;///=true if dirty and need to remake. MGPvector m_elements;///make_display_list(). ///When m_elements.size()!=0, the display lists are already made and m_elements includes ///the elements. mutable bool m_elementsShadeDirty:1;///=true if dirty and need to remake. MGPvector m_elementsShade;///manifold_dimension()>=2, this means ///m_gel's shading data is not made yet. MGColor m_colorStatic;///Color specified by setStaticAttribColor(). ///The color of the following Begin()(mgVBOLeaf generated) is set to this color. GLfloat m_lineWidthStatic;///The line width specified by setStaticAttribLineWidth. ///The size of the following Begin()(mgVBOLeaf generated) is set to this size. ///When m_lineWidthStatic<=0., the line width is undefined. GLfloat m_pointSizeStatic;///The point size specified by setStaticAttribPointSize. ///The size of the following Begin()(mgVBOLeaf generated) is set to this size. ///When m_pointSizeStatic<=0., the point size is undefined. short int m_stippleFactor;///Line stipple factor. ///If m_stippleFactor=0, line Stipple is disabled. /// m_stippleFactor<0, line stipple is undefined. GLushort m_LineStipplePattern;///m_LineStipplePatternindicates the pattern. ///light modeはm_elementsShadeに対してのみ有効。m_elementsに対しては常にlightはオフ int m_lightMode;/// <0: undefined, =0:Light is disabled, >0:Light is enabled. /// CoorinateType mgGLSL::CoordinateType m_coordinateType; std::unique_ptr m_builder; void execStaticColorAttrib(){mgGLSL::execStaticColorAttrib(m_colorStatic);}; /// execStaticGLAttribは固有のuniform値をセットするために /// Overrideする場合があるので、virtualはつけておいてください。 virtual void execStaticGLAttrib(); /// execModelTypeAttribは固有のuniform値をセットするために /// Overrideする場合があるので、virtualはつけておいてください。 virtual void execModelTypeAttrib(); void setDlName(unsigned name){m_dname=name;}; void setGel(const MGAttribedGel* gel); ///Set Elements target. void setElementTarget(ELEMENT_TARGET target); }; namespace MGCL{ ///getCurveCurvatureLength computes the maximum curvature graph length. ///Function's return value is the maximum length of the graph. MG_DLL_DECLR double getCurveCurvatureLength( const MGCurve& curve, double scale, int density, bool use_radius ); } ///Utility class to invoke mgVBO::setLightMode(). ///mgLightModeSwitcher saves the current lightmode and invoke input mode. ///When mgLightModeSwitcher is destructed, the saved original mode is restored. class MG_DLL_DECLR mgLightModeSwitcher{ public: mgLightModeSwitcher(mgVBO& vbo, mgGLSL::ShadeMode mode):m_vbo(vbo){ m_orgMode = m_vbo.getLightMode(); m_vbo.setLightMode(mode); }; ~mgLightModeSwitcher(){ m_vbo.setLightMode(m_orgMode); }; private: mgVBO& m_vbo; int m_orgMode; }; /** @} */ // end of DisplayHandling group #endif // !defined(_MGVBO__INCLUDED_)