openblocks/core/src/rendering/renderer.cpp

173 lines
5.5 KiB
C++
Raw Normal View History

2024-09-27 16:41:03 +00:00
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <GL/gl.h>
#include <cstdio>
2024-09-27 20:29:51 +00:00
#include <glm/ext.hpp>
2024-10-15 16:52:22 +00:00
#include <glm/ext/matrix_float4x4.hpp>
#include <glm/ext/matrix_transform.hpp>
2024-09-27 20:29:51 +00:00
#include <glm/glm.hpp>
2024-09-27 21:16:52 +00:00
#include <glm/trigonometric.hpp>
2025-01-17 08:24:18 +00:00
#include <memory>
#include <vector>
2024-09-27 20:29:51 +00:00
#include "physics/util.h"
2024-09-27 17:19:35 +00:00
#include "shader.h"
2024-09-27 18:12:45 +00:00
#include "mesh.h"
2024-09-27 18:26:14 +00:00
#include "defaultmeshes.h"
2024-09-27 20:48:57 +00:00
#include "../camera.h"
2025-01-17 08:24:18 +00:00
#include "../common.h"
#include "../objects/part.h"
2024-10-15 16:52:22 +00:00
#include "skybox.h"
2024-11-19 22:58:32 +00:00
#include "surface.h"
2024-11-19 11:46:43 +00:00
#include "texture3d.h"
2024-09-27 16:41:03 +00:00
2024-09-27 16:31:14 +00:00
#include "renderer.h"
2024-09-27 16:41:03 +00:00
2024-09-27 17:19:35 +00:00
Shader *shader = NULL;
2024-10-09 15:50:20 +00:00
Shader *skyboxShader = NULL;
2024-09-27 20:34:32 +00:00
extern Camera camera;
2024-10-15 16:52:22 +00:00
Skybox* skyboxTexture = NULL;
2024-11-19 11:46:43 +00:00
Texture3D* studsTexture = NULL;
2024-09-27 16:31:14 +00:00
static int viewportWidth, viewportHeight;
void renderInit(GLFWwindow* window, int width, int height) {
viewportWidth = width, viewportHeight = height;
glViewport(0, 0, width, height);
2024-09-27 16:41:03 +00:00
2024-09-27 18:26:14 +00:00
initMeshes();
2024-09-27 20:35:00 +00:00
glEnable(GL_DEPTH_TEST);
2024-11-19 11:46:43 +00:00
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2024-09-27 20:35:00 +00:00
2024-10-15 16:52:22 +00:00
skyboxTexture = new Skybox({
"assets/textures/skybox/null_plainsky512_lf.jpg",
2024-10-15 17:02:55 +00:00
"assets/textures/skybox/null_plainsky512_rt.jpg",
2024-10-15 16:52:22 +00:00
"assets/textures/skybox/null_plainsky512_up.jpg",
"assets/textures/skybox/null_plainsky512_dn.jpg",
"assets/textures/skybox/null_plainsky512_ft.jpg",
"assets/textures/skybox/null_plainsky512_bk.jpg",
}, GL_RGB);
2024-10-09 16:19:37 +00:00
2024-11-19 22:58:32 +00:00
studsTexture = new Texture3D("assets/textures/studs.png", 128, 128, 6, GL_RGBA);
2024-11-19 11:46:43 +00:00
2024-09-27 17:19:35 +00:00
// Compile shader
2024-09-27 20:29:51 +00:00
shader = new Shader("assets/shaders/phong.vs", "assets/shaders/phong.fs");
2024-10-09 15:50:20 +00:00
skyboxShader = new Shader("assets/shaders/skybox.vs", "assets/shaders/skybox.fs");
2024-09-27 16:31:14 +00:00
}
2024-10-09 15:50:20 +00:00
void renderParts() {
2024-10-15 17:02:55 +00:00
glDepthMask(GL_TRUE);
2024-09-27 16:41:03 +00:00
2024-09-27 17:19:35 +00:00
// Use shader
shader->use();
2024-11-17 12:11:53 +00:00
// shader->set("objectColor", glm::vec3(1.0f, 0.5f, 0.31f));
// shader->set("lightColor", glm::vec3(1.0f, 1.0f, 1.0f));
2024-09-27 20:29:51 +00:00
// view/projection transformations
glm::mat4 projection = glm::perspective(glm::radians(45.f), (float)viewportWidth / (float)viewportHeight, 0.1f, 100.0f);
2024-09-27 20:29:51 +00:00
glm::mat4 view = camera.getLookAt();
shader->set("projection", projection);
shader->set("view", view);
2024-11-17 12:11:53 +00:00
// shader->set("material", Material {
// // .ambient = glm::vec3(1.0f, 0.5f, 0.31f),
// .diffuse = glm::vec3(0.639216f, 0.635294f, 0.647059f),
// .specular = glm::vec3(0.5f, 0.5f, 0.5f),
// .shininess = 16.0f,
// });
2024-09-27 20:29:51 +00:00
shader->set("sunLight", DirLight {
.direction = glm::vec3(-0.2f, -1.0f, -0.3f),
.ambient = glm::vec3(0.2f, 0.2f, 0.2f),
.diffuse = glm::vec3(0.5f, 0.5f, 0.5f),
.specular = glm::vec3(1.0f, 1.0f, 1.0f),
});
shader->set("numPointLights", 0);
// shader->set("pointLights[0]", PointLight {
// .position = lightPos,
// .ambient = glm::vec3(0.4f, 0.4f, 0.4f),
// .diffuse = glm::vec3(1.0f, 1.0f, 1.0f),
// .specular = glm::vec3(1.0f, 1.0f, 1.0f),
// .constant = 1.0,
// .linear = 0.9,
// .quadratic = 0.32,
// });
2024-11-19 11:46:43 +00:00
studsTexture->activate(0);
shader->set("studs", 0);
2024-11-19 22:58:32 +00:00
// shader->set("surfaces[1]", SurfaceStuds);
shader->set("surfaces[1]", SurfaceStuds);
shader->set("surfaces[4]", SurfaceInlets);
2024-09-27 20:29:51 +00:00
// Pre-calculate the normal matrix for the shader
// Pass in the camera position
shader->set("viewPos", camera.cameraPos);
2025-01-17 08:24:18 +00:00
// TODO: Same as todo in src/physics/simulation.cpp
for (InstanceRef inst : workspace()->GetChildren()) {
2025-01-17 08:24:18 +00:00
if (inst->GetClass()->className != "Part") continue;
std::shared_ptr<Part> part = std::dynamic_pointer_cast<Part>(inst);
// if (inst->name == "Target") printf("(%f,%f,%f):(%f,%f,%f;%f,%f,%f;%f,%f,%f)\n",
// part->cframe.X(),
// part->cframe.Y(),
// part->cframe.Z(),
// part->cframe.RightVector().X(),
// part->cframe.UpVector().X(),
// part->cframe.LookVector().X(),
// part->cframe.RightVector().Y(),
// part->cframe.UpVector().Y(),
// part->cframe.LookVector().Y(),
// part->cframe.RightVector().Z(),
// part->cframe.UpVector().Z(),
// part->cframe.LookVector().Z()
// );
glm::mat4 model = part->cframe;
2025-02-09 10:35:19 +00:00
model = glm::scale(model, part->size);
shader->set("model", model);
shader->set("material", Material {
.diffuse = part->color,
.specular = glm::vec3(0.5f, 0.5f, 0.5f),
.shininess = 16.0f,
});
glm::mat3 normalMatrix = glm::mat3(glm::transpose(glm::inverse(model)));
shader->set("normalMatrix", normalMatrix);
2025-02-09 10:35:19 +00:00
shader->set("texScale", part->size);
2024-09-27 17:19:35 +00:00
CUBE_MESH->bind();
glDrawArrays(GL_TRIANGLES, 0, 36);
}
2024-10-09 15:50:20 +00:00
}
void renderSkyBox() {
2024-10-15 17:02:55 +00:00
glDepthMask(GL_FALSE);
2024-10-09 15:50:20 +00:00
skyboxShader->use();
glm::mat4 projection = glm::perspective(glm::radians(45.f), (float)viewportWidth / (float)viewportHeight, 0.1f, 100.0f);
2024-10-15 16:52:22 +00:00
// Remove translation component of view, making us always at (0, 0, 0)
glm::mat4 view = glm::mat4(glm::mat3(camera.getLookAt()));
2024-10-15 17:02:55 +00:00
2024-10-09 15:50:20 +00:00
skyboxShader->set("projection", projection);
skyboxShader->set("view", view);
2024-10-09 16:19:37 +00:00
skyboxShader->set("uTexture", 0);
2024-10-09 15:50:20 +00:00
CUBE_MESH->bind();
glDrawArrays(GL_TRIANGLES, 0, 36);
}
void render(GLFWwindow* window) {
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
renderSkyBox();
2024-10-15 17:02:55 +00:00
renderParts();
}
void setViewport(int width, int height) {
viewportWidth = width, viewportHeight = height;
2024-09-27 16:31:14 +00:00
}