SOL9 Sample: CubeWithColorEyeAndLightPositioner
|
1 Screenshot
2 Source code
/*
* CubeWithColorEyeAndLightPositioner.cpp
* Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
#include <sol/Stdio.h>
#include <sol/Vector3.h>
#include <sol/ColorPositioner.h>
#include <sol/EyePositioner.h>
#include <sol/LightPositioner.h>
#include <sol/opengl/OpenGLMainView.h>
#include <sol/opengl/OpenGLView.h>
#include <sol/opengl/OpenGLGC.h>
#include <sol/opengl/OpenGLLight.h>
#include <sol/opengl/OpenGLMaterial.h>
#include <sol/opengl/Vertex3.h>
#include <sol/opengl/Color4.h>
#include <sol/opengl/FaceIndices.h>
#include <sol/opengl/OpenGLClientState.h>
#include <sol/opengl/OpenGLQuadSurfaces.h>
#include "Resource.h"
namespace SOL {
class MainView :public OpenGLMainView {
private:
//Inner class starts.
class SimpleView: public OpenGLView {
private:
SmartPtr<OpenGLGC> gc;
float angle;
SmartPtr<OpenGLQuadSurfaces> surfaces;
SmartPtr<Normal3> normals;
size_t numNormals;
Vector3 vcolor;
Vector3 veye;
Vector3 vlight;
virtual void display()
{
static Vertex<3> vertices[] = {
{0.0, 0.0, 0.0},
{1.0, 0.0, 0.0},
{1.0, 1.0, 0.0},
{0.0, 1.0, 0.0},
{0.0, 0.0,-1.0},
{1.0, 0.0,-1.0},
{1.0, 1.0,-1.0},
{0.0, 1.0,-1.0},
};
static FaceIndices<4> indices[] = {
{0, 1, 2, 3},
{1, 5, 6, 2},
{5, 4, 7, 6},
{4, 0, 3, 7},
{4, 5, 1, 0},
{3, 2, 6, 7}
};
if (surfaces == NULL) {
surfaces = new OpenGLQuadSurfaces(vertices, CountOf(vertices), indices, CountOf(indices));
//Printf("calculateSurfaceNormals");
normals = surfaces->calculateSurfaceNormals(numNormals);
surfaces->displayNormals();
}
if (gc && normals) {
gc -> frustum(1 , -1 , -1 , 1 , 1 , 10);
gc -> clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gc -> loadIdentity();
gc -> matrixMode(GL_MODELVIEW);
gc -> clearColor(0.0, 1.0, 1.0, 1.0);
gc -> translate(-0.5f, -0.5f, 0.0f);
gc -> lookAt(veye.x, veye.y, veye.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
gc -> rotate(0.0, 0.0, 1.0, 0.0);
try {
gc -> enable(GL_LIGHTING);
OpenGLLight light(GL_LIGHT0);
light.position(vlight.x, vlight.y, vlight.z, 0.0);
OpenGLMaterial material(GL_FRONT);
material.diffuse (vcolor.x/255.0, vcolor.y/255.0, vcolor.z/255.0, 0.0f);
material.specular(1.0f, 1.0f, 1.0f, 0.0f);
material.shininess(100.0f);
gc->frontFace(GL_CCW);
gc->enable(GL_CULL_FACE);
gc->cullFace(GL_BACK);
glEnable(GL_NORMALIZE);
for (int i = 0; i< CountOf(indices); i++) {
gc->begin(GL_QUADS);
FaceIndices<4> quad = indices[i];
gc->draw( normals[i],
vertices[ quad.index[0] ],
vertices[ quad.index[1] ],
vertices[ quad.index[2] ],
vertices[ quad.index[3] ]
);
gc->flush();
gc ->end();
}
} catch (Exception& ex) {
caught(ex);
}
}
}
virtual void resize(int w, int h)
{
if (w == 0 || h == 0) {
return;
}
if (gc) {
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();
gc -> enable( GL_DEPTH_TEST );
}
public:
SimpleView(View* parent, const char* name, Args& args)
:OpenGLView(parent, name, args),
angle(0.0f),
numNormals(0)
{
memset(&vcolor, 0, sizeof(vcolor));
memset(&veye, 0, sizeof(veye));
memset(&vlight, 0, sizeof(vlight));
}
~SimpleView()
{
}
void setColorPosition(int x, int y, int z)
{
vcolor.x = (float)x;
vcolor.y = (float)y;
vcolor. z= (float)z;
postRenderRequest();
}
void setEyePosition(int x, int y, int z)
{
veye.x = (float)x;
veye.y = (float)y;
veye.z= (float)z;
postRenderRequest();
}
void setLightPosition(int x, int y, int z)
{
vlight.x = (float)x;
vlight.y = (float)y;
vlight.z= (float)z;
postRenderRequest();
}
};
//Inner class ends.
private:
SmartPtr<SimpleView> view;
SmartPtr<ColorPositioner> colorPositioner;
SmartPtr<EyePositioner> eyePositioner;
SmartPtr<LightPositioner> lightPositioner;
long size(Event& event)
{
int w, h;
event.getSize(w, h);
int posw = colorPositioner->getDefaultWidth();
int posh = lightPositioner->getDefaultHeight();
if (view.notNull() && colorPositioner.notNull() &&
eyePositioner.notNull() && lightPositioner.notNull()) {
view -> reshape(0, 0, w-posw-4, h);
view -> postResizeRequest(w-posw-4, h);
colorPositioner->reshape(w-posw-2, 2, posw, posh);
eyePositioner ->reshape(w-posw-2, 4+posh, posw, posh);
lightPositioner->reshape(w-posw-2, 6+posh*2, posw, posh);
}
return 1L;
}
virtual long colorPositionChanged(Event& event)
{
if (view.notNull() && colorPositioner.notNull()) {
view -> setColorPosition(colorPositioner->getX(),
colorPositioner->getY(),
colorPositioner->getZ());
}
return 0;
}
virtual long eyePositionChanged(Event& event)
{
if (view.notNull() && eyePositioner.notNull()) {
view -> setEyePosition(eyePositioner->getX(),
eyePositioner->getY(),
eyePositioner->getZ());
}
return 0;
}
virtual long lightPositionChanged(Event& event)
{
if (view.notNull() && lightPositioner.notNull()) {
view -> setLightPosition(lightPositioner->getX(),
lightPositioner->getY(),
lightPositioner->getZ());
}
return 0;
}
public:
MainView(Application& applet, const char* name, Args& args)
:OpenGLMainView(applet, name, args.set(XmNstyle, WS_CLIPCHILDREN|WS_CLIPSIBLINGS))
{
//1 SimpleView
Args ar;
view = new SimpleView(this, "", ar);
//2 ColorPositioner
ar.reset();
colorPositioner = new ColorPositioner(this, "", ar);
int red = 0;
int green = 0;
int blue = 0xff;
colorPositioner->setThumbPosition(red, green, blue);
view -> setColorPosition(red, green, blue);
//3 Create EyePositioner
ar.reset();
ar.set(XmNminimum, 0);
ar.set(XmNmaximum, 40);
eyePositioner = new EyePositioner(this, "", ar);
int eyeX = 2;
int eyeY = 6;
int eyeZ = 12;
eyePositioner->setThumbPosition(eyeX, eyeY, eyeZ);
view -> setEyePosition(eyeX, eyeY, eyeZ);
//4 Create LightPositioner
ar.reset();
ar.set(XmNminimum, 0);
ar.set(XmNmaximum, 40);
lightPositioner = new LightPositioner(this, "", ar);
int lightX = 0;
int lightY = 6;
int lightZ = 2;
lightPositioner->setThumbPosition(lightX, lightY, lightZ);
view -> setLightPosition(lightX, lightY, lightZ);
addEventHandler(colorPositioner->getPositionChangedMessage(), this,
(Handler)&MainView::colorPositionChanged, NULL);
addEventHandler(eyePositioner->getPositionChangedMessage(), this,
(Handler)&MainView::eyePositionChanged, NULL);
addEventHandler(lightPositioner->getPositionChangedMessage(), this,
(Handler)&MainView::lightPositionChanged, NULL);
view -> postRenderRequest();
}
~MainView()
{
}
};
}
//
void Main(int argc, char** argv)
{
try {
const char* name = appName(argv[0]);
Application applet(name, argc, argv);
Args args;
args.set(XmNwidth, 640);
args.set(XmNheight, 480);
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.