More accurate lighting.

Sacrificed one texture unit to make it such that point lights on things that are multi-textured look the same as on regular things.
This commit is contained in:
2024-07-16 20:09:49 -04:00
parent e550dea76e
commit 7f65fee891
13 changed files with 92 additions and 70 deletions

View File

@@ -49,7 +49,7 @@ CPMAddPackage(
CPMAddPackage(
NAME JGL
URL https://git.redacted.cc/josh/JGL/archive/Prerelease-18.zip
URL https://git.redacted.cc/josh/JGL/archive/Prerelease-20.zip
)
CPMAddPackage(

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.2 KiB

After

Width:  |  Height:  |  Size: 6.1 KiB

View File

@@ -49,7 +49,7 @@ private:
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.
GLenum globalFogMode = 0; ///There's Linear, GL_EXP, GL_EXP2.
GLfloat globalFogDensity = 0.0f;
public:

View File

@@ -22,7 +22,7 @@ private:
///Loads the geometry for it.
void loadGeometry();
///The scale it should be rendered at.
GLfloat scale = 1.0f;
Vector3 scale = {1.0f, 1.0f, 1.0f};
protected:
std::string modelPath;
std::string texturePath;
@@ -75,7 +75,7 @@ public:
[[nodiscard]] Entity* GetFamilyTreeRoot() const;
Entity *Add(Entity *newChild);
void setScale(const float& multiplier);
[[nodiscard]] GLfloat getScale() const;
[[nodiscard]] Vector3 getScale() const;
// TODO: Constrain to DerivedEntityType
template <typename T>

View File

@@ -8,11 +8,11 @@ public:
std::string name;
///The shader program
GLuint id = NULL;
GLuint id = 0;
///The individual shaders
GLuint vertex = NULL;
GLuint fragment = NULL;
GLuint vertex = 0;
GLuint fragment = 0;
Shader(const char* name, const std::string& vertexPath, const std::string& fragmentPath);
Shader(const char* name, const std::string& filePath, const GLenum& type);

View File

@@ -17,7 +17,7 @@ public:
//The "camera point" is the position the camera will want to be while following the player.
//We will probably end up having a different camera point class controlled by the "world".
void update(float elapsed) override;
//void update(float elapsed) override;
void pre_render() override;
void loadTexture() override;
Cube();

View File

@@ -1,15 +1,11 @@
#include <cube.h>
#include <Redacted3D/engine/engine.h>
#include <ball.h>
void Cube::pre_render() {
getGeometry();
setAngle(getAngle().Add(24 * engine->frameDelta));
}
void Cube::update(float elapsed) {
}
Cube::Cube() {
name = "cube";
modelPath = "assets/models/cube.obj";
@@ -19,13 +15,13 @@ Cube::Cube() {
void Cube::loadTexture() {
//MultiTexture texture(this, texturePath.c_str(), true);
std::vector<std::string> textures;
textures.emplace_back("assets/textures/motion/default.png");
textures.emplace_back("assets/textures/motion/1.png");
textures.emplace_back("assets/textures/motion/2.png");
textures.emplace_back("assets/textures/motion/3.png");
textures.emplace_back("assets/textures/motion/4.png");
textures.emplace_back("assets/textures/motion/5.png");
textures.emplace_back("assets/textures/motion/6.png");
std::vector<std::string> textures = {
"assets/textures/motion/default.png", "assets/textures/motion/1.png", "assets/textures/motion/2.png",
"assets/textures/motion/3.png", "assets/textures/motion/4.png", "assets/textures/motion/5.png",
"assets/textures/motion/6.png"
};
MotionTexture texture(this, textures, 250, true);
}

View File

@@ -39,19 +39,21 @@ void FreeCam::pre_render() {
setAngle(getAngle().x, getAngle().y, getAngle().z + 75.0f * engine->frameDelta);
}
Vector3 textAngle = {0,0,0};
void FreeCam::jglRenderPass() {
textAngle.x = textAngle.x - (96.f * engine->frameDelta);
using namespace JGL;
J3D::Begin();
J3D::DrawString(JGL::Colors::Red, "3D Sample Text.", {0, 0, 0}, 0.0225, 32, Fonts::Jupiteroid);
J3D::DrawString(JGL::Colors::Red, "Text", {0, -2, 0}, textAngle, 4.f, 32, Fonts::Jupiteroid);
J3D::End();
J2D::Begin();
J2D::FillRect(JGL::Colors::Blue, {32, 32}, {100.5, 100.5});
J2D::FillTriangle(JGL::Colors::Yellow, {{140, 200},{135, 100},{105, 100}});
J2D::FillRect({255,0,0,128}, {0, 72}, {100, 100});
J2D::FillCircle(JGL::Colors::White, {16, 128}, 12, 16);
J2D::DrawString(JGL::Colors::White, "Framerate: " + std::to_string((int) engine->framerate()), 0, -16, 1, 16, Fonts::Jupiteroid);
J2D::DrawString(JGL::Colors::White, "Framecount: " + std::to_string(engine->frameCount), 0, -33, 1,16, Fonts::Jupiteroid);
J2D::DrawString(JGL::Colors::White, "Position: " + std::to_string(position.x) + " " + std::to_string(position.y) + " " + std::to_string(position.z), 0, -50, 1,16, Fonts::Jupiteroid);
J2D::DrawString(JGL::Colors::White, "ViewAngle: " + std::to_string(getAngle().x) + " " + std::to_string(getAngle().y) + " " + std::to_string(getAngle().z), 0, -67, 1,16, Fonts::Jupiteroid);
J2D::DrawString(JGL::Colors::White, "ViewAngle: " + std::to_string(getAngle().x) + " " + std::to_string(getAngle().y) + " " + std::to_string(getAngle().z), 0, -67, 1,16, Fonts::Jupiteroid);
J2D::End();
}

View File

@@ -3,6 +3,7 @@
#include <JGL/JGL.h>
#include <Redacted3D/engine/engine.h>
#include <Redacted3D/types/entity/camera.h>
#include <jlog/jlog.hpp>
using namespace J3ML;
@@ -81,6 +82,7 @@ void Engine::initGL() const {
glDepthMask(GL_TRUE);
glEnable(GL_CULL_FACE);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_LIGHT0);
}
void Engine::init() {
@@ -110,8 +112,11 @@ void Engine::render() {
if (auto* c = dynamic_cast<Camera*>(e); c == nullptr) //If it's not a camera.
e->render();
if (glGetError() != GL_NO_ERROR)
int glError = glGetError();
if (glError != GL_NO_ERROR) {
FATAL("GL Error: " + std::to_string(glError))
exit(0);
}
}
void Engine::jglRenderPass() {

View File

@@ -42,7 +42,7 @@ Entity::Entity() {
ticksAlive = 0;
children = std::vector<Entity*> ();
parent = nullptr;
scale = 1.0f;
scale = {1.0f, 1.0f, 1.0f};
//UUIDv4::UUIDGenerator<std::mt19937_64> uuidGenerator;
//UUIDv4::UUID id = uuidGenerator.getUUID();
@@ -50,10 +50,10 @@ Entity::Entity() {
}
void Entity::setScale(const float &multiplier) {
scale = multiplier;
scale.Set(multiplier, multiplier, multiplier);
}
GLfloat Entity::getScale() const {
Vector3 Entity::getScale() const {
return scale;
}
@@ -79,24 +79,47 @@ VertexArray* Entity::getGeometry() {
return getGeometry();
}
inline Texture* baseTexture = new Texture;
void Entity::render() {
bool multiTexture = false;
bool motionTexture = false;
if (!baseTexture) {
Texture t;
glGenTextures(1, &t.id);
glBindTexture(GL_TEXTURE_2D, t.id);
if (auto* t = dynamic_cast<MultiTexture*>(getTexture()))
multiTexture = true;
unsigned char whitePixel[4] = { 255, 255, 255, 255};
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, whitePixel);
if (!multiTexture)
if (auto* t = dynamic_cast<MotionTexture*>(getTexture()))
motionTexture = true;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glActiveTexture(0);
baseTexture = new Texture(t);
}
MultiTexture texture;
texture.id = baseTexture->id;
if (auto* t = dynamic_cast<MultiTexture*>(getTexture())) {
texture.multi.emplace_back(*t);
for (const auto &item: t->multi)
texture.multi.push_back(item);
}
else if (auto* j = dynamic_cast<MotionTexture*>(getTexture()))
texture.multi.push_back(*j->current());
else if (auto* k = dynamic_cast<Texture*>(getTexture()))
texture.multi.push_back(*k);
glPushMatrix();
glTranslatef(position.x, position.y, position.z);
glRotatef(angle.x, 1.0f, 0.0f, 0.0f);
glRotatef(angle.y, 0.0f, 1.0f, 0.0f);
glRotatef(angle.z, 0.0f, 0.0f, 1.0f);
if (getScale() != 1.0f)
glScalef(getScale(), getScale(), getScale());
if (getScale().x != 1.0f || getScale().y != 1.0f || getScale().z != 1.0f)
glScalef(getScale().x, getScale().y, getScale().z);
//Set up texture.
glActiveTexture(GL_TEXTURE0);
@@ -104,7 +127,9 @@ void Entity::render() {
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBindTexture(GL_TEXTURE_2D, motionTexture ? ((MotionTexture*)getTexture())->current()->id : getTexture()->id);
glBindTexture(GL_TEXTURE_2D, texture.id);
//Texture unit mode.
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
if (!engine->useVBO)
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &getGeometry()->vertices[0].x),
@@ -114,35 +139,26 @@ void Entity::render() {
glBindBuffer(GL_ARRAY_BUFFER, getGeometry()->tbo),
glTexCoordPointer(2, GL_FLOAT, 0, nullptr);
if (!multiTexture)
getGeometry()->draw();
if (multiTexture) {
auto* multi = (MultiTexture*) getTexture();
int i = 0;
for(auto& t : texture.multi) {
glActiveTexture(GL_TEXTURE1 + i);
glClientActiveTexture(GL_TEXTURE1 + i);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
int i = 0;
GLfloat blendingFactor[3] = {engine->world->getAmbientLightColor().x * 0.2f, engine->world->getAmbientLightColor().y * 0.2f, engine->world->getAmbientLightColor().z * 0.2f};
for(auto& texture : multi->multi) {
glActiveTexture(GL_TEXTURE1 + i);
glClientActiveTexture(GL_TEXTURE1 + i);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
if (!engine->useVBO)
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &getGeometry()->vertices[0].x),
glTexCoordPointer(2, GL_FLOAT, sizeof(TextureCoordinate), &getGeometry()->texCoords[0].x);
if (!engine->useVBO)
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &getGeometry()->vertices[0].x),
glTexCoordPointer(2, GL_FLOAT, sizeof(TextureCoordinate), &getGeometry()->texCoords[0].x);
if (engine->useVBO)
glBindBuffer(GL_ARRAY_BUFFER, getGeometry()->tbo),
glTexCoordPointer(2, GL_FLOAT, 0, nullptr);
if (engine->useVBO)
glBindBuffer(GL_ARRAY_BUFFER, getGeometry()->tbo),
glTexCoordPointer(2, GL_FLOAT, 0, nullptr);
//Texture unit mode.
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
//Makes the lighting work the way you'd think it would.
glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, blendingFactor);
glBindTexture(GL_TEXTURE_2D, texture.id);
glBindTexture(GL_TEXTURE_2D, t.id);
i++;
}
getGeometry()->draw();
@@ -156,7 +172,6 @@ void Entity::render() {
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
}
//Reset texture unit 0.
glActiveTexture(GL_TEXTURE0);
@@ -220,9 +235,10 @@ void Entity::erase() {
AABB Entity::getAABB() {
AABB aabb = Collision::genMinimallyEnclosingAABB(getGeometry());
if (getScale() != 1)
aabb.minPoint *= scale,
aabb.maxPoint *= scale;
//TODO this won't look right.
if (getScale().x != 1)
aabb.minPoint *= scale.x,
aabb.maxPoint *= scale.x;
aabb.minPoint += position;
aabb.maxPoint += position;
@@ -231,7 +247,8 @@ AABB Entity::getAABB() {
Sphere Entity::getSphere() {
Sphere sphere = Collision::genMinimallyEnclosingSphere(getGeometry());
sphere.Radius *= scale;
//TODO this won't look right.
sphere.Radius *= scale.x;
sphere.Position = position;
return sphere;
}

View File

@@ -12,7 +12,7 @@ void Skybox::render() {
glTranslatef(position.x ,position.y, position.z);
glBindTexture(GL_TEXTURE_2D, getTexture()->id);
glCullFace(GL_FRONT);
glScalef(getScale(), getScale(), getScale());
glScalef(getScale().x, getScale().y, getScale().z);
getGeometry()->draw();
glCullFace(GL_BACK);
glBindTexture(GL_TEXTURE_2D,0);

View File

@@ -1,9 +1,10 @@
#include <Redacted3D/types/texture.h>
#include <ReTexture/rTexture.h>
#include <Redacted3D/types/texture.h>
#include <Redacted3D/engine/engine.h>
void Texture::load(Entity* entity, const std::string& file, bool storeOnTextureList) {
auto* texture = new RTexture(file, {RTextureFlag::INVERT_Y});
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
if (texture->format == RTextureFormat::RGBA)
@@ -11,6 +12,7 @@ void Texture::load(Entity* entity, const std::string& file, bool storeOnTextureL
if (texture->format == RTextureFormat::RGB)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture->width, texture->height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture->pixelData.data());
delete texture;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@@ -74,7 +76,7 @@ MultiTexture::MultiTexture(Entity* entity, const char* pathContainingTextures, b
//You cannot have more than 8 total textures rendered in one pass.
//In Fixed-Function OpenGL you only have 8 TMUs available.
//TODO: multi-pass multi-texturing (will be slower).
if (multi.size() > 7)
if (multi.size() > 6)
engine->setError(ENGINE_ERROR_CODE::MULTI_TEXTURE_SIZE_EXCEEDS, true);
}