1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
#include "main.h"
#include <stdexcept>
#include "graphics_factory_basic_gl.h"
#include "graphics_interface.h"
#include "util.h"
using namespace Shared::Platform;
using namespace Shared::Graphics;
using namespace Shared::Graphics::Gl;
using namespace Shared::Util;
using namespace std;
namespace Shared{ namespace G3dViewer{
// ===============================================
// class MainWindow
// ===============================================
const string MainWindow::versionString= "v1.3.4";
const string MainWindow::winHeader= "G3D viewer " + versionString + " - Built: " + __DATE__;
MainWindow::MainWindow(const string &modelPath):
wxFrame(
NULL, -1, winHeader.c_str(),
wxPoint(Renderer::windowX, Renderer::windowY),
wxSize(Renderer::windowW, Renderer::windowH))
{
renderer= Renderer::getInstance();
this->modelPath= modelPath;
model= NULL;
playerColor= Renderer::pcRed;
speed= 0.025f;
glCanvas = new GlCanvas(this);
glCanvas->SetCurrent();
renderer->init();
menu= new wxMenuBar();
//menu
menuFile= new wxMenu();
menuFile->Append(miFileLoad, "Load");
menu->Append(menuFile, "File");
//mode
menuMode= new wxMenu();
menuMode->AppendCheckItem(miModeNormals, "Normals");
menuMode->AppendCheckItem(miModeWireframe, "Wireframe");
menuMode->AppendCheckItem(miModeGrid, "Grid");
menu->Append(menuMode, "Mode");
//mode
menuSpeed= new wxMenu();
menuSpeed->Append(miSpeedSlower, "Slower");
menuSpeed->Append(miSpeedFaster, "Faster");
menu->Append(menuSpeed, "Speed");
//custom color
menuCustomColor= new wxMenu();
menuCustomColor->AppendCheckItem(miColorRed, "Red");
menuCustomColor->AppendCheckItem(miColorBlue, "Blue");
menuCustomColor->AppendCheckItem(miColorYellow, "Yellow");
menuCustomColor->AppendCheckItem(miColorGreen, "Green");
menu->Append(menuCustomColor, "Custom Color");
menuMode->Check(miModeGrid, true);
menuCustomColor->Check(miColorRed, true);
SetMenuBar(menu);
//misc
model= NULL;
rotX= 0.0f;
rotY= 0.0f;
zoom= 1.0f;
lastX= 0;
lastY= 0;
anim= 0.0f;
CreateStatusBar();
timer = new wxTimer(this);
timer->Start(40);
if(!modelPath.empty()){
Model *tmpModel= new ModelGl();
renderer->loadTheModel(tmpModel, modelPath);
model= tmpModel;
GetStatusBar()->SetStatusText(getModelInfo().c_str());
}
}
MainWindow::~MainWindow(){
delete renderer;
delete model;
delete timer;
delete glCanvas;
}
void MainWindow::onPaint(wxPaintEvent &event){
renderer->reset(GetClientSize().x, GetClientSize().y, playerColor);
renderer->transform(rotX, rotY, zoom);
renderer->renderGrid();
renderer->renderTheModel(model, anim);
glCanvas->SwapBuffers();
}
void MainWindow::onClose(wxCloseEvent &event){
delete this;
}
void MainWindow::onMouseMove(wxMouseEvent &event){
int x= event.GetX();
int y= event.GetY();
wxPaintEvent paintEvent;
if(event.LeftIsDown()){
rotX+= clamp(lastX-x, -10, 10);
rotY+= clamp(lastY-y, -10, 10);
onPaint(paintEvent);
}
else if(event.RightIsDown()){
zoom*= 1.0f+(lastX-x+lastY-y)/100.0f;
zoom= clamp(zoom, 0.1f, 10.0f);
onPaint(paintEvent);
}
lastX= x;
lastY= y;
}
void MainWindow::onMenuFileLoad(wxCommandEvent &event){
string fileName;
wxFileDialog fileDialog(this);
fileDialog.SetWildcard("G3D files (*.g3d)|*.g3d");
if(fileDialog.ShowModal()==wxID_OK){
delete model;
Model *tmpModel= new ModelGl();
renderer->loadTheModel(tmpModel, fileDialog.GetPath().c_str());
model= tmpModel;
GetStatusBar()->SetStatusText(getModelInfo().c_str());
}
}
void MainWindow::onMenuModeNormals(wxCommandEvent &event){
renderer->toggleNormals();
menuMode->Check(miModeNormals, renderer->getNormals());
}
void MainWindow::onMenuModeWireframe(wxCommandEvent &event){
renderer->toggleWireframe();
menuMode->Check(miModeWireframe, renderer->getWireframe());
}
void MainWindow::onMenuModeGrid(wxCommandEvent &event){
renderer->toggleGrid();
menuMode->Check(miModeGrid, renderer->getGrid());
}
void MainWindow::onMenuSpeedSlower(wxCommandEvent &event){
speed/= 1.5f;
}
void MainWindow::onMenuSpeedFaster(wxCommandEvent &event){
speed*= 1.5f;
}
void MainWindow::onMenuColorRed(wxCommandEvent &event){
playerColor= Renderer::pcRed;
menuCustomColor->Check(miColorRed, true);
menuCustomColor->Check(miColorBlue, false);
menuCustomColor->Check(miColorYellow, false);
menuCustomColor->Check(miColorGreen, false);
}
void MainWindow::onMenuColorBlue(wxCommandEvent &event){
playerColor= Renderer::pcBlue;
menuCustomColor->Check(miColorRed, false);
menuCustomColor->Check(miColorBlue, true);
menuCustomColor->Check(miColorYellow, false);
menuCustomColor->Check(miColorGreen, false);
}
void MainWindow::onMenuColorYellow(wxCommandEvent &event){
playerColor= Renderer::pcYellow;
menuCustomColor->Check(miColorRed, false);
menuCustomColor->Check(miColorBlue, false);
menuCustomColor->Check(miColorYellow, true);
menuCustomColor->Check(miColorGreen, false);
}
void MainWindow::onMenuColorGreen(wxCommandEvent &event){
playerColor= Renderer::pcGreen;
menuCustomColor->Check(miColorRed, false);
menuCustomColor->Check(miColorBlue, false);
menuCustomColor->Check(miColorYellow, false);
menuCustomColor->Check(miColorGreen, true);
}
void MainWindow::onTimer(wxTimerEvent &event){
wxPaintEvent paintEvent;
anim= anim+speed;
if(anim>1.0f){
anim-= 1.f;
}
onPaint(paintEvent);
}
string MainWindow::getModelInfo(){
string str;
if(model!=NULL){
str+= "Meshes: "+intToStr(model->getMeshCount());
str+= ", Vertices: "+intToStr(model->getVertexCount());
str+= ", Triangles: "+intToStr(model->getTriangleCount());
str+= ", Version: "+intToStr(model->getFileVersion());
}
return str;
}
BEGIN_EVENT_TABLE(MainWindow, wxFrame)
EVT_TIMER(-1, MainWindow::onTimer)
EVT_CLOSE(MainWindow::onClose)
EVT_MENU(miFileLoad, MainWindow::onMenuFileLoad)
EVT_MENU(miModeWireframe, MainWindow::onMenuModeWireframe)
EVT_MENU(miModeNormals, MainWindow::onMenuModeNormals)
EVT_MENU(miModeGrid, MainWindow::onMenuModeGrid)
EVT_MENU(miSpeedFaster, MainWindow::onMenuSpeedFaster)
EVT_MENU(miSpeedSlower, MainWindow::onMenuSpeedSlower)
EVT_MENU(miColorRed, MainWindow::onMenuColorRed)
EVT_MENU(miColorBlue, MainWindow::onMenuColorBlue)
EVT_MENU(miColorYellow, MainWindow::onMenuColorYellow)
EVT_MENU(miColorGreen, MainWindow::onMenuColorGreen)
END_EVENT_TABLE()
// =====================================================
// class GlCanvas
// =====================================================
GlCanvas::GlCanvas(MainWindow * mainWindow):
wxGLCanvas(mainWindow, -1)
{
this->mainWindow = mainWindow;
}
void GlCanvas::onMouseMove(wxMouseEvent &event){
mainWindow->onMouseMove(event);
}
void GlCanvas::onPaint(wxPaintEvent &event){
mainWindow->onPaint(event);
}
BEGIN_EVENT_TABLE(GlCanvas, wxGLCanvas)
EVT_MOTION(GlCanvas::onMouseMove)
EVT_PAINT(GlCanvas::onPaint)
END_EVENT_TABLE()
// ===============================================
// class App
// ===============================================
bool App::OnInit(){
string modelPath;
if(argc==2){
modelPath= argv[1];
}
mainWindow= new MainWindow(modelPath);
mainWindow->Show();
return true;
}
int App::MainLoop(){
try{
return wxApp::MainLoop();
}
catch(const exception &e){
wxMessageDialog(NULL, e.what(), "Exception", wxOK | wxICON_ERROR).ShowModal();
return 0;
}
}
int App::OnExit(){
return 0;
}
}}//end namespace
IMPLEMENT_APP(Shared::G3dViewer::App)
|