VIZ++ Sample: Polygon

VIZ++ Samples

1 Screenshot


2 Source code

/*
 * Polygon.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


//2017/02/10

#include <viz++/opengl/OpenGLApplet.h>
#include <viz++/opengl/OpenGLMainView.h>
#include <viz++/opengl/OpenGLGC.h>

#include <viz++/opengl/OpenGLLight.h>
#include <viz++/opengl/OpenGLMaterial.h>

namespace VIZ {

class MainView :public OpenGLMainView {
private:
  SmartPtr<OpenGLGC> gc;
  
public:
  //This method will be called from an expose callback of OpenGLView.
  virtual void display()
  {
    if (gc) {
      const GLfloat lightPosition[] = { 5.0 , 0.0 , -8.0 , 1.0 };
      const GLfloat lightColor[]    = { 0.5 , 1 , 0.5 , 1 };
      const GLfloat materialColor[] = { 0.2 , 0.6 , 0.2 , 1};
    
      OpenGLLight light(GL_LIGHT0);
      light.position(lightPosition);
      light.diffuse(lightColor);
      light.enable(); 
      
      OpenGLMaterial material(GL_FRONT_AND_BACK );
      material.diffuse(materialColor);
      
      gc -> clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      gc -> begin(GL_POLYGON);

      gc -> normal(3.0 , 0.0 , -2.0);
      gc -> vertex(0.0 , -0.9 , -2.0);
      gc -> vertex(3.0 , -0.9 , -7.0);
      gc -> vertex(0.0 , 0.9 , -2.0 );

      gc -> normal(-3.0 , 0.0 , -2.0);
      gc -> vertex(0.0 , -0.9 , -2.0);
      gc -> vertex(-3.0, -0.9 , -7.0);
      gc -> vertex(0.0, 0.9 , -2.0);

      gc -> end();
    }
  }
 
  //This method will be called from a structureNotiy event handler of OpenGLView.
  virtual void resize(int width, int height)
  {
      //Write your own resize procedure.
      //
      
      //You don't need to call set a viewport, because the setViewport will be called 
      //in reshape method of OpenGLMainView 
  }

  //This method will be called from a mapNotify event handler of OpenGLView.
  virtual void initialize()
  {
    gc = new OpenGLGC();
    gc -> enable(GL_LIGHTING);
    gc -> matrixMode(GL_PROJECTION);
    gc -> loadIdentity();
    gc -> frustum(1, -1, -1, 1, 2, 10);
  }
  

public:
  MainView(OpenGLApplet& applet, int width, int height, const char* name)
  :OpenGLMainView(applet, width, height, name)
  {
  }
  
  ~MainView()
  {

  }
};
}

//
int main(int argc, char** argv) 
{
  try {
    ModuleFileName module;
    const char*  name = module.getAppName();
    OpenGLApplet applet(argc, argv);
;
    MainView view(applet, 400, 300, name);
    view.realize();
    
    applet.run();
    
  } catch (Exception& ex) {
    caught(ex);
  }
  return 0;
}


Last modified: 10 Feb 2017

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