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/src/main.cpp

194 lines
4.5 KiB
C++
Raw Normal View History

2015-11-07 09:06:53 -05:00
#include "gl.hpp"
2015-11-07 08:56:06 -05:00
#include "program.hpp"
#include "object.hpp"
2015-11-08 14:27:50 -05:00
#include "camera.hpp"
2015-11-07 08:56:06 -05:00
2015-11-03 10:40:20 -05:00
#include <cstdlib>
2015-11-03 23:48:27 -05:00
#include <glm/glm.hpp>
2015-11-04 13:38:02 -05:00
#include <glm/gtc/matrix_transform.hpp>
2015-11-03 23:48:27 -05:00
2015-11-03 10:40:20 -05:00
#include <emscripten/emscripten.h>
2015-11-04 13:38:02 -05:00
#include <emscripten/html5.h>
2015-11-05 14:17:04 -05: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);
2015-11-07 09:06:53 -05:00
GLuint mvp_uniform;
GLuint local_modelview_uniform;
2015-11-07 08:13:03 -05:00
static GLuint texture_uniform;
2015-11-05 14:17:04 -05:00
static bool keys[GLFW_KEY_LAST];
2015-11-08 14:27:50 -05:00
static Camera camera;
2015-11-10 12:20:52 -05:00
static Camera camera1;
static Camera camera2;
static Camera camera3;
2015-11-04 19:24:06 -05:00
2015-11-05 14:17:04 -05:00
static Model *suzanne;
static Model *teapot;
static Model *bunny;
static Object *suzanne1;
static Object *teapot1;
static Object *bunny1;
2015-11-04 14:39:44 -05:00
2015-11-03 06:46:05 -05:00
int main()
{
2015-11-03 10:40:20 -05: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 14:00:55 -05:00
if (glewInit() != GLEW_OK)
{
glfwCloseWindow();
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetKeyCallback(on_key);
2015-11-04 13:38:02 -05:00
emscripten_set_mousemove_callback(nullptr, nullptr, false, on_em_mousemove);
2015-11-09 10:06:34 -05:00
Program program("textured");
2015-11-05 14:17:04 -05:00
program.use();
2015-11-03 14:00:55 -05:00
2015-11-07 08:13:03 -05:00
mvp_uniform = program.get_uniform_location("mvp");
local_modelview_uniform = program.get_uniform_location("local_modelview");
2015-11-07 08:13:03 -05:00
texture_uniform = program.get_uniform_location("texture");
glUniform1i(texture_uniform, 0);
2015-11-05 14:17:04 -05:00
2015-11-08 14:27:50 -05:00
camera.projection = glm::perspective(45.0f, (float)640 / (float)480, 0.1f, 10.0f);
camera.position.z = 4;
camera.position.y = 2;
2015-11-07 14:38:26 -05:00
2015-11-10 12:20:52 -05:00
camera1.projection = camera.projection;
camera1.position.y = 1.5;
camera1.position.x = -5;
camera1.angles.y = -90;
camera2.projection = camera.projection;
camera2.position.y = 1.5;
camera2.position.z = -5;
camera2.angles.y = -180;
camera3.projection = camera.projection;
camera3.position.y = -5;
camera3.angles.x = 90;
2015-11-09 10:28:25 -05:00
suzanne = new Model("suzanne.obj");
teapot = new Model("teapot.obj");
bunny = new Model("bunny.obj");
2015-11-05 14:17:04 -05:00
suzanne1 = new Object(*suzanne);
suzanne1->position.z = -2;
2015-11-07 07:03:52 -05:00
suzanne1->position.y = 2;
2015-11-05 11:50:43 -05:00
2015-11-05 14:17:04 -05:00
teapot1 = new Object(*teapot);
2015-11-07 07:03:52 -05:00
teapot1->position.x = -2.0;
teapot1->position.y = 1.0;
teapot1->angles.y = 45;
2015-11-03 14:42:02 -05:00
2015-11-05 14:17:04 -05:00
bunny1 = new Object(*bunny);
2015-11-07 07:03:52 -05:00
bunny1->position.x = 2.0;
2015-11-05 09:28:18 -05:00
2015-11-04 14:39:44 -05:00
glEnable(GL_DEPTH_TEST);
2015-11-06 17:47:24 -05:00
glClearColor(1, 1, 1, 0);
2015-11-03 14:00:55 -05:00
emscripten_set_main_loop(iterate, 0, 1);
}
2015-11-03 10:40:20 -05:00
void iterate()
{
2015-11-09 17:57:11 -05:00
if (keys[(unsigned char)'W'])
2015-11-04 16:33:53 -05:00
{
camera.position.x -= 0.1 * sin(glm::radians(camera.angles.y));
camera.position.z -= 0.1 * cos(glm::radians(camera.angles.y));
2015-11-04 16:33:53 -05:00
}
2015-11-09 17:57:11 -05:00
if (keys[(unsigned char)'S'])
2015-11-04 16:33:53 -05:00
{
camera.position.x += 0.1 * sin(glm::radians(camera.angles.y));
camera.position.z += 0.1 * cos(glm::radians(camera.angles.y));
2015-11-04 16:33:53 -05:00
}
2015-11-09 17:57:11 -05:00
if (keys[(unsigned char)'D'])
2015-11-04 16:33:53 -05:00
{
camera.position.x += 0.1 * cos(glm::radians(camera.angles.y));
camera.position.z -= 0.1 * sin(glm::radians(camera.angles.y));
2015-11-04 16:33:53 -05:00
}
2015-11-09 17:57:11 -05:00
if (keys[(unsigned char)'A'])
2015-11-04 16:33:53 -05:00
{
camera.position.x -= 0.1 * cos(glm::radians(camera.angles.y));
camera.position.z += 0.1 * sin(glm::radians(camera.angles.y));
2015-11-04 16:33:53 -05:00
}
suzanne1->angles.z = glfwGetTime() * 360 / 4;
teapot1->angles.x = glfwGetTime() * 360 / 6;
bunny1->angles.y = glfwGetTime() * 360 / 2;
2015-11-07 10:52:07 -05:00
2015-11-04 14:39:44 -05:00
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2015-11-03 14:00:55 -05:00
2015-11-08 14:27:50 -05:00
const glm::mat4 mvp = camera.transformation();
2015-11-10 12:20:52 -05:00
const glm::mat4 mvp1 = camera1.transformation();
const glm::mat4 mvp2 = camera2.transformation();
const glm::mat4 mvp3 = camera3.transformation();
2015-11-10 12:20:52 -05:00
glViewport(0, 0, 640 / 2, 480 / 2);
2015-11-05 14:17:04 -05:00
suzanne1->draw(mvp);
teapot1->draw(mvp);
bunny1->draw(mvp);
2015-11-10 12:20:52 -05:00
glViewport(640 / 2, 0, 640 / 2, 480 / 2);
suzanne1->draw(mvp1);
teapot1->draw(mvp1);
bunny1->draw(mvp1);
glViewport(640 / 2, 480 / 2, 640 / 2, 480 / 2);
suzanne1->draw(mvp2);
teapot1->draw(mvp2);
bunny1->draw(mvp2);
glViewport(0, 480 / 2, 640 / 2, 480 / 2);
suzanne1->draw(mvp3);
teapot1->draw(mvp3);
bunny1->draw(mvp3);
2015-11-03 06:46:05 -05:00
}
2015-11-04 13:38:02 -05:00
GLFWCALL void on_key(int key, int action)
{
keys[key] = action != GLFW_RELEASE;
}
2015-11-09 17:57:11 -05:00
EM_BOOL on_em_mousemove(__attribute__((unused)) int event_type,
const EmscriptenMouseEvent *mouse_event,
__attribute__((unused)) void *user_data)
2015-11-04 13:38:02 -05:00
{
camera.angles.y -= mouse_event->movementX;
camera.angles.x -= mouse_event->movementY;
2015-11-04 13:38:02 -05:00
if (camera.angles.y < 0)
camera.angles.y = 359;
2015-11-04 13:38:02 -05:00
else
if (camera.angles.y >= 360)
camera.angles.y = 0;
2015-11-04 13:38:02 -05:00
2015-11-07 14:38:26 -05:00
if (camera.angles.x < -90)
camera.angles.x = -90;
2015-11-04 13:38:02 -05:00
else
2015-11-07 14:38:26 -05:00
if (camera.angles.x > 90)
camera.angles.x = 90;
2015-11-04 13:38:02 -05:00
return true;
}