开源库之LibQglViewer-源码剖析6/15--Primitive.h

1. 引用头文件,命名空间定义, 前置类的前置引用说明;

#ifndef _PRIMITIVE_H_
#define _PRIMITIVE_H_

#include <vector>
#include "AxisAlignedBox.h"
#include "Vector3.h"
#include "NVector3.h"
#include "Types.h"

#ifdef WIN32
# include <windows.h> //Window系统运行环境,可以适配window系统文件
#endif

#ifdef __APPLE__
# include <OpenGL/gl.h>
#else
# include <GL/gl.h>
#endif

namespace vrender
{
    class Feedback3DColor ;
    class Primitive ;

}

1.1 .友元函数直接声明并定义在.h文件中

        //友元函数,重载
        friend inline Feedback3DColor operator*(const GLFLOAT & f,const Feedback3DColor& F)
        {
            return F*f ;
        }

1.2 primitive 图元基类,有virtue 抽象函数,= 0;

    /*
        图元
        1. 是否可见
        2. 图元接口(抽象函数)
    */
    class Primitive
    {
    public:
        virtual ~Primitive() {}


        virtual const Feedback3DColor& sommet3DColor(size_t) const =0 ;

        // Renvoie le ieme vertex modulo le nombre de vertex.
        virtual const Vector3& vertex(size_t) const = 0 ;

    //烦恼会
virtual AxisAlignedBox_xyz bbox() const = 0 ; virtual size_t nbVertices() const = 0 ; protected: int _vibility ; } ;

1.3  抽象函数的定义,实现,操作

1) 基类中定义抽象函数接口
    class Primitive
    {
    public:       
            virtual const Vector3& vertex(size_t) const = 0 ;       
        }     

(2)派生类中实现声明
    class Point: public Primitive
    {
            public:
        virtual const Vector3& vertex(size_t) const ;    
        }
(3)派生类cpp文件中实现
    
    const Vector3& Point::vertex(size_t) const
    {
    return _position_and_color.pos() ;
    }

3. 定义实现.cpp文件

#include <math.h>  //数学函数头文件
#include <assert.h>  //断言所在的头文件
#include "Primitive.h"
#include "Types.h"

using namespace vrender ;
using namespace std ;

4.

 

原文地址:https://www.cnblogs.com/icmzn/p/13547059.html