SOL9 Sample: RenderAndFrameBuffer
|
1 Screenshot
2 Source code
/*
* RenderAndFrameBuffer.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/OpenGLVertex3.h>
#include <sol/opengl/OpenGLClientState.h>
#include <sol/opengl/OpenGLLight.h>
#include <sol/opengl/OpenGLMaterial.h>
#include <sol/opengl2/OpenGLVertexAttribute.h>
#include <sol/openglarb/OpenGLVertexArray.h>
#include <sol/openglarb/OpenGLRenderBuffer.h>
#include <sol/openglarb/OpenGLFrameBuffer.h>
#include <sol/opengl/OpenGLTexturedCube.h>
#include <sol/opengl/OpenGLSphere.h>
#include "Resource.h"
namespace SOL {
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();
}
};
class SimpleView: public OpenGLView {
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) {
ex.display();
}
}
public:
SimpleView(View* parent, const char* name, Args& args)
:OpenGLView(parent, name, args),
angle(2.5f)
{
}
~SimpleView()
{
}
};
//Inner class ends
private:
SmartPtr<SimpleView> view;
long size(Event& event)
{
int w, h;
event.getSize(w, h);
if (view) {
view -> reshape(10, 10, w-20, h-20);
view -> postResizeRequest(w-20, h-20);
}
return 1L;
}
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, 480);
args.set(XmNheight, 480);
MainView view(applet, name, args);
view.realize();
applet.run();
} catch (Exception& ex) {
ex.display();
}
}
Last modified: 8 Dec 2016
Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.