Ambient light can now be set; Code cleanup

This commit is contained in:
Grayson Riffe (Laptop) 2021-11-15 14:05:44 -06:00
parent 231ac792d7
commit f884847513
14 changed files with 48 additions and 13 deletions

View File

@ -7,7 +7,7 @@ void MainState::onEnter() {
ap.load("example.nfpack");
test.create(ap.get("2mats.obj"), nf::Entity::Type::DYNAMIC);
test.setPosition(nf::Vec3(0.0, 1.5, -5.0));
plane.create(ap.get("env.obj"), nf::Entity::Type::ENVIRONMENT);
plane.create(ap.get("env.obj"), nf::Entity::Type::MAP);
plane.setScale(20.0);
plane.setPosition(0.0, -20.0, 0.0);
text.create("", nf::Vec2(0.1, 0.025), nf::Vec3(0.8));
@ -27,9 +27,9 @@ void MainState::onEnter() {
sound2.setVolume(3.0);
sound2.setEntity(test);
for (int x = 0; x < 5; x++) {
for (int y = 0; y < 5; y++) {
for (int z = 0; z < 5; z++) {
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
for (int z = 0; z < 3; z++) {
entities.push_back(new nf::Entity);
entities.back()->create(ap.get("2mats.obj"), nf::Entity::Type::DYNAMIC);
entities.back()->setPosition(nf::Vec3(5.0 + x * 2.05, 1.0 + y * 2.05, -5.0 + z * 2.05));
@ -40,6 +40,8 @@ void MainState::onEnter() {
grav = 2.0f;
setGravity(grav);
amb = 0.1f;
camera->setPosition(-20.0, 15.0, 0.0);
camera->setRotation(85.0, -30.0);
}
@ -103,6 +105,13 @@ void MainState::update(float deltaTime) {
}
gravText.setText("Gravity Scale: " + std::to_string(grav));
if (app->isKeyHeld(NFI_LEFT))
amb -= 0.01f;
if (app->isKeyHeld(NFI_RIGHT))
amb += 0.01f;
if (amb >= 0.0f)
setAmbientLight(amb);
if (app->isKeyPressed(NFI_ESCAPE))
app->quit();
}

View File

@ -29,6 +29,7 @@ private:
nf::Sound sound2;
float grav;
float amb;
std::vector<nf::Entity*> entities;
};

View File

@ -61,6 +61,7 @@
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)src\include\;$(ProjectDir)dep\include\</AdditionalIncludeDirectories>
<ObjectFileName>$(IntDir)obj\</ObjectFileName>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@ -92,6 +93,7 @@
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)src\include\;$(ProjectDir)dep\include\</AdditionalIncludeDirectories>
<ObjectFileName>$(IntDir)obj\</ObjectFileName>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>

View File

@ -22,6 +22,7 @@ uniform Light light[12];
uniform int numberOfLights;
uniform bool isContinued;
uniform float farPlane;
uniform float ambientStrength;
uniform sampler2D gBPos;
uniform sampler2D gBNorm;
@ -88,7 +89,6 @@ void main() {
float specPower = specTemp.r;
float matSpec = specTemp.g;
float ambientStrength = 0.1f;
vec3 ambient = ambientStrength * matDiff;
if (!isContinued)
color += ambient;

View File

@ -45,11 +45,21 @@ namespace nf {
SetClassLongPtr(m_window, GCLP_HCURSOR, (LONG_PTR)hCursor);
}
Renderer* Application::getRenderer() const {
if (!m_renderer)
Error("Application not running yet!");
return m_renderer;
}
AudioEngine* Application::getAudioEngine() const {
if(!m_audio)
Error("Application not running yet!");
return m_audio;
}
PhysicsEngine* Application::getPhysicsEngine() const {
if (!m_physics)
Error("Application not running yet!");
return m_physics;
}

View File

@ -54,6 +54,10 @@ namespace nf {
return camera;
}
void Gamestate::setAmbientLight(float strength) {
app->getRenderer()->setAmbient(strength);
}
void Gamestate::setGravity(const Vec3& gravity) {
app->getPhysicsEngine()->setGravity(gravity * 9.81f);
}

View File

@ -37,7 +37,7 @@ namespace nf {
bool convex = false, triangle = false;
if (m_type == Entity::Type::STATIC || m_type == Entity::Type::DYNAMIC)
convex = true;
else if (m_type == Entity::Type::ENVIRONMENT)
else if (m_type == Entity::Type::MAP)
triangle = true;
m_model = new Model(model, convex, triangle);
model->alreadyLoaded = true;

View File

@ -4,9 +4,6 @@
#include "Entity.h"
#include "Model.h"
//Remove this
#include "Input.h"
namespace nf {
class PhysicsErrorCallback : public PxErrorCallback {
virtual void reportError(PxErrorCode::Enum code, const char* message, const char* file, int line) {
@ -234,7 +231,7 @@ namespace nf {
act->userData = entity;
m_scene->addActor(*act);
}
else if (type == Entity::Type::ENVIRONMENT) {
else if (type == Entity::Type::MAP) {
PxRigidStatic* act = PxCreateStatic(*m_phy, PxTransform(PxIdentity), PxTriangleMeshGeometry(triangleMesh), *mat);
act->userData = entity;
m_scene->addActor(*act);

View File

@ -290,6 +290,12 @@ namespace nf {
SwapBuffers(m_hdc);
}
void Renderer::setAmbient(float am) {
if (am < 0.0f)
Error("Cannot have a negative ambient light strength!");
m_lightingShader->setUniform("ambientStrength", am);
}
void Renderer::loadBaseAssets() {
m_baseAP.load("base.nfpack");
const char* gBufferVertex = m_baseAP.get("gBufferVertex.shader")->data;

View File

@ -24,6 +24,7 @@ namespace nf {
void setWindowIcon(HICON hIcon);
void setWindowCursor(HCURSOR hCursor);
Renderer* getRenderer() const;
AudioEngine* getAudioEngine() const;
PhysicsEngine* getPhysicsEngine() const;
void addState(Gamestate* state, const std::string& stateName);

View File

@ -15,7 +15,7 @@ namespace nf {
enum class Type {
STATIC,
DYNAMIC,
ENVIRONMENT,
MAP,
DETAIL
};

View File

@ -29,6 +29,8 @@ namespace nf {
virtual void render(Renderer& renderer);
Camera* getCamera();
//Defaults to 0.1f
void setAmbientLight(float stength);
//In units of Earth gravity (9.81 m/s^2)
void setGravity(const Vec3& gravity);
void setGravity(float strength);

View File

@ -1,6 +1,8 @@
//TODO: Rework this file to only contain functions the frontend will need to access
#pragma once
//TODO: Rework this file to only contain functions the frontend will need to access
//Maybe a implementation define here?
#include <chrono>
#include <unordered_map>
#include <array>

View File

@ -30,6 +30,7 @@ namespace nf {
void render(Cubemap& in);
void doFrame(Camera* camera, float dT);
void setAmbient(float am);
~Renderer();
private: