Somewhat working camera

This commit is contained in:
Dynamitos
2022-03-19 22:45:30 +01:00
parent 84049a762c
commit cd28e433cc
41 changed files with 602 additions and 496 deletions
+35 -23
View File
@@ -74,23 +74,23 @@ void Actor::detachChild(PActor child)
child->setParent(nullptr);
child->notifySceneAttach(nullptr);
}
void Actor::setWorldLocation(Vector location)
{
rootComponent->setWorldLocation(location);
}
void Actor::setWorldRotation(Quaternion rotation)
{
rootComponent->setWorldRotation(rotation);
}
void Actor::setWorldRotation(Vector rotation)
{
rootComponent->setWorldRotation(rotation);
}
void Actor::setWorldScale(Vector scale)
{
rootComponent->setWorldScale(scale);
}
//void Actor::setAbsoluteLocation(Vector location)
//{
// rootComponent->setWorldLocation(location);
//}
//
//void Actor::setAbsoluteRotation(Quaternion rotation)
//{
// rootComponent->setAbsoluteRotation(rotation);
//}
//void Actor::setAbsoluteRotation(Vector rotation)
//{
// rootComponent->setAbsoluteRotation(rotation);
//}
//void Actor::setWorldScale(Vector scale)
//{
// rootComponent->setWorldScale(scale);
//}
void Actor::setRelativeLocation(Vector location)
{
rootComponent->setRelativeLocation(location);
@@ -107,17 +107,29 @@ void Actor::setRelativeScale(Vector scale)
{
rootComponent->setRelativeScale(scale);
}
void Actor::addWorldTranslation(Vector translation)
//void Actor::addAbsoluteTranslation(Vector translation)
//{
// rootComponent->addAbsoluteTranslation(translation);
//}
//void Actor::addAbsoluteRotation(Quaternion rotation)
//{
// rootComponent->addAbsoluteRotation(rotation);
//}
//void Actor::addAbsoluteRotation(Vector rotation)
//{
// rootComponent->addAbsoluteRotation(rotation);
//}
void Actor::addRelativeLocation(Vector translation)
{
rootComponent->addWorldTranslation(translation);
rootComponent->addRelativeLocation(translation);
}
void Actor::addWorldRotation(Quaternion rotation)
void Actor::addRelativeRotation(Quaternion rotation)
{
rootComponent->addWorldRotation(rotation);
rootComponent->addRelativeRotation(rotation);
}
void Actor::addWorldRotation(Vector rotation)
void Actor::addRelativeRotation(Vector rotation)
{
rootComponent->addWorldRotation(rotation);
rootComponent->addRelativeRotation(rotation);
}
PComponent Actor::getRootComponent()
{
+11 -7
View File
@@ -26,19 +26,23 @@ public:
void addChild(PActor child);
void detachChild(PActor child);
Array<PActor> getChildren();
void setWorldLocation(Vector location);
void setWorldRotation(Quaternion rotation);
void setWorldRotation(Vector rotation);
void setWorldScale(Vector scale);
//void setAbsoluteLocation(Vector location);
//void setAbsoluteRotation(Quaternion rotation);
//void setAbsoluteRotation(Vector rotation);
//void setWorldScale(Vector scale);
void setRelativeLocation(Vector location);
void setRelativeRotation(Quaternion rotation);
void setRelativeRotation(Vector rotation);
void setRelativeScale(Vector scale);
void addWorldTranslation(Vector translation);
void addWorldRotation(Quaternion rotation);
void addWorldRotation(Vector rotation);
//void addAbsoluteTranslation(Vector translation);
//void addAbsoluteRotation(Quaternion rotation);
//void addAbsoluteRotation(Vector rotation);
void addRelativeLocation(Vector translation);
void addRelativeRotation(Quaternion rotation);
void addRelativeRotation(Vector rotation);
PComponent getRootComponent();
void setRootComponent(PComponent newRoot);
-2
View File
@@ -14,8 +14,6 @@ CameraActor::CameraActor()
cameraComponent->aspectRatio = 1.777778f;
cameraComponent->setParent(sceneComponent);
cameraComponent->setOwner(this);
addWorldTranslation(Vector(0, 0, 50));
addWorldRotation(Vector(0, -100, 0));
}
CameraActor::~CameraActor()
+36 -41
View File
@@ -6,19 +6,16 @@
using namespace Seele;
CameraComponent::CameraComponent()
: aspectRatio(0)
, fieldOfView(0)
: aspectRatio(0)
, fieldOfView(0)
, bNeedsViewBuild(true)
, bNeedsProjectionBuild(true)
, originPoint(0, 0, 0)
, cameraPosition(0, 0, 50)
, eye(Vector())
, projectionMatrix(Matrix4())
, viewMatrix(Matrix4())
, projectionMatrix(Matrix4())
, viewMatrix(Matrix4())
{
distance = 50;
rotationX = 0;
rotationY = -1.f;
rotationY = 0;
setRelativeLocation(Vector(0, 10, -50));
}
CameraComponent::~CameraComponent()
@@ -27,54 +24,52 @@ CameraComponent::~CameraComponent()
void CameraComponent::mouseMove(float deltaX, float deltaY)
{
//0.01 mouse sensitivity
rotationX += deltaX/500.f;
rotationY += deltaY/500.f;
rotationY = std::clamp(rotationY, -1.5f, 1.5f);
getParent()->setWorldRotation(glm::rotate(glm::quat(), rotationX, Vector(0, 1, 0)));
getParent()->setWorldRotation(glm::rotate(glm::quat(), rotationY, Vector(1, 0, 0)));
bNeedsViewBuild = true;
rotationX -= deltaX / 1000.f;
rotationY += deltaY / 1000.f;
//std::cout << "X:" << rotationX << " Y: " << rotationY << std::endl;
setRelativeRotation(Vector(rotationY, rotationX, 0));
bNeedsViewBuild = true;
}
void CameraComponent::mouseScroll(double x)
void CameraComponent::mouseScroll(float x)
{
distance -= static_cast<float>(x);
distance = std::max(distance, 1.f);
getParent()->addWorldTranslation(Vector(0, 0, x));
bNeedsViewBuild = true;
addRelativeLocation(getTransform().getForward()*x);
bNeedsViewBuild = true;
}
void CameraComponent::moveOrigin(float up)
void CameraComponent::moveX(float amount)
{
originPoint.y += up;
getParent()->addWorldTranslation(Vector(0, up, 0));
bNeedsViewBuild = true;
addRelativeLocation(getTransform().getForward()*amount);
bNeedsViewBuild = true;
}
void CameraComponent::moveY(float amount)
{
addRelativeLocation(getTransform().getRight()*amount);
bNeedsViewBuild = true;
}
void CameraComponent::buildViewMatrix()
{
Matrix4 rotation = glm::rotate(Matrix4(1.0f), rotationX, Vector(0, 1, 0));
rotation = glm::rotate(rotation, rotationY, Vector(1, 0, 0));
Vector4 translation(0, 0, distance, 1);
translation = rotation * translation;
Vector eyePos = getTransform().getPosition();
Vector lookAt = eyePos + getTransform().getForward();
std::cout << "Eye: " << eyePos << " lookAt: " << lookAt << std::endl;
viewMatrix = glm::lookAt(eyePos, lookAt, Vector(0, 1, 0));
cameraPosition = originPoint + Vector(translation);
viewMatrix = glm::lookAt(cameraPosition, originPoint, Vector(0, 1, 0));
bNeedsViewBuild = false;
bNeedsViewBuild = false;
}
void CameraComponent::buildProjectionMatrix()
{
projectionMatrix = glm::perspective(fieldOfView, aspectRatio, 1.0f, 1000.f);
static Matrix4 correctionMatrix =
Matrix4(
Vector4(1, 0, 0, 0),
Vector4(0, 1, 0, 0),
Vector4(0, 0, 0.5f, 0),
Vector4(0, 0, 0.5f, 1));
projectionMatrix = correctionMatrix * projectionMatrix;
bNeedsProjectionBuild = false;
static Matrix4 correctionMatrix =
Matrix4(
Vector4(1, 0, 0, 0),
Vector4(0, 1, 0, 0),
Vector4(0, 0, 0.5f, 0),
Vector4(0, 0, 0.5f, 1));
projectionMatrix = correctionMatrix * projectionMatrix;
bNeedsProjectionBuild = false;
}
+34 -42
View File
@@ -10,51 +10,43 @@ public:
CameraComponent();
virtual ~CameraComponent();
Matrix4 getViewMatrix()
{
if (bNeedsViewBuild)
{
buildViewMatrix();
}
return viewMatrix;
}
Matrix4 getProjectionMatrix()
{
if (bNeedsProjectionBuild)
{
buildProjectionMatrix();
}
return projectionMatrix;
}
Vector getCameraPosition()
{
if(bNeedsViewBuild)
{
buildViewMatrix();
}
return cameraPosition;
}
void mouseMove(float deltaX, float deltaY);
void mouseScroll(double x);
void moveOrigin(float up);
float aspectRatio;
float fieldOfView;
Matrix4 getViewMatrix()
{
if (bNeedsViewBuild)
{
buildViewMatrix();
}
return viewMatrix;
}
Matrix4 getProjectionMatrix()
{
if (bNeedsProjectionBuild)
{
buildProjectionMatrix();
}
return projectionMatrix;
}
Vector getCameraPosition()
{
return getTransform().getPosition();
}
void mouseMove(float deltaX, float deltaY);
void mouseScroll(float x);
void moveX(float amount);
void moveY(float amount);
float aspectRatio;
float fieldOfView;
private:
bool bNeedsViewBuild;
bool bNeedsProjectionBuild;
void buildViewMatrix();
void buildProjectionMatrix();
bool bNeedsProjectionBuild;
void buildViewMatrix();
void buildProjectionMatrix();
float rotationX;
float rotationY;
float distance;
Vector eye;
Vector originPoint;
Vector cameraPosition;
//Transforms relative to actor
Matrix4 viewMatrix;
Matrix4 projectionMatrix;
//Transforms relative to actor
Matrix4 viewMatrix;
Matrix4 projectionMatrix;
float rotationX;
float rotationY;
};
DEFINE_REF(CameraComponent)
} // namespace Seele
+71 -135
View File
@@ -6,9 +6,6 @@ using namespace Seele;
Component::Component()
{
relativeLocation = Vector(0);
relativeRotation = Vector(0);
relativeScale = Vector(1, 1, 1);
}
Component::~Component()
{
@@ -89,162 +86,101 @@ void Component::notifySceneAttach(PScene scene)
child->notifySceneAttach(scene);
}
}
void Component::setWorldLocation(Vector location)
{
Vector newRelLocation = location;
if (parent != nullptr)
{
Transform parentToWorld = getParent()->getTransform();
newRelLocation = parentToWorld.inverseTransformPosition(location);
}
setRelativeLocation(newRelLocation);
}
void Component::setWorldRotation(Vector rotation)
{
Vector newRelRotator = rotation;
if (parent == nullptr)
{
setRelativeRotation(rotation);
}
else
{
setWorldRotation(toQuaternion(newRelRotator));
}
}
void Component::setWorldRotation(Quaternion rotation)
{
Quaternion newRelRotation = getRelativeWorldRotation(rotation);
setRelativeRotation(newRelRotation);
}
void Component::setWorldScale(Vector scale)
{
Vector newRelScale = scale;
if (parent != nullptr)
{
Transform parentToWorld = parent->getTransform();
newRelScale = scale * parentToWorld.getSafeScaleReciprocal(Vector4(parentToWorld.getScale(), 0));
}
setRelativeScale(newRelScale);
}
//void Component::setAbsoluteLocation(Vector location)
//{
// Vector newRelLocation = location;
// if (parent != nullptr)
// {
// Transform parentToWorld = getParent()->getTransform();
// newRelLocation = parentToWorld.inverseTransformPosition(location);
// }
// setRelativeLocation(newRelLocation);
//}
//void Component::setAbsoluteRotation(Vector rotation)
//{
// Vector newRelRotator = rotation;
// if (parent == nullptr)
// {
// setRelativeRotation(rotation);
// }
// else
// {
// setAbsoluteRotation(toQuaternion(newRelRotator));
// }
//}
//void Component::setAbsoluteRotation(Quaternion rotation)
//{
// Quaternion newRelRotation = getRelativeWorldRotation(rotation);
// setRelativeRotation(newRelRotation);
//}
//void Component::setWorldScale(Vector scale)
//{
// Vector newRelScale = scale;
// if (parent != nullptr)
// {
// Transform parentToWorld = parent->getTransform();
// newRelScale = scale * parentToWorld.getSafeScaleReciprocal(Vector4(parentToWorld.getScale(), 0));
// }
// setRelativeScale(newRelScale);
//}
void Component::setRelativeLocation(Vector location)
{
setRelativeLocationAndRotation(location, relativeRotation);
transform = Transform(location, transform.getRotation(), transform.getScale());
}
void Component::setRelativeRotation(Vector rotation)
{
setRelativeLocationAndRotation(relativeLocation, toQuaternion(rotation));
transform = Transform(transform.getPosition(), Quaternion(rotation), transform.getScale());
}
void Component::setRelativeRotation(Quaternion rotation)
{
setRelativeLocationAndRotation(relativeLocation, rotation);
transform = Transform(transform.getPosition(), rotation, transform.getScale());
}
void Component::setRelativeScale(Vector scale)
{
if (scale != relativeScale)
{
relativeScale = scale;
updateComponentTransform(relativeRotation);
}
transform = Transform(transform.getPosition(), transform.getRotation(), scale);
}
void Component::addWorldTranslation(Vector translation)
//void Component::addAbsoluteTranslation(Vector translation)
//{
// const Vector newWorldLocation = translation + getTransform().getPosition();
// setAbsoluteLocation(newWorldLocation);
//}
//void Component::addAbsoluteRotation(Vector rotation)
//{
// const Quaternion newWorldRotation = toQuaternion(rotation) * getTransform().getRotation();
// setAbsoluteRotation(newWorldRotation);
//}
//void Component::addAbsoluteRotation(Quaternion rotation)
//{
// const Quaternion newWorldRotation = rotation * getTransform().getRotation();
// setAbsoluteRotation(newWorldRotation);
//}
void Component::addRelativeLocation(Vector translation)
{
const Vector newWorldLocation = translation + getTransform().getPosition();
setWorldLocation(newWorldLocation);
transform = Transform(transform.getPosition() + translation, transform.getRotation(), transform.getScale());
}
void Component::addWorldRotation(Vector rotation)
void Component::addRelativeRotation(Vector rotation)
{
const Quaternion newWorldRotation = toQuaternion(rotation) * getTransform().getRotation();
setWorldRotation(newWorldRotation);
transform = Transform(transform.getPosition(), transform.getRotation() + Quaternion(rotation), transform.getScale());
}
void Component::addWorldRotation(Quaternion rotation)
void Component::addRelativeRotation(Quaternion rotation)
{
const Quaternion newWorldRotation = rotation * getTransform().getRotation();
setWorldRotation(newWorldRotation);
transform = Transform(transform.getPosition(), transform.getRotation(), transform.getScale());
}
Transform Component::getTransform() const
{
return transform;
}
void Component::internalSetTransform(Vector newLocation, Quaternion newRotation)
Transform Component::getAbsoluteTransform() const
{
if (parent != nullptr)
{
Transform parentTransform = parent->getTransform();
newLocation = parentTransform.inverseTransformPosition(newLocation);
newRotation = glm::inverse(parentTransform.getRotation()) * newRotation;
}
const Vector newRelRotation = toRotator(newRotation);
if (newLocation != relativeLocation || newRelRotation != relativeLocation)
{
relativeLocation = newLocation;
relativeRotation = newRelRotation;
updateComponentTransform(newRelRotation);
}
}
void Component::propagateTransformUpdate()
{
for (auto child : children)
{
child->updateComponentTransform(child->relativeRotation);
}
}
void Component::updateComponentTransform(Quaternion relativeRotationQuat)
{
if (parent != nullptr && !parent->bComponentTransformClean)
{
parent->updateComponentTransform(parent->relativeRotation);
if (bComponentTransformClean)
{
return;
}
}
bComponentTransformClean = true;
Transform newTransform;
const Transform relTransform(relativeLocation, relativeRotationQuat, relativeScale);
if(parent != nullptr)
{
newTransform = relTransform * parent->getTransform();
return transform + parent->getAbsoluteTransform();
}
else
{
newTransform = relTransform;
}
bool bHasChanged = !getTransform().equals(newTransform);
if (bHasChanged)
{
transform = newTransform;
propagateTransformUpdate();
}
}
Quaternion Component::getRelativeWorldRotation(Quaternion worldRotation)
{
Quaternion newRelRotation = worldRotation;
if (parent != nullptr)
{
const Transform parentToWorld = parent->getTransform();
const Quaternion parentToWorldQuat = parentToWorld.getRotation();
const Quaternion newRelQuat = glm::inverse(parentToWorldQuat) * worldRotation;
newRelRotation = newRelQuat;
}
return newRelRotation;
}
void Component::setRelativeLocationAndRotation(Vector newLocation, Quaternion newRotation)
{
if (!bComponentTransformClean)
{
updateComponentTransform(toQuaternion(relativeRotation));
}
const Transform desiredRelTransform(newLocation, newRotation);
const Transform desiredWorldTransform = desiredRelTransform; // Check for absolutes etc
internalSetTransform(desiredWorldTransform.getPosition(), desiredWorldTransform.getRotation());
}
return transform;
}
+13 -16
View File
@@ -28,27 +28,33 @@ public:
void addChildComponent(PComponent component);
virtual void notifySceneAttach(PScene scene);
void setWorldLocation(Vector location);
void setWorldRotation(Vector rotation);
void setWorldRotation(Quaternion rotation);
void setWorldScale(Vector scale);
//void setAbsoluteLocation(Vector location);
//void setAbsoluteRotation(Vector rotation);
//void setAbsoluteRotation(Quaternion rotation);
//void setWorldScale(Vector scale);
void setRelativeLocation(Vector location);
void setRelativeRotation(Vector rotation);
void setRelativeRotation(Quaternion rotation);
void setRelativeScale(Vector scale);
void addWorldTranslation(Vector translation);
void addWorldRotation(Vector rotation);
void addWorldRotation(Quaternion rotation);
//void addAbsoluteTranslation(Vector translation);
//void addAbsoluteRotation(Vector rotation);
//void addAbsoluteRotation(Quaternion rotation);
void addRelativeLocation(Vector translation);
void addRelativeRotation(Vector rotation);
void addRelativeRotation(Quaternion rotation);
Transform getTransform() const;
Transform getAbsoluteTransform() const;
template<typename ComponentType>
RefPtr<ComponentType> getComponent()
{
return tryFindComponent<ComponentType>();
}
private:
template<typename ComponentType>
RefPtr<ComponentType> tryFindComponent()
@@ -68,15 +74,6 @@ private:
}
return nullptr;
}
void internalSetTransform(Vector newLocation, Quaternion newRotation);
void propagateTransformUpdate();
void updateComponentTransform(Quaternion relativeRotationQuat);
Quaternion getRelativeWorldRotation(Quaternion worldRotation);
void setRelativeLocationAndRotation(Vector newLocation, Quaternion newRotation);
uint8 bComponentTransformClean : 1;
Vector relativeLocation;
Vector relativeRotation;
Vector relativeScale;
Transform transform;
PScene owningScene;
PActor owner;
@@ -50,5 +50,5 @@ void PrimitiveComponent::notifySceneAttach(PScene scene)
Matrix4 PrimitiveComponent::getRenderMatrix()
{
return getTransform().toMatrix();
return getAbsoluteTransform().toMatrix();
}
+14 -5
View File
@@ -19,12 +19,12 @@ Scene::Scene(Gfx::PGraphics graphics)
lightEnv.directionalLights[0].intensity = Vector4(1, 1, 1, 1);
lightEnv.numDirectionalLights = 1;
srand((unsigned int)time(NULL));
for(uint32 i = 0; i < 256; ++i)
for(uint32 i = 0; i < 16; ++i)
{
lightEnv.pointLights[i].colorRange = Vector4(frand(), frand(), frand(), frand() * 300);
lightEnv.pointLights[i].positionWS = Vector4(frand() * 100-50, frand(), frand() * 100-50, 1);
}
lightEnv.numPointLights = 256;
lightEnv.numPointLights = 16;
lightEnv.pointLights[0].colorRange = Vector4(1, 0, 1, 1000);
lightEnv.pointLights[0].positionWS = Vector4(0, 10, 0, 1);
}
@@ -41,19 +41,28 @@ void Scene::start()
}
}
static float lastUpdate;
static uint64 numUpdates;
Job Scene::beginUpdate(double deltaTime)
{
//std::cout << "Scene::beginUpdate" << std::endl;
auto startTime = std::chrono::high_resolution_clock::now();
Array<Job> jobs;
for(auto actor : rootActors)
{
jobs.addAll(actor->launchTick(static_cast<float>(deltaTime)));
}
co_await Job::all(jobs);
//std::cout << "Scene::beginUpdate finished waiting" << std::endl;
for(auto job : jobs)
auto endTime = std::chrono::high_resolution_clock::now();
float delta = std::chrono::duration_cast<std::chrono::seconds>(endTime - startTime).count();
lastUpdate += delta;
numUpdates++;
if(lastUpdate > 1.0f)
{
assert(job.done());
lastUpdate -= 1.0f;
std::cout << numUpdates << " updates per second" << std::endl;
numUpdates = 0;
}
}
+1
View File
@@ -19,6 +19,7 @@ struct DirectionalLight
struct PointLight
{
Vector4 positionWS;
//Vector4 positionVS;
Vector4 colorRange;
};
+1 -1
View File
@@ -123,7 +123,7 @@ public:
void update()
{
// lock should not be necessary, but lets keep it for now
std::scoped_lock lock(dataLock);
//std::scoped_lock lock(dataLock);
data = toBeWritten;
dirty = false;
}