VIZ++ Class: OpenGLColoredRegularIcosahedron

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

Source code

/*
 * OpenGLColoredRegularIcosahedron.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 OpenGLColoredRegularIcosahedron : 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:
  OpenGLColoredRegularIcosahedron(Color3<GLfloat>* colors=NULL, int numColors=0)
  :OpenGLIndexedVertices(),
  vertices(NULL),
  verticesDataSize(0),
  indices(NULL),
  indicesDataSize(0)
  {    
    GLfloat g = (1.0f + sqrt(5.0f))/2.0f; //golden ratio

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

    if (colors && numColors == VERTICES) {
      setVerticesColors(colors, numColors);
    }
       
    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);
  }
  
  ~OpenGLColoredRegularIcosahedron()
  {
    delete [] vertices;
    delete [] indices;
    vertices  = NULL;
    indices = NULL;
  }
    
  void setVerticesColors(Color3<GLfloat>* colors, int numColors)
  {
    if (colors && numColors == VERTICES) {
      for (int i = 0; i<numColors; i++) {
        vertices[i * STRIDE + 0] = colors[i].r;
        vertices[i * STRIDE + 1] = colors[i].g;
        vertices[i * STRIDE + 2] = colors[i].b;
      }  
    }
  }
    
  GLenum getInterleavedArraysFormat()
  {
    return GL_C3F_V3F;
  }
  
  GLenum getPrimitiveType()
  {
    return GL_TRIANGLES;
  }
  
  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->frontFace(GL_CCW);
    gc->enable(GL_CULL_FACE);
    gc->cullFace(GL_BACK);
    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.