VIZ++ Class: OpenGLRegularIcosahedron

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

Source code

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


// 2016/08/10 Icosahedron's vertices data used here is based on the following webstie 
//
// https://en.wikipedia.org/wiki/Platonic_solid

#pragma once

#include <viz++/opengl/OpenGLIndexedVertices.h>
#include <viz++/opengl/Color3.h>
#include <math.h>

namespace VIZ {
    
class OpenGLRegularIcosahedron : public OpenGLIndexedVertices {
private:
  GLfloat* vertices;
  int      verticesDataSize;
  int      numberOfVertices;
  
  GLuint* indices;
  int     indicesDataSize;
  int     numberOfIndices;

  static const int STRIDE   = 6;
  static const int FACES    = 20;
  static const int VERTICES = 12;
public:
  OpenGLRegularIcosahedron()
  :OpenGLIndexedVertices(),
  vertices(NULL),
  verticesDataSize(0),
  indices(NULL),
  indicesDataSize(0)
  {    
    GLfloat g = (1.0f + sqrt(5.0f))/2.0f; //golden ratio

    GLfloat hedron[] = {
      // X,      Y,  Z
      // 12 vertices
      0.0f, -1.0f,  -g   ,
      0.0f, +1.0f,  -g   , 
      0.0f, -1.0f,  +g   , 
      0.0f, +1.0f,  +g   ,
      
      -g,     0.0f, -1.0f, 
      -g,     0.0f, +1.0f, 
      +g,     0.0f, -1.0f, 
      +g,     0.0f, +1.0f,
      
      -1.0f, -g,     0.0f, 
      +1.0f, -g,     0.0f, 
      -1.0f, +g,     0.0f, 
      +1.0f, +g,     0.0f,
    };
    
    verticesDataSize = sizeof(hedron);
    vertices = new GLfloat[CountOf(hedron)];
    memcpy(vertices, hedron, verticesDataSize);
    numberOfVertices = CountOf(hedron);

       
    GLuint indexData[] = {    
    //20 faces
     0,  1,  6,
     1,  0,  4,
     2,  3,  5,
     3,  2,  7,
     4,  5, 10,
     5,  4,  8,
     6,  7,  9,
     7,  6, 11,
     8,  9,  2,
     9,  8,  0, 
    10, 11,  1,
    11, 10,  3,
      
     0,  6,  9,
     0,  8,  4, 
     1,  4, 10,
     1, 11,  6,
     2,  5,  8,
     2,  9,  7, 
     3,  7, 11,
     3, 10,  5
    };
    
    indicesDataSize = sizeof(indexData);
    
    indices = new GLuint[CountOf(indexData)];
    memcpy(indices, indexData, indicesDataSize);
    numberOfIndices = CountOf(indexData);
  }
  
  ~OpenGLRegularIcosahedron()
  {
    delete [] vertices;
    delete [] indices;
    vertices  = NULL;
    indices = NULL;
  }
    
    
  GLenum getInterleavedArraysFormat()
  {
    return GL_V3F;
  }
  
  GLenum getPrimitiveType()
  {
    return GL_POLYGON;
  }
  
  GLfloat* getVertices()
  {
    return vertices;
  }
  int getVerticesDataSize()
  {
    return verticesDataSize;
  }

  int getNumberOfVertices()
  {
    return numberOfVertices;
  }

  GLuint* getIndices()
  {
    return indices;
  }

  int getIndicesDataSize()
  {
    return indicesDataSize;
  }
  
  int getNumberOfIndices()
  {
    return numberOfIndices;
  }
  
  void draw(OpenGLGC* gc)
  {
    gc -> interleavedArrays(getInterleavedArraysFormat(), 0, NULL);
    gc -> drawElements(getPrimitiveType(), getNumberOfIndices(), GL_UNSIGNED_INT, NULL);
  }
};
  
}
  

Last modified: 10 Feb 2017

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