12 Commits

Author SHA1 Message Date
1089861aa3 feat: implement libraries in main 2025-10-13 18:25:55 +02:00
49219fc597 feat: assignment 3 2025-10-13 18:23:01 +02:00
db6e548476 feat: add assignment 3 files 2025-10-13 17:51:00 +02:00
22c46053c0 chore: change build dir name 2025-10-13 17:33:13 +02:00
41c9b68eb9 feat: add logger class 2025-10-13 17:30:05 +02:00
74a0bfca41 refactor: state into namespace 2025-10-13 17:17:28 +02:00
4e2c62aaf4 feat: engine class 2025-10-13 16:51:07 +02:00
f1a24f576b style: naming convention 2025-10-13 16:22:57 +02:00
f981cee4e0 feat: assignment 2 2025-10-13 15:04:53 +02:00
986e31dbef style: apply clang format 2025-10-13 14:39:27 +02:00
253bc47a5f fix: working dir 2025-10-13 13:45:09 +02:00
e07cdefacc chore: change sln dir 2025-10-13 13:36:58 +02:00
39 changed files with 8662 additions and 273 deletions

56
.clang-format Normal file
View File

@@ -0,0 +1,56 @@
Language: Cpp
BasedOnStyle: LLVM
AccessModifierOffset: -8
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: Consecutive
AlignConsecutiveDeclarations: Consecutive
AlignConsecutiveMacros: Consecutive
AlignEscapedNewlines: Left
AlignOperands: DontAlign
AlignTrailingComments: Always
AllowShortBlocksOnASingleLine: Never
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortLambdasOnASingleLine: None
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Custom
BreakInheritanceList: AfterColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma
BreakStringLiterals: false
ColumnLimit: 80
Cpp11BracedListStyle: true
EmptyLineBeforeAccessModifier: Always
IndentAccessModifiers: false
IndentCaseBlocks: false
IndentCaseLabels: false
IndentGotoLabels: false
IndentPPDirectives: None
IndentWidth: 8
InsertNewlineAtEOF: true
InsertTrailingCommas: None
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PackConstructorInitializers: NextLineOnly
PointerAlignment: Left
SortIncludes: Never
SpaceBeforeCaseColon: false
TabWidth: 8
UseTab: Always

64
.clang-tidy Normal file
View File

@@ -0,0 +1,64 @@
---
# C++ Core Guidelines style (CppCoreGuidelines)
Checks: >
cppcoreguidelines-*,
readability-identifier-naming,
readability-*,
modernize-*,
performance-*,
bugprone-*,
-modernize-use-trailing-return-type,
-readability-magic-numbers,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
-cppcoreguidelines-pro-type-reinterpret-cast
CheckOptions:
# C++ Core Guidelines: Types use PascalCase
- { key: readability-identifier-naming.ClassCase, value: CamelCase }
- { key: readability-identifier-naming.StructCase, value: CamelCase }
- { key: readability-identifier-naming.EnumCase, value: CamelCase }
- { key: readability-identifier-naming.UnionCase, value: CamelCase }
# C++ Core Guidelines: Functions use snake_case (STL style)
- { key: readability-identifier-naming.FunctionCase, value: lower_case }
- { key: readability-identifier-naming.MethodCase, value: lower_case }
# Member variables: snake_case with trailing underscore
- { key: readability-identifier-naming.PrivateMemberCase, value: lower_case }
- { key: readability-identifier-naming.PrivateMemberSuffix, value: '_' }
- { key: readability-identifier-naming.ProtectedMemberCase, value: lower_case }
- { key: readability-identifier-naming.ProtectedMemberSuffix, value: '_' }
- { key: readability-identifier-naming.PublicMemberCase, value: lower_case }
# Parameters and local variables: snake_case
- { key: readability-identifier-naming.ParameterCase, value: lower_case }
- { key: readability-identifier-naming.VariableCase, value: lower_case }
- { key: readability-identifier-naming.LocalVariableCase, value: lower_case }
# Constants and macros: UPPER_CASE
- { key: readability-identifier-naming.ConstantCase, value: UPPER_CASE }
- { key: readability-identifier-naming.ConstexprVariableCase, value: UPPER_CASE }
- { key: readability-identifier-naming.MacroDefinitionCase, value: UPPER_CASE }
- { key: readability-identifier-naming.EnumConstantCase, value: UPPER_CASE }
# Global variables: snake_case (Core Guidelines: avoid globals when possible)
- { key: readability-identifier-naming.GlobalVariableCase, value: lower_case }
# Namespaces: snake_case
- { key: readability-identifier-naming.NamespaceCase, value: lower_case }
# Template parameters: PascalCase (following STL conventions)
- { key: readability-identifier-naming.TypeTemplateParameterCase, value: CamelCase }
- { key: readability-identifier-naming.ValueTemplateParameterCase, value: lower_case }
# Type aliases: PascalCase (modern C++ style with 'using')
- { key: readability-identifier-naming.TypeAliasCase, value: CamelCase }
# Function size limits
- { key: readability-function-size.LineThreshold, value: 100 }
- { key: readability-function-size.StatementThreshold, value: 50 }
WarningsAsErrors: ''
HeaderFilterRegex: 'src/.*\.h$'
FormatStyle: file

View File

@@ -1,5 +0,0 @@
varying vec3 fcolor;
void main() {
gl_FragColor = vec4(fcolor, 1);
}

View File

