1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
matabstrix/examples/demo/main.cpp

256 lines
6.2 KiB
C++
Raw Permalink Normal View History

#include <adapters/adapter.hpp>
#include <scene.hpp>
#include <camera.hpp>
#include <lights/sun.hpp>
#include <models/raw.hpp>
#include <models/static.hpp>
#include <objects/with_model.hpp>
2015-11-07 13:56:06 +00:00
2015-11-03 15:40:20 +00:00
#include <cstdlib>
2015-11-04 04:48:27 +00:00
#include <glm/glm.hpp>
2015-11-04 18:38:02 +00:00
#include <glm/gtc/matrix_transform.hpp>
2015-11-04 04:48:27 +00:00
2015-11-03 15:40:20 +00:00
#include <emscripten/emscripten.h>
2015-11-04 18:38:02 +00:00
#include <emscripten/html5.h>
2015-11-11 20:21:28 +00:00
static Store store;
2015-11-16 13:05:04 +00:00
static const Adapter adapter(store);
2015-11-11 20:21:28 +00:00
2015-11-05 19:17:04 +00:00
static void iterate();
static GLFWCALL void on_key(int key, int action);
static EM_BOOL on_em_mousemove(int event_type, const EmscriptenMouseEvent *mouse_event, void *user_data);
static bool keys[GLFW_KEY_LAST];
2015-11-10 17:28:49 +00:00
static Scene scene;
2015-11-14 08:10:22 +00:00
static Lights::Sun sun;
2015-11-10 17:36:33 +00:00
static Camera camera(scene);
2015-11-12 01:16:44 +00:00
static float camera_angles_y = 0;
2015-11-10 21:51:25 +00:00
static float camera_angles_x = 0;
2015-11-11 17:28:11 +00:00
static const Model *protagonist;
2015-11-12 00:57:08 +00:00
static const Model *car;
2015-11-11 17:28:11 +00:00
static const Model *suzanne;
static const Model *teapot;
static const Model *bunny;
2015-11-12 06:24:27 +00:00
static const Model *untitled;
2015-11-05 19:17:04 +00:00
2015-11-10 20:34:40 +00:00
static Object *protagonist1;
2015-11-12 00:57:08 +00:00
static Object *car1;
2015-11-05 19:17:04 +00:00
static Object *suzanne1;
static Object *teapot1;
static Object *bunny1;
2015-11-12 06:24:27 +00:00
static Object *untitled1;
2015-11-04 19:39:44 +00:00
2015-11-12 01:16:44 +00:00
static bool in_car = false;
static float car_vel = 0.0;
2015-11-03 11:46:05 +00:00
int main()
{
2015-11-03 15:40:20 +00:00
if (!glfwInit())
exit(EXIT_FAILURE);
if (!glfwOpenWindow(640, 480, 8, 8, 8, 8, 16, 0, GLFW_WINDOW))
{
glfwTerminate();
exit(EXIT_FAILURE);
}
2015-11-03 19:00:55 +00:00
if (glewInit() != GLEW_OK)
{
glfwCloseWindow();
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetKeyCallback(on_key);
2015-11-04 18:38:02 +00:00
emscripten_set_mousemove_callback(nullptr, nullptr, false, on_em_mousemove);
2015-11-14 08:10:22 +00:00
sun.ambient = glm::vec3(0.2, 0.2, 0.2);
sun.diffuse = glm::vec3(1.0, 1.0, 1.0);
sun.specular = glm::vec3(1.0, 1.0, 1.0);
2015-11-23 13:30:23 +00:00
sun.direction = glm::vec3(0.0, -1.0, 0.0);
2015-11-14 08:10:22 +00:00
2015-11-12 00:57:08 +00:00
camera.projection = glm::perspective(45.0f, (float)640 / (float)480, 0.1f, 100.0f);
2015-11-07 19:38:26 +00:00
2015-11-16 13:05:04 +00:00
protagonist = adapter.load<Models::Static>("protagonist.obj");
car = adapter.load<Models::Static>("car.obj");
suzanne = adapter.load<Models::Static>("suzanne.obj");
teapot = adapter.load<Models::Static>("teapot.obj");
bunny = adapter.load<Models::Static>("bunny.obj");
untitled = adapter.load<Models::Raw>("untitled.raw");
2015-11-05 19:17:04 +00:00
protagonist1 = new Objects::WithModel(*protagonist);
2015-11-10 20:34:40 +00:00
protagonist1->position.z = 4;
car1 = new Objects::WithModel(*car);
2015-11-12 00:57:08 +00:00
car1->position.z = -10;
suzanne1 = new Objects::WithModel(*suzanne);
suzanne1->position.z = -2;
2015-11-07 12:03:52 +00:00
suzanne1->position.y = 2;
2015-11-05 16:50:43 +00:00
teapot1 = new Objects::WithModel(*teapot);
2015-11-07 12:03:52 +00:00
teapot1->position.x = -2.0;
teapot1->position.y = 1.0;
teapot1->angles.y = 45;
2015-11-03 19:42:02 +00:00
bunny1 = new Objects::WithModel(*bunny);
2015-11-07 12:03:52 +00:00
bunny1->position.x = 2.0;
2015-11-05 14:28:18 +00:00
untitled1 = new Objects::WithModel(*untitled);
2015-11-12 06:24:27 +00:00
untitled1->position.x = -6;
2015-11-14 08:10:22 +00:00
scene << &sun
<< protagonist1
2015-11-12 00:57:08 +00:00
<< car1
2015-11-10 20:34:40 +00:00
<< suzanne1
<< teapot1
2015-11-12 06:24:27 +00:00
<< bunny1
<< untitled1;
2015-11-10 17:28:49 +00:00
2015-11-10 22:41:28 +00:00
glViewport(0, 0, 640, 480);
2015-11-04 19:39:44 +00:00
glEnable(GL_DEPTH_TEST);
2015-11-06 22:47:24 +00:00
glClearColor(1, 1, 1, 0);
2015-11-03 19:00:55 +00:00
emscripten_set_main_loop(iterate, 0, 1);
}
2015-11-03 15:40:20 +00:00
void iterate()
{
2015-11-12 01:16:44 +00:00
if (in_car)
2015-11-04 21:33:53 +00:00
{
2015-11-12 01:16:44 +00:00
protagonist1->position = car1->position;
if (keys[(unsigned char)'W'])
car_vel += 0.1;
if (keys[(unsigned char)'S'])
car_vel -= 0.1;
if (car_vel > 2.0)
car_vel = 2.0;
else
if (car_vel < -1.0)
car_vel = -1.0;
if (keys[(unsigned char)'A'])
car1->angles.y += 0.5 * car_vel;
if (keys[(unsigned char)'D'])
car1->angles.y -= 0.5 * car_vel;
2015-11-04 21:33:53 +00:00
2015-11-12 01:16:44 +00:00
if (car1->angles.y < 0)
car1->angles.y = 359;
if (car1->angles.y >= 360)
car1->angles.y = 0;
}
else
2015-11-04 21:33:53 +00:00
{
2015-11-12 01:16:44 +00:00
car_vel *= 0.99;
protagonist1->angles.y = camera_angles_y;
if (keys[(unsigned char)'W'])
{
protagonist1->position.x -= 0.1 * sin(glm::radians(protagonist1->angles.y));
protagonist1->position.z -= 0.1 * cos(glm::radians(protagonist1->angles.y));
}
if (keys[(unsigned char)'S'])
{
protagonist1->position.x += 0.1 * sin(glm::radians(protagonist1->angles.y));
protagonist1->position.z += 0.1 * cos(glm::radians(protagonist1->angles.y));
}
if (keys[(unsigned char)'D'])
{
protagonist1->position.x += 0.1 * cos(glm::radians(protagonist1->angles.y));
protagonist1->position.z -= 0.1 * sin(glm::radians(protagonist1->angles.y));
}
if (keys[(unsigned char)'A'])
{
protagonist1->position.x -= 0.1 * cos(glm::radians(protagonist1->angles.y));
protagonist1->position.z += 0.1 * sin(glm::radians(protagonist1->angles.y));
}
2015-11-04 21:33:53 +00:00
}
2015-11-12 01:16:44 +00:00
car1->position.x += car_vel * 0.1 * sin(glm::radians(car1->angles.y));
car1->position.z += car_vel * 0.1 * cos(glm::radians(car1->angles.y));
if (in_car)
2015-11-04 21:33:53 +00:00
{
2015-11-12 01:16:44 +00:00
camera.base = car1->transformation()
* glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 1.0f, 0.0f))
* glm::rotate(glm::mat4(1.0f), glm::radians(camera_angles_y), glm::vec3(0.0f, 1.0f, 0.0f))
* glm::rotate(glm::mat4(1.0f), glm::radians(camera_angles_x), glm::vec3(1.0f, 0.0f, 0.0f));
camera.position.z = 8;
2015-11-04 21:33:53 +00:00
}
2015-11-12 01:16:44 +00:00
else
2015-11-04 21:33:53 +00:00
{
2015-11-12 01:16:44 +00:00
camera.base = protagonist1->transformation()
* glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 2.0f, 0.0f))
* glm::rotate(glm::mat4(1.0f), glm::radians(camera_angles_x), glm::vec3(1.0f, 0.0f, 0.0f));
camera.position.z = 2;
2015-11-04 21:33:53 +00:00
}
suzanne1->angles.z = glfwGetTime() * 360 / 4;
teapot1->angles.x = glfwGetTime() * 360 / 6;
bunny1->angles.y = glfwGetTime() * 360 / 2;
2015-11-07 15:52:07 +00:00
2015-11-04 19:39:44 +00:00
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2015-11-03 19:00:55 +00:00
2015-11-10 17:36:33 +00:00
camera.draw();
2015-11-03 11:46:05 +00:00
}
2015-11-04 18:38:02 +00:00
GLFWCALL void on_key(int key, int action)
{
keys[key] = action != GLFW_RELEASE;
2015-11-12 01:16:44 +00:00
if (key == 'E' && action == GLFW_PRESS)
{
if (in_car)
{
in_car = false;
protagonist1->visible = true;
}
else
if (glm::distance(protagonist1->position, car1->position) <= 1.0)
{
in_car = true;
protagonist1->visible = false;
}
}
}
2015-11-09 22:57:11 +00:00
EM_BOOL on_em_mousemove(__attribute__((unused)) int event_type,
const EmscriptenMouseEvent *mouse_event,
__attribute__((unused)) void *user_data)
2015-11-04 18:38:02 +00:00
{
2015-11-12 01:16:44 +00:00
camera_angles_y -= mouse_event->movementX;
2015-11-10 21:51:25 +00:00
camera_angles_x -= mouse_event->movementY;
2015-11-04 18:38:02 +00:00
2015-11-12 01:16:44 +00:00
if (camera_angles_y < 0)
camera_angles_y = 359;
2015-11-04 18:38:02 +00:00
else
2015-11-12 01:16:44 +00:00
if (camera_angles_y >= 360)
camera_angles_y = 0;
2015-11-04 18:38:02 +00:00
2015-11-10 21:51:25 +00:00
if (camera_angles_x < -90)
camera_angles_x = -90;
2015-11-04 18:38:02 +00:00
else
2015-11-10 21:51:25 +00:00
if (camera_angles_x > 90)
camera_angles_x = 90;
2015-11-04 18:38:02 +00:00
return true;
}