VIZ++ Sample: ColoredCube

VIZ++ Samples

1 Screenshot


2 Source code

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


//2017/02/10

#include <viz++/opengl/OpenGLMainView.h>
#include <viz++/opengl/OpenGLGC.h>
//#include <viz++/opengl/OpenGLClientState.h>
#include <viz++/opengl/OpenGLFace.h>

namespace VIZ {

class MainView :public OpenGLMainView {

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();  //2015/07/15
    }
  }
 
  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);
    }
  }


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


public:
  MainView(OpenGLApplet& applet, int width, int height, const char* name)
  :OpenGLMainView(applet, width, height, name)
  {
  }

  ~MainView()
  {
  }
};
}

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

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