VIZ++ Sample: RenderAndFrameBuffer

VIZ++ Samples

1 Screenshot


2 Source code

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


#include <viz++/opengl/OpenGLMainView.h>
#include <viz++/opengl/OpenGLGC.h>
#include <viz++/opengl/OpenGLVertex3.h>
#include <viz++/opengl/OpenGLClientState.h>
#include <viz++/opengl/OpenGLLight.h>
#include <viz++/opengl/OpenGLMaterial.h>

#include <viz++/opengl2/OpenGLVertexAttribute.h>
#include <viz++/openglarb/OpenGLVertexArray.h>
#include <viz++/openglarb/OpenGLRenderBuffer.h>
#include <viz++/openglarb/OpenGLFrameBuffer.h>
#include <viz++/opengl/OpenGLTexturedCube.h>
#include <viz++/opengl/OpenGLSphere.h>

#include "Resource.h"

namespace VIZ {

class MainView :public OpenGLMainView {

private:
  //Inner class starts.
  class EmptyTexturedCube :public OpenGLTexturedCube {
  public:
    EmptyTexturedCube(GLuint width, GLuint height)
    :OpenGLTexturedCube()
    {
      bind();

      pixelStore(GL_UNPACK_ALIGNMENT, 1);
      parameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR );
      parameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR );
      env(GL_TEXTURE_ENV_MODE, GL_DECAL );

      parameter(GL_TEXTURE_WRAP_S, GL_REPEAT);
      parameter(GL_TEXTURE_WRAP_T, GL_REPEAT);
      image(0, GL_RGBA8, width, height,
                   0, GL_RGBA, GL_UNSIGNED_BYTE, 0 );
  
      unbind();
    }
  };
  //Inner class ends.
  
private:
    SmartPtr<OpenGLGC>             gc;
    SmartPtr<OpenGLQuadric>        quadric;
    SmartPtr<OpenGLSphere>         sphere;
    
    SmartPtr<EmptyTexturedCube>    cube;
    SmartPtr<OpenGLRenderBuffer>   renderBuffer;
    SmartPtr<OpenGLFrameBuffer>    frameBuffer;
    SmartPtr<OpenGLLight>          light;
    SmartPtr<OpenGLMaterial>       material;
    
    GLfloat     angle;
    
    static const int WIDTH  = 256;
    static const int HEIGHT = 256;
    
private:
    
  void drawSphere(OpenGLGC* gc)
  {
    gc -> pushMatrix();

    GLint viewport[4];
    gc -> getViewport(CountOf(viewport), viewport);
   
    gc -> clear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    gc -> viewport( 0, 0, WIDTH, HEIGHT );
    
    gc -> matrixMode( GL_PROJECTION );
    gc -> loadIdentity();
    gc -> perspective(2.0, WIDTH / (double)HEIGHT, 1.0, 300.0 );
    gc -> matrixMode(GL_MODELVIEW );
    
    gc -> loadIdentity();
    gc -> lookAt( 8.0, 30.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );
    
    gc -> pushMatrix();
    
      gc -> scale( -1.0f, 1.0f, 1.0f );
      renderBuffer -> bind();
      frameBuffer  -> bind();
      sphere-> draw(1, 40, 40);

      renderBuffer -> unbind();
      frameBuffer  -> unbind();

    gc -> popMatrix();

    gc ->flush();
    
    gc -> viewport(CountOf(viewport), viewport); 

    gc -> popMatrix();
  }
    
public:
  virtual void display()
  {
    if (gc && sphere && cube) {
      drawSphere(gc);
      gc -> matrixMode( GL_PROJECTION );

      gc -> clearColor(0.7f, 0.7f, 0.7f, 1.0f);
      gc -> clear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
      gc -> matrixMode( GL_MODELVIEW );
      gc -> loadIdentity();
      gc -> lookAt( 10.0, 40.0, 150.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );

      gc -> color( 1.0f, 1.0f, 1.0f );
      gc -> enable(GL_TEXTURE_2D );
      gc -> enable(GL_COLOR_MATERIAL );
      
      cube -> bind();
      cube -> env(GL_TEXTURE_ENV_MODE, GL_MODULATE );
      gc -> pushMatrix();
      gc -> rotate( angle , 1.0f, 0.0f, 0.0f );

      cube -> draw(gc);
      
      gc -> popMatrix();
   
      gc ->disable( GL_TEXTURE_2D );
      cube -> unbind();
      
      gc -> disable(GL_COLOR_MATERIAL);
    }
  }
 
  virtual void resize(int w, int h)
  {
    if (w == 0 || h == 0) {
      return;
    }
    if (gc) {
      gc -> matrixMode(GL_PROJECTION);
      gc -> loadIdentity();
      gc -> perspective(1.0, (double)w / (double)h, 1.0, 300.0); 

      gc -> matrixMode(GL_MODELVIEW);
    }
  }

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

    gc -> matrixMode(GL_PROJECTION);
    gc -> loadIdentity();

    gc -> enable( GL_DEPTH_TEST );
    gc -> frustum(1 , -1 , -1 , 1 , 1 , 10);
    gc -> clearColor(0.5, 0.5, 0.5, 1.0);
    try {
      
      quadric = new OpenGLQuadric();
      quadric -> drawStyle(GLU_FILL);
      sphere = new OpenGLSphere(quadric);
      
      cube = new EmptyTexturedCube(WIDTH, HEIGHT);
 
      renderBuffer = new OpenGLRenderBuffer();
      renderBuffer -> bind();
      renderBuffer -> storage(GL_DEPTH_COMPONENT,
                               WIDTH, HEIGHT);
      
      frameBuffer  = new OpenGLFrameBuffer();
      frameBuffer -> bind();
      frameBuffer -> texture2D(GL_COLOR_ATTACHMENT0_EXT,
                        GL_TEXTURE_2D, *cube, NULL);
  
      frameBuffer -> attachRenderbuffer(GL_DEPTH_ATTACHMENT_EXT,
                       GL_RENDERBUFFER, *renderBuffer );
 
      renderBuffer -> unbind();
      
      frameBuffer -> unbind();
      
      light = new OpenGLLight();
      light -> position(100.0f, 150.0f, 200.0f, 1.0f);
      light -> ambient (  1.0f,   0.5f,   0.2f, 1.0f);

      light -> diffuse (  0.5f,   0.5f,   1.0f, 1.0f);

      light -> specular(  1.0f,   0.5f,   1.0f, 1.0f);
 
      material = new OpenGLMaterial(GL_FRONT);
      material -> ambient  (  0.3f, 0.3f, 0.1f, 1.0f );
      
      material -> diffuse  (  0.3f, 0.5f, 0.4f, 1.0f );
      material -> specular (  1.0f, 0.5f, 1.0f, 1.0f );
      material -> shininess( 100.0f );
     
    } catch (Exception& ex) {
      caught(ex);
    }
  }


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