GameObjects

Began to add GameObjects, starting with the player.
This commit is contained in:
onTheZero
2025-08-13 00:59:03 -04:00
parent 237272c17b
commit 4bf6b02255
37 changed files with 2640 additions and 222 deletions

262
main.cpp
View File

@@ -7,23 +7,29 @@
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <Camera.h>
#include <Player.h>
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <camera.h>
#include <cubeFactory.h>
#include "ResourceManager.h"
#include "PointLight.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
int SCR_WIDTH = 800;
int SCR_HEIGHT = 800;
int SCR_WIDTH = 1600;
int SCR_HEIGHT = 900;
//Camera camera(glm::vec3(20.0f, 1.0f, 20.0f));
glm::vec3 StartingPosition = glm::vec3(20.0f, 0.0f, 20.0f);
glm::vec3 StartingRotation = glm::vec3(0.0f, 0.0f, 0.0f);
Player PlayerCharacter(StartingPosition, StartingRotation);
Camera camera(glm::vec3(0.0f, 0.0f, 20.0f));
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;
@@ -32,17 +38,13 @@ float deltaTime = 0.0f;
float lastFrame = 0.0f;
float elapsedTime = 0.0f;
int amountOfCubes = 1000;
//glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
int main()
{
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
const char* glsl_version = "#version 330";
std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@@ -61,7 +63,7 @@ int main()
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
//glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD" << std::endl;
@@ -69,41 +71,76 @@ int main()
}
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
std::cout << "Loaded GLFW context, OpenGL 3.3" << std::endl;
CubeFactory cubeFactory;
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
float offset = 2.0f;
int xAmount = 10;
int yAmount = 10;
int zAmount = 10;
//ImGui_ImplGlfw_InitForOpenGL(window, true);
//ImGui_ImplOpenGL3_Init(glsl_version);
for (int x = 0; x < xAmount; x++)
{
for (int y = 0; y < yAmount; y++)
{
for (int z = 0; z < zAmount; z++)
{
glm::vec3 position = glm::vec3( (x * offset) - (xAmount * offset / 2.0f),
(y * offset) - (yAmount * offset / 2.0f),
(z * offset) - (zAmount * offset / 2.0f));
glm::vec3 scale = glm::vec3(1.0f, 1.0f, 1.0f);
glm::vec3 rotation = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 color = glm::vec3(x/10.0f, y/10.0f, z/10.0f);
cubeFactory.addCube(position, scale, rotation, color);
}
}
if (!ImGui_ImplGlfw_InitForOpenGL(window, true)) {
std::cout << "Failed to initialize ImGui GLFW backend" << std::endl;
}
if (!ImGui_ImplOpenGL3_Init(glsl_version)) {
std::cout << "Failed to initialize ImGui OpenGL backend" << std::endl;
}
//std::cout << "Amount of cubes: " << cubeFactory.getCubes().size() << std::endl;
Shader simple_depth_shader = ResourceManager::LoadShader("point_shadow_depth.vert", "point_shadow_depth.frag", "point_shadow_depth.geom", "simple_depth_shader");
Shader wall_shader = ResourceManager::LoadShader("wall_test.vert", "wall_test.frag", nullptr, "wall");
LevelMap level_map = ResourceManager::LoadLevelMap("map_test.txt", "test_level");
level_map.RenderSetup(&wall_shader, &wall_shader, &wall_shader);
int number_of_point_lights = 0;
std::vector<PointLight> point_lights_vector;
//PointLight point_light3(glm::vec3(30.0f, 3.0f, 30.0f), glm::vec3(1.0f, 1.0f, 1.0f), simple_depth_shader);
//PointLight point_light2(glm::vec3(70.0f, 2.0f, 55.0f), glm::vec3(1.0f, 1.0f, 1.0f), simple_depth_shader);
//PointLight point_light(glm::vec3(20.0f, 3.0f, 35.0f), glm::vec3(1.0f, 1.0f, 1.0f), simple_depth_shader);
//PointLight point_lights[3] = { point_light, point_light2, point_light3 };
/*unsigned int depthCubemap, depthMapFBO;
glGenFramebuffers(1, &depthMapFBO);
glGenTextures(1, &depthCubemap);
glBindTexture(GL_TEXTURE_CUBE_MAP, depthCubemap);
for (unsigned int i = 0; i < 6; ++i)
{
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_DEPTH_COMPONENT, SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthCubemap, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);*/
float r = 0.05f, g = 0.05f, b = 0.05f;
while (!glfwWindowShouldClose(window)) {
//timing
float currentFrame = glfwGetTime();
float currentFrame = (float)glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
elapsedTime += deltaTime;
@@ -111,25 +148,142 @@ int main()
//input
processInput(window);
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::Begin("Settings");
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
if (ImGui::Button("Create Light") && number_of_point_lights < 10) {
point_lights_vector.push_back(PointLight(PlayerCharacter.Position, glm::vec3(1.0f, 1.0f, 1.0f), simple_depth_shader));
number_of_point_lights = point_lights_vector.size();
}
//ImGui::SliderInt("Amount of cubes: ", &amountOfCubes, 0, 1000);
ImGui::End();
ImGui::Render();
// view/projection transformations
glClearColor(r, g, b, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// view/projection transformations
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
glm::mat4 view = camera.GetViewMatrix();
//glDisable(GL_CULL_FACE);
glm::vec3 lightPos = glm::vec3(100.2f, 100.0f, 200.0f);
//level_map.GetMeshes();
cubeFactory.render(view, projection, lightPos, deltaTime);
for (PointLight& point_light : point_lights_vector)
{
point_light.RenderSceneShadows(level_map.GetMeshes());
}
//for (int i = 0; i < numOfPointLights; i++)
//{
// point_lights[i].RenderSceneShadows(level_map.GetMeshes());
//}
glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
wall_shader.Use();
// CHANGED PROJECTION TO USE FLAT 45 TO GET PLAYER CHARACTER TO WORK
glm::mat4 projection = glm::perspective(glm::radians(45.0f), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
glm::mat4 view = PlayerCharacter.GetViewMatrix();
wall_shader.SetMatrix4("projection", projection);
wall_shader.SetMatrix4("view", view);
wall_shader.SetVector3f("viewPosition", PlayerCharacter.Position);
wall_shader.SetVector3f("material.ambientColor", glm::vec3(1.0f, 0.0f, 0.0f));
wall_shader.SetVector3f("material.diffuseColor", glm::vec3(0.5f, 0.5f, 0.5f));
wall_shader.SetVector3f("material.specularColor", glm::vec3(0.0f, 0.0f, 1.0f));
wall_shader.SetFloat("material.shininess", 1.0f);
// set lighting uniforms
wall_shader.SetInteger("numOfPointLights", number_of_point_lights);
int light_index = 0;
for (PointLight& point_light : point_lights_vector)
{
std::string name = "pointLights[" + std::to_string(light_index) + "].";
wall_shader.SetVector3f((name + "base.color").c_str(), point_light.color);
wall_shader.SetFloat((name + "base.ambientIntensity").c_str(), point_light.ambient_intensity);
wall_shader.SetFloat((name + "base.diffuseIntensity").c_str(), point_light.diffuse_intensity);
wall_shader.SetFloat((name + "atten.constant").c_str(), point_light.attenuation_constant);
wall_shader.SetFloat((name + "atten.linear").c_str(), point_light.attenuation_linear);
wall_shader.SetFloat((name + "atten.exponential").c_str(), point_light.attenuation_exponential);
wall_shader.SetVector3f((name + "position").c_str(), point_light.Position);
wall_shader.SetFloat((name + "farPlane").c_str(), point_light.far_plane);
wall_shader.SetInteger((name + "depthMap").c_str(), light_index);
glActiveTexture(GL_TEXTURE0 + light_index);
glBindTexture(GL_TEXTURE_CUBE_MAP, point_light.depth_map);
light_index++;
}
/*wall_shader.SetVector3f("pointLights[0].base.color", point_light.color);
wall_shader.SetVector3f("pointLights[0].base.ambientIntensity", point_light.ambient);
wall_shader.SetVector3f("pointLights[0].position", point_light.position);
wall_shader.SetVector3f("lightPosition2", point_light2.position);
wall_shader.SetVector3f("lightColor", point_light.color);*/
//wall_shader.SetInteger("shadows", 1); // enable/disable shadows by pressing 'SPACE'
//wall_shader.SetInteger("depthMap", 0);
GLenum err;
while(err = glGetError())
{
std::cout << "Error: " << err << std::endl;
}
for (auto* mesh : level_map.GetMeshes())
{
// //std::cout << "Rendering mesh" << std::endl;
mesh->Render(wall_shader);
}
/*float near_plane = 1.0f;
float far_plane = 50.0f;
glm::mat4 shadowProj = glm::perspective(glm::radians(90.0f), (float)SHADOW_WIDTH / (float)SHADOW_HEIGHT, near_plane, far_plane);*/
/*std::vector<glm::mat4> shadowTransforms;
shadowTransforms.push_back(shadowProj * glm::lookAt(lightPos, lightPos + glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
shadowTransforms.push_back(shadowProj * glm::lookAt(lightPos, lightPos + glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
shadowTransforms.push_back(shadowProj * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)));
shadowTransforms.push_back(shadowProj * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f)));
shadowTransforms.push_back(shadowProj * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
shadowTransforms.push_back(shadowProj * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, -1.0f, 0.0f)));*/
/*glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
glClear(GL_DEPTH_BUFFER_BIT);
glCullFace(GL_FRONT);
simpleDepthShader.Use();
for (unsigned int i = 0; i < 6; ++i)
simpleDepthShader.SetMatrix4("shadowMatrices[" + std::to_string(i) + "]", shadowTransforms[i]);
simpleDepthShader.SetFloat("far_plane", far_plane);
simpleDepthShader.SetVector3f("lightPos", lightPos);
levelMap.Update(projection, view);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glCullFace(GL_BACK);
glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_CUBE_MAP, depthCubemap);*/
//map.render(projection, view, lightPos, deltaTime, glm::vec3(SCR_WIDTH, SCR_HEIGHT, 0.0f), far_plane);
//level_map.Update(projection, view, point_light.position, point_light.color, camera.Position);
//cubeFactory.render(view, projection, lightPos, deltaTime, amountOfCubes);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
//check and call
glfwSwapBuffers(window);
glfwPollEvents();
}
//cubeFactory.clear();
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
@@ -145,18 +299,26 @@ void processInput(GLFWwindow* window)
const float cameraSpeed = 5.0f * deltaTime;
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, deltaTime);
PlayerCharacter.ProcessKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboard(BACKWARD, deltaTime);
PlayerCharacter.ProcessKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboard(LEFT, deltaTime);
PlayerCharacter.ProcessKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboard(RIGHT, deltaTime);
PlayerCharacter.ProcessKeyboard(RIGHT, deltaTime);
}
void mouse_callback(GLFWwindow* window, double xposIn, double yposIn)
{
//std::cout << "Mouse callback" << std::endl;
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) != GLFW_PRESS)
{
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
firstMouse = true;
return;
}
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
//std::cout << "Mouse callback" << std::endl;
float xpos = static_cast<float>(xposIn);
float ypos = static_cast<float>(yposIn);
@@ -173,10 +335,10 @@ void mouse_callback(GLFWwindow* window, double xposIn, double yposIn)
lastX = xpos;
lastY = ypos;
camera.ProcessMouseMovement(xoffset, yoffset);
PlayerCharacter.ProcessMouseMovement(xoffset, yoffset);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
camera.ProcessMouseScroll(static_cast<float>(yoffset));
//PlayerCharacter.ProcessMouseScroll(static_cast<float>(yoffset));
}