SOL9 Sample: PyramidWithEyeAndLightPositioner

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


#include <sol/Stdio.h>
#include <sol/Vector3.h>
#include <sol/EyePositioner.h>
#include <sol/LightPositioner.h>

#include <sol/opengl/OpenGLMainView.h>
#include <sol/opengl/OpenGLView.h>
#include <sol/opengl/OpenGLGC.h>
#include <sol/opengl/OpenGLLight.h>
#include <sol/opengl/OpenGLMaterial.h>

#include <sol/opengl/Vertex3.h>
#include <sol/opengl/Color4.h>
#include <sol/opengl/FaceIndices.h>

#include <sol/opengl/OpenGLClientState.h>
#include <sol/opengl/OpenGLTriangleSurfaces.h>

#include "Resource.h"

namespace SOL {

class MainView :public OpenGLMainView {

private:
  //Inner class starts.
  class SimpleView: public OpenGLView {
  private:
    SmartPtr<OpenGLGC>               gc;
    float                            angle;
    SmartPtr<OpenGLTriangleSurfaces> surfaces;
    SmartPtr<Normal3> normals; 
    size_t            numNormals;
    Vector3 veye;
    Vector3 vlight;
    
  virtual void display()
  {
    static Vertex<3> vertices[] = {
      { -1.0, -1.0,  1.0},
      {  1.0, -1.0,  1.0},
      {  1.0, -1.0, -1.0},
      { -1.0, -1.0, -1.0},
      {  0.0,  1.0,  0.0},
    };
              
    static FaceIndices<3> indices[] = {
      {0, 1, 4}, //Triangle: face1
      {1, 2, 4}, //Triangle: face2
      {2, 3, 4}, //Triangle: face3
      {3, 0, 4}, //Triangle] face4
    };
       
    if (surfaces == NULL) {
      surfaces = new OpenGLTriangleSurfaces(vertices, CountOf(vertices), indices, CountOf(indices));
      normals = surfaces->calculateSurfaceNormals(numNormals);
      surfaces->displayNormals();
    }
    
    if (gc && normals) { 
      gc -> frustum(1 , -1 , -1 , 1 , 1 , 10);

      gc -> clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      gc -> loadIdentity();
      gc -> matrixMode(GL_MODELVIEW);
      gc -> clearColor(0.0, 1.0, 1.0, 1.0);
      
      gc -> translate(0.0f,0.0f,-1.0f); 
      gc -> lookAt(veye.x, veye.y, veye.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 
      gc -> rotate(0.0, 0.0, 1.0, 0.0);
      
      try {
        gc -> enable(GL_LIGHTING);
 
        OpenGLLight light(GL_LIGHT0);
        light.position(vlight.x, vlight.y, vlight.z, 0.0);  

        OpenGLMaterial material(GL_FRONT);
        material.diffuse (0.0f, 0.0f, 1.0f, 0.0f);
        material.specular(1.0f, 1.0f, 1.0f, 0.0f);
        material.shininess(100.0f);
  
        gc->frontFace(GL_CCW);

        gc->enable(GL_CULL_FACE);
        gc->cullFace(GL_BACK);
        glEnable(GL_NORMALIZE);
        
        for (int i = 0; i< CountOf(indices); i++) {
          gc->begin(GL_TRIANGLES);
          FaceIndices<3> tri = indices[i];
          gc->draw( normals[i], 
                    vertices[ tri.index[0] ], 
                    vertices[ tri.index[1] ], 
                    vertices[ tri.index[2] ]);
          gc->flush();
          gc ->end();
        }
      } catch (Exception& ex) {
        caught(ex); 
      }
    }
  }
 
  virtual void resize(int w, int h)
  {
    if (w == 0 || h == 0) {
      return;
    }
    if (gc) {
      gc -> matrixMode(GL_PROJECTION);
      gc -> loadIdentity();
      gc -> perspective(16.0, (double)w / (double)h, 0.5, 40.0); 

      gc -> matrixMode(GL_MODELVIEW);
    }
  }

  virtual void initialize()
  {
    gc = new OpenGLGC();
    gc -> enable( GL_DEPTH_TEST );
  }

  public:
    SimpleView(View* parent, const char* name, Args& args)
    :OpenGLView(parent, name, args),
    angle(0.0f),
    numNormals(0)
    {
      memset(&veye,   0, sizeof(veye));
      memset(&vlight, 0, sizeof(vlight));
    } 

    ~SimpleView()
    {
    }

    void setEyePosition(int x, int y, int z)
    {
      veye.x = (float)x;
      veye.y = (float)y;
      veye.z= (float)z;
      postRenderRequest();
    }

    void setLightPosition(int x, int y, int z)
    {
      vlight.x = (float)x;
      vlight.y = (float)y;
      vlight.z = (float)z;
      postRenderRequest();
    }
  };
  //Inner class ends.
  
private:
  SmartPtr<SimpleView>      view;
  SmartPtr<EyePositioner>   eyePositioner;
  SmartPtr<LightPositioner> lightPositioner;
  
