SOL9 Sample: TorusWithColorEyeAndLightPositioner

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


#include <sol/Stdio.h>
#include <sol/Vector3.h>
#include <sol/LightPositioner.h>
#include <sol/EyePositioner.h>
#include <sol/ColorPositioner.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/OpenGLGeometry.h>

#include "Resource.h"

namespace SOL {

class MainView :public OpenGLMainView {

private:
  //Inner class starts.
  class SimpleView: public OpenGLView {
  private:
    SmartPtr<OpenGLGC> gc;
    float     angle;
 
    Vector3 vcolor;
    Vector3 veye;
    Vector3 vlight;
    
  virtual void display()
  {
    if (gc) { 
      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, 0.0f); 
      gc -> lookAt(veye.x, veye.y, veye.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); 

      gc -> enable(GL_CULL_FACE); 
      gc->cullFace(GL_BACK);
     
      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 (vcolor.x/255.0, vcolor.y/255.0, vcolor.z/255.0, 1.0f);
        material.specular(1.0f, 1.0f, 1.0f, 0.0f); //white
        material.shininess(100.0f);
  
        OpenGLGeometry geometry;
        gc -> pushMatrix();
        geometry.solidTorus(1.0, 1.8, 10.0, 50.0);
        gc -> popMatrix();
        
      } 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)
    {
      memset(&vcolor, 0, sizeof(vcolor));
      memset(&veye,   0, sizeof(veye));
      memset(&vlight, 0, sizeof(vlight));      
    } 

    ~SimpleView()
    {
    }

    void setColorPosition(int x, int y, int z)
    {
      vcolor.x = (float)x;
      vcolor.y = (float)y;
      vcolor.z = (float)z;
      postRenderRequest();
    }

    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<ColorPositioner> colorPositioner;
  SmartPtr<EyePositioner>   eyePositioner;
  SmartPtr<LightPositioner> lightPositioner;
  
  long size(Event& event)
  {
    int w, h;
    event.getSize(w, h);
    if (view && colorPositioner && 
        eyePositioner && lightPositioner) {
     int posw = colorPositioner->getDefaultWidth();
     int posh = lightPositioner->getDefaultHeight();
    
      view -> reshape(0, 0, w-posw-4, h);
      view -> postResizeRequest(w-posw-4, h);

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

  virtual long colorPositionChanged(Event& event)
  {   
    if (view && colorPositioner) {
      view -> setColorPosition(colorPositioner->getX(), 
                           colorPositioner->getY(),
                           colorPositioner->getZ());
    }
    return 0;
  }
  
  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))
  {
    //1 Create a simple View.
    Args ar;
    view = new SimpleView(this, "", ar);
    
    //2 Create a colorPositioner
    ar.reset();
    colorPositioner = new ColorPositioner(this, "", ar);
    int red   = 0;
    int green = 0;
    int blue  = 0xff;
    colorPositioner->setThumbPosition(red, green, blue);
    view -> setColorPosition(red, green, blue);

    //3 Create an eyePositioner
    ar.reset();
    ar.set(XmNminimum, -40);
    ar.set(XmNmaximum,  40);
    eyePositioner = new EyePositioner(this, "", ar);
 
    int eyeX = 2;
    int eyeY = 8;
    int eyeZ = 30;
    eyePositioner->setThumbPosition(eyeX, eyeY, eyeZ);
    view -> setEyePosition(eyeX, eyeY, eyeZ);

    //4 Create a lightPositioner.
    ar.reset();
    ar.set(XmNminimum, -40);
    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(colorPositioner->getPositionChangedMessage(), this, 
      (Handler)&MainView::colorPositionChanged, NULL);

    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) 
{
  glutInit(&argc, 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.