@@ -0,0 +1,12 @@
uniform int useTexture;
uniform sampler2D texSampler;
varying vec3 fcolor;
varying vec2 ftexcoord;
void main() {
if (useTexture == 1) {
gl_FragColor = texture2D(texSampler, ftexcoord);
} else {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
}

View File

@@ -1,9 +1,12 @@
uniform mat4 mvp;
attribute vec3 vpos;
attribute vec3 vcolor;
attribute vec2 vtexcoord;
varying vec3 fcolor;
varying vec2 ftexcoord;
void main() {
gl_Position = mvp * vec4(vpos, 1);
fcolor = vcolor;
ftexcoord = vtexcoord;
}

BIN
data/textures/front.png (Stored with Git LFS) Normal file

Binary file not shown.

BIN
data/textures/top.png (Stored with Git LFS) Normal file

Binary file not shown.

7187
lib/stb/stb_image.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,71 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
<Filter Include="glfw">
<UniqueIdentifier>{422e20da-2cf0-4bda-9833-a89b7ab453a1}</UniqueIdentifier>
</Filter>
<Filter Include="glew">
<UniqueIdentifier>{296e1ba9-28bc-4e0b-8d4e-413551edca96}</UniqueIdentifier>
</Filter>
<Filter Include="Shaders">
<UniqueIdentifier>{0628083b-a31c-4825-822c-11b6f933e7bd}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\lib\glfw\glfw3.h">
<Filter>glfw</Filter>
</ClInclude>
<ClInclude Include="..\lib\glfw\glfw3native.h">
<Filter>glfw</Filter>
</ClInclude>
<ClInclude Include="..\lib\glew\GL\glew.h">
<Filter>glew</Filter>
</ClInclude>
<ClInclude Include="..\src\vertex.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="..\src\shader.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="..\src\buffer.h">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lib\glew\glew.c">
<Filter>glew</Filter>
</ClCompile>
<ClCompile Include="..\src\vertex.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\shader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\buffer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="..\data\fragment.glsl">
<Filter>Shaders</Filter>
</None>
<None Include="..\data\vertex.glsl">
<Filter>Shaders</Filter>
</None>
</ItemGroup>
</Project>

View File

@@ -1,14 +1,15 @@
#include "buffer.h"
#include <vector>
#include <iostream>
#include "../lib/glew/GL/glew.h"
#include "../lib/glfw/glfw3.h"
#include "logger.h"
#include "vertex.h"
Buffer::Buffer(const std::vector<Vertex>& vertices, const std::vector<uint16_t>& indices)
Buffer::Buffer(const std::vector<Vertex>& vertices,
const std::vector<uint16_t>& indices)
{
index_count_ = static_cast<GLsizei>(indices.size());
@@ -19,13 +20,19 @@ Buffer::Buffer(const std::vector<Vertex>& vertices, const std::vector<uint16_t>&
glBindVertexArray(vao_);
glBindBuffer(GL_ARRAY_BUFFER, vbo_);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex),
vertices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(uint16_t), indices.data(), GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(uint16_t),
indices.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), nullptr);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
nullptr);
glEnableVertexAttribArray(0);
Logger::info(sstr("Buffer created with ", vertices.size(),
" vertices and ", indices.size(), " indices"));
}
Buffer::~Buffer()
@@ -33,13 +40,14 @@ Buffer::~Buffer()
glDeleteVertexArrays(1, &vao_);
glDeleteBuffers(1, &vbo_);
glDeleteBuffers(1, &ebo_);
Logger::info("Buffer destroyed");
}
void Buffer::Draw(const Shader& shader) const
void Buffer::draw(const Shader& shader) const
{
glBindVertexArray(vao_);
glBindBuffer(GL_ARRAY_BUFFER, vbo_);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_);
shader.SetupAttribs();
shader.setup_attribs();
glDrawElements(GL_TRIANGLES, index_count_, GL_UNSIGNED_SHORT, nullptr);
}

View File

@@ -10,14 +10,15 @@
class Buffer {
public:
Buffer(const std::vector<Vertex>& vertices, const std::vector<uint16_t>& indices);
Buffer(const std::vector<Vertex>& vertices,
const std::vector<uint16_t>& indices);
~Buffer();
void Draw(const Shader& shader) const;
void draw(const Shader& shader) const;
private:
uint32_t vao_, vbo_, ebo_;
GLsizei index_count_;
GLsizei index_count_;
};
#endif // BUFFER_H_

41
src/camera.cpp Normal file
View File

