VIZ++ Class: OpenGLQuadSurfaces

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

Source code

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


//2016/09/10 On calculateSurfaceNormals method, see: https://www.opengl.org/wiki/Calculating_a_Surface_Normal

#pragma once

#include <viz++/Stdio.h>
#include <viz++/opengl/Normal3.h>
#include <viz++/Vertex.h>
#include <viz++/opengl/QuadIndices.h>
#include <viz++/Vector3f.h>
#include <viz++/opengl/FaceIndices.h>

namespace VIZ {
  
class OpenGLQuadSurfaces {
private:
  const Vertex<3>* vertexArray;
  size_t         numberOfVertices;
  const FaceIndices<4>* faceArray;
  size_t         numberOfFaces;
  
  Normal3*       normals;
  size_t         numberOfNormals;
  
public:
  OpenGLQuadSurfaces(__in const Vertex<3>* vertices, __in size_t numVertices, 
              __in const FaceIndices<4>* faces, __in size_t numFaces)
  :vertexArray(NULL),
  numberOfVertices(numVertices),
  faceArray(NULL),
  numberOfFaces(numFaces),
  normals(NULL),
  numberOfNormals(0)
  {
    //Deep copy
    if (vertices && numVertices >0) {
      vertexArray = new Vertex<3>[numVertices];
      memcpy((void*)vertexArray, (const void*)vertices, sizeof(Vertex3)* numVertices);
    } else {
      throw IException("Invalid vertices parameters.");
    }
    if (faces && numFaces >0) {
      faceArray = new FaceIndices<4>[numFaces];
      memcpy((void*)faceArray, (const void*)faces, sizeof(FaceIndices<4>)* numFaces);
    } else {
      throw IException("Invalid faces parameters.");
    }
  }
  
  
  ~OpenGLQuadSurfaces()
  {
    delete [] vertexArray;
    vertexArray = NULL;
    delete [] faceArray;
    faceArray   = NULL;
    delete [] normals;
    normals = NULL;
  }
  

  Normal3* calculateSurfaceNormals(__out size_t& numNormals) //,
  {
    numberOfNormals = numberOfFaces;
    if (normals == NULL) {
      normals = new Normal3[numberOfFaces];
    }
      
    for (int s = 0; s<numberOfFaces; s++) {
      FaceIndices<4> quad = faceArray[s];
      Vector3f v[4] = {
        Vector3f(vertexArray[ quad.index[0]]),
        Vector3f(vertexArray[ quad.index[1]]),
        Vector3f(vertexArray[ quad.index[2]]),
        Vector3f(vertexArray[ quad.index[3]]),
      };
      
      Vector3f normal;
      
      for (int i=0; i<4; i++) {
        int j = (i+1) % 4;
        normal[0] += (v[i][1] - v[j][1]) * (v[i][2] + v[j][2]);
        normal[1] += (v[i][2] - v[j][2]) * (v[i][0] + v[j][0]);
        normal[2] += (v[i][0] - v[j][0]) * (v[i][1] + v[j][1]);
      }
      normal.normalize();
        
      normals[s].x = normal[0];
      normals[s].y = normal[1];
      normals[s].z = normal[2];
    } 
    numNormals = numberOfNormals;
    return normals;    
  }
  
  void displayNormals()
  {
    for (int i =0; i<numberOfFaces; i++) {
      Printf("%d x=%f, y=%f, z=%f\n", i, 
         normals[i].x, normals[i].y, normals[i].z);      
    }
  }
  
};

}

    

Last modified: 10 Feb 2017

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