|
|
57bda0 |
|
|
|
57bda0 |
#include <string></string>
|
|
|
57bda0 |
#include <iostream></iostream>
|
|
|
57bda0 |
|
|
|
57bda0 |
#include <gl gl.h=""></gl>
|
|
|
57bda0 |
#include <sdl2 sdl.h=""></sdl2>
|
|
|
57bda0 |
|
|
|
1bcd85 |
#include "geometry.h"
|
|
|
57bda0 |
#include "scene.h"
|
|
|
57bda0 |
#include "simulator.h"
|
|
|
d2b2b5 |
#include "tool.h"
|
|
|
57bda0 |
#include "loader.h"
|
|
|
8ee194 |
#include "collider.h"
|
|
|
e31ea0 |
#include "generator.h"
|
|
|
0a0ecd |
#include "generatorradial.h"
|
|
|
8ee194 |
|
|
|
57bda0 |
|
|
|
57bda0 |
class Main {
|
|
|
57bda0 |
public:
|
|
|
57bda0 |
bool sdl_initialized;
|
|
|
57bda0 |
SDL_Window *mainWindow;
|
|
|
57bda0 |
SDL_GLContext mainContext;
|
|
|
57bda0 |
Scene *scene;
|
|
|
57bda0 |
|
|
|
57bda0 |
Main():
|
|
|
57bda0 |
sdl_initialized(),
|
|
|
57bda0 |
mainWindow(),
|
|
|
57bda0 |
mainContext(),
|
|
|
57bda0 |
scene()
|
|
|
57bda0 |
{ }
|
|
|
57bda0 |
|
|
|
57bda0 |
~Main() { deinit(); }
|
|
|
57bda0 |
|
|
|
57bda0 |
bool init() {
|
|
|
57bda0 |
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
|
|
57bda0 |
std::cerr << "SDL_Init failed" << std::endl;
|
|
|
57bda0 |
deinit();
|
|
|
57bda0 |
return false;
|
|
|
57bda0 |
}
|
|
|
57bda0 |
sdl_initialized = true;
|
|
|
57bda0 |
|
|
|
57bda0 |
mainWindow = SDL_CreateWindow(
|
|
|
57bda0 |
"simu",
|
|
|
57bda0 |
SDL_WINDOWPOS_CENTERED,
|
|
|
57bda0 |
SDL_WINDOWPOS_CENTERED,
|
|
|
57bda0 |
512,
|
|
|
57bda0 |
512,
|
|
|
57bda0 |
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE );
|
|
|
57bda0 |
|
|
|
57bda0 |
if (!mainWindow)
|
|
|
57bda0 |
{
|
|
|
57bda0 |
std::cerr << "cannot create window" << std::endl;
|
|
|
57bda0 |
std::cerr << SDL_GetError() << std::endl;
|
|
|
57bda0 |
SDL_ClearError();
|
|
|
57bda0 |
deinit();
|
|
|
57bda0 |
return false;
|
|
|
57bda0 |
}
|
|
|
57bda0 |
|
|
|
57bda0 |
mainContext = SDL_GL_CreateContext(mainWindow);
|
|
|
57bda0 |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
|
|
57bda0 |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
|
|
57bda0 |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
|
|
|
57bda0 |
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
|
|
57bda0 |
|
|
|
57bda0 |
// vsync
|
|
|
57bda0 |
SDL_GL_SetSwapInterval(1);
|
|
|
57bda0 |
|
|
|
57bda0 |
scene = new Scene();
|
|
|
57bda0 |
|
|
|
57bda0 |
glClearColor(0.0, 0.0, 0.0, 0.0);
|
|
|
57bda0 |
glEnable(GL_LINE_SMOOTH);
|
|
|
57bda0 |
glEnable(GL_POINT_SMOOTH);
|
|
|
57bda0 |
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
|
|
|
57bda0 |
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
|
|
|
e31ea0 |
|
|
|
e31ea0 |
glEnable(GL_NORMALIZE);
|
|
|
b2ca59 |
glDisable(GL_DEPTH_TEST);
|
|
|
57bda0 |
|
|
|
57bda0 |
glEnable(GL_LIGHT0);
|
|
|
57bda0 |
float light0[] = {1, 1, 1, 0};
|
|
|
57bda0 |
glLightfv(GL_LIGHT0, GL_POSITION, light0);
|
|
|
57bda0 |
|
|
|
57bda0 |
glEnable(GL_LIGHT1);
|
|
|
57bda0 |
float light1[] = {-1, -1, -1, 0};
|
|
|
8ee194 |
float color[] = {1, 1, 1, 1};
|
|
|
57bda0 |
glLightfv(GL_LIGHT1, GL_POSITION, light1);
|
|
|
8ee194 |
glLightfv(GL_LIGHT1, GL_DIFFUSE, color);
|
|
|
8ee194 |
glLightfv(GL_LIGHT1, GL_SPECULAR, color);
|
|
|
57bda0 |
|
|
|
57bda0 |
glEnable(GL_COLOR_MATERIAL);
|
|
|
57bda0 |
|
|
|
57bda0 |
resize(512, 512);
|
|
|
57bda0 |
|
|
|
57bda0 |
return true;
|
|
|
57bda0 |
}
|
|
|
57bda0 |
|
|
|
57bda0 |
void deinit() {
|
|
|
57bda0 |
if (scene) { delete scene; scene = 0; }
|
|
|
57bda0 |
if (mainContext) { SDL_GL_DeleteContext(mainContext); mainContext = 0; }
|
|
|
57bda0 |
if (mainWindow) { SDL_DestroyWindow(mainWindow); mainWindow = 0; }
|
|
|
57bda0 |
if (sdl_initialized) { SDL_Quit(); sdl_initialized = false; }
|
|
|
57bda0 |
}
|
|
|
57bda0 |
|
|
|
57bda0 |
void resize(int width, int height) {
|
|
|
57bda0 |
glViewport(0, 0, width, height);
|
|
|
57bda0 |
glMatrixMode(GL_PROJECTION);
|
|
|
1bcd85 |
glLoadMatrixd( Matrix4::perspective(90.0, (Real)width/height, 1, 1000).a );
|
|
|
57bda0 |
glMatrixMode(GL_MODELVIEW);
|
|
|
57bda0 |
}
|
|
|
57bda0 |
|
|
|
8ee194 |
void update_collider() {
|
|
|
8ee194 |
if (scene->collider) {
|
|
|
8ee194 |
Real l = scene->collider->distance_to_model(scene->tool_pos, scene->tool_dir);
|
|
|
8ee194 |
if (!isinf(l))
|
|
|
8ee194 |
scene->tool_pos = scene->tool_pos + scene->tool_dir*l;
|
|
|
8ee194 |
}
|
|
|
8ee194 |
}
|
|
|
8ee194 |
|
|
|
57bda0 |
void loop() {
|
|
|
8ee194 |
update_collider();
|
|
|
8ee194 |
|
|
|
57bda0 |
bool quit = false;
|
|
|
57bda0 |
Uint32 prev_time = SDL_GetTicks();
|
|
|
57bda0 |
while(!quit) {
|
|
|
57bda0 |
SDL_Event event;
|
|
|
57bda0 |
while (SDL_PollEvent(&event)) {
|
|
|
57bda0 |
if (event.type == SDL_QUIT)
|
|
|
57bda0 |
quit = true;
|
|
|
57bda0 |
if (event.type == SDL_WINDOWEVENT) {
|
|
|
57bda0 |
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
|
|
|
57bda0 |
resize(event.window.data1, event.window.data2);
|
|
|
57bda0 |
}
|
|
|
57bda0 |
}
|
|
|
57bda0 |
if (event.type == SDL_KEYDOWN) {
|
|
|
57bda0 |
switch (event.key.keysym.sym) {
|
|
|
57bda0 |
case SDLK_ESCAPE:
|
|
|
57bda0 |
quit = true;
|
|
|
57bda0 |
break;
|
|
|
8ee194 |
case SDLK_w:
|
|
|
8ee194 |
scene->tool_pos.y += 0.5;
|
|
|
8ee194 |
update_collider();
|
|
|
8ee194 |
break;
|
|
|
8ee194 |
case SDLK_s:
|
|
|
8ee194 |
scene->tool_pos.y -= 0.5;
|
|
|
8ee194 |
update_collider();
|
|
|
8ee194 |
break;
|
|
|
8ee194 |
case SDLK_a:
|
|
|
8ee194 |
scene->tool_pos.x -= 0.5;
|
|
|
8ee194 |
update_collider();
|
|
|
8ee194 |
break;
|
|
|
8ee194 |
case SDLK_d:
|
|
|
8ee194 |
scene->tool_pos.x += 0.5;
|
|
|
8ee194 |
update_collider();
|
|
|
57bda0 |
break;
|
|
|
57bda0 |
default:
|
|
|
57bda0 |
break;
|
|
|
57bda0 |
}
|
|
|
57bda0 |
}
|
|
|
57bda0 |
if (event.type == SDL_MOUSEMOTION) {
|
|
|
57bda0 |
if (event.motion.state & SDL_BUTTON_LMASK) {
|
|
|
57bda0 |
scene->angles.z += event.motion.xrel;
|
|
|
57bda0 |
scene->angles.x += event.motion.yrel;
|
|
|
57bda0 |
}
|
|
|
57bda0 |
if (event.motion.state & SDL_BUTTON_RMASK) {
|
|
|
57bda0 |
scene->offset.z *= pow(2, event.motion.yrel/100.0);
|
|
|
57bda0 |
}
|
|
|
57bda0 |
if (event.motion.state & SDL_BUTTON_MMASK) {
|
|
|
e31ea0 |
scene->offset.x += 0.1*event.motion.xrel;
|
|
|
e31ea0 |
scene->offset.y -= 0.1*event.motion.yrel;
|
|
|
57bda0 |
}
|
|
|
57bda0 |
}
|
|
|
57bda0 |
}
|
|
|
57bda0 |
|
|
|
57bda0 |
Uint32 curr_time = SDL_GetTicks();
|
|
|
57bda0 |
double dt = (curr_time - prev_time)/1000.0;
|
|
|
57bda0 |
prev_time = curr_time;
|
|
|
57bda0 |
|
|
|
57bda0 |
scene->update(dt);
|
|
|
57bda0 |
|
|
|
57bda0 |
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
57bda0 |
scene->draw();
|
|
|
57bda0 |
SDL_GL_SwapWindow(mainWindow);
|
|
|
57bda0 |
}
|
|
|
57bda0 |
}
|
|
|
57bda0 |
};
|
|
|
57bda0 |
|
|
|
57bda0 |
|
|
|
57bda0 |
int main(int argc, char **argv)
|
|
|
57bda0 |
{
|
|
|
57bda0 |
Main app;
|
|
|
57bda0 |
if (!app.init()) return -1;
|
|
|
8ee194 |
|
|
|
e31ea0 |
bool load_track = false;
|
|
|
1bcd85 |
bool load_model = false;
|
|
|
e31ea0 |
|
|
|
8ee194 |
Model model;
|
|
|
d2b2b5 |
SimulatorXYZA simulator_xyza(400, 360);
|
|
|
0a0ecd |
SimulatorXYZ simulator_xyz(400, 400);
|
|
|
0a0ecd |
FlatTool tool(1.5);// + 0.5);
|
|
|
0a0ecd |
//ConeTool tool(1.25, 10, 0.75);
|
|
|
8ee194 |
Collider collider(model, tool);
|
|
|
8ee194 |
|
|
|
8ee194 |
Vector3 dir(1, 3, -3);
|
|
|
8ee194 |
dir = dir*(1/dir.len());
|
|
|
8ee194 |
|
|
|
8ee194 |
if (argc > 1 && std::string(argv[1]) != ".") {
|
|
|
57bda0 |
Loader::load(app.scene->track, argv[1]);
|
|
|
0a0ecd |
//app.scene->simulator = &simulator_xyza;
|
|
|
0a0ecd |
app.scene->simulator = &simulator_xyz;
|
|
|
8ee194 |
app.scene->simulator->simulate(app.scene->track, tool);
|
|
|
e31ea0 |
load_track = true;
|
|
|
57bda0 |
}
|
|
|
8ee194 |
|
|
|
8ee194 |
if (argc > 2 && std::string(argv[2]) != ".") {
|
|
|
8ee194 |
Loader::load(model, argv[2]);
|
|
|
b2ca59 |
//Loader::save(app.scene->track, "output/tmp.tap");
|
|
|
8ee194 |
app.scene->model = &model;
|
|
|
1bcd85 |
load_model = true;
|
|
|
1bcd85 |
}
|
|
|
1bcd85 |
|
|
|
1bcd85 |
if (argc > 3 && std::string(argv[3]) != ".") {
|
|
|
1bcd85 |
if (!load_track && load_model) {
|
|
|
0a0ecd |
if (0) {
|
|
|
0a0ecd |
GeneratorRowsZAX generator;
|
|
|
0a0ecd |
generator.max_z = 26;
|
|
|
0a0ecd |
generator.min_z = -10;
|
|
|
0a0ecd |
//generator.step_z = 2.0;
|
|
|
0a0ecd |
generator.step_z = 5.0;
|
|
|
0a0ecd |
//generator.step_z = 0;
|
|
|
0a0ecd |
//generator.step_a = 2*(tool.radius - 0.6);
|
|
|
0a0ecd |
generator.step_a = 0.75;
|
|
|
0a0ecd |
generator.step_x = 0.5;
|
|
|
0a0ecd |
generator.skip_middle_layers = true;
|
|
|
0a0ecd |
generator.feed_speed = 150;
|
|
|
0a0ecd |
generator.generate(collider, app.scene->track);
|
|
|
0a0ecd |
app.scene->simulator = &simulator_xyza;
|
|
|
0a0ecd |
} else {
|
|
|
0a0ecd |
GeneratorRadialXYZ generator;
|
|
|
0a0ecd |
generator.max_z = 26;
|
|
|
0a0ecd |
generator.min_z = 0;
|
|
|
0a0ecd |
//generator.min_r = 6.5 - tool.radius - 0.1;
|
|
|
0a0ecd |
generator.max_r = 17.45 + tool.radius - 0.1;
|
|
|
0a0ecd |
//generator.full_radius = true;
|
|
|
0a0ecd |
generator.step_z = 2.0;
|
|
|
0a0ecd |
generator.step_a = 2*tool.radius - 0.5;
|
|
|
0a0ecd |
generator.step_r = 0.1;
|
|
|
0a0ecd |
//generator.skip_middle_layers = true;
|
|
|
0a0ecd |
generator.feed_speed = 150;
|
|
|
0a0ecd |
generator.generate(collider, app.scene->track);
|
|
|
0a0ecd |
app.scene->simulator = &simulator_xyz;
|
|
|
0a0ecd |
}
|
|
|
0a0ecd |
|
|
|
e31ea0 |
app.scene->simulator->simulate(app.scene->track, tool);
|
|
|
1bcd85 |
Loader::save(app.scene->track, argv[3]);
|
|
|
e31ea0 |
}
|
|
|
8ee194 |
}
|
|
|
8ee194 |
|
|
|
8ee194 |
app.scene->tool = &tool;
|
|
|
8ee194 |
app.scene->tool_dir = dir;
|
|
|
8ee194 |
app.scene->collider = &collider;
|
|
|
8ee194 |
|
|
|
57bda0 |
app.loop();
|
|
|
57bda0 |
return 0;
|
|
|
57bda0 |
}
|