@@ -0,0 +1,41 @@
#include "camera.h"
#include "state.h"
#include "../lib/glew/GL/glew.h"
#include "../lib/glm/gtc/matrix_transform.hpp"
Camera::Camera()
: projection_(glm::mat4(1.0f))
, viewport_(0, 0, 800, 600)
, clear_color_(0.0f, 0.0f, 0.0f)
{
}
void Camera::prepare()
{
// Set projection matrix
state::projection_matrix = projection_;
// Calculate view matrix
// For a camera, we need the inverse transformation:
// View = inverse(Translation * Rotation)
// Which is: Rotation^-1 * Translation^-1
glm::mat4 view = glm::mat4(1.0f);
// Inverse rotation (rotate in opposite direction)
view = glm::rotate(view, -rotation_.z, glm::vec3(0.0f, 0.0f, 1.0f));
view = glm::rotate(view, -rotation_.y, glm::vec3(0.0f, 1.0f, 0.0f));
view = glm::rotate(view, -rotation_.x, glm::vec3(1.0f, 0.0f, 0.0f));
// Inverse translation (translate in opposite direction)
view = glm::translate(view, -position_);
state::view_matrix = view;
// Set viewport
glViewport(viewport_.x, viewport_.y, viewport_.z, viewport_.w);
glScissor(viewport_.x, viewport_.y, viewport_.z, viewport_.w);
// Set clear color and clear buffers
glClearColor(clear_color_.r, clear_color_.g, clear_color_.b, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

46
src/camera.h Normal file
View File

@@ -0,0 +1,46 @@
#ifndef CAMERA_H_
#define CAMERA_H_
#include "entity.h"
#include "../lib/glm/glm.hpp"
class Camera : public Entity {
public:
Camera();
[[nodiscard]] const glm::mat4& projection() const
{
return projection_;
}
void set_projection(const glm::mat4& proj)
{
projection_ = proj;
}
[[nodiscard]] const glm::ivec4& viewport() const
{
return viewport_;
}
void set_viewport(const glm::ivec4& vp)
{
viewport_ = vp;
}
[[nodiscard]] const glm::vec3& clear_color() const
{
return clear_color_;
}
void set_clear_color(const glm::vec3& color)
{
clear_color_ = color;
}
void prepare();
private:
glm::mat4 projection_;
glm::ivec4 viewport_;
glm::vec3 clear_color_;
};
#endif // CAMERA_H_

274
src/engine.cpp Normal file
View File

@@ -0,0 +1,274 @@
#include "engine.h"
#include <vector>
#include "../lib/glew/GL/glew.h"
#include "../lib/glfw/glfw3.h"
#include "../lib/glm/glm.hpp"
#include "../lib/glm/gtc/matrix_transform.hpp"
#include "buffer.h"
#include "camera.h"
#include "logger.h"
#include "material.h"
#include "mesh.h"
#include "model.h"
#include "shader.h"
#include "state.h"
#include "texture.h"
#include "vertex.h"
#include "world.h"
constexpr int screen_width = 800;
constexpr int screen_height = 600;
constexpr double fps_limit = 1.0 / 60.0;
Engine::Engine()
: window_(nullptr)
, screen_width_(screen_width)
, screen_height_(screen_height)
, last_update_time_(0.0)
, last_frame_time_(0.0)
, angle_(0.0)
{
Logger::info("Engine created");
}
Engine::~Engine()
{
Logger::info("Engine destroyed");
destroy();
}
void Engine::initialize()
{
Logger::info("Initializing engine...");
// Initialize GLFW
if (!glfwInit()) {
Logger::error("Failed to initialize GLFW");
return;
}
Logger::info("GLFW initialized successfully");
glfwWindowHint(GLFW_RESIZABLE, false);
glfwWindowHint(GLFW_SAMPLES, 8);
// Create window
window_ = glfwCreateWindow(screen_width_, screen_height_,
"Daniel Poveda", nullptr, nullptr);
if (window_ == nullptr) {
Logger::error("Failed to create OpenGL window");
glfwTerminate();
return;
}
glfwMakeContextCurrent(window_);
// Enable OpenGL features
glEnable(GL_DEPTH_TEST);
glEnable(GL_SCISSOR_TEST);
Logger::info(
sstr("OpenGL initialized, version: ",
reinterpret_cast<const char*>(glGetString(GL_VERSION))));
// Initialize GLEW
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (err != GLEW_OK) {
Logger::error(sstr(
"Failed to initialize GLEW: ",
reinterpret_cast<const char*>(glewGetErrorString(err))));
glfwTerminate();
return;
}
// Initialize default shader
Logger::info("Loading default shaders...");
state::default_shader = std::make_shared<Shader>(
"data/shaders/vertex.glsl", "data/shaders/fragment.glsl");
if (std::strlen(state::default_shader->error()) > 0) {
Logger::error(sstr("Failed to initialize shaders: ",
state::default_shader->error()));
} else {
Logger::info("Default shaders loaded successfully");
}
Logger::info("Engine initialization complete");
}
void Engine::run()
{
Logger::info("Starting game loop...");
setup();
start();
last_update_time_ = glfwGetTime();
last_frame_time_ = glfwGetTime();
while (is_running()) {
const double now = glfwGetTime();
const double delta_time = now - last_update_time_;
process_input();
update(delta_time);
if (now - last_frame_time_ >= fps_limit) {
render();
last_frame_time_ = now;
}
last_update_time_ = now;
}
Logger::info("Game loop ended");
}
void Engine::destroy()
{
if (window_) {
Logger::info("Shutting down engine...");
glfwTerminate();
window_ = nullptr;
Logger::info("Engine shutdown complete");
}
}
void Engine::setup()
{
Logger::info("Setting up scene...");
// Create world
world_ = std::make_unique<World>();
Logger::info("World created");
// Create camera at position (0, 1, 3) with -20 degrees X rotation
camera_ = std::make_shared<Camera>();
camera_->set_position(glm::vec3(0.0f, 1.0f, 3.0f));
camera_->set_rotation(glm::vec3(glm::radians(-20.0f), 0.0f, 0.0f));
camera_->set_projection(
glm::perspective(glm::radians(45.0f),
static_cast<float>(screen_width_)
/ static_cast<float>(screen_height_),
0.1f, 100.0f));
camera_->set_viewport(glm::ivec4(0, 0, screen_width_, screen_height_));
camera_->set_clear_color(glm::vec3(0.1f, 0.1f, 0.1f));
world_->add_entity(camera_);
Logger::info("Camera created and added to world");
// Load textures
auto top_texture = Texture::load("data/textures/top.png");
auto front_texture = Texture::load("data/textures/front.png");
// Create cube mesh with two buffers
mesh_ = std::make_shared<Mesh>();
// Buffer 1: Top and bottom faces
std::vector<Vertex> top_bottom_vertices = {
// Top face (y = 0.5)
{{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
{{0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
{{0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
{{-0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
// Bottom face (y = -0.5)
{{-0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
{{0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
{{0.5f, -0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
{{-0.5f, -0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}}};
std::vector<uint16_t> top_bottom_indices = {// Top face
0, 1, 2, 0, 2, 3,
// Bottom face
4, 6, 5, 4, 7, 6};
auto top_bottom_buffer =
std::make_shared<Buffer>(top_bottom_vertices, top_bottom_indices);
Material top_material(top_texture);
mesh_->add_buffer(top_bottom_buffer, top_material);
// Buffer 2: Front, back, left, right faces
std::vector<Vertex> side_vertices = {
// Front face (z = 0.5)
{{-0.5f, -0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
{{0.5f, -0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
{{0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
{{-0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
// Back face (z = -0.5)
{{-0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
{{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
{{0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
{{0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
// Left face (x = -0.5)
{{-0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}},
{{-0.5f, -0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
{{-0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
{{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
// Right face (x = 0.5)
{{0.5f, -0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 0.0f}},
{{0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}, {1.0f, 1.0f}},
{{0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
{{0.5f, -0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}};
std::vector<uint16_t> side_indices = {// Front face
0, 1, 2, 0, 2, 3,
// Back face
4, 5, 6, 4, 6, 7,
// Left face
8, 9, 10, 8, 10, 11,
// Right face
12, 13, 14, 12, 14, 15};
auto side_buffer =
std::make_shared<Buffer>(side_vertices, side_indices);
Material side_material(front_texture);
mesh_->add_buffer(side_buffer, side_material);
Logger::info("Cube mesh created with two buffers");
// Create model at origin
auto model = std::make_shared<Model>(mesh_);
model->set_position(glm::vec3(0.0f, 0.0f, 0.0f));
models_.push_back(model);
world_->add_entity(model);
Logger::info("Scene setup complete");
}
void Engine::start()
{
// Called once after setup, before the main loop
// Can be used for initialization that needs the scene to be ready
}
void Engine::process_input()
{
glfwPollEvents();
}
void Engine::update(const double delta_time)
{
// Update angle for rotation
angle_ += 32.0 * delta_time;
// Update rotation for all models
for (auto& model : models_) {
model->set_rotation(glm::vec3(
0.0f, glm::radians(static_cast<float>(angle_)), 0.0f));
}
// Update world
world_->update(static_cast<float>(delta_time));
}
void Engine::render()
{
// Draw world
world_->draw();
// Swap buffers
glfwSwapBuffers(window_);
}
bool Engine::is_running() const
{
return window_ && !glfwWindowShouldClose(window_)
&& !glfwGetKey(window_, GLFW_KEY_ESCAPE);
}

51
src/engine.h Normal file
View File

@@ -0,0 +1,51 @@
#ifndef ENGINE_H_
#define ENGINE_H_
#include <memory>
#include <vector>
#include "../lib/glew/GL/glew.h"
#include "../lib/glfw/glfw3.h"
class World;
class Camera;
class Model;
class Mesh;
class Engine {
public:
Engine();
~Engine();
void initialize();
void run();
void destroy();
private:
// Lifecycle methods
void setup();
void start();
void process_input();
void update(const double delta_time);
void render();
[[nodiscard]] bool is_running() const;
// Window and OpenGL
GLFWwindow* window_;
int screen_width_;
int screen_height_;
// Game objects
std::unique_ptr<World> world_;
std::shared_ptr<Camera> camera_;
std::vector<std::shared_ptr<Model>> models_;
std::shared_ptr<Mesh> mesh_;
// Game loop timing
double last_update_time_;
double last_frame_time_;
double angle_;
};
#endif // ENGINE_H_

13
src/entity.cpp Normal file
View File

@@ -0,0 +1,13 @@
#include "entity.h"
Entity::Entity()
: position_(0.0f, 0.0f, 0.0f)
, rotation_(0.0f, 0.0f, 0.0f)
, scale_(1.0f, 1.0f, 1.0f)
{
}
void Entity::move(const glm::vec3& vec)
{
position_ += vec;
}

55
src/entity.h Normal file
View File

@@ -0,0 +1,55 @@
#ifndef ENTITY_H_
#define ENTITY_H_
#include "../lib/glm/glm.hpp"
class Entity {
public:
Entity();
virtual ~Entity() = default;
[[nodiscard]] const glm::vec3& position() const
{
return position_;
}
void set_position(const glm::vec3& pos)
{
position_ = pos;
}
[[nodiscard]] const glm::vec3& rotation() const
{
return rotation_;
}
void set_rotation(const glm::vec3& rot)
{
rotation_ = rot;
}
[[nodiscard]] const glm::vec3& scale() const
{
return scale_;
}
void set_scale(const glm::vec3& scale)
{
scale_ = scale;
}
void move(const glm::vec3& vec);
virtual void update(const float delta_time)
{
// ...
}
virtual void draw()
{
// ...
}
protected:
glm::vec3 position_;
glm::vec3 rotation_;
glm::vec3 scale_;
};
#endif // ENTITY_H_

57
src/logger.cpp Normal file
View File

@@ -0,0 +1,57 @@
#include "logger.h"
#include <chrono>
#include <iostream>
namespace {
std::string current_datetime_string()
{
auto now = std::chrono::system_clock::now();
auto now_time_t = std::chrono::system_clock::to_time_t(now);
struct tm now_tm {};
#ifdef _WIN32
localtime_s(&now_tm, &now_time_t);
#else
localtime_r(&now_time_t, &now_tm);
#endif
char buffer[30];
std::strftime(buffer, sizeof(buffer) / sizeof(char), "%Y-%m-%d %H:%M:%S",
&now_tm);
return std::string{buffer};
}
} // namespace
void Logger::info(const std::string& message)
{
LogEntry entry;
entry.type = LogType::info;
entry.message =
"[" + current_datetime_string() + "] [INFO]: " + message;
std::cout << "\x1B[32m" << entry.message << "\033[0m" << std::endl;
messages_.push_back(entry);
}
void Logger::warn(const std::string& message)
{
LogEntry entry;
entry.type = LogType::warn;
entry.message =
"[" + current_datetime_string() + "] [WARN]: " + message;
std::cout << "\x1B[93m" << entry.message << "\033[0m" << std::endl;
messages_.push_back(entry);
}
void Logger::error(const std::string& message)
{
LogEntry entry;
entry.type = LogType::error;
entry.message = "[" + current_datetime_string() + "] [ERROR]: " + message;
std::cerr << "\x1B[91m" << entry.message << "\033[0m" << std::endl;
messages_.push_back(entry);
}

48
src/logger.h Normal file
View File

@@ -0,0 +1,48 @@
#ifndef LOGGER_H_
#define LOGGER_H_
#include <sstream>
#include <string>
#include <vector>
// Utility: Concatenate any types into a string
template <typename... Args>
std::string sstr(Args&&... args)
{
std::ostringstream stream;
stream << std::dec;
((stream << args), ...);
return stream.str();
}
// Log types
enum class LogType {
info = 0,
warn,
error
};
// Log entry structure
struct LogEntry {
LogType type{LogType::info};
std::string message{};
};
// Logger class
class Logger {
public:
static void info(const std::string& message);
static void warn(const std::string& message);
static void error(const std::string& message);
[[nodiscard]] static const std::vector<LogEntry>& messages()
{
return messages_;
}
static void clear() { messages_.clear(); }
private:
inline static std::vector<LogEntry> messages_{};
};
#endif // LOGGER_H_

View File

@@ -2,117 +2,18 @@
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
#endif*/
#define STB_IMAGE_IMPLEMENTATION
#include "../lib/stb/stb_image.h"
#include <iostream>
#include <vector>
#include "engine.h"
#include "../lib/glew/GL/glew.h"
#include "../lib/glfw/glfw3.h"
#include "../lib/glm/glm.hpp"
#include "../lib/glm/gtc/matrix_transform.hpp"
int main()
{
Engine engine;
#include "vertex.h"
#include "shader.h"
#include "buffer.h"
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
int main() {
// Initialize OpenGL
if (!glfwInit()) {
std::cerr << "Failed to initialize glfw\n";
return 1;
}
glfwWindowHint(GLFW_RESIZABLE, false);
glfwWindowHint(GLFW_SAMPLES, 8);
//glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
//glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* win = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Daniel Poveda", nullptr, nullptr);
if (win == nullptr) {
std::cerr << "Failed to create opengl window\n";
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(win);
glEnable(GL_DEPTH_TEST);
glEnable(GL_SCISSOR_TEST);
std::cout << "OpenGL initialized, version: " << glGetString(GL_VERSION) << "\n";
// Initialize GLEW
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (err != GLEW_OK) {
std::cerr << "Failed to initialize GLEW: " << glewGetErrorString(err) << "\n";
glfwTerminate();
return 1;
}
// Logic
std::vector<Vertex> vertices = {
{{0.0f, 0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}},
{{-0.5f, -0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}},
{{0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}}
};
std::vector<uint16_t> indices = { 0, 1, 2 };
Buffer buffer(vertices, indices);
Shader shader("data/vertex.glsl", "data/fragment.glsl");
if (std::strlen(shader.getError()) > 0) {
std::cerr << "Failed to initialize shaders: " << shader.getError() << "\n";
}
glm::mat4 projection = glm::perspective(glm::radians(45.0f), static_cast<float>(SCREEN_WIDTH) / SCREEN_HEIGHT, 0.1f, 100.0f);
//glm::mat4 view = glm::translate(glm::mat4(1.0f), glm::vec3(0, 0, 6));
glm::mat4 view = glm::lookAt(glm::vec3(0, 0, 6), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
// main loop
double angle = 0.0;
double lastTime = glfwGetTime();
while (!glfwWindowShouldClose(win) && !glfwGetKey(win, GLFW_KEY_ESCAPE)) {
// Delta
double delta_time = glfwGetTime() - lastTime;
lastTime = glfwGetTime();
// Reset window
int screenWidth, screenHeight;
glfwGetWindowSize(win, &screenWidth, &screenHeight);
glViewport(0, 0, screenWidth, screenHeight);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader.Use();
// Logic
angle += 32.0 * delta_time;
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 3; ++col) {
glm::mat4 model = glm::translate(glm::mat4(1.0), glm::vec3(-3.0f + static_cast<float>(col) * 3.0f, 0.0f, static_cast<float>(-row) * 3.0f));
model = glm::rotate(model, glm::radians(static_cast<float>(angle)), glm::vec3(0, 1, 0));
glm::mat4 mvp = projection * view * model;
Shader::setMat4(shader.getLocation("mvp"), mvp);
buffer.Draw(shader);
}
}
// Refresh screen
glfwSwapBuffers(win);
glfwPollEvents();
}
// Shutdown
glfwTerminate();
engine.initialize();
engine.run();
engine.destroy();
return 0;
}

61
src/material.cpp Normal file
View File

@@ -0,0 +1,61 @@
#include "material.h"
#include "../lib/glm/glm.hpp"
#include "shader.h"
#include "state.h"
#include "texture.h"
Material::Material(const std::shared_ptr<Texture>& tex,
const std::shared_ptr<Shader>& shader)
: shader_(shader), texture_(tex)
{
}
const std::shared_ptr<Shader>& Material::shader() const
{
return shader_ ? shader_ : state::default_shader;
}
std::shared_ptr<Shader>& Material::shader()
{
return shader_ ? shader_ : state::default_shader;
}
void Material::set_shader(const std::shared_ptr<Shader>& shader)
{
shader_ = shader;
}
void Material::prepare()
{
// Activate shader
std::shared_ptr<Shader> active_shader = shader();
if (!active_shader)
return;
active_shader->use();
// Set uniform variables
const glm::mat4 mvp =
state::projection_matrix * state::view_matrix * state::model_matrix;
const int mvp_loc = active_shader->uniform_location("mvp");
Shader::set_mat4(mvp_loc, mvp);
// Set texture-related uniforms
const int use_texture_loc =
active_shader->uniform_location("useTexture");
if (texture_) {
Shader::set_int(use_texture_loc, 1);
const int sampler_loc =
active_shader->uniform_location("texSampler");
Shader::set_int(sampler_loc, 0); // Texture unit 0
} else {
Shader::set_int(use_texture_loc, 0);
}
// Bind texture if available
if (texture_) {
texture_->bind();
}
}

35
src/material.h Normal file
View File

@@ -0,0 +1,35 @@
#ifndef MATERIAL_H_
#define MATERIAL_H_
#include <memory>
class Shader;
class Texture;
class Material {
public:
Material(const std::shared_ptr<Texture>& tex = nullptr,
const std::shared_ptr<Shader>& shader = nullptr);
~Material() = default;
[[nodiscard]] const std::shared_ptr<Shader>& shader() const;
[[nodiscard]] std::shared_ptr<Shader>& shader();
void set_shader(const std::shared_ptr<Shader>& shader);
[[nodiscard]] const std::shared_ptr<Texture>& texture() const
{
return texture_;
}
void set_texture(const std::shared_ptr<Texture>& tex)
{
texture_ = tex;
}
void prepare();
private:
std::shared_ptr<Shader> shader_;
std::shared_ptr<Texture> texture_;
};
#endif // MATERIAL_H_

20
src/mesh.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include "mesh.h"
#include "buffer.h"
#include "material.h"
void Mesh::add_buffer(const std::shared_ptr<Buffer>& buffer,
const Material& material)
{
buffers_.push_back(buffer);
materials_.push_back(material);
}
void Mesh::draw()
{
// Draw each buffer with its material
for (size_t i = 0; i < buffers_.size(); ++i) {
materials_[i].prepare();
buffers_[i]->draw(*materials_[i].shader());
}
}

48
src/mesh.h Normal file
View File

@@ -0,0 +1,48 @@
#ifndef MESH_H_
#define MESH_H_
#include <memory>
#include <vector>
#include "material.h"
class Buffer;
class Mesh {
public:
Mesh() = default;
~Mesh() = default;
void add_buffer(const std::shared_ptr<Buffer>& buffer,
const Material& material);
[[nodiscard]] size_t num_buffers() const
{
return buffers_.size();
}
[[nodiscard]] const std::shared_ptr<Buffer>& buffer(size_t index) const
{
return buffers_[index];
}
[[nodiscard]] std::shared_ptr<Buffer>& buffer(size_t index)
{
return buffers_[index];
}
[[nodiscard]] const Material& material(size_t index) const
{
return materials_[index];
}
[[nodiscard]] Material& material(size_t index)
{
return materials_[index];
}
void draw();
private:
std::vector<std::shared_ptr<Buffer>> buffers_;
std::vector<Material> materials_;
};
#endif // MESH_H_

35
src/model.cpp Normal file
View File

@@ -0,0 +1,35 @@
#include "model.h"
#include "mesh.h"
#include "state.h"
#include "../lib/glm/gtc/matrix_transform.hpp"
Model::Model(const std::shared_ptr<Mesh>& mesh)
: mesh_(mesh)
{
}
void Model::draw()
{
if (!mesh_)
return;
// Build model matrix: Translation * Rotation * Scale
glm::mat4 model = glm::mat4(1.0f);
// Translation
model = glm::translate(model, position_);
// Rotation (applying X, Y, Z rotations)
model = glm::rotate(model, rotation_.x, glm::vec3(1.0f, 0.0f, 0.0f));
model = glm::rotate(model, rotation_.y, glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::rotate(model, rotation_.z, glm::vec3(0.0f, 0.0f, 1.0f));
// Scale
model = glm::scale(model, scale_);
// Set the model matrix in state
state::model_matrix = model;
// Draw the mesh
mesh_->draw();
}

19
src/model.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef MODEL_H_
#define MODEL_H_
#include "entity.h"
#include <memory>
class Mesh;
class Model : public Entity {
public:
Model(const std::shared_ptr<Mesh>& mesh);
virtual void draw() override;
private:
std::shared_ptr<Mesh> mesh_;
};
#endif // MODEL_H_

View File

@@ -1,19 +1,20 @@
#include "shader.h"
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include "../lib/glew/GL/glew.h"
#include "../lib/glfw/glfw3.h"
#include "logger.h"
#include "vertex.h"
Shader::Shader(const std::string& vertexPath, const std::string& fragmentPath)
Shader::Shader(const std::string& vertex_path, const std::string& fragment_path)
{
const uint32_t vshader = CompileShader(GL_VERTEX_SHADER, vertexPath);
const uint32_t fshader = CompileShader(GL_FRAGMENT_SHADER, fragmentPath);
const uint32_t vshader = compile_shader(GL_VERTEX_SHADER, vertex_path);
const uint32_t fshader =
compile_shader(GL_FRAGMENT_SHADER, fragment_path);
if (vshader == 0 || fshader == 0) {
program_id_ = 0;
@@ -30,7 +31,8 @@ Shader::Shader(const std::string& vertexPath, const std::string& fragmentPath)
glGetProgramiv(program_id_, GL_LINK_STATUS, &ret);
if (ret == GL_FALSE) {
char buffer[1024];
glGetProgramInfoLog(program_id_, sizeof(buffer), nullptr, buffer);
glGetProgramInfoLog(program_id_, sizeof(buffer), nullptr,
buffer);
error_ = buffer;
glDeleteProgram(program_id_);
program_id_ = 0;
@@ -40,67 +42,80 @@ Shader::Shader(const std::string& vertexPath, const std::string& fragmentPath)
glDeleteShader(fshader);
}
void Shader::Use() const
void Shader::use() const
{
if (program_id_ != 0)
glUseProgram(program_id_);
}
void Shader::SetupAttribs() const
void Shader::setup_attribs() const
{
int loc = glGetAttribLocation(program_id_, "vpos");
if (loc != -1) {
glEnableVertexAttribArray(loc);
glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, position)));
glVertexAttribPointer(
loc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<const void*>(offsetof(Vertex, position)));
}
loc = glGetAttribLocation(program_id_, "vcolor");
if (loc != -1) {
glEnableVertexAttribArray(loc);
glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast<const void*>(offsetof(Vertex, color)));
glVertexAttribPointer(
loc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<const void*>(offsetof(Vertex, color)));
}
loc = glGetAttribLocation(program_id_, "vtexcoord");
if (loc != -1) {
glEnableVertexAttribArray(loc);
glVertexAttribPointer(
loc, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
reinterpret_cast<const void*>(offsetof(Vertex, tex_coord)));
}
}
int Shader::getLocation(const char* key) const
int Shader::uniform_location(const char* key) const
{
return glGetUniformLocation(program_id_, key);
}
void Shader::setInt(int loc, int value)
void Shader::set_int(int loc, int value)
{
if (loc != -1)
glUniform1i(loc, value);
}
void Shader::setFloat(int loc, float value)
void Shader::set_float(int loc, float value)
{
if (loc != -1)
glUniform1f(loc, value);
}
void Shader::setVec3(int loc, const glm::vec3& value)
void Shader::set_vec3(int loc, const glm::vec3& value)
{
if (loc != -1)
glUniform3fv(loc, 1, &value[0]);
}
void Shader::setVec4(int loc, const glm::vec4& value)
void Shader::set_vec4(int loc, const glm::vec4& value)
{
if (loc != -1)
glUniform4fv(loc, 1, &value[0]);
}
void Shader::setMat4(int loc, const glm::mat4& value)
void Shader::set_mat4(int loc, const glm::mat4& value)
{
if (loc != -1)
glUniformMatrix4fv(loc, 1, GL_FALSE, &value[0][0]);
}
std::string Shader::ReadShaderFile(const std::string& filename)
std::string Shader::read_shader_file(const std::string& filename)
{
std::ifstream file(filename);
if (!file) {
error_ = "Failed to open shader file: " + filename + "\n";
error_ = "Failed to open shader file: " + filename;
Logger::error(sstr("Failed to open shader file: ", filename));
return std::string{0};
}
@@ -109,15 +124,20 @@ std::string Shader::ReadShaderFile(const std::string& filename)
return buffer.str();
}
uint32_t Shader::CompileShader(uint32_t type, const std::string& source_path)
uint32_t Shader::compile_shader(uint32_t type, const std::string& source_path)
{
std::string source = ReadShaderFile(source_path);
//std::cout << "SHADER FILE: " << source << "\n";
const char* shader_type_name =
(type == GL_VERTEX_SHADER) ? "vertex" : "fragment";
std::string source = read_shader_file(source_path);
if (source.empty())
return 0;
const uint32_t shader = glCreateShader(type);
const char* shader_code = source.c_str();
Logger::info(
sstr("Compiling ", shader_type_name, " shader: ", source_path));
const uint32_t shader = glCreateShader(type);
const char* shader_code = source.c_str();
glShaderSource(shader, 1, &shader_code, nullptr);
glCompileShader(shader);
@@ -129,9 +149,12 @@ uint32_t Shader::CompileShader(uint32_t type, const std::string& source_path)
char buffer[1024];
glGetShaderInfoLog(shader, sizeof(buffer), nullptr, buffer);
error_ = buffer;
Logger::error(sstr("Shader compilation failed (",
shader_type_name, "): ", buffer));
glDeleteShader(shader);
return 0;
}
Logger::info(sstr(shader_type_name, " shader compiled successfully"));
return shader;
}

View File

@@ -9,29 +9,35 @@
class Shader {
public:
Shader(const std::string& vertex_path, const std::string& fragment_path);
Shader(const std::string& vertex_path,
const std::string& fragment_path);
void Use() const;
void SetupAttribs() const;
void use() const;
void setup_attribs() const;
uint32_t getId() const { return program_id_; }
const char* getError() const { return error_.c_str(); }
[[nodiscard]] uint32_t id() const
{
return program_id_;
}
[[nodiscard]] const char* error() const
{
return error_.c_str();
}
int getLocation(const char* key) const;
[[nodiscard]] int uniform_location(const char* key) const;
static void setInt(int loc, int value);
static void setFloat(int loc, float value);
static void setVec3(int loc, const glm::vec3& value);
static void setVec4(int loc, const glm::vec4& value);
static void setMat4(int loc, const glm::mat4& value);
static void set_int(int loc, int value);
static void set_float(int loc, float value);
static void set_vec3(int loc, const glm::vec3& value);
static void set_vec4(int loc, const glm::vec4& value);
static void set_mat4(int loc, const glm::mat4& value);
private:
uint32_t program_id_;
uint32_t program_id_;
std::string error_;
std::string ReadShaderFile(const std::string& filename);
uint32_t CompileShader(uint32_t type, const std::string& source_path);
std::string read_shader_file(const std::string& filename);
uint32_t compile_shader(uint32_t type, const std::string& source_path);
};
#endif // SHADER_H_

18
src/state.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef STATE_H_
#define STATE_H_
#include <memory>
#include "../lib/glm/glm.hpp"
class Shader;
namespace state {
inline std::shared_ptr<Shader> default_shader = nullptr;
inline glm::mat4 projection_matrix = glm::mat4(1.0f);
inline glm::mat4 view_matrix = glm::mat4(1.0f);
inline glm::mat4 model_matrix = glm::mat4(1.0f);
} // namespace state
#endif // STATE_H_

81
src/texture.cpp Normal file
View File

@@ -0,0 +1,81 @@
#include "texture.h"
#include <algorithm>
#include "../lib/glew/GL/glew.h"
#include "../lib/glfw/glfw3.h"
#include "../lib/stb/stb_image.h"
#include "logger.h"
Texture::Texture(uint32_t id, const glm::ivec2& size)
: id_(id), size_(size)
{
}
Texture::~Texture()
{
if (id_ != 0) {
glDeleteTextures(1, &id_);
Logger::info(sstr("Texture ", id_, " destroyed"));
}
}
std::shared_ptr<Texture> Texture::load(const char* filename)
{
Logger::info(sstr("Loading texture: ", filename));
// Load image
int width, height, channels;
stbi_uc* pixels =
stbi_load(filename, &width, &height, &channels, STBI_rgb_alpha);
if (!pixels) {
Logger::error(sstr("Failed to load texture: ", filename, " - ",
stbi_failure_reason()));
return nullptr;
}
// Flip image vertically (OpenGL expects bottom row first)
const int row_size = width * 4; // 4 channels (RGBA)
auto* row_buffer = new stbi_uc[row_size];
for (int y = 0; y < height / 2; ++y) {
stbi_uc* row1 = pixels + y * row_size;
stbi_uc* row2 = pixels + (height - 1 - y) * row_size;
std::copy(row1, row1 + row_size, row_buffer);
std::copy(row2, row2 + row_size, row1);
std::copy(row_buffer, row_buffer + row_size, row2);
}
delete[] row_buffer;
// Generate OpenGL texture
GLuint texture_id;
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
// Upload texture data
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, pixels);
// Set trilinear filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Generate mipmaps
glGenerateMipmap(GL_TEXTURE_2D);
// Free image data
stbi_image_free(pixels);
Logger::info(sstr("Texture loaded successfully: ", filename, " (",
width, "x", height, ", ", channels, " channels)"));
return std::shared_ptr<Texture>(
new Texture(texture_id, glm::ivec2(width, height)));
}
void Texture::bind() const
{
glBindTexture(GL_TEXTURE_2D, id_);
}

35
src/texture.h Normal file
View File

@@ -0,0 +1,35 @@
#ifndef TEXTURE_H_
#define TEXTURE_H_
#include <cstdint>
#include <memory>
#include <string>
#include "../lib/glm/glm.hpp"
class Texture {
public:
Texture() = delete;
~Texture();
static std::shared_ptr<Texture> load(const char* filename);
[[nodiscard]] uint32_t id() const
{
return id_;
}
[[nodiscard]] const glm::ivec2& size() const
{
return size_;
}
void bind() const;
private:
Texture(uint32_t id, const glm::ivec2& size);
uint32_t id_;
glm::ivec2 size_;
};
#endif // TEXTURE_H_

View File

@@ -4,8 +4,9 @@
#include "../lib/glm/glm.hpp"
struct Vertex {
glm::vec3 position{ 0.0f, 0.0f, 0.0f };
glm::vec3 color{ 0.0f, 0.0f, 0.0f };
glm::vec3 position{0.0f, 0.0f, 0.0f};
glm::vec3 color{0.0f, 0.0f, 0.0f};
glm::vec2 tex_coord{0.0f, 0.0f};
};
#endif // VERTEX_H_

68
src/world.cpp Normal file
View File

@@ -0,0 +1,68 @@
#include "world.h"
#include <algorithm>
#include "camera.h"
#include "entity.h"
#include "logger.h"
void World::add_entity(const std::shared_ptr<Entity>& entity)
{
entities_.push_back(entity);
// Check if entity is a camera
std::shared_ptr<Camera> camera =
std::dynamic_pointer_cast<Camera>(entity);
if (camera) {
cameras_.push_back(camera);
Logger::info(sstr("Camera added to world (total cameras: ",
cameras_.size(), ")"));
} else {
Logger::info(sstr("Entity added to world (total entities: ",
entities_.size(), ")"));
}
}
void World::remove_entity(const std::shared_ptr<Entity>& entity)
{
// Remove from entities list
auto it = std::find(entities_.begin(), entities_.end(), entity);
if (it != entities_.end()) {
entities_.erase(it);
Logger::info("Entity removed from world");
}
// Check if entity is a camera and remove from cameras list
std::shared_ptr<Camera> camera =
std::dynamic_pointer_cast<Camera>(entity);
if (camera) {
auto camIt =
std::find(cameras_.begin(), cameras_.end(), camera);
if (camIt != cameras_.end()) {
cameras_.erase(camIt);
Logger::info("Camera removed from world");
}
}
}
void World::update(float delta_time)
{
for (auto& entity : entities_) {
entity->update(delta_time);
}
}
void World::draw()
{
// Draw for each camera
for (auto& camera : cameras_) {
// Prepare the camera (sets viewport, projection, view, clears
// screen)
camera->prepare();
// Draw all entities
for (auto& entity : entities_) {
entity->draw();
}
}
}

38
src/world.h Normal file
View File

@@ -0,0 +1,38 @@
#ifndef WORLD_H_
#define WORLD_H_
#include <memory>
#include <vector>
class Entity;
class Camera;
class World {
public:
World() = default;
void add_entity(const std::shared_ptr<Entity>& entity);
void remove_entity(const std::shared_ptr<Entity>& entity);
[[nodiscard]] size_t num_entities() const
{
return entities_.size();
}
[[nodiscard]] const std::shared_ptr<Entity>& entity(size_t index) const
{
return entities_[index];
}
[[nodiscard]] std::shared_ptr<Entity>& entity(size_t index)
{
return entities_[index];
}
void update(float delta_time);
void draw();
private:
std::vector<std::shared_ptr<Entity>> entities_;
std::vector<std::shared_ptr<Camera>> cameras_;
};
#endif // WORLD_H_

View File

@@ -72,20 +72,20 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<OutDir>$(SolutionDir)Build\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)Build\obj\$(Platform)\$(Configuration)\</IntDir>
<OutDir>$(SolutionDir)build\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)build\obj\$(Platform)\$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutDir>$(SolutionDir)Build\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)Build\obj\$(Platform)\$(Configuration)\</IntDir>
<OutDir>$(SolutionDir)build\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)build\obj\$(Platform)\$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<OutDir>$(SolutionDir)Build\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)Build\obj\$(Platform)\$(Configuration)\</IntDir>
<OutDir>$(SolutionDir)build\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)build\obj\$(Platform)\$(Configuration)\</IntDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<OutDir>$(SolutionDir)Build\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)Build\obj\$(Platform)\$(Configuration)\</IntDir>
<OutDir>$(SolutionDir)build\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)build\obj\$(Platform)\$(Configuration)\</IntDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
@@ -93,12 +93,12 @@
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>../lib/glew</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>lib/glew</AdditionalIncludeDirectories>
<PreprocessorDefinitions>GLEW_STATIC;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>../lib/glfw</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>lib/glfw</AdditionalLibraryDirectories>
<AdditionalDependencies>glfw3.win32.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
@@ -108,12 +108,12 @@
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>../lib/glew</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>lib/glew</AdditionalIncludeDirectories>
<PreprocessorDefinitions>GLEW_STATIC;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>../lib/glfw</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>lib/glfw</AdditionalLibraryDirectories>
<AdditionalDependencies>glfw3.win64.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
@@ -125,14 +125,14 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>../lib/glew</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>lib/glew</AdditionalIncludeDirectories>
<PreprocessorDefinitions>GLEW_STATIC;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>../lib/glfw</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>lib/glfw</AdditionalLibraryDirectories>
<AdditionalDependencies>glfw3.win32.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
@@ -144,35 +144,55 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>../lib/glew</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>lib/glew</AdditionalIncludeDirectories>
<PreprocessorDefinitions>GLEW_STATIC;_MBCS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<LanguageStandard>stdcpp20</LanguageStandard>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>../lib/glfw</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>lib/glfw</AdditionalLibraryDirectories>
<AdditionalDependencies>glfw3.win64.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\lib\glew\GL\glew.h" />
<ClInclude Include="..\lib\glfw\glfw3.h" />
<ClInclude Include="..\lib\glfw\glfw3native.h" />
<ClInclude Include="..\src\buffer.h" />
<ClInclude Include="..\src\shader.h" />
<ClInclude Include="..\src\vertex.h" />
<ClInclude Include="lib\glew\GL\glew.h" />
<ClInclude Include="lib\glfw\glfw3.h" />
<ClInclude Include="lib\glfw\glfw3native.h" />
<ClInclude Include="lib\stb\stb_image.h" />
<ClInclude Include="src\buffer.h" />
<ClInclude Include="src\camera.h" />
<ClInclude Include="src\engine.h" />
<ClInclude Include="src\entity.h" />
<ClInclude Include="src\logger.h" />
<ClInclude Include="src\material.h" />
<ClInclude Include="src\mesh.h" />
<ClInclude Include="src\model.h" />
<ClInclude Include="src\shader.h" />
<ClInclude Include="src\state.h" />
<ClInclude Include="src\texture.h" />
<ClInclude Include="src\vertex.h" />
<ClInclude Include="src\world.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\lib\glew\glew.c" />
<ClCompile Include="..\src\buffer.cpp" />
<ClCompile Include="..\src\main.cpp" />
<ClCompile Include="..\src\shader.cpp" />
<ClCompile Include="..\src\vertex.cpp" />
<ClCompile Include="lib\glew\glew.c" />
<ClCompile Include="src\buffer.cpp" />
<ClCompile Include="src\camera.cpp" />
<ClCompile Include="src\engine.cpp" />
<ClCompile Include="src\entity.cpp" />
<ClCompile Include="src\logger.cpp" />
<ClCompile Include="src\main.cpp" />
<ClCompile Include="src\material.cpp" />
<ClCompile Include="src\mesh.cpp" />
<ClCompile Include="src\model.cpp" />
<ClCompile Include="src\shader.cpp" />
<ClCompile Include="src\texture.cpp" />
<ClCompile Include="src\vertex.cpp" />
<ClCompile Include="src\world.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="..\data\fragment.glsl" />
<None Include="..\data\vertex.glsl" />
<None Include="data\shaders\fragment.glsl" />
<None Include="data\shaders\vertex.glsl" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

134
ugine3d.vcxproj.filters Normal file
View File

@@ -0,0 +1,134 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
<Filter Include="glfw">
<UniqueIdentifier>{422e20da-2cf0-4bda-9833-a89b7ab453a1}</UniqueIdentifier>
</Filter>
<Filter Include="glew">
<UniqueIdentifier>{296e1ba9-28bc-4e0b-8d4e-413551edca96}</UniqueIdentifier>
</Filter>
<Filter Include="stb">
<UniqueIdentifier>{e3c5a7f2-1d4b-4a9c-8f2e-9b5c8d7a6e4f}</UniqueIdentifier>
</Filter>
<Filter Include="Shaders">
<UniqueIdentifier>{0628083b-a31c-4825-822c-11b6f933e7bd}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="lib\glfw\glfw3.h">
<Filter>glfw</Filter>
</ClInclude>
<ClInclude Include="lib\glfw\glfw3native.h">
<Filter>glfw</Filter>
</ClInclude>
<ClInclude Include="lib\glew\GL\glew.h">
<Filter>glew</Filter>
</ClInclude>
<ClInclude Include="lib\stb\stb_image.h">
<Filter>stb</Filter>
</ClInclude>
<ClInclude Include="src\vertex.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="src\shader.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="src\buffer.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="src\state.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="src\mesh.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="src\entity.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="src\model.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="src\camera.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="src\world.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="src\engine.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="src\logger.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="src\material.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="src\texture.h">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="lib\glew\glew.c">
<Filter>glew</Filter>
</ClCompile>
<ClCompile Include="src\vertex.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\shader.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\buffer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\mesh.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\entity.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\model.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\camera.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\world.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\engine.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\logger.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\material.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\texture.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="data\shaders\fragment.glsl">
<Filter>Shaders</Filter>
</None>
<None Include="data\shaders\vertex.glsl">
<Filter>Shaders</Filter>
</None>
</ItemGroup>
</Project>

View File

@@ -1,19 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerWorkingDirectory>..</LocalDebuggerWorkingDirectory>
<LocalDebuggerWorkingDirectory>$(ProjectDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LocalDebuggerWorkingDirectory>..</LocalDebuggerWorkingDirectory>
<LocalDebuggerWorkingDirectory>$(ProjectDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LocalDebuggerWorkingDirectory>..</LocalDebuggerWorkingDirectory>
<LocalDebuggerWorkingDirectory>$(ProjectDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LocalDebuggerWorkingDirectory>..</LocalDebuggerWorkingDirectory>
<LocalDebuggerWorkingDirectory>$(ProjectDir)</LocalDebuggerWorkingDirectory>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>