--- /dev/null
+// Copyright (C) 2006 Erik Dahlberg
+//
+// This file is part of LSystem3d.
+//
+// LSystem3D is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// LSystem3D is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with LSystem3D; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+
+
+
+#include <GL/gl.h>
+#include <GL/glu.h>
+
+#include "fx.h"
+#include "fx3d.h"
+
+#include "renderingsurface.h"
+
+
+
+// message map
+FXDEFMAP(RenderingSurface) RenderingSurfaceMap[] =
+{
+ FXMAPFUNC(SEL_LEFTBUTTONPRESS, RenderingSurface::ID_CANVAS, RenderingSurface::onLeftMouseDown),
+ FXMAPFUNC(SEL_LEFTBUTTONRELEASE, RenderingSurface::ID_CANVAS, RenderingSurface::onLeftMouseUp),
+ FXMAPFUNC(SEL_MIDDLEBUTTONPRESS, RenderingSurface::ID_CANVAS, RenderingSurface::onMiddleMouseDown),
+ FXMAPFUNC(SEL_MIDDLEBUTTONRELEASE, RenderingSurface::ID_CANVAS, RenderingSurface::onMiddleMouseUp),
+ FXMAPFUNC(SEL_RIGHTBUTTONPRESS, RenderingSurface::ID_CANVAS, RenderingSurface::onRightMouseDown),
+ FXMAPFUNC(SEL_RIGHTBUTTONRELEASE, RenderingSurface::ID_CANVAS, RenderingSurface::onRightMouseUp),
+ FXMAPFUNC(SEL_MOTION, RenderingSurface::ID_CANVAS, RenderingSurface::onMouseMove),
+ FXMAPFUNC(SEL_PAINT, RenderingSurface::ID_CANVAS, RenderingSurface::onRepaint)
+};
+
+// macro to set up class implementation
+FXIMPLEMENT(RenderingSurface, FXMainWindow, RenderingSurfaceMap, ARRAYNUMBER(RenderingSurfaceMap))
+
+
+
+/**
+ * Constructor
+ * @param application the FOX application object
+ * @param lsystem the Lindenmayer-system
+ */
+RenderingSurface::RenderingSurface(FXApp *application, LindenmayerSystem *lsystem) : FXMainWindow(application, "LSystem3D", NULL, NULL, DECOR_ALL, 100, 100, 800, 800)
+{
+ _lsystem = lsystem;
+
+ _modelX = 0.0;
+ _modelY = 0.0;
+ _modelZ = -10.0;
+
+ _modelRotationY = 0.0;
+
+ _leftMouseDown = _rightMouseDown = _middleMouseDown = false;
+
+
+ // pixel format info
+ _visual = new FXGLVisual(getApp(), VISUAL_DOUBLEBUFFER);
+
+ // OpenGL drawing area
+ FXVerticalFrame *mainFrame = new FXVerticalFrame(this, LAYOUT_FILL_X | LAYOUT_FILL_Y);
+ _canvas = new FXGLCanvas(mainFrame, _visual, this, ID_CANVAS, LAYOUT_FILL_X | LAYOUT_FILL_Y);
+}
+
+
+
+/**
+ * Create and initialize the window
+ */
+void RenderingSurface::create()
+{
+ // create the window
+ FXMainWindow::create();
+
+ // set up OpenGL
+ _canvas->makeCurrent();
+ initOpenGL();
+
+ // make the window appear
+ show();
+}
+
+
+
+/**
+ * Initialize OpenGL
+ */
+void RenderingSurface::initOpenGL()
+{
+ // projection matrix
+ glMatrixMode(GL_PROJECTION);
+ glLoadIdentity();
+ gluPerspective(65, 1, 1, 500);
+
+ // modelview matrix
+ glMatrixMode(GL_MODELVIEW);
+ glLoadIdentity();
+
+
+ // global ambient light
+ GLfloat ambientLight[] = {0.7, 0.7, 0.7, 1.0};
+ glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
+
+ // diffuse light
+ GLfloat lightPosition[] = {150.0, 150.0, 0.0, 0.0};
+ GLfloat whiteLight[] = {1.0, 1.0, 1.0, 1.0};
+ glLightfv(GL_LIGHT0, GL_DIFFUSE, whiteLight);
+ glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
+
+
+ glEnable(GL_COLOR_MATERIAL);
+ glEnable(GL_LIGHTING);
+ glEnable(GL_LIGHT0);
+ glEnable(GL_DEPTH_TEST);
+}
+
+
+
+/**
+ * Force an update of the rendering surface
+ */
+void RenderingSurface::draw()
+{
+ _canvas->update();
+}
+
+
+
+/**
+ * Called by the system when the left mouse button is pressed
+ * @param sender the sender object
+ * @param selector message type and id
+ * @param data event related data
+ * @return
+ */
+long RenderingSurface::onLeftMouseDown(FXObject *sender, FXSelector selector, void *data)
+{
+ _leftMouseDown = true;
+}
+
+
+
+/**
+ * Called by the system when the left mouse button is released
+ * @param sender the sender object
+ * @param selector message type and id
+ * @param data event related data
+ * @return
+ */
+long RenderingSurface::onLeftMouseUp(FXObject *sender, FXSelector selector, void *data)
+{
+ _leftMouseDown = false;
+}
+
+
+
+/**
+ * Called by the system when the middle mouse button is pressed
+ * @param sender the sender object
+ * @param selector message type and id
+ * @param data event related data
+ * @return
+ */
+long RenderingSurface::onMiddleMouseDown(FXObject *sender, FXSelector selector, void *data)
+{
+ _middleMouseDown = true;
+}
+
+
+
+/**
+ * Called by the system when the middle mouse button is released
+ * @param sender the sender object
+ * @param selector message type and id
+ * @param data event related data
+ * @return
+ */
+long RenderingSurface::onMiddleMouseUp(FXObject *sender, FXSelector selector, void *data)
+{
+ _middleMouseDown = false;
+}
+
+
+
+/**
+ * Called by the system when the right mouse button is pressed
+ * @param sender the sender object
+ * @param selector message type and id
+ * @param data event related data
+ * @return
+ */
+long RenderingSurface::onRightMouseDown(FXObject *sender, FXSelector selector, void *data)
+{
+ _rightMouseDown = true;
+}
+
+
+
+/**
+ * Called by the system when the right mouse button is released
+ * @param sender the sender object
+ * @param selector message type and id
+ * @param data event related data
+ * @return
+ */
+long RenderingSurface::onRightMouseUp(FXObject *sender, FXSelector selector, void *data)
+{
+ _rightMouseDown = false;
+}
+
+
+
+/**
+ * Called by the system when the mouse is moved
+ * @param sender the sender object
+ * @param selector message type and id
+ * @param data event related data
+ * @return
+ */
+long RenderingSurface::onMouseMove(FXObject *sender, FXSelector selector, void *data)
+{
+ // check status of the mouse buttons and move model
+
+ // left mouse button
+ if (_leftMouseDown)
+ {
+ // move in z-plane
+
+ FXEvent *event = (FXEvent*)data;
+
+ // TODO: "gluPerspective(65, 1, 1, 500)" -> "translateFactor = 610.0 / (-_modelZ)", ugly...
+ double translateFactor = 610.0 / (-_modelZ);
+
+ _modelX += (event->win_x - event->last_x) / translateFactor;
+ _modelY += -(event->win_y - event->last_y) / translateFactor;
+
+ _canvas->update();
+ }
+
+ // middle mouse button
+ if (_middleMouseDown)
+ {
+ // zoom in/out
+
+ FXEvent *event = (FXEvent*)data;
+
+ _modelZ += -(event->win_y - event->last_y) / 10.0;
+
+ // stop zooming at -1.0
+ if (_modelZ >= -1.0)
+ {
+ _modelZ = -1.0;
+ }
+
+ _canvas->update();
+ }
+
+ // right mouse button
+ if (_rightMouseDown)
+ {
+ // rotate around y-axis
+
+ FXEvent *event = (FXEvent*)data;
+
+ _modelRotationY = ((int)_modelRotationY + event->win_x - event->last_x) % 360;
+
+ _canvas->update();
+ }
+}
+
+
+
+/**
+ * Called by the system when the rendering surface needs a repaint
+ * @param sender the sender object
+ * @param selector message type and id
+ * @param data event related data
+ * @return
+ */
+long RenderingSurface::onRepaint(FXObject *sender, FXSelector selector, void *data)
+{
+ glMatrixMode (GL_MODELVIEW);
+ glLoadIdentity();
+
+
+ // clear scene
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+ // position and orientation of model
+ glTranslatef(_modelX, _modelY, _modelZ);
+ glRotatef(_modelRotationY, 0.0, 1.0, 0.0);
+
+ // render model
+ _lsystem->getModel()->draw();
+
+
+ // make it appear
+ _canvas->swapBuffers();
+}