SOL9 Sample: ColoredCube

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


#include <sol/opengl/OpenGLMainView.h>
#include <sol/opengl/OpenGLView.h>
#include <sol/opengl/OpenGLGC.h>
#include <sol/opengl/OpenGLClientState.h>
#include <sol/opengl/OpenGLFace.h>
#include "Resource.h"

namespace SOL {

class MainView :public OpenGLMainView {

private:
  //Inner class starts
  class SimpleView: public OpenGLView {
  private:
    SmartPtr<OpenGLGC> gc;
    
  public:
    virtual void display()
    {
      if (gc) {
      //  gc->matrixMode(GL_MODELVIEW);     
        gc->loadIdentity();                 
        gc->translate(-2.0f, 0.0f, -6.0f); 
        gc -> clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        gc->clearColor(0, 0, 0, 0);
 
        gc->begin(GL_QUADS);         

        OpenGLFace top = {
          { 1.0f, 1.0f, -1.0f},
          {-1.0f, 1.0f, -1.0f},
          {-1.0f, 1.0f,  1.0f},
          { 1.0f, 1.0f,  1.0f} };

        OpenGLFace bottom = {
          { 1.0f, -1.0f,  1.0f},
          {-1.0f, -1.0f,  1.0f},
          {-1.0f, -1.0f, -1.0f},
          { 1.0f, -1.0f, -1.0f} };

        OpenGLFace front = {
          { 1.0f,  1.0f, 1.0f},
          {-1.0f,  1.0f, 1.0f},
          {-1.0f, -1.0f, 1.0f},
          { 1.0f, -1.0f, 1.0f} };
 
        OpenGLFace back = {
          { 1.0f, -1.0f, -1.0f},
          {-1.0f, -1.0f, -1.0f},
          {-1.0f,  1.0f, -1.0f},
          { 1.0f,  1.0f, -1.0f} };
 
        OpenGLFace left = {
          {-1.0f,  1.0f,  1.0f},
          {-1.0f,  1.0f, -1.0f},
          {-1.0f, -1.0f, -1.0f},
          {-1.0f, -1.0f,  1.0f} };
 
        OpenGLFace right = {
          { 1.0f,  1.0f, -1.0f},
          { 1.0f,  1.0f,  1.0f},
          { 1.0f, -1.0f,  1.0f},
          { 1.0f, -1.0f, -1.0f} };

        gc->color(0.0f, 1.0f, 0.0f);     
        gc->draw(top);

        gc->color(0.5f, 0.5f, 0.0f);    
        gc->draw(bottom);

        gc->color(1.0f, 0.0f, 0.0f);  
        gc->draw(front);

        gc->color(0.5f, 0.0f, 0.5f); 
        gc->draw(back);

        gc->color(0.0f, 0.0f, 1.0f);  
        gc->draw(left);

        gc->color(0.5f, 0.5f, 1.0f);  
        gc->draw(right);
        
        gc->end(); 
      }
    }
 
    virtual void resize(int w, int h)
    {
      if (w == 0 || h == 0) {
        return;
      }
      if (gc) {
        gc->matrixMode(GL_PROJECTION); 
        gc->loadIdentity();

        gc->perspective(50.0f, (GLfloat)w/(GLfloat)h, 0.1f, 50.0f);
     
        gc -> matrixMode(GL_MODELVIEW);
      }
    }


    virtual void initialize()
    {
      gc = new OpenGLGC();
    }

  public:
    SimpleView(View* parent, const char* name, Args& args)
    :OpenGLView(parent, name, args)
    {
    } 

    ~SimpleView()
    {
    }
  };
  //Inner class ends
  
private:
  SmartPtr<SimpleView>  view;

  long size(Event& event)
  {
    int w, h;
    event.getSize(w, h);
    if (view.notNull()) {
      view -> reshape(10, 10, w-20, h-20);
      view -> postResizeRequest(w-20, h-20);
    }
    return 0L;
  }
  
public:
  MainView(Application& applet, const char* name, Args& args)
  :OpenGLMainView(applet, name, args) 
  {
    Args ar;
    view = new SimpleView(this, "", ar);
  }

  ~MainView()
  {
  }
};
}

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

    Args args;
    args.set(XmNwidth, 400);
    args.set(XmNheight, 300);    
    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.