couch/core/couch.cpp

149 lines
3.6 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-15 16:15:47 -06:00
#include "Shaders/FlatShader.h"
2021-01-19 16:01:37 -06:00
#include "Shaders/ScreenShader.h"
#include "Screen.h"
2021-01-13 10:42:57 -06:00
#include "Ball.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 "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-14 11:52:01 -06:00
if (curr->IsDrawable()) {
if (curr->IsTransformable()) {
Spatial *spatial = dynamic_cast<Spatial*>(curr);
model = glm::rotate(model, spatial->transform.rotation.x, Vector3(1.0f, 0.0f, 0.0f));
model = glm::rotate(model, spatial->transform.rotation.y, Vector3(0.0f, 1.0f, 0.0f));
model = glm::rotate(model, spatial->transform.rotation.z, Vector3(0.0f, 0.0f, 1.0f));
model = glm::translate(model, spatial->transform.position);
2021-01-15 16:15:47 -06:00
shader->UpdateModel(model);
2021-01-14 11:52:01 -06:00
}
Mesh *mesh = dynamic_cast<Mesh*>(curr);
if (mesh->material->usesColor) {
shader->UpdateColor(true, mesh->material->color);
} else {
shader->UpdateColor(false);
}
if (mesh->material->usesTex) {
shader->UpdateTex(true, mesh->material->tex);
} else {
shader->UpdateTex(false);
}
mesh->Draw();
2021-01-14 11:52:01 -06:00
}
for (Node *child : curr->children) {
render(child, shader, model);
}
}
2021-01-13 16:47:16 -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
Screen screen;
ScreenShader *screenShader = new ScreenShader();
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-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-13 20:50:08 -06:00
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));
2021-01-13 16:47:16 -06:00
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-14 11:52:01 -06:00
// Render the scene tree
render(root, shader, Matrix(1.0f));
2021-01-19 16:01:37 -06:00
// Stop rendering to texture
screen.Disable();
// // Render the screen
screenShader->Use();
screenShader->UpdateTex(true, screen.tex);
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
lua->Close();
2021-01-13 10:42:57 -06:00
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}