  long size(Event& event)
  {
    int w, h;
    event.getSize(w, h);
    if (view && eyePositioner && lightPositioner) {
      int posw = eyePositioner->getDefaultWidth();
      int posh = eyePositioner->getDefaultHeight();
    
      view -> reshape(0, 0, w-posw-4, h);
      view -> postResizeRequest(w-posw-4, h);

      eyePositioner->reshape(w-posw-2, 2, posw, posh);
      lightPositioner->reshape(w-posw-2, 4+posh, posw, posh);
    }
    return 1L;
  }

  virtual long eyePositionChanged(Event& event)
  {   
    if (view && eyePositioner) {
      view -> setEyePosition(eyePositioner->getX(), 
                           eyePositioner->getY(),
                           eyePositioner->getZ());
    }
    return 0;
  }
  
  virtual long lightPositionChanged(Event& event)
  {
    if (view && lightPositioner) {
      view -> setLightPosition(lightPositioner->getX(),
                             lightPositioner->getY(),
                             lightPositioner->getZ());
    }
    return 0;
  }

public:
  MainView(Application& applet, const char* name, Args& args)
  :OpenGLMainView(applet, name, args.set(XmNstyle, WS_CLIPCHILDREN|WS_CLIPSIBLINGS))
  {
    Args ar;
    view = new SimpleView(this, "", ar);
    
    ar.reset();
    ar.set(XmNminimum, 0);
    ar.set(XmNmaximum, 40);
    eyePositioner = new EyePositioner(this, "", ar);
    int eyeX = 3;
    int eyeY = 6;
    int eyeZ = 12;
    eyePositioner->setThumbPosition(eyeX, eyeY, eyeZ);
    view -> setEyePosition(eyeX, eyeY, eyeZ);

    ar.reset();
    ar.set(XmNminimum, 0);
    ar.set(XmNmaximum, 40);
    lightPositioner = new LightPositioner(this, "", ar);
    int lightX = 0;
    int lightY = 6;
    int lightZ = 2;
    lightPositioner->setThumbPosition(lightX, lightY, lightZ);
    view -> setLightPosition(lightX, lightY, lightZ);
    
    addEventHandler(eyePositioner->getPositionChangedMessage(), this, 
      (Handler)&MainView::eyePositionChanged, NULL);

    addEventHandler(lightPositioner->getPositionChangedMessage(), this, 
      (Handler)&MainView::lightPositionChanged, NULL);
    view -> postRenderRequest();
  }

  ~MainView()
  {
  }
};
}

//
void Main(int argc, char** argv) 
{
  try {    
    const char*  name = appName(argv[0]);
    Application applet(name, argc, argv);

    Args args;
    args.set(XmNwidth,  640);
    args.set(XmNheight, 480);
    
    MainView view(applet, name, args);
    view.realize();

    applet.run();
    
  } catch (Exception& ex) {
    caught(ex);
  }
}


Last modified: 25 Feb 2017

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