|
|
223ac6 |
|
|
|
223ac6 |
#include <ctime></ctime>
|
|
|
223ac6 |
#include <cstdlib></cstdlib>
|
|
|
223ac6 |
|
|
|
223ac6 |
#include <string></string>
|
|
|
223ac6 |
#include <iostream></iostream>
|
|
|
223ac6 |
|
|
|
223ac6 |
#include <gl gl.h=""></gl>
|
|
|
223ac6 |
#include <sdl2 sdl.h=""></sdl2>
|
|
|
223ac6 |
|
|
|
223ac6 |
#include "geometry.h"
|
|
|
223ac6 |
#include "scene.h"
|
|
|
223ac6 |
|
|
|
223ac6 |
|
|
|
223ac6 |
class Main {
|
|
|
223ac6 |
public:
|
|
|
223ac6 |
bool sdl_initialized;
|
|
|
223ac6 |
SDL_Window *mainWindow;
|
|
|
223ac6 |
SDL_GLContext mainContext;
|
|
|
223ac6 |
Scene *scene;
|
|
|
223ac6 |
|
|
|
223ac6 |
Main():
|
|
|
223ac6 |
sdl_initialized(),
|
|
|
223ac6 |
mainWindow(),
|
|
|
223ac6 |
mainContext(),
|
|
|
223ac6 |
scene()
|
|
|
223ac6 |
{ }
|
|
|
223ac6 |
|
|
|
223ac6 |
~Main() { deinit(); }
|
|
|
223ac6 |
|
|
|
223ac6 |
bool init() {
|
|
|
223ac6 |
srand(time(NULL));
|
|
|
223ac6 |
|
|
|
223ac6 |
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
|
|
223ac6 |
std::cerr << "SDL_Init failed" << std::endl;
|
|
|
223ac6 |
deinit();
|
|
|
223ac6 |
return false;
|
|
|
223ac6 |
}
|
|
|
223ac6 |
sdl_initialized = true;
|
|
|
223ac6 |
|
|
|
223ac6 |
mainWindow = SDL_CreateWindow(
|
|
|
223ac6 |
"simu",
|
|
|
223ac6 |
SDL_WINDOWPOS_CENTERED,
|
|
|
223ac6 |
SDL_WINDOWPOS_CENTERED,
|
|
|
223ac6 |
512,
|
|
|
223ac6 |
512,
|
|
|
223ac6 |
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE );
|
|
|
223ac6 |
|
|
|
223ac6 |
if (!mainWindow)
|
|
|
223ac6 |
{
|
|
|
223ac6 |
std::cerr << "cannot create window" << std::endl;
|
|
|
223ac6 |
std::cerr << SDL_GetError() << std::endl;
|
|
|
223ac6 |
SDL_ClearError();
|
|
|
223ac6 |
deinit();
|
|
|
223ac6 |
return false;
|
|
|
223ac6 |
}
|
|
|
223ac6 |
|
|
|
223ac6 |
mainContext = SDL_GL_CreateContext(mainWindow);
|
|
|
223ac6 |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
|
|
223ac6 |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
|
|
223ac6 |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
|
|
|
223ac6 |
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
|
|
223ac6 |
|
|
|
223ac6 |
// vsync
|
|
|
223ac6 |
SDL_GL_SetSwapInterval(1);
|
|
|
223ac6 |
|
|
|
223ac6 |
scene = new Scene();
|
|
|
223ac6 |
|
|
|
223ac6 |
glClearColor(0.0, 0.0, 0.0, 0.0);
|
|
|
223ac6 |
glEnable(GL_LINE_SMOOTH);
|
|
|
223ac6 |
glEnable(GL_POINT_SMOOTH);
|
|
|
223ac6 |
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
|
|
|
223ac6 |
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
|
|
|
223ac6 |
|
|
|
223ac6 |
glEnable(GL_NORMALIZE);
|
|
|
223ac6 |
glDisable(GL_DEPTH_TEST);
|
|
|
223ac6 |
|
|
|
223ac6 |
glEnable(GL_LIGHT0);
|
|
|
223ac6 |
float light0[] = {1, 1, 1, 0};
|
|
|
223ac6 |
glLightfv(GL_LIGHT0, GL_POSITION, light0);
|
|
|
223ac6 |
|
|
|
223ac6 |
glEnable(GL_LIGHT1);
|
|
|
223ac6 |
float light1[] = {-1, -1, -1, 0};
|
|
|
223ac6 |
float color[] = {0.5, 0.5, 0.5, 1};
|
|
|
223ac6 |
glLightfv(GL_LIGHT1, GL_POSITION, light1);
|
|
|
223ac6 |
glLightfv(GL_LIGHT1, GL_DIFFUSE, color);
|
|
|
223ac6 |
//glLightfv(GL_LIGHT1, GL_SPECULAR, color);
|
|
|
223ac6 |
|
|
|
223ac6 |
glEnable(GL_COLOR_MATERIAL);
|
|
|
223ac6 |
|
|
|
223ac6 |
resize(512, 512);
|
|
|
223ac6 |
|
|
|
223ac6 |
return true;
|
|
|
223ac6 |
}
|
|
|
223ac6 |
|
|
|
223ac6 |
void deinit() {
|
|
|
223ac6 |
if (scene) { delete scene; scene = 0; }
|
|
|
223ac6 |
if (mainContext) { SDL_GL_DeleteContext(mainContext); mainContext = 0; }
|
|
|
223ac6 |
if (mainWindow) { SDL_DestroyWindow(mainWindow); mainWindow = 0; }
|
|
|
223ac6 |
if (sdl_initialized) { SDL_Quit(); sdl_initialized = false; }
|
|
|
223ac6 |
}
|
|
|
223ac6 |
|
|
|
223ac6 |
void resize(int width, int height) {
|
|
|
223ac6 |
glViewport(0, 0, width, height);
|
|
|
223ac6 |
glMatrixMode(GL_PROJECTION);
|
|
|
223ac6 |
glLoadMatrixd( Matrix4::perspective(90.0, (Real)width/height, 0.1, 100).a );
|
|
|
223ac6 |
glMatrixMode(GL_MODELVIEW);
|
|
|
223ac6 |
}
|
|
|
223ac6 |
|
|
|
223ac6 |
void loop() {
|
|
|
223ac6 |
bool quit = false;
|
|
|
223ac6 |
Uint32 prev_time = SDL_GetTicks();
|
|
|
223ac6 |
while(!quit) {
|
|
|
223ac6 |
SDL_Event event;
|
|
|
223ac6 |
while (SDL_PollEvent(&event)) {
|
|
|
223ac6 |
if (event.type == SDL_QUIT)
|
|
|
223ac6 |
quit = true;
|
|
|
223ac6 |
if (event.type == SDL_WINDOWEVENT) {
|
|
|
223ac6 |
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
|
|
|
223ac6 |
resize(event.window.data1, event.window.data2);
|
|
|
223ac6 |
}
|
|
|
223ac6 |
}
|
|
|
223ac6 |
if (event.type == SDL_KEYDOWN) {
|
|
|
223ac6 |
switch (event.key.keysym.sym) {
|
|
|
223ac6 |
case SDLK_ESCAPE:
|
|
|
223ac6 |
quit = true;
|
|
|
223ac6 |
break;
|
|
|
223ac6 |
case SDLK_w:
|
|
|
223ac6 |
scene->wireframe = !scene->wireframe;
|
|
|
223ac6 |
break;
|
|
|
223ac6 |
case SDLK_r:
|
|
|
223ac6 |
scene->generate();
|
|
|
223ac6 |
break;
|
|
|
223ac6 |
default:
|
|
|
223ac6 |
break;
|
|
|
223ac6 |
}
|
|
|
223ac6 |
}
|
|
|
223ac6 |
if (event.type == SDL_MOUSEMOTION) {
|
|
|
223ac6 |
if (event.motion.state & SDL_BUTTON_LMASK) {
|
|
|
223ac6 |
scene->angles.z += event.motion.xrel;
|
|
|
223ac6 |
scene->angles.x += event.motion.yrel;
|
|
|
223ac6 |
}
|
|
|
223ac6 |
if (event.motion.state & SDL_BUTTON_RMASK) {
|
|
|
223ac6 |
scene->offset.z *= pow(2, event.motion.yrel/100.0);
|
|
|
223ac6 |
}
|
|
|
223ac6 |
if (event.motion.state & SDL_BUTTON_MMASK) {
|
|
|
223ac6 |
scene->offset.x += 0.1*event.motion.xrel;
|
|
|
223ac6 |
scene->offset.y -= 0.1*event.motion.yrel;
|
|
|
223ac6 |
}
|
|
|
223ac6 |
}
|
|
|
223ac6 |
}
|
|
|
223ac6 |
|
|
|
223ac6 |
Uint32 curr_time = SDL_GetTicks();
|
|
|
223ac6 |
double dt = (curr_time - prev_time)/1000.0;
|
|
|
223ac6 |
prev_time = curr_time;
|
|
|
223ac6 |
|
|
|
223ac6 |
scene->update(dt);
|
|
|
223ac6 |
|
|
|
223ac6 |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
223ac6 |
scene->draw();
|
|
|
223ac6 |
SDL_GL_SwapWindow(mainWindow);
|
|
|
223ac6 |
}
|
|
|
223ac6 |
}
|
|
|
223ac6 |
};
|
|
|
223ac6 |
|
|
|
223ac6 |
|
|
|
223ac6 |
int main(int argc, char **argv)
|
|
|
223ac6 |
{
|
|
|
223ac6 |
Main app;
|
|
|
223ac6 |
if (!app.init()) return -1;
|
|
|
223ac6 |
app.loop();
|
|
|
223ac6 |
return 0;
|
|
|
223ac6 |
}
|