Skip to content

Commit

Permalink
feat: common utils (#18)
Browse files Browse the repository at this point in the history
* feat: common utils

* fix: problems

* fix: cylinder utils path

* fix: gitignore

---------
  • Loading branch information
MarioRodrigues10 authored Mar 7, 2024
1 parent 3017ed3 commit b9ec44f
Show file tree
Hide file tree
Showing 32 changed files with 906 additions and 12,249 deletions.
48 changes: 47 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,58 @@
*.cmake
Makefile
generator/Makefile
engine/Makefile
common/Makefile
generator/CMakeCache.txt
engine/CMakeCache.txt
common/CMakeCache.txt
generator/CMakeFiles/*
engine/CMakeFiles/*
common/CMakeFiles/*
.idea/*
.vscode/*
generator/generator
engine/engine
build
common/common
build
CMakeCache.txt
compile_commands.json
CMakeFiles/*
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj
*.bin


# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

.vscode
build
.3d
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.5)

# Project Name
project(cg)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 20)
set(OpenGL_GL_PREFERENCE "GLVND")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic")

add_subdirectory(common)
add_subdirectory(generator)
add_subdirectory(engine)
target_include_directories(common PUBLIC common/include)
target_include_directories(generator PUBLIC generator/include)
target_include_directories(engine PUBLIC engine/include)
182 changes: 182 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.28

# Default target executed when no arguments are given to make.
default_target: all
.PHONY : default_target

# Allow only one "make -f Makefile2" at a time, but pass parallelism.
.NOTPARALLEL:

#=============================================================================
# Special targets provided by cmake.

# Disable implicit rules so canonical targets will work.
.SUFFIXES:

# Disable VCS-based implicit rules.
% : %,v

# Disable VCS-based implicit rules.
% : RCS/%

# Disable VCS-based implicit rules.
% : RCS/%,v

# Disable VCS-based implicit rules.
% : SCCS/s.%

# Disable VCS-based implicit rules.
% : s.%

.SUFFIXES: .hpux_make_needs_suffix_list

# Command-line flag to silence nested $(MAKE).
$(VERBOSE)MAKESILENT = -s

#Suppress display of executed commands.
$(VERBOSE).SILENT:

# A target that is always out of date.
cmake_force:
.PHONY : cmake_force

#=============================================================================
# Set environment variables for the build.

# The shell in which to execute make rules.
SHELL = /bin/sh

# The CMake executable.
CMAKE_COMMAND = /usr/bin/cmake

# The command to remove a file.
RM = /usr/bin/cmake -E rm -f

# Escaping for special characters.
EQUALS = =

# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = /home/mario/CG-Project

# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /home/mario/CG-Project

#=============================================================================
# Targets provided globally by CMake.

# Special rule for the target edit_cache
edit_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "Running CMake cache editor..."
/usr/bin/ccmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : edit_cache

# Special rule for the target edit_cache
edit_cache/fast: edit_cache
.PHONY : edit_cache/fast

# Special rule for the target rebuild_cache
rebuild_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "Running CMake to regenerate build system..."
/usr/bin/cmake --regenerate-during-build -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : rebuild_cache

# Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache
.PHONY : rebuild_cache/fast

# The main all target
all: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/mario/CG-Project/CMakeFiles /home/mario/CG-Project//CMakeFiles/progress.marks
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 all
$(CMAKE_COMMAND) -E cmake_progress_start /home/mario/CG-Project/CMakeFiles 0
.PHONY : all

# The main clean target
clean:
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 clean
.PHONY : clean

# The main clean target
clean/fast: clean
.PHONY : clean/fast

# Prepare targets for installation.
preinstall: all
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall

# Prepare targets for installation.
preinstall/fast:
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall/fast

# clear depends
depend:
$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
.PHONY : depend

#=============================================================================
# Target rules for targets named common

# Build rule for target.
common: cmake_check_build_system
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 common
.PHONY : common

# fast build rule for target.
common/fast:
$(MAKE) $(MAKESILENT) -f common/CMakeFiles/common.dir/build.make common/CMakeFiles/common.dir/build
.PHONY : common/fast

#=============================================================================
# Target rules for targets named generator

# Build rule for target.
generator: cmake_check_build_system
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 generator
.PHONY : generator

# fast build rule for target.
generator/fast:
$(MAKE) $(MAKESILENT) -f generator/CMakeFiles/generator.dir/build.make generator/CMakeFiles/generator.dir/build
.PHONY : generator/fast

#=============================================================================
# Target rules for targets named engine

# Build rule for target.
engine: cmake_check_build_system
$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 engine
.PHONY : engine

# fast build rule for target.
engine/fast:
$(MAKE) $(MAKESILENT) -f engine/CMakeFiles/engine.dir/build.make engine/CMakeFiles/engine.dir/build
.PHONY : engine/fast

# Help Target
help:
@echo "The following are some of the valid targets for this Makefile:"
@echo "... all (the default if no target is provided)"
@echo "... clean"
@echo "... depend"
@echo "... edit_cache"
@echo "... rebuild_cache"
@echo "... common"
@echo "... engine"
@echo "... generator"
.PHONY : help



#=============================================================================
# Special targets to cleanup operation of make.

# Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:
$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system

9 changes: 9 additions & 0 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.5)

# Project Name
project(common)

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

file(GLOB SRC_FILES src/*.cpp)
add_library(${PROJECT_NAME} ${SRC_FILES})
1 change: 1 addition & 0 deletions engine/include/utils.hpp → common/include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef struct Point {

// Function to convert Point to string
std::vector<Point> parseFile(std::string filename);
void saveToFile(const std::vector<Point>& points, const char* filepath);

// Function to parse OBJ file
std::vector<Point> parseOBJfile(std::string filename, std::string type);
Expand Down
38 changes: 27 additions & 11 deletions engine/src/utils.cpp → common/src/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,50 @@
#include "utils.hpp"
#include "../include/utils.hpp"

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

#define DIR "../models/"

std::vector<Point> parseFile(std::string filename) {
std::ifstream file(filename);
std::vector<Point> points;
std::ifstream file(filename);

if (!file.is_open()) {
std::cerr << "Error: Unable to open file " << filename << std::endl;
return points;
}

printf("filename: %s\n", filename.c_str());
if (filename.find(".obj") != std::string::npos) {
points = parseOBJfile(filename, "obj");
} else if (filename.find(".3d") != std::string::npos) {
points = parse3Dfile(filename);
} else {
std::cerr << "Error: File format not supported" << std::endl;
Point point;
while (file >> point.x >> point.y >> point.z) {
points.push_back(point);
}

file.close();

return points;
}

void saveToFile(
const std::vector<Point>& points,
const char* filepath) { // Changed parameter type to const char*

std::string buf(DIR);
buf.append(filepath);

std::ofstream file(buf);

if (file.is_open()) {
for (const auto& point : points) {
file << point.x << " " << point.y << " " << point.z << "\n";
}
file.close();
std::cout << "File saved successfully.\n";
} else {
std::cerr << "Unable to open file: " << filepath << std::endl;
}
}

std::vector<Point> parseOBJfile(std::string filename, std::string type) {
std::vector<Point> points;
std::vector<Point> sorted_points;
Expand Down
6 changes: 2 additions & 4 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ cmake_minimum_required(VERSION 3.5)
# Project Name
project(engine)

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

file(GLOB SRC_FILES src/scene/*.cpp src/*.cpp)
file(GLOB SRC_FILES src/*.cpp)
add_executable(${PROJECT_NAME} ${SRC_FILES})

include_directories(${PROJECT_NAME} PUBLIC include)
include_directories(../common PUBLIC include)
target_link_libraries(${PROJECT_NAME} common)

find_package(OpenGL REQUIRED)
include_directories(${OpenGL_INCLUDE_DIRS})
Expand Down
2 changes: 1 addition & 1 deletion engine/include/Camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include <string>

#include "../../common/include/utils.hpp"
#include "Camera.hpp"
#include "utils.hpp"

class Camera {
public:
Expand Down
2 changes: 1 addition & 1 deletion engine/include/draw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

#include <vector>

#include "../../common/include/utils.hpp"
#include "draw.hpp"
#include "utils.hpp"

void drawTriangles(const std::vector<Point>& points);

Expand Down
2 changes: 1 addition & 1 deletion engine/src/draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

#include <vector>

#include "../../common/include/utils.hpp"
#include "draw.hpp"
#include "utils.hpp"

void drawTriangles(const std::vector<Point>& points) {
glBegin(GL_TRIANGLES);
Expand Down
Loading

0 comments on commit b9ec44f

Please sign in to comment.