VIZ++ Class: OpenGLMultiTexturedCube

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

Source code

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

 
#pragma once

#include <viz++/opengl/OpenGLTexture2D.h>
#include <viz++/opengl/TextureCoord2Vertex3.h>
#include <viz++/opengl/TextureCoordTriangularCube.h>

namespace VIZ {

class OpenGLMultiTexturedCube : public OpenGLObject {
private:
  static const int FACES = 6;
  OpenGLTexture2D* textures[FACES];
  
protected:
  OpenGLMultiTexturedCube()
  :OpenGLObject()
  {    
    for (int i = 0; i<FACES; i++) {
      textures[i] = new OpenGLTexture2D();
    }
  }
  

public:
  OpenGLMultiTexturedCube(size_t size, const char* filenames[])
  :OpenGLObject()
  {
    if (size != FACES) {
      throw IException("Invalid size parameter: %d", size);
    }
    if (filenames == NULL) {
      throw IException("Invalid filenames parameter");
    }
    
    for (int i = 0; i<FACES; i++) {
      textures[i] = new OpenGLTexture2D();
    }
    load(size, filenames);
  }

  public:
  ~OpenGLMultiTexturedCube()
  {
    for (int i = 0; i<FACES; i++) {
      textures[i] -> unbind(); 
      delete textures[i];
      textures[i] = NULL;
    }
  }
  
  virtual void load(size_t size, const char* filenames[])
  {
    if (size != FACES) {
      throw IException("Invalid size parameter: %d", size);
    }
    if (filenames == NULL) {
      throw IException("Invalid filenames parameter");
    }

    for (int i = 0; i<FACES; i++) { 
      textures[i] -> bind();

      textures[i] -> pixelStore(GL_UNPACK_ALIGNMENT, 4);

      textures[i] -> parameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
      textures[i] -> parameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      textures[i] -> env(GL_TEXTURE_ENV_MODE, GL_MODULATE);

      textures[i] -> generate(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP);
      textures[i] -> generate(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP);

      textures[i] -> parameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
      textures[i] -> parameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
      try {
        textures[i] -> imageFromFile(filenames[i]);//, GL_TEXTURE_2D);
      } catch (Exception& ex) {
        ex.display();
      }
    
      textures[i] -> unbind();
    }
  }
    
  public:
  virtual void draw(OpenGLGC* gc)
  {
      TextureCoordTriangularCube ttc;

      TextureCoord2Vertex3* cube = ttc.getData();;
      
      int numFaces = ttc.getNumberOfFaces();
      int numVerticesPerFace = ttc.getNumberOfVerticesPerFace();
      
      if (gc) { 
        gc -> enable(GL_NORMALIZE);
        //gc -> enable( GL_CULL_FACE );
        //gc -> cullFace ( GL_FRONT );
        gc -> enable(GL_BLEND);
        //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        gc -> enable(GL_TEXTURE_2D);
        
        for (int i = 0; i<numFaces; i++) {
          
        
          textures[i] -> bind();
          gc -> begin(GL_TRIANGLES);

          for (int j = 0; j<numVerticesPerFace; j++) {
            textures[i] -> coordVertex(cube[i*numFaces + j]);
          }
       
          textures[i] -> unbind();
          gc ->flush();

          gc -> end();
        }
        gc ->disable(GL_TEXTURE_2D);
      }
   }
 
 };
}
 

Last modified: 10 Feb 2017

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