Skip to content

Commit

Permalink
feat: engine
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJPinto committed Feb 28, 2024
1 parent b5b0b14 commit a441398
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ engine/CMakeFiles/*
models/*
.idea/*
.vscode/*
generator/generator
generator/generator
engine/engine
56 changes: 56 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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)
add_executable(${PROJECT_NAME} ${SRC_FILES})

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

find_package(OpenGL REQUIRED)
include_directories(${OpenGL_INCLUDE_DIRS})
link_directories(${OpenGL_LIBRARY_DIRS})
add_definitions(${OpenGL_DEFINITIONS})

if(NOT OPENGL_FOUND)
message(ERROR " OPENGL not found!")
endif(NOT OPENGL_FOUND)

if (WIN32)

message(STATUS "Toolkits_DIR set to: " ${TOOLKITS_FOLDER})
set(TOOLKITS_FOLDER "" CACHE PATH "Path to Toolkits folder")

if (NOT EXISTS "${TOOLKITS_FOLDER}/glut/GL/glut.h" OR NOT EXISTS "${TOOLKITS_FOLDER}/glut/glut32.lib")
message(ERROR ": GLUT not found")
endif (NOT EXISTS "${TOOLKITS_FOLDER}/glut/GL/glut.h" OR NOT EXISTS "${TOOLKITS_FOLDER}/glut/glut32.lib")


include_directories(${TOOLKITS_FOLDER}/glut )
target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES}
${TOOLKITS_FOLDER}/glut/glut32.lib)

if (EXISTS "${TOOLKITS_FOLDER}/glut/glut32.dll" )
file(COPY ${TOOLKITS_FOLDER}/glut/glut32.dll DESTINATION ${CMAKE_BINARY_DIR})
endif(EXISTS "${TOOLKITS_FOLDER}/glut/glut32.dll" )

set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ${PROJECT_NAME})

else (WIN32) #Linux and Mac

set( CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} Wno-deprecated")
find_package(GLUT REQUIRED)
include_directories(${GLUT_INCLUDE_DIR})
link_directories(${GLUT_LIBRARY_DIRS})
add_definitions(${GLUT_DEFINITIONS})

target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES})
if(NOT GLUT_FOUND)
message(ERROR ": GLUT not found!")
endif(NOT GLUT_FOUND)

endif(WIN32)
13 changes: 13 additions & 0 deletions engine/include/draw.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef DRAW_HPP
#define DRAW_HPP

#include <math.h>
#include <vector>
#include "draw.hpp"
#include "utils.hpp"

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

void drawFile(char* filename);

#endif //DRAW_HPP
26 changes: 26 additions & 0 deletions engine/include/utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef UTILS_HPP
#define UTILS_HPP

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

#include "utils.hpp"

// Define Point struct
typedef struct Point {
float x;
float y;
float z;

// Constructor
Point(float x_val = 0.0f, float y_val = 0.0f, float z_val = 0.0f)
: x(x_val), y(y_val), z(z_val) {}
} Point;

// Function to convert Point to string
std::vector<Point> parseFile(std::string filename);

#endif // UTILS_HPP
3 changes: 0 additions & 3 deletions engine/main.cpp

This file was deleted.

36 changes: 36 additions & 0 deletions engine/src/draw.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include "draw.hpp"
#include "utils.hpp"

void drawTriangles(const std::vector<Point>& points) {
glBegin(GL_TRIANGLES);
for (size_t i = 0; i < points.size(); i += 3) {
// Draw each triangle
glVertex3f(points[i].x, points[i].y, points[i].z);
glVertex3f(points[i + 1].x, points[i + 1].y, points[i + 1].z);
glVertex3f(points[i + 2].x, points[i + 2].y, points[i + 2].z);
}
glEnd();
}

void drawFile(char* filename) {

std::string dir = "../models/";
dir.append(filename);

std::vector<Point> points = parseFile(dir);
if (points.empty()) {
// File not found or empty, handle this case
return;
}
drawTriangles(points);
}

89 changes: 89 additions & 0 deletions engine/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#define _USE_MATH_DEFINES
#include <math.h>
#include "draw.hpp"


char* file = "";

void changeSize(int w, int h) {
// Prevent a divide by zero, when window is too short
// (you can�t make a window with zero width).
if (h == 0) h = 1;

float ratio = w * 1.0f / h;
// Set the projection matrix as current
glMatrixMode(GL_PROJECTION);
// Load the identity matrix
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the perspective
gluPerspective(45.0f, ratio, 1.0f, 1000.0f);
// return to the model view matrix mode
glMatrixMode(GL_MODELVIEW);
}

void renderScene(void) {
// clear buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// set camera
glLoadIdentity();
gluLookAt(0.0f, 0.0f, 5.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f);

// put drawing instructions here
glBegin(GL_LINES);
// x-axis (red)
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
// y-axis (green)
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, -1.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
// z-axis (blue)
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 0.0f, -1.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glEnd();
glEnd();

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
drawFile(file);


// End of frame
glutSwapBuffers();
}

int main(int argc, char **argv) {
// put GLUT�s init here
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(800, 800);
glutCreateWindow("CG@DI");

// put callback registry here
file = argv[1];
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene);
glutDisplayFunc(renderScene);


// some OpenGL settings
glEnable(GL_DEPTH_TEST);
//glEnable(GL_CULL_FACE);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

// enter GLUT�s main cycle
glutMainLoop();

return 1;
}
24 changes: 24 additions & 0 deletions engine/src/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "utils.hpp"

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

std::vector<Point> parseFile(std::string 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;
}

Point point;
while (file >> point.x >> point.y >> point.z) {
points.push_back(point);
}
file.close();
return points;
}
1 change: 0 additions & 1 deletion generator/src/shapes/plane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ std::vector<Point> planeTriangles(float length, int divisions) {
}



bool generatePlane(float length, int divisions, const char* filepath) { // Changed parameter type to const char*
std::vector<Point> triangles = planeTriangles(length, divisions);

Expand Down

0 comments on commit a441398

Please sign in to comment.