JNR
stageModelViewer.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 #include "stdafx.h"
31 #include "laStagesJR.h"
32 
33 double dViewAngle = 0;
34 double dZoom = 1;
35 double dOfsX=0, dOfsY=0;
36 
37 class uiWindow_Receiver: public uiWindow{
38 public:
39 
40  //Command event
41  void onCommand(unsigned long uID, unsigned long nCmd)
42  {
43  //MSG("dff");
44  laTimer* pt = ::laSystemIntegrator::getEnvironment()->getTimer();
45  switch(uID)
46  {
47  case 1: break;
48  case 2: dViewAngle += pt->delta()*100; break;
49  case 3: dViewAngle -= pt->delta()*100; break;
50  case 4: dZoom -= pt->delta()*1; break;
51  case 5: dZoom += pt->delta()*1; break;
52  }
53  }
54 
55 }wReceiver;
56 
57 stageModelViewer::stageModelViewer(void)
58 {
59  _pModel = NULL;
60 }
61 
62 stageModelViewer::~stageModelViewer(void)
63 {
64 }
65 
66 void stageModelViewer::onInit()
67 {
68  // Create GUI
69  //
70  _uiDesktop.create("ui\\desktop-mm.dsk");
71 
72  // Add "Proceed..." button
73  //
74  uiButton* pb = new uiButton();
75  pb->create( laPoint3(10, 10), laPoint3(150, 40));
76  pb->setText("Load Another Model...");
77  pb->setReceiver(&wReceiver);
78  pb->setID(1);
79  _uiDesktop.insert( pb );
80 
81  // Add "<< >>" buttons
82  //
83  uiButton* pb1 = new uiButton();
84  pb1->create( laPoint3(), laPoint3(50, 50));
85  pb1->setText("<<");
86  pb1->setReceiver(&wReceiver);
87  pb1->setID(2);
88  pb1->toggleRepetitive(M_TRUE);
89  _uiDesktop.insert( pb1 );
90  pb1->move( laPoint3(40, 0), M_AL, M_AM);
91 
92  uiButton* pb2 = new uiButton();
93  pb2->create( laPoint3(), laPoint3(50, 50));
94  pb2->setText(">>");
95  pb2->setReceiver(&wReceiver);
96  pb2->toggleRepetitive(M_TRUE);
97  pb2->setID(3);
98  _uiDesktop.insert( pb2 );
99  pb2->move( laPoint3(20, 0), M_AR, M_AM);
100 
101  // Add "+ -" zoom buttons
102  //
103  uiButton* pb3 = new uiButton();
104  pb3->create( laPoint3(), laPoint3(40, 40));
105  pb3->setText("+");
106  pb3->setReceiver(&wReceiver);
107  pb3->setID(4);
108  pb3->toggleRepetitive(M_TRUE);
109  _uiDesktop.insert( pb3 );
110  pb3->move( laPoint3(70, 20), M_AR, M_AT);
111 
112  uiButton* pb4 = new uiButton();
113  pb4->create( laPoint3(), laPoint3(40, 40));
114  pb4->setText("-");
115  pb4->setReceiver(&wReceiver);
116  pb4->toggleRepetitive(M_TRUE);
117  pb4->setID(5);
118  _uiDesktop.insert( pb4 );
119  pb4->move( laPoint3(20, 20), M_AR, M_AT);
120 
121 
122  // Add drag label
123  //
124  uiLabel* l = new uiLabel();
125  l->create( laPoint3(), laPoint3(100, 40));
126  l->setText("[Right-Click and Drag to Pan View]");
127  l->size(15);
128  l->align(M_AR, M_AM);
129  _uiDesktop.insert( l );
130  l->move( laPoint3(10, 10), M_AR, M_AB);
131 
132 }
133 
134 void stageModelViewer::onFini()
135 {
136 }
137 
138 void stageModelViewer::onActivate()
139 {
140  laSystemIntegrator::getEnvironment()->setTitle("[Model Viewer] by Atanas Laskov");
141 
142  _pModel = new laAnimatedModel();
143  _pModel->load("ron_a.jra");
144  //_pModel->edNormalize();
145  //_pModel->edScale( laPoint3(M_UNIT,M_UNIT,M_UNIT));
146 }
147 
148 void stageModelViewer::onDeactivate()
149 {
150 }
151 
152 void stageModelViewer::onFrame(laRenderer *pr, laInputManager *pi, laTimer *pt)
153 {
154  // Draw bg
155  //
156  pr->modeInterface();
157  _uiDesktop.drawBackground();
158 
159  // Draw model
160  //
161  pr->modeWorld( laPoint3( dOfsX, dOfsY-1*M_UNIT, M_UNIT*2*dZoom ), laPoint3(10, 0) );
162  pr->transRotate( dViewAngle, laPoint3(0,1,0) );
163 
164  //if(_nVisible==1) pr->styleSet( laColor(255, 255, 255, alpha) );
165  /*else*/ pr->styleSet( laColor(255, 255, 255, 255) );
166 
167  pr->modeLight(M_TRUE);
168  pr->lightAmbient(0, laColor(100,100,100,255) );
169  pr->lightMakeSunlight(0, laPoint3( 0, 0, -M_UNIT*10 ) );
170 
171  _pModel->draw( pr );
172  _pModel->animate(*pt);
173 
174  pr->styleSet( laColor(255,0,0) );
175  pr->styleLine( 3 );
176 
177 
178  //pr->modeDepthBuffer(M_FALSE);
179 
180  for(unsigned i=0; i<_pModel->framePtr()->vertexCount(); i++)
181  {
182  laPoint3 pt = _pModel->sample(i);
183 
184  pr->transPush();
185  pr->transTranslate(pt);
186  pr->transRotate(-dViewAngle, laPoint3(0,1,0));
187  pr->font("plain")->ctlSize(M_UNIT*0.015);
188  pr->font("plain")->draw(laPoint3(), "%d", i);
189  pr->transPop();
190 
191  //pr->drawRect(pt, laPoint3(M_UNIT*0.05, M_UNIT*0.05), laPoint2(), laPoint2());
192  //pr->drawLine(pt - laPoint3(M_UNIT*0.01, 0), pt + laPoint3(M_UNIT*0.01, 0));
193  //pr->drawLine(pt - laPoint3(0, M_UNIT*0.01), pt + laPoint3(0, M_UNIT*0.01));
194  //pr->drawLine(pt - laPoint3(0, 0, M_UNIT*0.01), pt + laPoint3(0, 0, M_UNIT*0.01));
195  }
196 
197  // Weapon Hook
198  //
199  /*static laAnimatedModel* pw = NULL;
200 
201  if(!pw)
202  {
203  pw = new laAnimatedModel();
204  pw->load("potion.jrm");
205  }
206 
207  laModelHook hook(670, 672);
208  hook.sample( _pModel );
209 
210  pr->transPush();
211  laPoint3 y_axis(0,1,0);
212  laPoint3 rot_axis = y_axis.cross(hook.vector);
213 
214  pr->transTranslate( hook.arPointSample[0] );
215  pr->transRotate( M_R2D(hook.ay)-180, rot_axis );
216  pw->draw( laPoint3() );
217  pr->transPop();*/
218 
219  //Pivot
220  pr->modeTexture(M_FALSE);
221  pr->drawLine(laPoint3(M_UNIT*0.1, 0),laPoint3(M_UNIT*0.1, 0));
222  pr->drawLine(laPoint3(0, M_UNIT*0.1), laPoint3(0, M_UNIT*0.1));
223  pr->drawLine(laPoint3(0, 0, M_UNIT*0.1), laPoint3(0, 0, M_UNIT*0.1));
224 
225  // Draw interface
226  //
227  pr->modeInterface();
228  //_uiDesktop.drawBackground();
229  _uiDesktop.reply();
230  _uiDesktop.draw();
231 
232  //static double mx = 0;
233  //static double my = 0;
234 
235  if(in::mouse_b0())
236  {
237  dOfsX -= (in::mouse_ofsx())*M_UNIT*0.1*pt->delta();
238  dOfsY -= (in::mouse_ofsy())*M_UNIT*0.1*pt->delta();
239  }
240 }
laFont * font(char *strName)
Get a font renderer.
Definition: laRenderer.h:192
virtual void create(laPoint3 pos, laPoint3 size)
Create new empty window.
Definition: uiButton.cpp:55
virtual void modeTexture(M_BOOL bOn)=0
Enable/disable texturing.
#define M_AB
Text align bottom.
#define M_UNIT
Unit of 1 meter.
virtual void move(laPoint3 ptNewPos, unsigned nHReference=M_AL, unsigned nVReference=M_AT)
Change window position.
Definition: uiWindow.cpp:51
virtual void create(laPoint3 pos, laPoint3 size)
Create new empty window.
Definition: uiLabel.cpp:52
#define M_AM
Text align middle.
Animated 3D Model.
#define M_AL
Text align left.
#define M_AT
Text align top.
#define M_AR
Text align right.
GUI Push-button.
Definition: uiButton.h:41
void setReceiver(uiWindow *pWnd)
Set receiver window (fires onCommand when the button is clicked)
Definition: uiButton.h:95
Base class for GUI windows.
Definition: uiWindow.h:45
Virtual interface for the Engine graphics renderer.
Definition: laRenderer.h:98
virtual void modeInterface()=0
Switch to interface rendering (2D projection mode)
Text Label.
Definition: uiLabel.h:41
virtual void modeLight(M_BOOL bOn)=0
Enable/disable lighting.
virtual void modeWorld(const laPoint3 &pos, const laPoint3 &ang=laPoint3())=0
Switch to "world rendering" (3D projection mode)
virtual void onCommand(unsigned long uID, unsigned long nCmd)
Command event handler.
Definition: uiWindow.cpp:263