SOL9 Sample: SphereWithColorAndLightPositioner

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * SphereWithColorAndLightPositioner.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/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/OpenGLSphere.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<OpenGLQuadric>   quadric;
    SmartPtr<OpenGLSphere>    sphere;
 
    Vector3 vcolor;
    Vector3 vlight;
    
  virtual void display()
  {
    if (gc && sphere) { 
      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(2.0, 6.0, 10.0, 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);
        material.shininess(100.0f);
  
        sphere-> draw(1, 40, 40);
        
      } 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 );
    quadric = new OpenGLQuadric();
    quadric -> drawStyle(GLU_FILL);
    quadric -> normals(GLU_SMOOTH);
    sphere = new OpenGLSphere(quadric);
  }

  public:
    SimpleView(View* parent, const char* name, Args& args)
    :OpenGLView(parent, name, args),
    angle(0.0f)
    {
      memset(&vcolor, 0, sizeof(vcolor));
      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 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<LightPositioner> lightPositioner;
  
  long size(Event& event)
  {
    int w, h;
    event.getSize(w, h);
    if (view && colorPositioner && 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);
      lightPositioner->reshape(w-posw-2, 4+posh, posw, posh);
    }
    
    return 1L;
  }

  virtual long colorPositionChanged(Event& event)
  {   
    if (view && colorPositioner) {
      view -> setColorPosition(colorPositioner->getX(), 
                           colorPositioner->getY(),
                           colorPositioner->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();
    colorPositioner = new ColorPositioner(this, "", ar);
    int red   = 0;
    int green = 0;
    int blue  = 0xff;
    colorPositioner->setThumbPosition(red, green, blue);
    view -> setColorPosition(red, green, blue);

    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(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.