VIZ++ Sample: FileSave

VIZ++ Samples

1 Screenshot


2 Source code

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


//2015/08/22 On MouseRightButton clicked,  
// 1 Show OpenGLFileSaveDialog
// 2 Let select a jpg filename.
// 3 Save an image data of OpenGLMainView to the jpg file.
//2017/02/10 Updated.

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

#include <viz++/opengl/OpenGLGC.h>
#include <viz++/opengl/OpenGLLight.h>
#include <viz++/opengl/OpenGLMaterial.h>
#include <viz++/opengl/OpenGLSphere.h>
#include <viz++/opengl/OpenGLBitmap.h>
#include <viz++/opengl/OpenGLFileSaveDialog.h>

namespace VIZ {

class MainView :public OpenGLMainView {

private:
  SmartPtr<OpenGLGC>      gc;
  SmartPtr<OpenGLQuadric> quadric;
  SmartPtr<OpenGLSphere>  sphere;
  int width;
  int height;
  bool saved;

public:
  virtual void display()
  {
    if (gc && sphere) {
       gc -> loadIdentity();
       gc -> clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

       gc -> lookAt(2.0, 4.0, 8.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

       gc -> clearColor(0.0, 0.0, 0.0, 1.0);
       gc -> enable(GL_CULL_FACE); 
       gc -> enable(GL_LIGHTING);

       OpenGLLight light(GL_LIGHT0);
       GLfloat lightPosition[] = {10, 10, 10, 1.0};  
       light.position(lightPosition);

       GLfloat white[] = {1.0f, 1.0f, 1.0f, 1.0f};
       GLfloat blue[]  = {0.0f, 0.0f, 1.0f, 1.0f};
       GLfloat shininess[] = {100.0};

       OpenGLMaterial material(GL_FRONT);
       material.diffuse(blue);
       material.specular(white);
       material.shininess(shininess);

       sphere-> draw(1, 40, 40);
    }
  }
  

  virtual void resize(int w, int h)
  {
    if (w == 0 || h == 0) {
          return;
    }
    if (gc) {
       width  = w;
       height = h;
       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();
      
    quadric = new OpenGLQuadric();
    quadric -> drawStyle(GLU_FILL);
    quadric -> normals(GLU_SMOOTH);
    sphere = new OpenGLSphere(quadric);
  }

    
  virtual void mouseButtonCallback(int button, int action, int mods)
  {
     if (button ==RIGHT_BUTTON && action == BUTTON_DOWN) {
      saveImage();
     }
  }
  
  void saveImage()
  {
    StringT<char> folderName;
    getModuleFolderName(folderName);
    printf("moduleFolder %s\n", (const char*)folderName);

    OpenGLFileSaveDialog dialog(this, 
                         (const char*)folderName,
                         "JPG Files (*.jpg)\0*.jpg\0All Files (*.*)\0*.*\0", 
                         "jpg");
       
    if (dialog.popup() ) {
      StringT<char> name;
      dialog.getFileName(name);    
      const char* filename = (const char*)name;
      int rw = (width/8)*8;
      OpenGLBitmap bitmap(0, 0, rw, height, 24, GL_RGB);
      //OpenGLBitmap bitmap(0, 0, width, height, 8, GL_RGBA);
      try {
        //const char* filename = ".\\test.jpg";
        bitmap.read(GL_FRONT);
        bitmap.saveAsJPG(filename);
        //bitmap.saveAsPNG(".\\test.png");
        printf("Saved a bitmap image of current OpenGLMainView to %s\n", filename); 
      } catch (Exception& ex) {
        caught(ex);
      }
    }
  }

  //Please define your own method in your subclass derived from this class.
  virtual void menuCallback(WORD menuId)
  {
    HWND hwnd = getHwnd();
    char message[1024];
    sprintf_s(message, CountOf(message), "Default menu callback. MenuId(%d)", menuId);
    switch (menuId) {
    case IDM_SAVE:
    case IDM_SAVEAS:
      {
        saveImage();
      }
      break;
      
    case IDM_EXIT:
      {
        int rc = MessageBox(hwnd, "Are you sure to want to exit this program?", "Confirmation",  MB_OKCANCEL|MB_ICONINFORMATION);
        if (rc == IDOK) {
          ::PostQuitMessage(0);
        }
      }
      break;
      
    case IDM_VERSION:
      MessageBox(hwnd, "VIZ++  Copyright(C) Antillia.com 2017", "Version",  MB_OK|MB_ICONINFORMATION);
      break;
      
    default:
      MessageBox(hwnd, message, "MenuCallback", MB_OK|MB_ICONINFORMATION);
    }
  }
  
public:
  MainView(OpenGLApplet& applet, int width, int height, const char* name)
  :OpenGLMainView(applet, width, height, name),
  saved(false)
  {
    enableMouseButtonCallback();
  }
  
  ~MainView()
  {
  }
};

}

//
int main(int argc, char** argv) 
{
  try {
    ModuleFileName module;
    const char* name = module.getAppName();
    OpenGLApplet applet(argc, argv);

    MainView view(applet, 480, 480, 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.