couch/core/couch.cpp

187 lines
4.5 KiB
C++
Raw Normal View History

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-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
// Thirdparty Includes
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
2021-01-13 10:42:57 -06:00
Window *window;
const int width = 800;
const int height = 600;
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
}
}
2021-01-13 10:42:57 -06:00
int main() {
2021-01-13 16:47:16 -06:00
2021-01-13 10:42:57 -06:00
int err;
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
2021-01-17 12:41:14 -06:00
glfwWindowHint(GLFW_FLOATING, GLFW_TRUE);
glfwWindowHintString(GLFW_X11_CLASS_NAME, "Couch");
2021-01-13 10:42:57 -06:00
window = glfwCreateWindow(width, height, "Couch", NULL, NULL);
if (!window) {
std::cerr << "Error creating window" << std::endl;
return 1;
}
glfwMakeContextCurrent(window);
err = glewInit();
if (err != GLEW_OK) {
std::cerr << "Error initializing glew" << std::endl;
}
glViewport(0, 0, width, height);
2021-01-14 11:52:01 -06:00
root = Node::GetRoot();
2021-01-13 18:51:58 -06:00
Input *input = Input::GetInstance();
input->Use(window);
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-01-13 16:47:16 -06:00
double lastTime = glfwGetTime();
double delta = 0.0;
2021-01-13 13:17:31 -06:00
2021-01-13 10:42:57 -06:00
while(!glfwWindowShouldClose(window)) {
2021-01-24 22:55:36 -06:00
// Physics update()
world->Step(delta);
2021-01-19 16:01:37 -06:00
// Start rendering to texture;
screen.Enable();
2021-01-13 13:17:31 -06:00
lua->Update(delta);
2021-01-13 16:47:16 -06:00
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-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-01-19 16:01:37 -06:00
// Stop rendering to texture
screen.Disable();
// // Render the screen
screenShader->Use();
2021-01-22 15:27:32 -06:00
screenShader->UpdateTex(screen.tex);
2021-01-19 16:01:37 -06:00
glViewport(0, 0, width, height);
screen.Draw();
2021-01-13 10:42:57 -06:00
glfwSwapBuffers(window);
glfwPollEvents();
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-13 16:47:16 -06:00
2021-01-27 16:37:23 -06:00
delete screenShader;
delete skyboxShader;
delete flatShader()
lua->Close();
2021-01-27 16:37:23 -06:00
delete lua;
2021-01-13 10:42:57 -06:00
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}