couch/core/couch.cpp

165 lines
3.9 KiB
C++
Raw Normal View History

// C includes
#include <unistd.h>
2021-01-13 16:47:16 -06:00
// C++ includes
2021-01-13 10:42:57 -06:00
#include <iostream>
2021-01-13 16:47:16 -06:00
// OpenGL Includes
2021-01-13 10:42:57 -06:00
#include <GL/glew.h>
#include <GLFW/glfw3.h>
2021-01-13 13:17:31 -06:00
#include <glm/gtc/matrix_transform.hpp>
2021-01-13 10:42:57 -06:00
#include "types.h"
2021-03-08 13:50:50 -06:00
#include "Window.h"
2021-01-13 10:42:57 -06:00
2021-01-21 15:26:39 -06:00
#include "Util.h"
2021-01-15 16:15:47 -06:00
#include "Shaders/FlatShader.h"
2021-01-19 16:01:37 -06:00
#include "Shaders/ScreenShader.h"
2021-01-23 11:54:34 -06:00
#include "Shaders/SkyboxShader.h"
2021-01-19 16:01:37 -06:00
#include "Screen.h"
2021-01-13 16:47:16 -06:00
#include "Camera.h"
2021-01-13 18:51:58 -06:00
#include "Input.h"
2021-01-14 11:52:01 -06:00
#include "Node.h"
#include "Mesh.h"
2021-01-21 15:26:39 -06:00
#include "Light.h"
2021-01-23 11:54:34 -06:00
#include "Skybox.h"
2021-01-24 22:55:36 -06:00
#include "Rigidbody.h"
#include "World.h"
2021-01-19 16:16:54 -06:00
#include "Scripting/Lua.h"
2021-01-13 10:42:57 -06:00
2021-01-14 11:52:01 -06:00
Node *root;
2021-01-13 16:47:16 -06:00
2021-01-15 16:15:47 -06:00
void render(Node *curr, Shader *shader, Matrix model) {
2021-01-26 16:42:28 -06:00
Spatial *spatial = dynamic_cast<Spatial*>(curr);
if (spatial) {
2021-01-26 23:28:20 -06:00
Transform transform = spatial->GetTransform();
model = glm::rotate(model, transform.rotation.x, Vector3(1.0f, 0.0f, 0.0f));
model = glm::rotate(model, transform.rotation.y, Vector3(0.0f, 1.0f, 0.0f));
model = glm::rotate(model, transform.rotation.z, Vector3(0.0f, 0.0f, 1.0f));
model = glm::translate(model, transform.position);
model = glm::scale(model, transform.scale);
2021-01-24 22:55:36 -06:00
shader->UpdateModel(model);
shader->UpdateNormal(glm::mat3(glm::transpose(glm::inverse(model))));
}
2021-01-26 16:42:28 -06:00
Mesh *mesh = dynamic_cast<Mesh*>(curr);
if (mesh) {
2021-01-20 14:54:54 -06:00
mesh->Draw(shader);
2021-01-14 11:52:01 -06:00
}
2021-01-26 16:42:28 -06:00
for (Node *child : curr->GetChildren()) {
2021-01-24 22:55:36 -06:00
render(child, shader, model);
2021-01-14 11:52:01 -06:00
}
}
int main(int argc, char *argv[]) {
if (argc == 2) {
chdir(argv[1]);
}
2021-03-08 13:50:50 -06:00
Window window;
window.Init();
2021-01-14 11:52:01 -06:00
root = Node::GetRoot();
2021-01-13 18:51:58 -06:00
Input *input = Input::GetInstance();
2021-08-02 14:34:49 -05:00
input->Use(&window);
2021-01-13 18:51:58 -06:00
2021-01-13 16:47:16 -06:00
Camera defaultCamera;
2021-01-19 16:01:37 -06:00
2021-01-24 22:55:36 -06:00
World *world = World::GetWorld();
2021-01-19 16:01:37 -06:00
Screen screen;
ScreenShader *screenShader = new ScreenShader();
2021-01-23 11:54:34 -06:00
SkyboxShader *skyboxShader = new SkyboxShader();
2021-01-13 16:47:16 -06:00
2021-01-15 16:15:47 -06:00
FlatShader *shader = new FlatShader();
2021-01-13 10:42:57 -06:00
2021-01-13 13:17:31 -06:00
Matrix projection = glm::perspective(glm::radians(45.0f), 800.0f/600.0f, 0.1f, 100.0f);
2021-01-13 16:47:16 -06:00
// TODO Allow multiple scripting languages
Lua *lua = new Lua();
lua->Initialize();
2021-04-21 15:06:03 -05:00
2021-01-13 16:47:16 -06:00
double lastTime = glfwGetTime();
double delta = 0.0;
2021-01-13 13:17:31 -06:00
2021-03-08 13:50:50 -06:00
while(!window.ShouldClose()) {
2021-03-09 16:40:21 -06:00
// Physics update
2021-01-24 22:55:36 -06:00
world->Step(delta);
2021-01-13 13:17:31 -06:00
2021-03-09 16:40:21 -06:00
// Script update
lua->Update(delta);
2021-03-09 16:40:21 -06:00
// Delete freed nodes
root->DoFree();
2021-01-13 16:47:16 -06:00
2021-03-09 16:40:21 -06:00
// Start rendering to screen
screen.Enable();
2021-01-19 16:01:37 -06:00
shader->Use();
shader->UpdateProjection(projection);
2021-01-13 16:47:16 -06:00
Matrix view(1.0f);
Camera *camera = Camera::GetCurrentCamera();
2021-01-26 23:28:20 -06:00
Transform camera_transform = camera->GetTransform();
view = glm::rotate(view, -camera_transform.rotation.x, Vector3(1.0f, 0.0f, 0.0f));
view = glm::rotate(view, -camera_transform.rotation.y, Vector3(0.0f, 1.0f, 0.0f));
view = glm::rotate(view, -camera_transform.rotation.z, Vector3(0.0f, 0.0f, 1.0f));
view = glm::translate(view, -camera_transform.position);
2021-01-15 16:15:47 -06:00
shader->UpdateView(view);
2021-01-13 16:47:16 -06:00
2021-01-21 15:26:39 -06:00
// Find the lights
DirectionalLight *dirLight = Util::FindNodeByType<DirectionalLight>(root, "DirectionalLight");
if (dirLight) {
shader->UpdateDirectionalLight(*dirLight);
} else {
// No light in scene
shader->UpdateDirectionalLight(DirectionalLight());
}
2021-03-04 14:29:48 -06:00
PointLightList pointLights = Util::FindNodesByType<PointLight>(root, "PointLight");
shader->UpdatePointLights(pointLights);
2021-01-14 11:52:01 -06:00
// Render the scene tree
render(root, shader, Matrix(1.0f));
2021-01-19 16:01:37 -06:00
2021-01-23 11:54:34 -06:00
// Render the skybox
Skybox *skybox = Util::FindNodeByType<Skybox>(root, "Skybox");
if (skybox) {
glDepthFunc(GL_LEQUAL);
skyboxShader->Use();
skyboxShader->UpdateView(glm::mat4(glm::mat3(view)));
skyboxShader->UpdateProjection(projection);
skyboxShader->UpdateSkybox(*skybox);
skybox->DrawSkybox();
glDepthFunc(GL_LESS);
}
2021-03-09 16:40:21 -06:00
// Stop rendering to texture
screen.Disable();
2021-01-19 16:01:37 -06:00
screenShader->Use();
2021-01-22 15:27:32 -06:00
screenShader->UpdateTex(screen.tex);
2021-03-09 16:40:21 -06:00
2021-01-19 16:01:37 -06:00
screen.Draw();
2021-03-08 13:50:50 -06:00
window.Update();
2021-01-13 16:47:16 -06:00
double curTime = glfwGetTime();
delta = curTime - lastTime;
lastTime = curTime;
2021-01-13 10:42:57 -06:00
}
2021-01-27 16:37:23 -06:00
2021-03-08 13:54:36 -06:00
lua->Close();
2021-03-08 13:50:50 -06:00
window.Close();
2021-01-13 10:42:57 -06:00
return 0;
}