Updated to use Phong-Bunn Lighting

This commit is contained in:
OusmBlueNinja 2025-04-02 10:41:31 -05:00
parent 939941b8f8
commit 20dab3b4a7
10 changed files with 527 additions and 228 deletions

View File

@ -23,16 +23,19 @@ GLuint g_LightIconTex = 0;
ImTextureID g_LightIconImTex = 0;
// Helper function to load a texture.
bool LoadIconTexture(const char* filepath, GLuint &texOut, ImTextureID &imTexOut) {
bool LoadIconTexture(const char *filepath, GLuint &texOut, ImTextureID &imTexOut)
{
int w, h, channels;
unsigned char *data = stbi_load(filepath, &w, &h, &channels, 0);
if (!data) {
if (!data)
{
printf("Failed to load icon texture: %s\n", filepath);
return false;
}
// Overwrite loaded data with white pixels (all channels set to 255)
int total = w * h * channels;
for (int i = 0; i < total; i++) {
for (int i = 0; i < total; i++)
{
data[i] = 255;
}
glGenTextures(1, &texOut);
@ -50,7 +53,8 @@ bool LoadIconTexture(const char* filepath, GLuint &texOut, ImTextureID &imTexOut
}
// Editor camera.
struct EditorCamera {
struct EditorCamera
{
glm::vec3 position = glm::vec3(0.0f, 0.0f, 8.0f);
float yaw = -90.0f;
float pitch = 0.0f;
@ -62,7 +66,8 @@ struct EditorCamera {
EditorCamera editorCamera;
void ProcessEditorCamera(GLFWwindow* window, float deltaTime) {
void ProcessEditorCamera(GLFWwindow *window, float deltaTime)
{
glm::vec3 front;
front.x = cos(glm::radians(editorCamera.yaw)) * cos(glm::radians(editorCamera.pitch));
front.y = sin(glm::radians(editorCamera.pitch));
@ -78,25 +83,35 @@ void ProcessEditorCamera(GLFWwindow* window, float deltaTime) {
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
editorCamera.position += right * editorCamera.speed * deltaTime;
static double lastX = 0, lastY = 0;
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS) {
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS)
{
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
if (lastX == 0 && lastY == 0) { lastX = xpos; lastY = ypos; }
if (lastX == 0 && lastY == 0)
{
lastX = xpos;
lastY = ypos;
}
float offsetX = (float)(xpos - lastX);
float offsetY = (float)(lastY - ypos);
lastX = xpos;
lastY = ypos;
editorCamera.yaw += offsetX * editorCamera.sensitivity;
editorCamera.pitch += offsetY * editorCamera.sensitivity;
if (editorCamera.pitch > 89.0f) editorCamera.pitch = 89.0f;
if (editorCamera.pitch < -89.0f) editorCamera.pitch = -89.0f;
} else {
if (editorCamera.pitch > 89.0f)
editorCamera.pitch = 89.0f;
if (editorCamera.pitch < -89.0f)
editorCamera.pitch = -89.0f;
}
else
{
lastX = lastY = 0;
}
editorCamera.view = glm::lookAt(editorCamera.position, editorCamera.position + front, glm::vec3(0, 1, 0));
}
int main() {
int main()
{
if (!Engine::Init())
return 1;
// Load the light icon.
@ -120,13 +135,6 @@ int main() {
cube1->modelComponent->specularColor = glm::vec3(1.0f, 1.0f, 1.0f);
cube1->modelComponent->shininess = 32.0f;
Entity* cube2 = new Entity(EntityType::CUBE);
cube2->transform.position = glm::vec3(2.0f, 0.0f, 0.0f);
cube2->modelComponent = new ModelComponent();
cube2->modelComponent->diffuseColor = glm::vec3(0.2f, 0.8f, 0.2f);
cube2->modelComponent->specularColor = glm::vec3(1.0f, 1.0f, 1.0f);
cube2->modelComponent->shininess = 16.0f;
// Create two light entities.
Entity *light1 = new Entity(EntityType::LIGHT);
light1->transform.position = glm::vec3(0.0f, 10.0f, 0.0f);
@ -141,7 +149,6 @@ int main() {
light2->lightComponent->intensity = 1.0f;
entities.push_back(cube1);
entities.push_back(cube2);
entities.push_back(light1);
entities.push_back(light2);
@ -153,7 +160,8 @@ int main() {
static char newModelPath[256] = "";
float lastFrameTime = (float)glfwGetTime();
while (!glfwWindowShouldClose(Engine::GetWindow())) {
while (!glfwWindowShouldClose(Engine::GetWindow()))
{
float currentFrameTime = (float)glfwGetTime();
float deltaTime = currentFrameTime - lastFrameTime;
lastFrameTime = currentFrameTime;
@ -180,15 +188,18 @@ int main() {
// Left Panel: Entity List with Add/Remove buttons.
ImGui::Begin("Entity List");
for (int i = 0; i < entities.size(); i++) {
for (int i = 0; i < entities.size(); i++)
{
char label[32];
sprintf(label, "Entity %d", i);
if (ImGui::Selectable(label, selectedIndex == i)) {
if (ImGui::Selectable(label, selectedIndex == i))
{
selectedEntity = entities[i];
selectedIndex = i;
}
}
if (ImGui::Button("Add Cube")) {
if (ImGui::Button("Add Cube"))
{
Entity *newCube = new Entity(EntityType::CUBE);
newCube->transform.position = glm::vec3(0.0f);
newCube->modelComponent = new ModelComponent();
@ -199,7 +210,8 @@ int main() {
entities.push_back(newCube);
}
ImGui::SameLine();
if (ImGui::Button("Add Light")) {
if (ImGui::Button("Add Light"))
{
Entity *newLight = new Entity(EntityType::LIGHT);
newLight->transform.position = glm::vec3(0.0f);
newLight->lightComponent = new LightComponent();
@ -207,10 +219,12 @@ int main() {
newLight->lightComponent->intensity = 1.0f;
entities.push_back(newLight);
}
if (selectedEntity && ImGui::Button("Remove Selected")) {
if (selectedEntity && ImGui::Button("Remove Selected"))
{
// Remove selected entity.
auto it = std::find(entities.begin(), entities.end(), selectedEntity);
if (it != entities.end()) {
if (it != entities.end())
{
delete *it;
entities.erase(it);
selectedEntity = nullptr;
@ -221,62 +235,75 @@ int main() {
// Right Panel: Entity Inspector.
ImGui::Begin("Entity Inspector");
if (selectedEntity) {
if (selectedEntity)
{
ImGui::Text("Transform");
ImGui::DragFloat3("Position", glm::value_ptr(selectedEntity->transform.position), 0.1f);
ImGui::DragFloat3("Rotation", glm::value_ptr(selectedEntity->transform.rotation), 0.5f);
ImGui::DragFloat3("Scale", glm::value_ptr(selectedEntity->transform.scale), 0.1f);
if (selectedEntity->GetType() == EntityType::CUBE && selectedEntity->modelComponent) {
if (selectedEntity->GetType() == EntityType::CUBE && selectedEntity->modelComponent)
{
ImGui::Separator();
ImGui::Text("Model Properties");
ImGui::ColorEdit3("Diffuse", glm::value_ptr(selectedEntity->modelComponent->diffuseColor));
ImGui::ColorEdit3("Specular", glm::value_ptr(selectedEntity->modelComponent->specularColor));
ImGui::DragFloat("Shininess", &selectedEntity->modelComponent->shininess, 1.0f, 1.0f, 128.0f);
// Button to open model editor popup.
if (ImGui::Button("Change Model")) {
if (ImGui::Button("Change Model"))
{
showModelPopup = true;
// Pre-fill popup with current model path.
strncpy(newModelPath, selectedEntity->modelComponent->modelPath.c_str(), sizeof(newModelPath));
}
}
if (selectedEntity->GetType() == EntityType::LIGHT && selectedEntity->lightComponent) {
if (selectedEntity->GetType() == EntityType::LIGHT && selectedEntity->lightComponent)
{
ImGui::Separator();
ImGui::Text("Light Properties");
ImGui::ColorEdit3("Light Color", glm::value_ptr(selectedEntity->lightComponent->color));
ImGui::DragFloat("Intensity", &selectedEntity->lightComponent->intensity, 0.1f, 0.0f, 10.0f);
}
} else {
}
else
{
ImGui::Text("No entity selected.");
}
ImGui::End();
// Model Editor Popup.
if (showModelPopup && selectedEntity && selectedEntity->modelComponent) {
if (showModelPopup && selectedEntity && selectedEntity->modelComponent)
{
ImGui::OpenPopup("Edit Model");
showModelPopup = false;
}
if (ImGui::BeginPopupModal("Edit Model", NULL, ImGuiWindowFlags_AlwaysAutoResize)) {
if (ImGui::BeginPopupModal("Edit Model", NULL, ImGuiWindowFlags_AlwaysAutoResize))
{
ImGui::InputText("Model Path", newModelPath, sizeof(newModelPath));
if (ImGui::Button("Load Model", ImVec2(120, 0))) {
if (ImGui::Button("Load Model", ImVec2(120, 0)))
{
// Update the model component with the new model path.
selectedEntity->modelComponent->LoadModel(newModelPath);
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if (ImGui::Button("Cancel", ImVec2(120, 0))) {
if (ImGui::Button("Cancel", ImVec2(120, 0)))
{
ImGui::CloseCurrentPopup();
}
ImGui::Separator();
ImGui::Text("Submesh Textures:");
// Display a grid of textures.
if (selectedEntity->modelComponent->meshes.empty()) {
if (selectedEntity->modelComponent->meshes.empty())
{
ImGui::Text("None");
} else {
}
else
{
int columns = 12; // Change this value for a different number of columns.
int count = 0;
for (size_t i = 0; i < selectedEntity->modelComponent->meshes.size(); i++) {
for (size_t i = 0; i < selectedEntity->modelComponent->meshes.size(); i++)
{
if (count % columns != 0)
ImGui::SameLine();
// Convert the diffuse texture to an ImTextureID.
@ -289,27 +316,58 @@ int main() {
ImGui::EndPopup();
}
// New Panel: File Operations (Save/Load).
ImGui::Begin("Scene File");
if (ImGui::Button("Save")) {
// --- List Files in Scene Directory ---
static std::vector<std::string> sceneFiles;
static int selectedSceneFile = -1;
if (ImGui::Button("Refresh Files"))
{
sceneFiles.clear();
std::cout << "Refreshing" << std::endl;
for (const auto &entry : std::filesystem::directory_iterator("./assets/scenes/"))
{
if (entry.is_regular_file() && entry.path().extension() == ".yaml")
{
sceneFiles.push_back(entry.path().filename().string());
}
}
}
ImGui::Text("Available Scenes:");
for (int i = 0; i < sceneFiles.size(); i++)
{
if (ImGui::Selectable(sceneFiles[i].c_str(), selectedSceneFile == i))
{
selectedSceneFile = i;
}
}
// --- Specify New File Name for Saving ---
static char newSceneFileName[128] = "new_scene.yaml";
ImGui::InputText("New Scene File", newSceneFileName, IM_ARRAYSIZE(newSceneFileName));
// --- Save Scene ---
if (ImGui::Button("Save"))
{
std::cout << "[Info] Saving" << std::endl;
YAML::Emitter out;
out << YAML::BeginMap;
// Save an array of entities.
out << YAML::Key << "entities" << YAML::Value << YAML::BeginSeq;
for (auto e : entities) {
for (auto e : entities)
{
out << YAML::BeginMap;
// Save the type.
out << YAML::Key << "type" << YAML::Value << (e->GetType() == EntityType::CUBE ? "cube" : "light");
// Save transform.
out << YAML::Key << "type" << YAML::Value
<< (e->GetType() == EntityType::CUBE ? "cube" : "light");
out << YAML::Key << "transform" << YAML::Value << e->transform.Save();
// Save model component if cube.
if (e->GetType() == EntityType::CUBE && e->modelComponent) {
if (e->modelComponent)
{
out << YAML::Key << "model" << YAML::Value << e->modelComponent->Save();
}
// Save light component if light.
if (e->GetType() == EntityType::LIGHT && e->lightComponent) {
if (e->lightComponent)
{
out << YAML::Key << "light" << YAML::Value << e->lightComponent->Save();
}
out << YAML::EndMap;
@ -317,31 +375,74 @@ int main() {
out << YAML::EndSeq;
out << YAML::EndMap;
std::ofstream fout("default.yaml");
// Save file to the scenes directory.
std::string path = std::string("./assets/scenes/") + newSceneFileName;
std::ofstream fout(path);
fout << out.c_str();
fout.close();
std::cout << "[Done] Saving" << std::endl;
}
if (ImGui::Button("Load")) {
YAML::Node node = YAML::LoadFile("default.yaml");
if (node["entities"]) {
// --- Load Scene (delete & recreate entities) ---
if (ImGui::Button("Load"))
{
std::cout << "[Info] Loading" << std::endl;
// Only proceed if a file is selected from the list.
if (selectedSceneFile >= 0 && selectedSceneFile < sceneFiles.size())
{
std::string path = "./assests/scenes/" + sceneFiles[selectedSceneFile];
YAML::Node node = YAML::LoadFile(path);
if (node["entities"])
{
// Delete current entities.
for (auto e : entities)
{
delete e; // Assumes deletion handles components properly.
}
entities.clear();
YAML::Node entitiesNode = node["entities"];
int idx = 0;
for (auto entityNode : entitiesNode) {
if (idx < entities.size()) {
for (auto entityNode : entitiesNode)
{
std::string type = entityNode["type"].as<std::string>();
Entity *newEntity = nullptr;
// Create entity based on type.
if (type == "cube")
{
newEntity = new Entity(EntityType::CUBE);
}
else if (type == "light")
{
newEntity = new Entity(EntityType::LIGHT);
}
if (newEntity)
{
// Load transform.
if (entityNode["transform"])
entities[idx]->transform.Load(entityNode["transform"]);
// Load model for cubes.
if (entities[idx]->GetType() == EntityType::CUBE && entityNode["model"])
entities[idx]->modelComponent->Load(entityNode["model"]);
// Load light for lights.
if (entities[idx]->GetType() == EntityType::LIGHT && entityNode["light"])
entities[idx]->lightComponent->Load(entityNode["light"]);
newEntity->transform.Load(entityNode["transform"]);
// Create and load model component for cubes.
if (newEntity->GetType() == EntityType::CUBE && entityNode["model"])
{
newEntity->modelComponent = new ModelComponent();
newEntity->modelComponent->Load(entityNode["model"]);
}
idx++;
// Create and load light component for lights.
if (newEntity->GetType() == EntityType::LIGHT && entityNode["light"])
{
newEntity->lightComponent = new LightComponent();
newEntity->lightComponent->Load(entityNode["light"]);
}
entities.push_back(newEntity);
}
}
}
std::cout << "[Done] Loading" << std::endl;
}
}
ImGui::End();
// Bottom Panel: Rendered Output.

View File

@ -3,6 +3,9 @@
#include <iostream>
#include <vector>
#include <filesystem> // C++17 filesystem
#include <string>
#include <fstream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "imgui.h"

View File

@ -15,14 +15,13 @@ float Engine::rotationAngle = 0.0f;
int Engine::fbWidth = 640;
int Engine::fbHeight = 400;
// Global normal map texture (if needed for legacy models; otherwise each model handles its own)
GLuint normalMapTexture = 0;
bool Engine::Init() {
if (!glfwInit()) {
bool Engine::Init()
{
if (!glfwInit())
{
std::cout << "Failed to initialize GLFW\n";
return false;
}
@ -31,7 +30,8 @@ bool Engine::Init() {
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(1280, 800, "Engine Window", nullptr, nullptr);
if (!window) {
if (!window)
{
std::cout << "Failed to create GLFW window\n";
glfwTerminate();
return false;
@ -39,7 +39,8 @@ bool Engine::Init() {
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
if (glewInit() != GLEW_OK)
{
std::cout << "Failed to initialize GLEW\n";
return false;
}
@ -53,21 +54,25 @@ bool Engine::Init() {
ResizeFramebuffer(fbWidth, fbHeight);
// Setup scene-wide shader (this shader is now used to render all models)
if (!SetupScene()) {
if (!SetupScene())
{
std::cout << "Failed to set up scene\n";
return false;
}
return true;
}
GLFWwindow* Engine::GetWindow() {
GLFWwindow *Engine::GetWindow()
{
return window;
}
GLuint Engine::GetShader() {
GLuint Engine::GetShader()
{
return shaderProgram;
}
void Engine::ResizeFramebuffer(int width, int height) {
void Engine::ResizeFramebuffer(int width, int height)
{
// Avoid division by zero.
if (height <= 0)
height = 1;
@ -79,9 +84,12 @@ void Engine::ResizeFramebuffer(int width, int height) {
// Adjust dimensions to maintain the target aspect ratio.
int newWidth = width;
int newHeight = height;
if (currentAspect > targetAspect) {
if (currentAspect > targetAspect)
{
newWidth = static_cast<int>(height * targetAspect);
} else if (currentAspect < targetAspect) {
}
else if (currentAspect < targetAspect)
{
newHeight = static_cast<int>(width / targetAspect);
}
@ -91,10 +99,12 @@ void Engine::ResizeFramebuffer(int width, int height) {
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
// Delete old attachments if they exist.
if (colorTexture) {
if (colorTexture)
{
glDeleteTextures(1, &colorTexture);
}
if (depthRenderbuffer) {
if (depthRenderbuffer)
{
glDeleteRenderbuffers(1, &depthRenderbuffer);
}
@ -113,19 +123,22 @@ void Engine::ResizeFramebuffer(int width, int height) {
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthRenderbuffer);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) {
if (status != GL_FRAMEBUFFER_COMPLETE)
{
std::cout << "Framebuffer is not complete! Status: " << status << std::endl;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
GLuint Engine::CompileShader(const char* vertexSrc, const char* fragmentSrc) {
GLuint Engine::CompileShader(const char *vertexSrc, const char *fragmentSrc)
{
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexSrc, nullptr);
glCompileShader(vertexShader);
int success;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success) {
if (!success)
{
char infoLog[512];
glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
std::cout << "Vertex shader compilation failed: " << infoLog << std::endl;
@ -135,7 +148,8 @@ GLuint Engine::CompileShader(const char* vertexSrc, const char* fragmentSrc) {
glShaderSource(fragmentShader, 1, &fragmentSrc, nullptr);
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success) {
if (!success)
{
char infoLog[512];
glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog);
std::cout << "Fragment shader compilation failed: " << infoLog << std::endl;
@ -146,7 +160,8 @@ GLuint Engine::CompileShader(const char* vertexSrc, const char* fragmentSrc) {
glAttachShader(program, fragmentShader);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
if (!success)
{
char infoLog[512];
glGetProgramInfoLog(program, 512, nullptr, infoLog);
std::cout << "Shader program linking failed: " << infoLog << std::endl;
@ -159,9 +174,10 @@ GLuint Engine::CompileShader(const char* vertexSrc, const char* fragmentSrc) {
// SetupScene now creates the shader program used for all models.
// It no longer creates cube-specific VAOs, since ModelComponent will store mesh data.
bool Engine::SetupScene() {
bool Engine::SetupScene()
{
// Vertex Shader remains largely unchanged.
const char *vertexShaderSrc = R"(
#version 330 core
layout(location = 0) in vec3 aPos;
@ -187,6 +203,7 @@ bool Engine::SetupScene() {
}
)";
// Fragment Shader with support for optional diffuse and normal maps and using Blinn-Phong lighting.
const char* fragmentShaderSrc = R"(
#version 330 core
out vec4 FragColor;
@ -200,8 +217,11 @@ bool Engine::SetupScene() {
uniform vec3 lightColors[2];
uniform int numLights;
uniform vec3 viewPos;
// Texture uniforms.
uniform sampler2D diffuseTexture;
uniform sampler2D normalMap;
uniform bool useDiffuseTexture;
uniform bool useNormalMap; // Control flag for normal mapping
// Material properties.
@ -210,54 +230,60 @@ bool Engine::SetupScene() {
uniform float materialShininess;
void main() {
// Optionally sample the diffuse texture.
vec3 diffuseTex = useDiffuseTexture ? texture(diffuseTexture, TexCoords).rgb : vec3(1.0);
// Determine the normal.
vec3 finalNormal;
if(useNormalMap) {
// Sample and transform normal map.
// Sample the normal map and convert from [0,1] to [-1,1].
vec3 normMap = texture(normalMap, TexCoords).rgb;
normMap = normalize(normMap * 2.0 - 1.0);
// (The z-flip here depends on your texture convention.)
normMap.z = -normMap.z;
vec3 T = normalize(Tangent);
vec3 B = normalize(cross(Normal, T));
mat3 TBN = mat3(T, B, normalize(Normal));
finalNormal = normalize(TBN * normMap);
} else {
// Compute a flat normal from screen-space derivatives.
finalNormal = normalize(cross(dFdx(FragPos), dFdy(FragPos)));
// Use the interpolated vertex normal if no normal map is provided.
finalNormal = normalize(Normal);
}
vec3 diffuseTex = texture(diffuseTexture, TexCoords).rgb;
// Start with an ambient term.
vec3 ambient = 0.1 * materialDiffuse * diffuseTex;
vec3 lighting = ambient;
// Loop through each light.
for(int i = 0; i < numLights; i++) {
vec3 lightDir = normalize(lightPositions[i] - FragPos);
// Diffuse term.
float diff = max(dot(finalNormal, lightDir), 0.0);
vec3 diffuse = diff * materialDiffuse * diffuseTex * lightColors[i];
// Blinn-Phong Specular term.
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, finalNormal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), materialShininess);
vec3 halfDir = normalize(lightDir + viewDir);
float spec = pow(max(dot(finalNormal, halfDir), 0.0), materialShininess);
vec3 specular = materialSpecular * spec * lightColors[i];
lighting += diffuse + specular;
}
FragColor = vec4(lighting, 1.0);
}
)";
shaderProgram = CompileShader(vertexShaderSrc, fragmentShaderSrc);
if (shaderProgram == 0) {
if (shaderProgram == 0)
{
return false;
}
return true;
}
// RenderScene now uses the new model system.
// For each entity of type CUBE with a valid ModelComponent, update uniforms and call Draw() on the model.
ImTextureID Engine::RenderScene(const glm::mat4 &view, const glm::mat4 &projection, const glm::vec3 &viewPos, const std::vector<Entity*>& entities) {
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glViewport(0, 0, fbWidth, fbHeight);
@ -270,7 +296,7 @@ ImTextureID Engine::RenderScene(const glm::mat4 &view, const glm::mat4 &projecti
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniform3f(glGetUniformLocation(shaderProgram, "viewPos"), viewPos.x, viewPos.y, viewPos.z);
// Gather lights (up to 2) from entities of type LIGHT.
// Gather up to 2 lights.
glm::vec3 lightPositions[2] = { glm::vec3(0.0f), glm::vec3(0.0f) };
glm::vec3 lightColors[2] = { glm::vec3(1.0f), glm::vec3(1.0f) };
int lightCount = 0;
@ -297,20 +323,30 @@ for (auto e : entities) {
// Loop through all submeshes in the model component.
for (const auto &mesh : e->modelComponent->meshes) {
// Set material properties for the current submesh.
// Set material properties.
glUniform3fv(glGetUniformLocation(shaderProgram, "materialDiffuse"), 1, glm::value_ptr(mesh.diffuseColor));
glUniform3fv(glGetUniformLocation(shaderProgram, "materialSpecular"), 1, glm::value_ptr(mesh.specularColor));
glUniform1f(glGetUniformLocation(shaderProgram, "materialShininess"), mesh.shininess);
// Bind the diffuse texture.
// Bind diffuse texture if available.
if (mesh.diffuseTexture != 0) {
glUniform1i(glGetUniformLocation(shaderProgram, "useDiffuseTexture"), 1);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mesh.diffuseTexture);
glUniform1i(glGetUniformLocation(shaderProgram, "diffuseTexture"), 0);
} else {
glUniform1i(glGetUniformLocation(shaderProgram, "useDiffuseTexture"), 0);
}
// If you have a normal texture, bind it similarly (adjust as needed).
// glActiveTexture(GL_TEXTURE1);
// glBindTexture(GL_TEXTURE_2D, mesh.normalTexture);
// glUniform1i(glGetUniformLocation(shaderProgram, "normalMap"), 1);
// Bind normal map if available.
if (mesh.normalTexture != 0) {
glUniform1i(glGetUniformLocation(shaderProgram, "useNormalMap"), 1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, mesh.normalTexture);
glUniform1i(glGetUniformLocation(shaderProgram, "normalMap"), 1);
} else {
glUniform1i(glGetUniformLocation(shaderProgram, "useNormalMap"), 0);
}
// Bind the submesh's VAO and draw its elements.
glBindVertexArray(mesh.VAO);
@ -320,16 +356,18 @@ for (auto e : entities) {
}
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return (ImTextureID)(intptr_t)colorTexture;
}
ImTextureID Engine::GetFinalRenderingTexture() {
ImTextureID Engine::GetFinalRenderingTexture()
{
return (ImTextureID)(intptr_t)colorTexture;
}
void Engine::Shutdown() {
void Engine::Shutdown()
{
glDeleteProgram(shaderProgram);
glDeleteFramebuffers(1, &framebuffer);
glDeleteTextures(1, &colorTexture);

Binary file not shown.

View File

@ -0,0 +1,91 @@
entities:
- type: cube
transform:
position:
- -2
- 0
- 0
rotation:
- 0
- 0
- 0
scale:
- 1
- 1
- 1
model:
modelPath: ./assets/models/sponza.obj
globalDiffuseColor:
- 0.800000012
- 0.200000003
- 0.200000003
globalSpecularColor:
- 1
- 1
- 1
globalShininess: 32
- type: cube
transform:
position:
- 0.5
- -1.20000005
- 5.19999981
rotation:
- 0
- 90
- 0
scale:
- 0.00999999978
- 0.00999999978
- 0.00999999978
model:
modelPath: ./assets/models/sponza.obj
globalDiffuseColor:
- 0
- 1
- 0
globalSpecularColor:
- 1
- 1
- 1
globalShininess: 126
- type: light
transform:
position:
- 0
- 10
- 0
rotation:
- 0
- 0
- 0
scale:
- 1
- 1
- 1
light:
color:
- 1
- 1
- 1
intensity: 2.0999999
- type: light
transform:
position:
- 2.9000001
- -9.69999981
- 7.30000019
rotation:
- 0
- 0
- 0
scale:
- 1
- 1
- 1
light:
color:
- 0.200000003
- 0.200000003
- 1
intensity: 9

View File

@ -0,0 +1,66 @@
entities:
- type: cube
transform:
position:
- 0
- -1.29999995
- 0.600000024
rotation:
- 0
- 90
- 0
scale:
- 0.00999999978
- 0.00999999978
- 0.00999999978
model:
modelPath: ./assets/models/sponza.obj
globalDiffuseColor:
- 1
- 1
- 1
globalSpecularColor:
- 1
- 1
- 1
globalShininess: 32
- type: light
transform:
position:
- 0
- 10
- 0
rotation:
- 0
- 0
- 0
scale:
- 1
- 1
- 1
light:
color:
- 0.995098054
- 0.878314674
- 0.814614594
intensity: 2.29999995
- type: light
transform:
position:
- 0
- -10
- 0
rotation:
- 0
- 0
- 0
scale:
- 1
- 1
- 1
light:
color:
- 0
- 2.38418579e-07
- 1
intensity: 1.79999995

Binary file not shown.

Binary file not shown.

Binary file not shown.