Support for multiple cameras.

This commit is contained in:
2024-05-31 12:04:25 -04:00
parent 8bf605ce33
commit 98631e48ea
4 changed files with 44 additions and 35 deletions

View File

@@ -4,19 +4,19 @@
#include <types/texture.h>
#include <types/shader.h>
#include <types/entity/entity.h>
#include <J3ML/LinearAlgebra/Vector4.h>
#include <J3ML/LinearAlgebra/Vector3.h>
using J3ML::LinearAlgebra::Vector4;
using J3ML::LinearAlgebra::Vector3;
using J3ML::LinearAlgebra::Vector2;
class Camera; //Forward declaration of Camera.
// TODO: Move data to Entity / or rename to DataModelEntry
struct ByteArray
{
struct ByteArray {
std::vector<u8> bytes;
};
class Serializable
{
class Serializable {
virtual ByteArray Serialize() const;
virtual Serializable Deserialize(const ByteArray& data);
};
@@ -25,35 +25,27 @@ class Serializable
// A wrapper around a Tree Hierarchy Data Model.
class DataModel : public Entity {
public:
Entity * GetParent() const override
{
return nullptr;
}
Entity * GetParent() const override {return nullptr;}
std::vector<Entity> GetFlatEntityList();
template <class T>
void Serialize(T& archive)
{
void Serialize(T& archive) {
Entity::SerializeMemberData(archive);
archive & GetFlatEntityList();
}
void SetParent(Entity *parent) override
{
void SetParent(Entity *parent) override {
throw std::runtime_error("Cannot set parent of Hierarchy Root!");
}
DataModel() : Entity() {
}
DataModel() : Entity() {}
[[nodiscard]] int getEntityCount() const;
[[nodiscard]] int getMobyCount() const;
};
class World : public DataModel {
private:
Vector4 globalLightColor = {0, 0, 0, 0};
Camera* activeCamera = nullptr;
Vector3 globalLightColor = {0, 0, 0};
Vector4 globalFogColor = {0, 0, 0, 0};
Vector2 globalFogRange = {0, 0};
GLenum globalFogMode = NULL; //There's Linear, GL_EXP, GL_EXP2.
@@ -61,20 +53,22 @@ private:
public:
World() : DataModel() {}
void setAmbientLightColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);
void setAmbientLightColor(GLfloat red, GLfloat green, GLfloat blue);
void setGlobalFogColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);
void setGlobalFogRange(GLfloat start, GLfloat end);
void setGlobalFogDensity(GLfloat density);
void setGlobalFogMode(GLenum mode);
void setActiveCamera(Camera* camera);
GLenum getGlobalFogMode();
[[nodiscard]] GLenum getGlobalFogMode() const;
Vector4 getGlobalFogColor();
Vector2 getGlobalFogRange();
Vector4 getAmbientLightColor();
GLfloat getGlobalFogDensity();
Vector3 getAmbientLightColor();
[[nodiscard]] GLfloat getGlobalFogDensity() const;
Camera* getActiveCamera();
std::string name;
std::vector<VertexArray> geometryList;
std::vector<VertexArray> geometryList; //TODO store this as pointers.
std::vector<Texture*> textureList;
std::vector<Shader> shaderList;

View File

@@ -14,13 +14,14 @@ int main()
engine->init();
engine->window->setVsyncEnabled(false);
engine->window->setResizable(false);
engine->world->setAmbientLightColor(1.0f, 1.0f, 1.0f, 0.0f);
engine->world->setAmbientLightColor(1.0f, 1.0f, 1.0f);
Shader test("test", engine->workingDir + "/assets/shaders/defaultVertex.glsl", engine->workingDir + "/assets/shaders/defaultFragment.glsl");
auto* camera = new(FreeCam);
camera->SetPos({0.0f, -2.0f, -5.0f});
camera->angle.y = 0.0f;
camera->SetParent(engine->world);
engine->world->setActiveCamera(camera);
auto* cube1 = new(Cube);
cube1->name = "cube1";

View File

@@ -1,6 +1,8 @@
#include <sstream>
#include <thread>
#include <engine/engine.h>
#include <engine/utils/instanceOf.h>
#include <types/entity/camera.h>
#include <JGL/JGL.h>
#include <JGL/Colors.h>
@@ -98,7 +100,7 @@ void Engine::initGL() const {
engine->frameCount++;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (auto& e : engine->world->GetChildren())
for (auto& e : world->GetChildren())
e->pre_render();
}
@@ -108,9 +110,13 @@ void Engine::initGL() const {
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
for (auto& e : engine->world->GetChildren())
if(world->getActiveCamera() != nullptr)
world->getActiveCamera()->render();
for (auto& e : world->GetChildren())
if (e->draw)
e->render();
if (auto* c = dynamic_cast<Camera*>(e); c == nullptr) //If it's not a camera.
e->render();
if (glGetError() != GL_NO_ERROR)
exit(0);

View File

@@ -116,13 +116,13 @@ Entity *Entity::GetFamilyTreeRoot() const {
return parent->GetFamilyTreeRoot();
}
void World::setAmbientLightColor(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) {
GLfloat ambient[4] = {red, green, blue, alpha};
globalLightColor.x = red; globalLightColor.y = green; globalLightColor.z = blue; globalLightColor.w = alpha;
void World::setAmbientLightColor(GLfloat red, GLfloat green, GLfloat blue) {
GLfloat ambient[4] = {red, green, blue, 0.0f};
globalLightColor.x = red; globalLightColor.y = green; globalLightColor.z = blue;
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
}
Vector4 World::getAmbientLightColor() {
Vector3 World::getAmbientLightColor() {
return globalLightColor;
}
@@ -152,11 +152,11 @@ void World::setGlobalFogMode(GLenum mode) {
globalFogMode = mode;
}
GLenum World::getGlobalFogMode() {
GLenum World::getGlobalFogMode() const {
return globalFogMode;
}
GLfloat World::getGlobalFogDensity() {
GLfloat World::getGlobalFogDensity() const {
return globalFogDensity;
}
@@ -164,3 +164,11 @@ void World::setGlobalFogDensity(GLfloat density) {
glFogf(GL_FOG_DENSITY, density);
globalFogDensity = density;
}
Camera *World::getActiveCamera() {
return activeCamera;
}
void World::setActiveCamera(Camera *camera) {
activeCamera = camera;
}