VIZ++ Class: OpenGLRegularPolygon

 VIZ++ Class Library  VIZ++ Samples  VIZ++ ClassTree 

Source code

/*
 * OpenGLRegularPolygon.h 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


#pragma once

#include <viz++/Exception.h>
#include <viz++/opengl/OpenGLObject.h>
#include <viz++/opengl/OpenGLQuadric.h>
#include <viz++/opengl/Vertex3.h>
#include <math.h>

namespace VIZ {

class OpenGLRegularPolygon :public OpenGLObject {
private:
  //OpenGLQuadric* quadric;
  GLuint   number;
  GLenum   primitive;
  Vertex3* polygon;
  
public:
  OpenGLRegularPolygon(GLuint n, GLenum p=GL_TRIANGLE_FAN, float z=0.0f)
  :OpenGLObject(),
  number(n),
  primitive(p),
  polygon(NULL)
  {
    float delta = 2.0 * MATH_PI / (float)n;
    polygon = new Vertex3[n+1];
    for (int i = 0; i<=n; i++) {
     polygon[i].x = cos(delta * i);
     polygon[i].y = sin(delta * i);
     polygon[i].z = z; 
    }
  }
  
  ~OpenGLRegularPolygon()
  {
    delete [] polygon;
    polygon = NULL;
  }
  
  void draw(OpenGLGC* gc)
  {
    gc-> begin(primitive);
    
    if (primitive == GL_TRIANGLE_FAN) {
      gc -> vertex(0.0f, 0.0f, 0.0f);
    }
  
    for (int i= 0; i<= number; i++) {
      gc->vertex(polygon[i].x, polygon[i].y, polygon[i].z);
    }
    
    gc->end();
  }
 
};

}


Last modified: 10 Feb 2017

Copyright (c) 2009-2017 Antillia.com ALL RIGHTS RESERVED.